Reputation: 1
I am loading some data from a file, do a transformation and then extract the real part, however, the np.real function does not seem to work. It prints:
[[(0.99023793104890667+0.034016079376604003j) 0.9905214315431583 0.99033818364852688 ..., 0.86609847609098078 0.87048246890045189 None]]
where clearly the first element is complex.
import numpy as np
import scipy.io as sio
import os.path
import PLVHilbertImplementation as pet
import matplotlib.pyplot as plt
import Likelihood_gen as lg
#for the interictal files
for j in range(1, 2, 1):
filename = '/home/kam/Documents/kaggle/Dog_5/Dog_5_interictal_segment_' + str(j).zfill(4) + '.mat'
PLVs=[]
if os.path.isfile(filename):
#For the files that exist do this
data=sio.loadmat(filename)['interictal_segment_1'][0][0][0][1:1024]
numchannels, numpoints = data.shape
label=np.ones((1,numpoints))
for i in range(0, 2, 1):
for j in range(i+1, 2, 1):
PLVs.append(np.asarray((pet.PLV(data[i,:],np.transpose(data[j,:]), 1024, 5, 128))))
print(np.real(PLVs)) #this is where the problem is
# Metric=np.sum(np.exp(np.real(np.asarray(PLVs)),1))
# plt.plot(Metric)
# plt.show
else:
print('no', filename)
break
#for the preictal files
for j in range(1, 1, 1):
filename = 'Dog_1_preictal_segment_' + str(j).zfill(4) + '.mat'
if os.path.isfile(filename):
data=sio.loadmat(filename)[0][0][0]
numchannels, numpoints = shape(data)
print(numchannels)
else:
print('no', filename)
break
Upvotes: 0
Views: 4465
Reputation: 231385
[[(0.99023793104890667+0.034016079376604003j) 0.9905214315431583
0.99033818364852688 ..., 0.86609847609098078 0.87048246890045189 None]]
there are several clues that this is not an ordinary array of complex floats, which is what the .real
method is meant for.
The complex number is enclosed in ()
Not like:
In [1011]: np.arange(5)+np.arange(2,7)*1j
Out[1011]: array([ 0.+2.j, 1.+3.j, 2.+4.j, 3.+5.j, 4.+6.j])
In [1013]: (np.arange(5)+np.arange(2,7)*1j).real
Out[1013]: array([ 0., 1., 2., 3., 4.])
And there's a None
in the array. nan
is a valid float, not None
.
I'm guessing the shape is 2d (but you should print that), and that the dtype is object - but you need to show that.
I can create something that prints like this with:
In [1014]: data=np.empty((5,),dtype=object) # 1d, could be 2
In [1015]: data # default empty fill None
Out[1015]: array([None, None, None, None, None], dtype=object)
In [1016]: data[0]=1+3.j
In [1017]: data[1:4]=[1.2,3,4.2]
In [1018]: data # the repr display
Out[1018]: array([(1+3j), 1.2, 3, 4.2, None], dtype=object)
In [1019]: print(data) # the str display
[(1+3j) 1.2 3 4.2 None]
In [1021]: data.real
Out[1021]: array([(1+3j), 1.2, 3, 4.2, None], dtype=object)
In [1022]: data[0].real
Out[1022]: 1.0
convert to complex (could slice off the None
with data[:-1]
)
In [1027]: data.astype(complex)
Out[1027]: array([ 1.0 +3.j, 1.2 +0.j, 3.0 +0.j, 4.2 +0.j, nan+nanj])
In [1028]: data.astype(complex).real
Out[1028]: array([ 1. , 1.2, 3. , 4.2, nan])
Upvotes: 1
Reputation: 6111
real should be used as numpy array obj function
a = numpy.array([1+2j, 3+4j])
print a.real
array([ 1., 3.])
Upvotes: 0