Cannot select Labels while applying inverse operator to Epochs object

In trying to compute STC from an Epochs object from just a select number of labels, I get the following error: TypeError: cannot unpack non-iterable Exception object.

I did a test run with just one Label object and I get the same error. I don’t understand why the Label becomes construed as an Exception object.

Any help would be greatly appreciated. Thanks :slight_smile:

OS details:

  • MNE version: 1.3.0
  • operating system: Ubuntu 20.04

Code snippet:

roi_names = ['rostralanteriorcingulate-lh', 'postcentral-lh', # Left ACC, Left S1,
             'insula-lh', 'superiorfrontal-lh', # Left Insula, Left DL-PFC,
             'medialorbitofrontal-lh', # Left Medial-PFC
             'rostralanteriorcingulate-rh', 'postcentral-rh', # Right ACC, Right S1
             'insula-rh', 'superiorfrontal-rh', # Right Insula, Right DL-PFC
             'medialorbitofrontal-rh'] # Right Medial-PFC

selected_labels = [mne.read_labels_from_annot(subject, regexp=roi, subjects_dir=subjects_dir)[0] for roi in roi_names]

apply_inverse_kwargs = dict(
    epochs=epochs,
    inverse_operator=inverse_operator,
    lambda2 = 1. / snr ** 2, # regularizer parameter (λ²)
    method='dSPM',
    pick_ori=None,
    verbose=True)

for i in range(int(len(selected_labels)/2)): # half because mne automatically saves both hemispheres
    #dSPM
    stc_tmp = mne.minimum_norm.apply_inverse_epochs(label=selected_labels[i],
                                                         **apply_inverse_kwargs)
    stc_tmp.save(save_path+f"{sub_num}_{selected_labels[i].name[:-3]}_dSPM",overwrite=True)
    

Full error:

Preparing the inverse operator for use...
    Scaled noise and source covariance from nave = 1 to nave = 1
    Created the regularized inverter
    Created an SSP operator (subspace dimension = 1)
    Created the whitener using a noise covariance matrix with rank 51 (1 small eigenvalues omitted)
    Computing noise-normalization factors (dSPM)...
[done]
Picked 52 channels from the data
Computing inverse...
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[22], line 19
     16 print(f"************\t{i}\t{selected_labels[i].name}\t\t************\n")
     18 #dSPM
---> 19 stc_dSPM_tmp = mne.minimum_norm.apply_inverse_epochs(label=selected_labels[i],
     20                                                      **apply_inverse_kwargs)
     21 stc_dSPM_tmp.save(save_path+f"{sub_num}_{selected_labels[i].name[:-3]}_{method1}",overwrite=True)
     23 #LCMV

File <decorator-gen-464>:10, in apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label, nave, pick_ori, return_generator, prepared, method_params, use_cps, verbose)

File ~/anaconda3/envs/eegenv/lib/python3.9/site-packages/mne/minimum_norm/inverse.py:1269, in apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label, nave, pick_ori, return_generator, prepared, method_params, use_cps, verbose)
   1262 stcs = _apply_inverse_epochs_gen(
   1263     epochs, inverse_operator, lambda2, method=method, label=label,
   1264     nave=nave, pick_ori=pick_ori, verbose=verbose, prepared=prepared,
   1265     method_params=method_params, use_cps=use_cps)
   1267 if not return_generator:
   1268     # return a list
-> 1269     stcs = [stc for stc in stcs]
   1271 return stcs

File ~/anaconda3/envs/eegenv/lib/python3.9/site-packages/mne/minimum_norm/inverse.py:1269, in <listcomp>(.0)
   1262 stcs = _apply_inverse_epochs_gen(
   1263     epochs, inverse_operator, lambda2, method=method, label=label,
   1264     nave=nave, pick_ori=pick_ori, verbose=verbose, prepared=prepared,
   1265     method_params=method_params, use_cps=use_cps)
   1267 if not return_generator:
   1268     # return a list
-> 1269     stcs = [stc for stc in stcs]
   1271 return stcs

File ~/anaconda3/envs/eegenv/lib/python3.9/site-packages/mne/minimum_norm/inverse.py:1164, in _apply_inverse_epochs_gen(epochs, inverse_operator, lambda2, method, label, nave, pick_ori, prepared, method_params, use_cps, verbose)
   1162 logger.info('Picked %d channels from the data' % len(sel))
   1163 logger.info('Computing inverse...')
-> 1164 K, noise_norm, vertno, source_nn = _assemble_kernel(
   1165     inv, label, method, pick_ori, use_cps)
   1167 tstep = 1.0 / epochs.info['sfreq']
   1168 tmin = epochs.times[0]

File <decorator-gen-461>:12, in _assemble_kernel(inv, label, method, pick_ori, use_cps, verbose)

File ~/anaconda3/envs/eegenv/lib/python3.9/site-packages/mne/minimum_norm/inverse.py:733, in _assemble_kernel(inv, label, method, pick_ori, use_cps, verbose)
    730 source_nn = inv['source_nn']
    732 if label is not None:
--> 733     vertno, src_sel = label_src_vertno_sel(label, src)
    735     if method not in ["MNE", "eLORETA"]:
    736         noise_norm = noise_norm[src_sel]

TypeError: cannot unpack non-iterable Exception object

I decided I do not want to use stc.save() since I would have to save each epoch individually. I’d rather just work with the object from memory.

I will leave up the question anyway in case a solution is required.

Thanks,
George

I could be entirely wrong here, but isn’t what you want to do selected_labels[0][i] in the for loop as you put the labels object into a list?