Is there a GUI to see contents of .npy file?

Viewed 11284

I am working with Python 2.

I have saved a dict of arrays to a .npy file on my computer. If I open it as a text file, a just see a mess of ASCII characters, as one would expect since I am not just saving arrays.

I can see its contents by np.load it in a Python console, but I was wondering whether there is a GUI that allows me to see the contents of the file directly, i.e. without going through a python console?

Basically: If I went back, I would save it as a json or hdf5 file so that I can open them with some viewer and could clearly see the data.
Having not done that, and having saved .npy files, can I see its contents in a similar way if they were json or hdf5?

3 Answers

I think this tool can help you do what you need. It can help you edit the contents of .npy files like spreadsheets. It also has options to visualize .npy files as 2D grayscale images and 3D point clouds:

https://github.com/csmailis/NPYViewer

np.load (or a clone) is the only why to load a npy:

In [84]: adict = {'a':np.arange(3), 'b':np.ones((2,3),int)}
In [85]: np.save('foo.npy',adict)
In [86]: np.load('foo.npy')
Out[86]: 
array({'a': array([0, 1, 2]), 'b': array([[1, 1, 1],
       [1, 1, 1]])}, dtype=object)

Note that the dictionary has been wrapped in an 0d object dtype array. The dictionary structure was then pickled. And arrays with the dictionary were pickled with np.save format. Both a dictionary and an object dtype array can contain pointers to other Python objects, which need their own pickling method. In other words, it's Python all the way down!.

If you'd attempted to use json you'd have found that numpy arrays are not json serializable. You would have had to apply tolist() to turn the arrays into lists. json just saves dictionaries, lists and strings (basic javascript structures that many languages share). There may also be a third party serializer to numpy arrays.

h5 is written in C++, with lots of language interfaces. h5dump is a handy viewer. But to write the dictionary from your Python dictionary you'd have to use h5py (or pytables). A dictionary maps nicely onto h5 groups, and numpy arrays to h5 datasets.

There is currently no GUI capable of reading the contents of .npy as plain text. However, if you think that such a program would be beneficial to the community, then you can easily create such a tool.

Let's look at a couple ways in which the aforementioned tool could work:

  1. Simply output the results of np.load to a file using the console command:

    $ python your_program.py > file.txt

    This redirects the console output from your_program.py to file.txt.

  2. Use the contextlib.redirect_stdout() function to output only a the relevant data from stdout into file.txt.

Like so:

from contextlib import redirect_stdout
    with open("help.txt", "w") as f:
         with redirect_stdout(f):
             print('it now prints to 'help.text')
Related