eeg data is a list of lists, the inner lists are of different lengths, how to convert this data to numpy array

  • following this data sets MWT , I need to create Raw object with MNE, the data originally is .mat format files , I extract the metadat like the eeg_01 and eeg_02 and sample frequency when i read the data at the first time, now to create the raw object first of all i need to concatenate the two eeg data into one variables and there labels as well using :
# Concatenate 'eeg_O1_data' and 'eeg_O2_data' along axis 1 to create 'eeg_data'
eeg_data = np.hstack((eeg_O1_data, eeg_O2_data))

# Concatenate 'labels_O1' and 'labels_O2' along axis 1 to create 'eeg_labels'
eeg_labels = np.hstack((labels_O1, labels_O2))
  • but I always get this error :
> ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[23], line 4
      1 # EEG data 
      2 
      3 # Concatenate 'eeg_O1_data' and 'eeg_O2_data' along axis 1 to create 'eeg_data'
----> 4 eeg_data = np.hstack((eeg_O1_data, eeg_O2_data))
      6 # Concatenate 'labels_O1' and 'labels_O2' along axis 1 to create 'eeg_labels'
      7 eeg_labels = np.hstack((labels_O1, labels_O2))

File <__array_function__ internals>:200, in hstack(*args, **kwargs)

File ~/anaconda3/envs/mne/lib/python3.11/site-packages/numpy/core/shape_base.py:363, in hstack(tup, dtype, casting)
    359 if not overrides.ARRAY_FUNCTION_ENABLED:
    360     # raise warning if necessary
    361     _arrays_for_stack_dispatcher(tup, stacklevel=2)
--> 363 arrs = atleast_1d(*tup)
    364 if not isinstance(arrs, list):
    365     arrs = [arrs]

File <__array_function__ internals>:200, in atleast_1d(*args, **kwargs)

File ~/anaconda3/envs/mne/lib/python3.11/site-packages/numpy/core/shape_base.py:65, in atleast_1d(*arys)
     63 res = []
     64 for ary in arys:
---> 65     ary = asanyarray(ary)
     66     if ary.ndim == 0:
     67         result = ary.reshape(1)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (76,) + inhomogeneous part.
  • please advice me on this ?

and also how can i deal with the data which is a list with different lenghts inner lists to convert it to numpy array?

I tried with this :

#convert eeg_O1_data to numpy array to be compatible with mne requirements :

eeg_O1_data = np.array(eeg_O1_data)

but i got this error :slight_smile:

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[37], line 3
      1 #convert eeg_O1_data to numpy array to be compatible with mne requirements :
----> 3 eeg_O1_data = np.array(eeg_O1_data)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (76,) + inhomogeneous part.

Hi,

Numpy on purpose does not support this functionality. If you want to create arrays the dimensions of the vectors should match.

Your best chance here is to trim the data in a way that makes the dimensions equal and preserves the part of the signals you are interested in, or possibly add zeros. It really depends on the data you have though.

2 Likes