The objective is to plot the connectivity for specific bands which has been calculated using the ‘cwt_morlet’.
However, I’m not so clear how to extract information for specific band-range (e.g., delta, theta, alpha,beta,gamma) from the con
array of the spectral_connectivity
.
The snippet below is to plot circular graph
for each band for each connectivity measure method
for con ,measure_type in zip(epochs.ch_names,set_data['connectivity_methods']):
all_fig = [plot_conn ( con [:, :, idx], all_ch, idx, band) for idx, band in
enumerate ( ["delta", "theta", "alpha", "beta", "gamma"] )]
However, I’m not sure what is the proper setting to select specific freq band (e.g., alpha) in the con [:, :, idx]
.
Appreciate for any insight about this.
The full code together with the explanation is as below.
import mne
from mne.connectivity import spectral_connectivity
from mne.viz import circular_layout, plot_connectivity_circle
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
# Formating setting
Freq_Bands = {"delta": [1.25, 4.0],"theta": [4.0, 8.0],
"alpha": [8.0, 13.0],"beta": [13.0, 30.0],"gamma": [30.0, 49.0]}
nband=len(Freq_Bands)
# Setting for number of channels, connectivity ploting, and frequency range for the 'cwt_morlet' calclation
set_data = {'label_names': ['FP1', 'FP2', 'F3', 'F4', 'F7', 'F8', 'C3', 'C4','T3', 'T4', 'O1', 'O2'],
'lh_labels': ['FP1', 'F7', 'F3', 'C3', 'T3', 'O1'],
'rh_labels': ['FP2', 'F8', 'F4', 'C4', 'T4', 'O2'], 'connectivity_methods': ["coh",'wpli2_debiased'],
'cwt_freqs' :np.arange(1, 49, 2),'cwt_n_cycles': np.arange(1, 49, 2) / 5.}
# Setting for the epochs
sig_setting=dict(n_epochs = 5,n_channels = len ( set_data ['label_names'] ),n_times = 1000,
sfreq = 256,wave_freq = 10 )
def create_epochs ():
np.random.seed ( 42 )
data = np.random.rand ( sig_setting['n_epochs'], sig_setting['n_channels'], sig_setting['n_times'] )
epoch_len = sig_setting['n_times'] / sig_setting['sfreq']
for i in range ( sig_setting['n_epochs']):
for c in range ( sig_setting['n_channels'] ):
phase = np.random.rand ( 1 ) * 10
data [i, c] = np.squeeze ( np.sin ( np.linspace ( -sig_setting['wave_freq'] * epoch_len * np.pi + phase,
sig_setting['wave_freq'] * epoch_len * np.pi + phase, sig_setting['n_times'] ) ) )
return mne.EpochsArray ( data, mne.create_info ( ch_names=set_data ['label_names'],
ch_types=['eeg'] * len ( set_data ['label_names'] ),
sfreq=sig_setting['sfreq']) )
def plot_conn (conmat, all_ch, idx, bands, set_data):
# Generate circular graph
node_order = set_data ['lh_labels'] + set_data ['rh_labels']
node_angles = circular_layout ( all_ch, node_order, start_pos=90,
group_boundaries=[0, len ( all_ch ) // 2] )
fig = plt.figure ( num=None, figsize=(8, 8), facecolor='black' )
canvas = FigureCanvas ( fig )
plot_connectivity_circle ( conmat, all_ch, n_lines=300,
node_angles=node_angles,
title=f'All-to-All Connectivity_ band_{bands}', fig=fig )
canvas.draw ()
s, (width, height) = canvas.print_to_buffer ()
im0 = np.frombuffer ( s, np.uint8 ).reshape ( (height, width, 4) )
return im0
# Create the epochs
epochs=epochs=create_epochs ( )
# Get the connectivity measure
con_all, freqs, times, _, _ = spectral_connectivity(epochs,method=set_data['connectivity_methods'],
mode='cwt_morlet', sfreq=epochs.info['sfreq'] ,
cwt_freqs=set_data['cwt_freqs'], cwt_n_cycles=set_data['cwt_n_cycles'], n_jobs=1)
# Plot the connectivity
for con ,measure_type in zip(epochs.ch_names,set_data['connectivity_methods']):
all_fig = [plot_conn ( con [:, :, idx], all_ch, idx, band) for idx, band in
enumerate ( ["delta", "theta", "alpha", "beta", "gamma"] )]
plt.imsave ( f'connectivity_{measure_type}.png', np.hstack ( all_fig ) )
Due to complain received in last post, I had trimmed the code-structure to a reasonable mean possible without compromising the readability level.