error while converting a mat file to mne raw file

while converting a mat file using mne.io.raw Array function , a value error occured .
mne:lateset version
os: windows
my code is like ,

data = loadmat(filepath,struct_as_record=True)
dat = list(data.items())

np_arr = np.array(dat)

raw = mne.io.RawArray(np_arr,info)

This has raised a value error

ValueError                                Traceback (most recent call last)
<ipython-input-18-b04e9a0d29d1> in <module>()
      2 np_arr = np.array(np_arr)
      3 
----> 4 raw = mne.io.RawArray(np_arr,info)

<decorator-gen-210> in __init__(self, data, info, first_samp, copy, verbose)

1 frames
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py in asanyarray(a, dtype, order)
    134 
    135     """
--> 136     return array(a, dtype, copy=False, order=order, subok=True)
    137 
    138 

ValueError: could not convert string to float: '__header__'

your np_arr is probably not an ndarray of float values. Try to print(np_arr) to confirm that this is the case. You should also do print(data) and inspect which part of the data loaded from the mat file contains the array you want to turn into a RawArray.

For example, this is what loading some mat file I had locally via loadmat looks like:

{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri Jan 20 11:38:07 2017',
 '__version__': '1.0',
 '__globals__': [],
 'choiceMat': array([[[2.        , 1.        , 1.        , ..., 1.        ,
          1.        , 2.        ],
         [1.        , 1.        , 1.        , ..., 2.        ,
          2.        , 2.        ],
         [1.        , 2.        , 2.        , ..., 2.        ,
          2.        , 2.        ],
         ...,
         [1.        , 2.        , 1.        , ..., 2.        ,
          2.        , 1.        ],
         [1.        , 2.        , 1.        , ..., 2.        ,
          2.        , 1.        ],
         [1.        , 2.        , 1.        , ..., 2.        ,
          2.        , 1.        ]],
...

In this case, instead of doing dat = list(data.items()), one should do dat = data["choiceMat"] (or whatever your array in the matfile is called. In my case, it happens to be "choiceMat").

You can then confirm that this is the array you expected by inspecting dat.shape, dat.dtype, and so on.

3 Likes

i print the mat file.

{'__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Mon Jul  7 19:24:17 2008', '__version__': '1.0', '__globals__': [], 'mrk': array([[(array([[  2095,   2895,   3695,   4495,   5295,   6095,   6895,   7695,
          8495,   9295,  10095,  10895,  11695,  12495,  13295,  16294,
         17094,  17894,  18694,  19494,  20294,  21094,  21894,  22694,
         23494,  24295,  25095,  25895,  26695,  27495,  30494,  31294,
         32094,  32894,  33694,  34494,  35294,  36094,  36894,  37694,
         38494,  39294,  40094,  40894,  41694,  44693,  45493,  46293,
         47093,  47893,  48693,  49493,  50293,  51093,  51893,  52693,
         53493,  54293,  55093,  55893,  58892,  59692,  60492,  61292,
         62092,  62892,  63692,  64492,  65292,  66093,  66893,  67693,
         68493,  69293,  70093,  73092,  73892,  74692,  75492,  76292,
         77092,  77892,  78692,  79492,  80292,  81092,  81892,  82692,
         83492,  84292,  87291,  88091,  88891,  89691,  90491,  91291,
         92091,  92891,  93691,  94491,  97292,  98092,  98892,  99692,
        100492, 101292, 102092, 102892, 103692, 104492, 105292, 106092,
        106892, 107692, 108492, 111491, 112291, 113091, 113891, 114691,
        115491, 116291, 117091, 117891, 118691, 119492, 120292, 121091,
        121891, 122692, 125691, 126491, 127291, 128091, 128891, 129691,
        130491, 131291, 132091, 132891, 133691, 134491, 135291, 136091,
        136891, 139890, 140690, 141490, 142290, 143090, 143890, 144690,
        145490, 146290, 147090, 147890, 148690, 149490, 150290, 151090,
        154089, 154889, 155689, 156489, 157289, 158090, 158890, 159690,
        160490, 161290, 162090, 162890, 163690, 164490, 165290, 168289,
        169089, 169889, 170689, 171489, 172289, 173089, 173889, 174689,
        175489, 176289, 177089, 177890, 178689, 179489, 182488, 183288,
        184088, 184888, 185688, 186488, 187288, 188088, 188888, 189688]],
      dtype=int32), array([[ 1,  1,  1, -1,  1,  1,  1, -1,  1,  1,  1, -1,  1, -1, -1, -1,
         1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1, -1,  1, -1,  1,  1,
         1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1,  1,  1,  1, -1,
        -1, -1,  1, -1, -1, -1, -1,  1,  1, -1, -1,  1, -1, -1, -1,  1,
         1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1,  1, -1,  1,  1, -1,
         1,  1, -1, -1,  1,  1, -1,  1, -1,  1,  1, -1,  1, -1, -1,  1,
        -1, -1, -1, -1, -1, -1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1,
         1,  1,  1,  1, -1, -1,  1, -1,  1, -1, -1,  1,  1,  1,  1,  1,
        -1,  1,  1, -1,  1, -1, -1,  1, -1,  1, -1, -1, -1, -1,  1,  1,
        -1,  1, -1,  1, -1, -1, -1,  1,  1, -1,  1, -1,  1, -1, -1, -1,
         1,  1,  1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1,  1,  1,  1,
        -1,  1,  1, -1, -1,  1,  1, -1,  1,  1, -1, -1, -1,  1,  1,  1,
         1,  1, -1, -1,  1,  1, -1, -1]], dtype=int16))]],
      dtype=[('pos', 'O'), ('y', 'O')]), 'cnt': array([[  -36,  -138,   -50, ...,   -58,   -81,  -270],
       [ -203,  -774,  -280, ...,  -308,  -438, -1479],
       [ -384, -1463,  -517, ...,  -532,  -787, -2709],
       ...,
       [ -256,  -262,    13, ...,  -136,  -185,   106],
       [ -287,  -243,    -8, ...,  -148,  -204,   193],
       [ -289,  -158,    -2, ...,  -123,  -195,   322]], dtype=int16), 'nfo': array([[(array([[100]], dtype=uint8), array([[array(['left'], dtype='<U4'), array(['right'], dtype='<U5')]],
      dtype=object), array([[array(['AF3'], dtype='<U3'), array(['AF4'], dtype='<U3'),
        array(['F5'], dtype='<U2'), array(['F3'], dtype='<U2'),
        array(['F1'], dtype='<U2'), array(['Fz'], dtype='<U2'),
        array(['F2'], dtype='<U2'), array(['F4'], dtype='<U2'),
        array(['F6'], dtype='<U2'), array(['FC5'], dtype='<U3'),
        array(['FC3'], dtype='<U3'), array(['FC1'], dtype='<U3'),
        array(['FCz'], dtype='<U3'), array(['FC2'], dtype='<U3'),
        array(['FC4'], dtype='<U3'), array(['FC6'], dtype='<U3'),
        array(['CFC7'], dtype='<U4'), array(['CFC5'], dtype='<U4'),
        array(['CFC3'], dtype='<U4'), array(['CFC1'], dtype='<U4'),
        array(['CFC2'], dtype='<U4'), array(['CFC4'], dtype='<U4'),
        array(['CFC6'], dtype='<U4'), array(['CFC8'], dtype='<U4'),
        array(['T7'], dtype='<U2'), array(['C5'], dtype='<U2'),
        array(['C3'], dtype='<U2'), array(['C1'], dtype='<U2'),
        array(['Cz'], dtype='<U2'), array(['C2'], dtype='<U2'),
        array(['C4'], dtype='<U2'), array(['C6'], dtype='<U2'),
        array(['T8'], dtype='<U2'), array(['CCP7'], dtype='<U4'),
        array(['CCP5'], dtype='<U4'), array(['CCP3'], dtype='<U4'),
        array(['CCP1'], dtype='<U4'), array(['CCP2'], dtype='<U4'),
        array(['CCP4'], dtype='<U4'), array(['CCP6'], dtype='<U4'),
        array(['CCP8'], dtype='<U4'), array(['CP5'], dtype='<U3'),
        array(['CP3'], dtype='<U3'), array(['CP1'], dtype='<U3'),
        array(['CPz'], dtype='<U3'), array(['CP2'], dtype='<U3'),
        array(['CP4'], dtype='<U3'), array(['CP6'], dtype='<U3'),
        array(['P5'], dtype='<U2'), array(['P3'], dtype='<U2'),
        array(['P1'], dtype='<U2'), array(['Pz'], dtype='<U2'),
        array(['P2'], dtype='<U2'), array(['P4'], dtype='<U2'),
        array(['P6'], dtype='<U2'), array(['PO1'], dtype='<U3'),
        array(['PO2'], dtype='<U3'), array(['O1'], dtype='<U2'),
        array(['O2'], dtype='<U2')]], dtype=object), array([[-0.20109028],
       [ 0.20109028],
       [-0.48547489],
       [-0.32894737],
       [-0.16535231],
       [ 0.        ],
       [ 0.16535231],
       [ 0.32894737],
       [ 0.48547489],
       [-0.60591541],
       [-0.39919579],
       [-0.19765935],
       [ 0.        ],
       [ 0.19765935],
       [ 0.39919579],
       [ 0.60591541],
       [-0.74834683],
       [-0.52472976],
       [-0.30963911],
       [-0.10226303],
       [ 0.10226303],
       [ 0.30963911],
       [ 0.52472976],
       [ 0.74834683],
       [-0.87719298],
       [-0.64569058],
       [-0.421549  ],
       [-0.20773757],
       [ 0.        ],
       [ 0.20773757],
       [ 0.421549  ],
       [ 0.64569058],
       [ 0.87719298],
       [-0.74834683],
       [-0.52472976],
       [-0.30963911],
       [-0.10226303],
       [ 0.10226303],
       [ 0.30963911],
       [ 0.52472976],
       [ 0.74834683],
       [-0.60591541],
       [-0.39919579],
       [-0.19765935],
       [ 0.        ],
       [ 0.19765935],
       [ 0.39919579],
       [ 0.60591541],
       [-0.48547489],
       [-0.32894737],
       [-0.16535231],
       [ 0.        ],
       [ 0.16535231],
       [ 0.32894737],
       [ 0.48547489],
       [-0.10395865],
       [ 0.10395865],
       [-0.17113186],
       [ 0.17113186]]), array([[ 0.68656518],
       [ 0.68656518],
       [ 0.52547424],
       [ 0.46520183],
       [ 0.43208641],
       [ 0.421549  ],
       [ 0.43208641],
       [ 0.46520183],
       [ 0.52547424],
       [ 0.27165704],
       [ 0.23384348],
       [ 0.21394494],
       [ 0.20773757],
       [ 0.21394494],
       [ 0.23384348],
       [ 0.27165704],
       [ 0.15177169],
       [ 0.12553103],
       [ 0.11086096],
       [ 0.10426648],
       [ 0.10426648],
       [ 0.11086096],
       [ 0.12553103],
       [ 0.15177169],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [ 0.        ],
       [-0.15177169],
       [-0.12553103],
       [-0.11086096],
       [-0.10426648],
       [-0.10426648],
       [-0.11086096],
       [-0.12553103],
       [-0.15177169],
       [-0.27165704],
       [-0.23384348],
       [-0.21394494],
       [-0.20773757],
       [-0.21394494],
       [-0.23384348],
       [-0.27165704],
       [-0.52547424],
       [-0.46520183],
       [-0.43208641],
       [-0.421549  ],
       [-0.43208641],
       [-0.46520183],
       [-0.52547424],
       [-0.65583812],
       [-0.65583812],
       [-0.86033797],
       [-0.86033797]]))]],
      dtype=[('fs', 'O'), ('classes', 'O'), ('clab', 'O'), ('xpos', 'O'), ('ypos', 'O')])}

and then np_array = dat[‘mrk’] is like,

[[(array([[  2095,   2895,   3695,   4495,   5295,   6095,   6895,   7695,
          8495,   9295,  10095,  10895,  11695,  12495,  13295,  16294,
         17094,  17894,  18694,  19494,  20294,  21094,  21894,  22694,
         23494,  24295,  25095,  25895,  26695,  27495,  30494,  31294,
         32094,  32894,  33694,  34494,  35294,  36094,  36894,  37694,
         38494,  39294,  40094,  40894,  41694,  44693,  45493,  46293,
         47093,  47893,  48693,  49493,  50293,  51093,  51893,  52693,
         53493,  54293,  55093,  55893,  58892,  59692,  60492,  61292,
         62092,  62892,  63692,  64492,  65292,  66093,  66893,  67693,
         68493,  69293,  70093,  73092,  73892,  74692,  75492,  76292,
         77092,  77892,  78692,  79492,  80292,  81092,  81892,  82692,
         83492,  84292,  87291,  88091,  88891,  89691,  90491,  91291,
         92091,  92891,  93691,  94491,  97292,  98092,  98892,  99692,
        100492, 101292, 102092, 102892, 103692, 104492, 105292, 106092,
        106892, 107692, 108492, 111491, 112291, 113091, 113891, 114691,
        115491, 116291, 117091, 117891, 118691, 119492, 120292, 121091,
        121891, 122692, 125691, 126491, 127291, 128091, 128891, 129691,
        130491, 131291, 132091, 132891, 133691, 134491, 135291, 136091,
        136891, 139890, 140690, 141490, 142290, 143090, 143890, 144690,
        145490, 146290, 147090, 147890, 148690, 149490, 150290, 151090,
        154089, 154889, 155689, 156489, 157289, 158090, 158890, 159690,
        160490, 161290, 162090, 162890, 163690, 164490, 165290, 168289,
        169089, 169889, 170689, 171489, 172289, 173089, 173889, 174689,
        175489, 176289, 177089, 177890, 178689, 179489, 182488, 183288,
        184088, 184888, 185688, 186488, 187288, 188088, 188888, 189688]],
      dtype=int32), array([[ 1,  1,  1, -1,  1,  1,  1, -1,  1,  1,  1, -1,  1, -1, -1, -1,
         1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1, -1,  1, -1,  1,  1,
         1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,  1,  1,  1,  1, -1,
        -1, -1,  1, -1, -1, -1, -1,  1,  1, -1, -1,  1, -1, -1, -1,  1,
         1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1,  1, -1,  1,  1, -1,
         1,  1, -1, -1,  1,  1, -1,  1, -1,  1,  1, -1,  1, -1, -1,  1,
        -1, -1, -1, -1, -1, -1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1,
         1,  1,  1,  1, -1, -1,  1, -1,  1, -1, -1,  1,  1,  1,  1,  1,
        -1,  1,  1, -1,  1, -1, -1,  1, -1,  1, -1, -1, -1, -1,  1,  1,
        -1,  1, -1,  1, -1, -1, -1,  1,  1, -1,  1, -1,  1, -1, -1, -1,
         1,  1,  1,  1, -1,  1, -1, -1,  1, -1, -1,  1, -1,  1,  1,  1,
        -1,  1,  1, -1, -1,  1,  1, -1,  1,  1, -1, -1, -1,  1,  1,  1,
         1,  1, -1, -1,  1,  1, -1, -1]], dtype=int16))]]

and the print type(np_arr) and np_arr.shape has given me like ,

<class 'numpy.ndarray'>
(1, 1)

Thanks for your post. Is there any way to find name of array (i.e. “choiceMat”) automatically without its printing? I have lots of data, and its not possible to check what is their array name.

.mat files can contain a lot of things. If you don’t have any documentation about the data at hand (any README shipped with the data? contact to the original curator of the data?), then you will have to do some detective work.

That could include looping over all your .mat files, and collecting the keys from the dict you get after calling loadmat. It would also mean spending some time understanding the data behind each of these keys and deciding which of these data you actually need to work on.

1 Like