Code Monkey home page Code Monkey logo

pyheatmagic's Introduction

heat

pypiv pyv Licence Thanks

IPython magic command to profile and view your python code as a heat map using py-heat.

Demo

Demo

In case the demo was too fast, here is a snapshot of the last step of the demo for deeper contemplation :)

What is the magic command?

%%heat

Setup

Using pip

pip install py-heat-magic

Directly from the repository

git clone https://github.com/csurfer/pyheatmagic.git
python pyheatmagic/setup.py install

Load Extension in IPython

%load_ext heat

Usage

# To view the heatmap
%%heat

# To save the heatmap as a file
%%heat -o file.png

Contributing

Bug Reports and Feature Requests

Please use issue tracker for reporting bugs or feature requests.

Development

Pull requests are most welcome.

Buy the developer a cup of coffee!

If you found the utility helpful you can buy me a cup of coffee using

Donate

pyheatmagic's People

Contributors

csurfer avatar masasin avatar timkpaine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyheatmagic's Issues

Accessing variables from previous notebook cells

Example notebook

Usually, one can access from the current notebook cell variables from previous cells (for instance, we define a in cell 2 and access it in 3). However, I can not get that to work when using heat (example in cell 4), which is sometimes annoying (especially when creating such variable can take up some time).

Full traceback (click to expand)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-6d5dc00d7528> in <module>()
----> 1 get_ipython().run_cell_magic('heat', '', 'print(a)')

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2085             magic_arg_s = self.var_expand(line, stack_depth)
   2086             with self.builtin_trap:
-> 2087                 result = fn(magic_arg_s, cell)
   2088             return result
   2089 

<decorator-gen-124> in heat(self, line, cell)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/heat.py in heat(self, line, cell)
     61 
     62         pyheat = PyHeat(tmp_file)
---> 63         pyheat.create_heatmap()
     64         pyheat.show_heatmap(output_file=filename)
     65         pyheat.close_heatmap()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyheat/pyheat.py in create_heatmap(self)
     44         """Method to define the heatmap using profile data."""
     45         # Profile the file.
---> 46         self.__profile_file()
     47         # Map profile stats to heatmap data.
     48         self.__fetch_heatmap_data_from_profile()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyheat/pyheat.py in __profile_file(self)
     71         self.line_profiler = pprofile.Profile()
     72         self.line_profiler.runfile(
---> 73             open(self.pyfile.path, 'r'), {}, self.pyfile.path)
     74 
     75     def __get_line_profile_data(self):

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pprofile.py in runfile(self, fd, argv, fd_name, compile_flags, dont_inherit, globals)
    606         try:
    607             sys.argv[:] = argv
--> 608             return self.runctx(code, ctx_globals, None)
    609         finally:
    610             sys.argv[:] = original_sys_argv

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pprofile.py in runctx(self, cmd, globals, locals)
    586         """Similar to profile.Profile.runctx ."""
    587         with self():
--> 588             exec(cmd, globals, locals)
    589         return self
    590 

/Users/Jean/Desktop/ipython_cell_input.py in <module>()
----> 1 print(a)

NameError: name 'a' is not defined

Would it be possible to add that feature? I have never worked on iPython extensions, but I would be glad to contribute to that one, if I'm pointed to the right part of code to improve.

Anyway, thank you for the great extension ๐Ÿ‘

TypeError on run

Trying this out, I got a TypeError on first try.
specs:

  • anaconda 4.3.1
  • python 3.5.2
  • windows10 pro (64bit)
  • jupyter 4.4.0

image

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-91c61b03397a> in <module>()
----> 1 get_ipython().run_cell_magic('heat', '', 'def test():\n    50 * 50\n    1 + 2 \n    5 / 4')

C:\Users\User\Anaconda3\envs\poker\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2113             magic_arg_s = self.var_expand(line, stack_depth)
   2114             with self.builtin_trap:
-> 2115                 result = fn(magic_arg_s, cell)
   2116             return result
   2117 

<decorator-gen-126> in heat(self, line, cell)

C:\Users\User\Anaconda3\envs\poker\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

C:\Users\User\Anaconda3\envs\poker\lib\site-packages\heat.py in heat(self, line, cell)
     49         tmp_file = 'ipython_cell_input.py'
     50         with open(tmp_file, 'wb') as f:
---> 51             f.write(cell)
     52 
     53         pyheat = PyHeat(tmp_file)

TypeError: a bytes-like object is required, not 'str'

ENH: numpy support?

This is a great magic!!

I wonder why this code is not working:

   %%heat 
   def gauss(A, b):
       Ab = np.column_stack((A, b))
       for p, pivot_vrsta in enumerate(Ab[:-1]):
       for vrsta in Ab[p + 1:]:
            if pivot_vrsta[p]:
                vrsta[p:] = vrsta[p:] - pivot_vrsta[p:] * vrsta[p] / pivot_vrsta[p]
            else:
                raise Exception('Division with 0.')
        return Ab

   import numpy as np
   n = 40
   gauss(np.random.random((n, n)),np.random.random(n))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.