Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and load an array of complex numbers using numpy.savetxt?

Tags:

I want to use numpy.savetxt() to save an array of complex numbers to a text file. Problems:

  • If you save the complex array with the default format string, the imaginary part is discarded.
  • If you use fmt='%s', then numpy.loadtxt() can't load it unless you specify dtype=complex, converters={0: lambda s: complex(s)}. Even then, if there are NaN's in the array, loading still fails.

It looks like someone has inquired about this multiple times on the Numpy mailing list and even filed a bug, but has not gotten a response. Before I put something together myself, is there a canonical way to do this?

like image 595
ptomato Avatar asked Jun 27 '11 14:06

ptomato


People also ask

How do I save and load a NumPy array?

savetxt() function saves a NumPy array to a text file and the numpy. loadtxt() function loads a NumPy array from a text file in Python. The numpy. save() function takes the name of the text file, the array to be saved, and the desired format as input parameters and saves the array inside the text file.

How do I save an array in NumPy?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.

Can NumPy handle complex numbers?

Python offers the built-in math package for basic processing of complex numbers. As an alternative, we use here the external package numpy , which is used later for various purposes. A complex number c=a+ib can be plotted as a point (a,b) in the Cartesian coordinate system.


2 Answers

It's easier and saves a few temporary arrays to just reinterpret the array as a real array.

Saving:

numpy.savetxt('outfile.txt', array.view(float))

Loading:

array = numpy.loadtxt('outfile.txt').view(complex)

If you prefer to have real and imaginary part on the same line in the file, you can use

numpy.savetxt('outfile.txt', array.view(float).reshape(-1, 2))

or

array = numpy.loadtxt('outfile.txt').view(complex).reshape(-1)

respectively.

(Note that neither view() nor reshape() copies the array -- it will just reinterpret the same data in a different way.)

Addendum from the question asker:

If you want to save more than one complex array in the same file, you can do it like so:

numpy.savetxt('outfile.txt', numpy.column_stack([
    array1.view(float).reshape(-1, 2),
    array2.view(float).reshape(-1, 2),
]))

array1, array2 = numpy.loadtxt('outfile.txt', unpack=True).view(complex)

The reshaping is necessary because numpy.view() doesn't operate on strided arrays.

like image 129
Sven Marnach Avatar answered Sep 21 '22 04:09

Sven Marnach


Here's my solution, in case anybody hits this question from Google.

Saving:

numpy.savetxt('outfile.txt', numpy.column_stack([array.real, array.imag]))

Loading:

array_real, array_imag = numpy.loadtxt('outfile.txt', unpack=True)
array = array_real + 1j * array_imag

I will still award the checkmark to a better solution!

like image 42
ptomato Avatar answered Sep 21 '22 04:09

ptomato