Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a numpy array into C from a file that was saved with numpy.save(...)

Tags:

c++

python

c

numpy

I'd like to access data in a C program that resides in a file that was written from python using numpy.save(...). So far, I was looking for a way to call the C version of numpy.load my C program, but it seems there is no directly accessible C version, because numpy.load uses the pickle module. Another solution would be to start an embedded python interpreter from C and call numpy.load in that interpreter, which should return the numpy array, which I can then access in the usual manner. However the last appraoch seems a bit to heavy, because I have to use the interpreter. Is there a better way to directly load the numpy array into C?

like image 587
Christian Avatar asked Mar 15 '11 15:03

Christian


1 Answers

The .npy format is not really meant to be read from outside NumPy. There are many formats available with provide both, excellent C and Python libraries, like HDF5 and NetCDF, so I can't see any reason whatsoever to access native NumPy files from C.

If you want to do this anyway, here are the docs of the .npy format. It does not use pickle internally unless you stored Python objects in your array (but in that case, I would wonder what you are going to do with those in your C program).

like image 200
Sven Marnach Avatar answered Sep 25 '22 22:09

Sven Marnach