Bug in mne_connectivity.spectral_connectivity_epochs

I am using mne version 3.10.10 on Windows 10

I am performing an EEG connectivity studi on resting state EEG, but i am not an expert.

When developing my code for the preprocessing and feature extraction, I got following error code when using the code below. MNE was successful in computing the first connectivity matrix but when starting the second one it gives this weird error and i really don’t know what is happening.

I always get this error if i run the command spectral_connectivity_epochs more than once!

Can someone help me?

Lots of thanks from a Thesis Student!

ps: label_ts β†’ labeled time series data per epoch with respect to a parcellation of the brain (Freesurfer aparc 69 parcelation)

conn = []
indices = ([0, 0, 0, 0, 0], [1, 2, 3, 4, 5])
for method in ['pli', 'wpli', 'dpli']:
    conn.append(
        spectral_connectivity_epochs(
        label_ts, 
        method = method, 
        sfreq = sfreq,
        indices = indices,
        faverage = True,
        n_jobs = 1
        ).get_data()[:,0]
    )
conn

THE ERROR message!!!

computing connectivity for epoch 166 [done] [Connectivity computation done] 

Connectivity computation...

--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) 
Cell In[50], line 5 
 indices = ([0, 0, 0, 0, 0], [1, 2, 3, 4, 5]) 
 for method in ['pli', 'wpli', 'dpli']: 
 conn.append( ----> 
 spectral_connectivity_epochs( 
 label_ts, 
 method = method, 
 sfreq = sfreq, 
 indices = indices,
 faverage = True, 
 n_jobs = 1  
).get_data()[:,0] 
 ) 
 conn 
File :12, in spectral_connectivity_epochs(data, names, method, indices, sfreq, mode, fmin, fmax, fskip, faverage, tmin, tmax, mt_bandwidth, mt_adaptive, mt_low_bias, cwt_freqs, cwt_n_cycles, block_size, n_jobs, verbose) 
File [c:\Users\arons\anaconda_New3\envs\mne\lib\site-packages\mne_connectivity\spectral\epochs.py:1126](file:///C:/Users/arons/anaconda_New3/envs/mne/lib/site-packages/mne_connectivity/spectral/epochs.py:1126), in spectral_connectivity_epochs(data, names, method, indices, sfreq, mode, fmin, fmax, fskip, faverage, tmin, tmax, mt_bandwidth, mt_adaptive, mt_low_bias, cwt_freqs, cwt_n_cycles, block_size, n_jobs, verbose) 
1124 # compute final connectivity scores 
1125 con = list() -> 
1126 for conn_method, n_args in zip(con_methods, n_comp_args): 
1127 # future estimators will need to be handled here 
1128 if n_args == 3: 
1129 # compute all scores at once 
1130 conn_method.compute_con(slice(0, n_cons), n_epochs) 
UnboundLocalError: local variable 'con_methods' referenced before assignment

I found the solution, I used a generator variable as the src estimate but this gets exhausted if you use it once!!
I solved it by just writing a function that creates the generator once it is called upon:

def Label(inv,labels,epochs):
    snr = 1.0
    lambda2 = 1.0 / snr ** 2
    stcs = apply_inverse_epochs(epochs, inv,lambda2, method = "dSPM",
                            pick_ori=None, return_generator=True)
    src = inv['src']
    label_ts = mne.extract_label_time_course(stcs, labels, src, mode='mean_flip',
                                         return_generator=True, allow_empty = True)
    return label_ts

and then implementing it as label_ts in the code provided in the question above.