Reputation: 21
I am trying to read a matlab file created by a colleague. I'm using python 3.7 and h5py in order to convert the data into comtrade format. The data from matlab is in the attached Screenshot: Timeseries Matalab Screenshot
I need to be able to access the time series data that is stored to put it into a numpy array. I've spent the day reviewing various tips and tricks here and here but seem to be stuck. I can't seem to address the data or even find the signals. I've got a sample down to a simple file that should have 3 signals as shown above and I'm trying to extract the data:
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
>>> import h5py as h5
... mat_dir = r'C:\Users\Perry\Desktop\testing\Matlab'
... file_name = r'\threePhaseSignal.mat'
... f = h5.File(mat_dir + file_name, 'r')
>>> list(f.keys())
['#refs#', '#subsystem#', 'tfrOut']
>>> tfr = f['tfrOut']
>>> tfr['signals']
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 506, in __getitem__
new_dtype = readtime_dtype(self.id.dtype, names)
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 48, in readtime_dtype
raise ValueError("Field names only allowed for compound types")
ValueError: Field names only allowed for compound types
>>> tfr['Time']
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 506, in __getitem__
new_dtype = readtime_dtype(self.id.dtype, names)
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 48, in readtime_dtype
raise ValueError("Field names only allowed for compound types")
ValueError: Field names only allowed for compound types
>>> tfr.dtype
dtype('uint32')
>>> tfr.ref
<HDF5 object reference>
>>> tfr.value
C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py:313: H5pyDeprecationWarning: dataset.value has been deprecated. Use dataset[()] instead.
"Use dataset[()] instead.", H5pyDeprecationWarning)
array([[3707764736, 2, 1, 1, 1,
5]], dtype=uint32)
>>> tfr[0,0]
3707764736
>>> tfr['Data:1'][0,0]
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 506, in __getitem__
new_dtype = readtime_dtype(self.id.dtype, names)
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\dataset.py", line 48, in readtime_dtype
raise ValueError("Field names only allowed for compound types")
ValueError: Field names only allowed for compound types
Clearly tfr is an object reference but I can't seem to do anything with it. Does anyone know how I can use this to actually address the timeseries data? Or even find my signals?
I have also tried this:
>>> for element in tfr:
... data = f[element][0][:]
...
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.2\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 2, in <module>
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\group.py", line 262, in __getitem__
oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "C:\Program Files\Python37\lib\site-packages\h5py\_hl\base.py", line 137, in _e
name = name.encode('ascii')
AttributeError: 'numpy.ndarray' object has no attribute 'encode'
>>> element
array([3707764736, 2, 1, 1, 1,
5], dtype=uint32)
Upvotes: 2
Views: 1976
Reputation: 11
This is not a solution but more of a workaround since I am struggling with the same issue. You can convert all the TimeSeries data in the *.mat
file into two dimensional arrays containing the Time and Data values from the TimeSeries, then save it to another *.mat
file. Once that is done and you load the file as you did above, trf.value
will display the array containing the data. Just remember to save the *.mat
file with the '-v7.3' option if you want to keep using h5py.
Upvotes: 1