Visualising numpy arrays in PyCharm - numpy

I'm trying to use PyCharm for developing and debugging numpy code, and I would like to have a similar tool for inspecting numpy arrays. According to this link https://www.jetbrains.com/pycharm/features/scientific_tools.html there is built-in numpy array viewer. However, when stopped in the debugger, all I could see is this:
which is a really low-level representation. Is this really what they mean by the "array viewer" or there is the feature hidden in somewhere.
Thanks!

To view a NumPy array, run your project in a debug mode and find the NumPy array in the variables list shown in the PyCharm`s graphical debugger. Right-click it and select “View as Array”.
It should look like this (not the low-level representation you're seeing.):
Source: https://blog.jetbrains.com/pycharm/2014/11/third-pycharm-4-eap-numpy-array-viewer-ipython-notebook-improvements-and-more/

If a numpy array is an image and you want to see its content visually, you can use OpenCV Image Viewer Plugin, which I've just released. It works with any JetBrains IDE, which support Python (directly or via plugin).
https://plugins.jetbrains.com/plugin/14371-opencv-image-viewer

Related

Tensorflow "decode_png" keeps printing "Cleanup called..."

I'm using tensorflow to open some .png images and every image it opens, an annoying message is printed.
def open_img(path):
img = tf.io.read_file(path)
img = tf.io.decode_png(img)
return tf.image.resize(img, [IMG_HEIGHT, IMG_WIDTH])
Every time i try to open an image it says "Cleanup called...", even while training:
(This code is running on Kaggle)
tensorflow version: 2.6.3
How can i solve this annoying thing please?
Updating my TensorFlow installation to version 2.8 fixed the issue for me.
If you're running the code on Kaggle, upgrading Tensorflow to 2.8 will break CuDNN dependencies for GPU. I found that downgrading Tensorflow to 2.4.1 one will remove the debugging message while being able to work with GPU.
I believe, these "Cleanup called..." lines might be generated by some C++ code running under the hood of TF, as I was not able to redirect them to a file using redirect_stdout(). Technically, it would be best to intercept those lines and just not output them or dump them to smth like /dev/null. E.g., here, Eli Bendersky showcases how to redirect C-level output, but in my case it didn't work out with Python 3.7. Perhaps, Eli's code needs adjustment for a Python version newer than 3.4.
Concerning the given answers with upgrading/downgrading, this likely won't work with committed Kaggle notebooks as, I believe, there is no way to restart the kernel once the notebook is committed.
For committed Kaggle notebooks, there is the following workaround w/o upgrading/downgrading anything: if you need to view the output of a cell that produces "Cleanup called…" later, log it to a file or both to a file and the console (here's a code snippet for doing so), and invoke the following code at the end of the cell:
from IPython.display import clear_output
clear_output()
This will clear the output of that cell so that, once you open the "Notebook" tab of the committed notebook after it's been finished, it won't be littered with "Cleanup called…" lines and, as such, will open swiftly. Simple logging to a file (like in the above linked snipped) will not capture "Cleanup called…" lines, so one will be able to view the entire log of that cell in that log file in the "Data" tab of the committed notebook. The "Logs" tab, sadly, will still be cluttered with "Cleanup called…" lines.

How to save an interactive plot produced by matplot

I got a super graph that I would like to export as an html file to be showed on an website but I don't know how to figure that. Have you got an idea ?
You can use mpld3. This is a great tutorial. The mpld3 library's main functionality is to take an existing matplotlib visualization and transform it into some HTML code that you can embed on your website.
Use fig_to_html file, which accepts a matplotlib figure object as its sole argument and returns HTML.
To use the fig_to_html method , simply add the following code to the end of our Python script:
html_str = mpld3.fig_to_html(fig)
Html_file= open("index.html","w")
Html_file.write(html_str)
Html_file.close()

pydev autocomplete for matplotlib

Here is the code I write in pydev combined with eclipse.
import matplotlib.pyplot as plt
fig=plt.figure()
as I know, 'fig' is a instance of 'matplotlib.Figure' class,when I write :
fig.
it seems pydev can't provide method calltip for fig. I cannot figure out what's going on, since for other module , like numpy, it works well.by the way, if i use a matlab-like interface, for example,
plt.plot()
pydev does provide the calltip for function arguments.
is there a way to solve this problem? I will appreciate it if anyone give a solution .
Forgive my poor english:-D
fig is an instance of matplotlib.figure.Figure so what you can do is importing import matplotlib.figure and creating an instance of that. Then, writing fig to the editor, you should get the tooltip you want.
The following is a screenshot from Spyder, so I haven't actually tested it in pydev.
I am not aware of any other possibility. The reason is that for the requested functionality to work the editor would need to load all kinds of modules, which are not actually imported in the script.
The issue is that some cases are too dynamic for PyDev to know about the actual type of the object that some method returns (which appears to be the case).
If you know the type, you can manually type it locally.
i.e.: Add the comment:
#: :type fig: matplotlib.figure.Figure
right before the fig assignment.
See: http://www.pydev.org/manual_adv_type_hints.html for more details.

How to save PyPlot figures to video file in Julia?

I want to do something like the following using a loop and PyPlot to plot in figure windows. My question is how to save the figure windows to a movie file within the loop?
using PyPlot
for k=1:5
pcolormesh(rand(10,10))
if k==1; colorbar(); end
# save figure window to movie file here??
sleep(.5)
end
This is possible by directly using the animation submodule of matplotlib (the underlying Python library that PyPlot.jl wraps). However, it is painful; see e.g. the following notebook (in Spanish):
https://github.com/dpsanders/fisica_computacional/blob/master/como_animar.ipynb
The simplest way, however, seems to be using Plots.jl:
https://github.com/tbreloff/ExamplePlots.jl/blob/master/docs/pyplot_examples.md#functions-adding-data-and-animations.

Transparent inline matplotlibs in IPython

I'd like the background of my matplotlib plots to be transparent in my IPython notebook. This may sound silly because the notebook itself defaults to a white background but:
1) I use a solarized background and
2) more importantly, I want them to be transparent for when I embed the notebook directly into my blog via nbconvert.
It's easy enough to use something like savefig('file', transparent=True) , but I'm not saving the figures, I am displaying them inline (by calling IPython with ipython notebook --matplotlib inline.
I've been playing around with the IPython notebook configuration file, especially with c.InlineBackend.rc. For example, I upgraded to the dev version of matplotlib to get access to its new savefig.transparent rcParam, and tried configuring that with c.InlineBackend.rc = {'savefig.transparent': True}, but as expected it only affects plots saved with savefig.
Note that I am using the recent IPython 2.0 release. This must be possible somehow, right? Any light that you can shed would be appreciated.
Just to follow up, the issue opened on Github by tillsten has been patched so something like this:
rcParams['figure.facecolor'] = (0,0,0,0)
should work now after you update IPython. Three cheers for open source.
The inline plots are html objects (<img>) with class ui-resizable. So you can change their default behavior by customizing the CSS for your notebooks:
locate your settings for notebooks: in a terminal, type
ipython locate
in the indicated directory, go to subdir profile_default\static\custom (or any profile you want to use instead)
edit or create a CSS file named custom.css
put this in it:
img.ui-resizable
{
opacity:0.4;
}
Close your notebooks, kill IPython and restart it (so that it recreates the served files).
It should work with exported notebooks, as long as you export them as html and you change the css there too.
It's not exactly what you want, but it does the job.