# Cannot select Labels while applying inverse operator to Epochs object

**URL:** <https://mne.discourse.group/t/cannot-select-labels-while-applying-inverse-operator-to-epochs-object/6923>\
**Category:** Support & Discussions\
**Tags:** ﻿source-localization, epochs\
**Created:** [May 18, 2023, 9:49pm UTC](https://mne.discourse.group/t/cannot-select-labels-while-applying-inverse-operator-to-epochs-object/6923 "2023-05-18T21:49:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgekenefati](https://avatars.discourse-cdn.com/v4/letter/g/34f0e0/32.png) [@georgekenefati](https://mne.discourse.group/u/georgekenefati)\
**Post date:** [May 18, 2023, 9:49pm UTC](https://mne.discourse.group/t/cannot-select-labels-while-applying-inverse-operator-to-epochs-object/6923/1 "2023-05-18T21:49:02Z")

</div>

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 🙂

**OS details:**

- MNE version: 1.3.0
- operating system: Ubuntu 20.04

**Code snippet:**

```python
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:**

```python
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

```

---

<div class="post-metadata">

**Author:** ![georgekenefati](https://avatars.discourse-cdn.com/v4/letter/g/34f0e0/32.png) [@georgekenefati](https://mne.discourse.group/u/georgekenefati)\
**Post date:** [May 19, 2023, 7:44pm UTC](https://mne.discourse.group/t/cannot-select-labels-while-applying-inverse-operator-to-epochs-object/6923/2 "2023-05-19T19:44:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TijmenWB](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/tijmenwb/32/2665_2.png) [@TijmenWB](https://mne.discourse.group/u/TijmenWB)\
**Post date:** [May 22, 2023, 3:27pm UTC](https://mne.discourse.group/t/cannot-select-labels-while-applying-inverse-operator-to-epochs-object/6923/3 "2023-05-22T15:27:26Z")

</div>

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?
