I am trying to implement connectivity as a feature within my code, but am unsure of how to fix this error code. Here is my code up until the point of the error.

import mne
import matplotlib.pyplot as plt
import numpy as np

from mne.time_frequency import psd_welch
from mne.connectivity import spectral_connectivity

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, plot_confusion_matrix
from sklearn.metrics import confusion_matrix
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import FunctionTransformer
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import KFold, cross_val_predict

#Importing data
pre = mne.io.read_raw_eeglab('Subject31pre.set')
prop = mne.io.read_raw_eeglab('Subject33prop.set')

#Create events in regular intervals, used as markers to generate epochs
preEvents = mne.make_fixed_length_events(
    pre, id=1, start=0, stop=None, duration=1.0, first_samp=True, overlap=0.0)
propEvents = mne.make_fixed_length_events(
    prop, id=2, start=0, stop=None, duration=1.0, first_samp=True, overlap=0.0)

#Plotting of Power Spectral Density
pre.plot(duration=20, n_channels=64)
prop.plot(duration=20, n_channels=64)

#Epoch parameters
event_id = None
tmin = 0
tmax = 1
baseline = None
picks = None
reject = None

#Definition of epochs, for both pre and prop data
preEpochs = mne.Epochs(pre, preEvents, event_id, tmin, tmax, proj=True, baseline=baseline, picks=picks, reject=reject, preload=True)
propEpochs = mne.Epochs(prop, propEvents, event_id, tmin, tmax, proj=True, baseline=baseline, picks=picks, reject=reject, preload=True)

#Merge all epochs into one variable
epochsAll = mne.concatenate_epochs([preEpochs, propEpochs])

def eeg_connectivity(epochs):
    FREQ_BANDS = {"delta": [0.5, 4.5],
              "theta": [4.5, 8.5],
              "alpha": [8.5, 11.5],
              "sigma": [11.5, 15.5],
              "beta": [15.5, 30]}
    print("inside: ",type(epochs))

    sfreq = epochs.info['sfreq']

    con, freqs, times, n_epochs, n_tapers = \
        spectral_connectivity(epochs, method='coh', mode='multitaper', sfreq=sfreq,
        fmin=0.5, fmax=30, faverage=True, n_jobs=1)

    freqsNd = np.array(freqs)

    X = []
    for fmin, fmax in FREQ_BANDS.values():
        con_band = con[:, :, (freqsNd >= fmin) & (freqsNd < fmax)].mean(axis=-1)
        X.append(con_band.reshape(len(con), -1))  # -1

    return np.concatenate(X, axis=-1)

And here is the error received:

  File "", line 71, in eeg_connectivity
    con_band = con[:, :, (freqsNd >= fmin) & (freqsNd < fmax)].mean(axis=-1)
IndexError: too many indices for array: array is 3-dimensional, but 4 were indexed

Thank you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.