2

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)
2
  • If you have a matlab installation available, you can simply save matlab arrays to *.npy files using github.com/kwikteam/npy-matlab These can be loaded into Python by import numpy as np ; arr=np.load("File.npy") Commented Mar 14, 2019 at 9:23
  • unfortunately I do not. I'm stuck with the *.mat file I get I'll see if I can work with my colleague to adjust the output. Commented Mar 15, 2019 at 12:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I assume you're doing converting into two-dimensional arrays in matlab as I can't access the timeseries data from python.
Yes, sorry I wasn't clear. I did the conversion in Matlab to two dimensional arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.