local variable 'con_methods' can't be accessed

Hello,

When trying to compute spectral connectivity in source space epochs, this function will run to what seems like completion and then throw an error that the local variable ā€˜con_methodsā€™ canā€™t be accessed.

Thank you,
George

  • MNE version: 1.6.1
  • MNE-Connectivity version 0.6.0
  • operating system: Ubuntu 20.04
import mne_connectivity

method="wpli"
sfreq=400
fmin=4
fmax=8

con_epochs = mne_connectivity.spectral_connectivity_epochs(
            label_ts,
            method=method,
            mode="multitaper",
            sfreq=sfreq,
            fmin=fmin,
            fmax=fmax,
            mt_adaptive=True,
            n_jobs=1,
        )

Hello,

Can you share the full traceback of the error to figure out what failed on which line?

Thanks,
Mathieu

1 Like

Hi Mathieu,

Thanks for your response, I just figured it out.

I use indices of stimulus labels to separate label_ts by stimulus. One subject didnā€™t receive that stimulus type, so len(label_ts) == 0.

It would have helped to have a more accurate error thrown indicating something about label_ts being wrong, or something at least more related to the data itself. Since I only had one method selected, the con_methods error was really confusing.

Below is the full traceback of the error anyway.
Please let me know if I should let someone know about this.

Thanks,
George

---------------------------------------------------------------------------
	"name": "UnboundLocalError",
	"message": "cannot access local variable 'con_methods' where it is not associated with a value",
	"stack": "---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[6], line 4
2 for sub_id in sub_ids_CP:
3     print(sub_id)
----> 4     sub_avg_cons = fc.compute_sub_avg_con(
5         sub_id,
6         \"Chronic Pain\",
7         processed_data_path,
8         zscored_epochs_data_path,
9         EO_resting_data_path,
10         EC_resting_data_path,
11         con_methods,
12         conditions,
13         roi_names,
14         Freq_Bands,
15         tmin,
16         tmax,
17         sfreq,
18         plot_freq_matrix=False,
19     )
20     break

File ~/Documents/George Kenefati/Code/eeg_toolkit/eeg_toolkit/functional_connectivity.py:244, in compute_sub_avg_con(sub_id, group_name, processed_data_path, zscored_epochs_data_path, EO_resting_data_path, EC_resting_data_path, connectivity_methods, conditions, roi_names, Freq_Bands, tmin, tmax, sfreq, plot_freq_matrix)
242 print(tabulate(table, tablefmt=\"grid\"))
243 if isinstance(label_ts, list):
--> 244     con_epochs = compute_connectivity_epochs(
245         label_ts,
246         roi_names,
247         method,
248         fmin,
249         fmax,
250         tmin,
251         tmax,
252         sfreq,
253     )
254     # average epochs within subject first
255     con_epochs_mean = con_epochs.get_data()

File ~/Documents/George Kenefati/Code/eeg_toolkit/eeg_toolkit/functional_connectivity.py:126, in compute_connectivity_epochs(label_ts, roi_names, method, fmin, fmax, tmin, tmax, sfreq)
116 def compute_connectivity_epochs(
117     label_ts,
118     roi_names,
(...)
124     sfreq=400,
125 ):
--> 126     con_epochs = mne_conn.spectral_connectivity_epochs(
127         label_ts,
128         method=method,
129         mode=\"multitaper\",
130         sfreq=sfreq,
131         fmin=fmin,
132         fmax=fmax,
133         mt_adaptive=True,
134         n_jobs=1,
135     )
136     print(f\"*con_epochs shape = {con_epochs.shape}*\")
137     return con_epochs

File <decorator-gen-388>: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, gc_n_lags, rank, block_size, n_jobs, verbose)

File ~/mambaforge/envs/mne/lib/python3.11/site-packages/mne_connectivity/spectral/epochs.py:1161, 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, gc_n_lags, rank, block_size, n_jobs, verbose)
1159 con = list()
1160 patterns = list()
-> 1161 for method_i, conn_method in enumerate(con_methods):
1162     # future estimators will need to be handled here
1163     if conn_method.accumulate_psd:
1164         # compute scores block-wise to save memory
1165         for i in range(0, n_cons, block_size):

UnboundLocalError: cannot access local variable 'con_methods' where it is not associated with a value"
}

we are always interested in making error messages more useful / interpretable. So if you have time please do open an issue saying basically ā€œI passed in X and got unhelpful error message Y, please make error more informativeā€, and include a link back to this forum thread.

1 Like