Jan SE
Jan SE

Reputation: 427

Convert numbers from mathematica csv export to numpy complex array

I have exported data from mathematica to a csv file. The file structure looke as follows:

"x","y","Ex","Ey"
0.,0.,0.+0.*I,-3.0434726787506006*^-12+3.4234894344189825*^-12*I
0.,0.,0.+0.*I,-5.0434726787506006*^-12+10.4234894344189825*^-13*I
...

I'm reading in the data with pandas, but I get an error

import csv
import pandas as pd
import numpy as np

df=pd.read_csv('filename.csv')

df.columns=['x', 'y', 'Ex','Ey']

df['Ey'] = df['Ey'].str.replace('*^','E')
df['Ey'] = df['Ey'].str.replace('I','1j').apply(lambda x: np.complex(x))

Edit: I'm getting the following error in the second last line of my code:

Traceback (most recent call last):
  File "plot.py", line 6, in <module>
    df['Ey'] = df['Ey'].str.replace('*^','E')
  File "/home/.../.local/lib/python2.7/site-packages/pandas/core/strings.py", line 1579, in replace
    flags=flags)
  File "/home/.../.local/lib/python2.7/site-packages/pandas/core/strings.py", line 424, in str_replace
    regex = re.compile(pat, flags=flags)
  File "/usr/lib/python2.7/re.py", line 194, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

When I write instead

df['Ey'] = df['Ey'].str.replace('*','E')

or

df['Ey'] = df['Ey'].str.replace('^','E')

I'm not getting an error. It seems like one can only give one charcter which is replaced?

Upvotes: 4

Views: 1008

Answers (1)

agentp
agentp

Reputation: 6989

Why beat yourself up messing with ascii encoded floats?

here is how to exchange complex arrays between python and mathematica using raw binary files.

in mathematica:

 cdat = RandomComplex[{0, 1 + I}, 5]

{0.0142816 + 0.0835513 I, 0.434109 + 0.977644 I, 0.579678 + 0.337286 I, 0.426271 + 0.166166 I, 0.363249 + 0.0867334 I}

 f = OpenWrite["test", BinaryFormat -> True]
 BinaryWrite[f, cdat, "Complex64"]
 Close[f]

or:

 Export["test", cdat, "Binary", "DataFormat" -> "Complex64"]

in python:

import numpy as np
x=np.fromfile('test',np.complex64)
print x

[ 0.01428160+0.0835513j 0.43410850+0.97764391j 0.57967812+0.3372865j 0.42627081+0.16616575j 0.36324903+0.08673338j]

going the other way:

y=np.array([[1+2j],[3+4j]],np.complex64)
y.tofile('test')

f = OpenRead["test", BinaryFormat -> True]
BinaryReadList[f, "Complex64"]
Close[f]

note this will be several orders of magnitude faster than exchanging data by csv.

Upvotes: 5

Related Questions