def preprocessNormalizeDownsample(self,
                                      df,
                                      df_interictal,
                                      fs,
                                      fsds,
                                      montage="bipolar",
                                      prewhiten=True):
        #% Preprocessing
        data, data_ref, _, data_filt, channels = echobase.preprocess(
            df, fs, fsds, montage=montage, prewhiten=prewhiten)
        dataII, _, _, dataII_filt, channelsII = echobase.preprocess(
            df_interictal, fs, fsds, montage=montage, prewhiten=prewhiten)

        ###DID NOT CHECK IF channels and channelsII are equal. echobase.preprocess may got rid of channels in one but not the other if there are any Nans.

        print("\n\n\nNormalizing data")
        #normalize
        dataII_scaler = echomodel.scaleData(dataII_filt, dataII_filt)
        data_scaler = echomodel.scaleData(data_filt, dataII_filt)
        #get annotated segments of preprocessed data
        #alter annotations to include time_step seconds beforehand
        #downsample
        print("\n\n\nDownsampling data")
        dataII_scalerDS = self.downsample(dataII_scaler, fs, fsds)
        data_scalerDS = self.downsample(data_scaler, fs, fsds)
        return dataII_scaler, data_scaler, dataII_scalerDS, data_scalerDS, channels
Ejemplo n.º 2
0

patientsWithseizures = DataJson.get_patientsWithSeizuresAndInterictal()

#%% generate example EEG

i = 42
sub = np.array(patientsWithseizures["subject"])[i]
idKey = np.array(patientsWithseizures["idKey"])[i]
AssociatedInterictal = np.array(patientsWithseizures["AssociatedInterictal"])[i]

df, fs, ictalStartIndex, ictalStopIndex = DataJson.get_precitalIctalPostictal(sub, "Ictal", idKey, username, password, BIDS = BIDS, dataset= dataset, session = session, secondsBefore = secondsBeforeSpread, secondsAfter = secondsAfterSpread)
df_interictal, _ = DataJson.get_iEEGData(sub, "Interictal", AssociatedInterictal, username, password, BIDS = BIDS, dataset= dataset, session = session, startKey = "Start")
print("\n\n\nPreprocessing\n\n\n")
dataII_scaler, data_scaler, dataII_scalerDS, data_scalerDS, channels = DataJson.preprocessNormalizeDownsample(df, df_interictal, fs, fsds, montage = montage, prewhiten = prewhiten)
data, data_ref, _, _, channels = echobase.preprocess(df, fs, fsds, montage = montage, prewhiten = prewhiten)



data_plotFilt = echobase.elliptic_bandFilter(data_ref, int(fs))[0]

nsamp, nchan = df.shape

#c = [62,61,60,59,58,53,52,51,43,42,36,32,31,27,21,17,7,6,5]#car
c = [47,45,64, 63,62,59,40,33,27,26, 18, 17,16,6,5] #bipolar
#c.reverse()
c_copy = copy.deepcopy(c)
fs_plot = copy.deepcopy(fs)

data_scalerDSPlot = data_scalerDS[:,c]
i = 86#RID0309: 33-43 ; RID0278:19; RID0596: 86
RID = np.array(patientsWithseizures["subject"])[i]
idKey = np.array(patientsWithseizures["idKey"])[i]
AssociatedInterictal = np.array(patientsWithseizures["AssociatedInterictal"])[i]


#%%
df, fs, ictalStartIndex, ictalStopIndex = DataJson.get_precitalIctalPostictal(RID, "Ictal", idKey, username, password,BIDS = BIDS, dataset= datasetiEEG, session = session, secondsBefore = secondsBefore, secondsAfter = secondsAfter)
df_interictal, _ = DataJson.get_iEEGData(RID, "Interictal", AssociatedInterictal, username, password, BIDS = BIDS, dataset= datasetiEEG, session = session, startKey = "Start")

                   
print("preprocessing")
dataII_scaler, data_scaler, dataII_scalerDS, data_scalerDS, channels = DataJson.preprocessNormalizeDownsample(df, df_interictal, fs, fsds, montage = montage)


data, data_avgref, data_ar, data_filt, channels = echobase.preprocess(df, fs, fsds, montage = montage)
data_scalerDSDS = DataJson.downsample(data_scaler, fs, 16)
data_arDSDS = DataJson.downsample(data_ar, fs, 16)

nsamp, nchan = data_filt.shape



# %% Plot eeg  Optional
#DataJson.plot_eeg(data_scalerDSDS, 16, markers = [secondsBefore*16], dpi = 25, aspect=aspect*2, height=nchan/aspect/2)  

#for plotting eeg of 595
#data_DSDS = DataJson.downsample(data_filt, fs, 16)
#DataJson.plot_eeg(data_DSDS, 16, markers = [secondsBefore*16], dpi = 25, aspect=aspect*2, height=nchan/aspect/2)  

#%%Meaure seizure spread
Ejemplo n.º 4
0
#%%Get data

patientsWithseizures = DataJson.get_patientsWithSeizuresAndInterictal()


i = 0
RID = np.array(patientsWithseizures["subject"])[i]
idKey = np.array(patientsWithseizures["idKey"])[i]
AssociatedInterictal = np.array(patientsWithseizures["AssociatedInterictal"])[i]

df, fs, ictalStartIndex, ictalStopIndex = DataJson.get_precitalIctalPostictal(RID, "Ictal", idKey, username, password, fpath = fpath_EEG, secondsBefore = secondsBeforeSpread, secondsAfter = secondsAfterSpread)
df_interictal, _ = DataJson.get_iEEGData(RID, "Interictal", AssociatedInterictal, username, password, fpath = fpath_EEG, startKey = "Start")
print("preprocessing")
dataII_scaler, data_scaler, dataII_scalerDS, data_scalerDS = DataJson.preprocessNormalizeDownsample(df, df_interictal, fs, fsds)
data, data_avgref, data_ar, data_filt = echobase.preprocess(df, fs, fsds)
data_scalerDSDS = DataJson.downsample(data_scaler, fs, 16)
data_arDSDS = DataJson.downsample(data_ar, fs, 16)

nsamp, nchan = df.shape

DataJson.plot_eeg(data_scalerDSDS, 16, markers = [secondsBeforeSpread*16], nchan = nchan, dpi = 25, aspect=aspect*2, height=nchan/aspect/2)  


#%%window to fit model
data_scalerDS_X = echomodel.overlapping_windows(data_scalerDS, time_step, skip)
windows, _, _ = data_scalerDS_X.shape

probWN = np.zeros(shape = (windows, nchan))  
probCNN = np.zeros(shape = (windows, nchan))  
probLSTM = np.zeros(shape = (windows, nchan))  
    def get_FunctionalConnectivity(self,
                                   sub,
                                   idKey,
                                   username,
                                   password,
                                   BIDS,
                                   dataset,
                                   session,
                                   functionalConnectivityPath=None,
                                   secondsBefore=30,
                                   secondsAfter=30,
                                   startKey="EEC",
                                   fsds=256,
                                   montage="bipolar",
                                   FCtype="pearson"):

        #checking if file is saved. If true, then just pull from that file
        if not functionalConnectivityPath == None:
            FCname = utils.baseSplitext(
                self.getEEGfileName(sub, "Ictal", idKey, BIDS, dataset,
                                    session, secondsBefore, secondsAfter))[:-4]
            fname = join(
                functionalConnectivityPath, FCname +
                f"functionalConnectivity_{FCtype}_{montage}_interictalPreictalIctalPostictal.pickle"
            )

        if utils.checkIfFileExists(fname, printBOOL=False):
            FC = utils.open_pickle(fname)
            return FC
        else:
            # get data
            AssociatedInterictalidKey = self.get_associatedInterictal(
                sub, idKey)
            seizure, fs, ictalStartIndex, ictalStopIndex = self.get_precitalIctalPostictal(
                sub,
                "Ictal",
                idKey,
                username,
                password,
                BIDS,
                dataset,
                session,
                secondsBefore=180,
                secondsAfter=180,
                load=True)

            interictal, fs = self.get_iEEGData(sub,
                                               "Interictal",
                                               AssociatedInterictalidKey,
                                               username,
                                               password,
                                               BIDS,
                                               dataset,
                                               session,
                                               startKey="Start",
                                               load=True)

            print("\nPreprocessing data: Filtering and Downsampling")
            ###filtering and downsampling
            if FCtype == "coherence":
                #Get Not prewhitened data (coherence measurement should not be used with pre-whitened data)
                _, _, _, seizureFilt, channels = echobase.preprocess(
                    seizure, fs, fs, montage=montage, prewhiten=False)
                _, _, _, interictalFilt, _ = echobase.preprocess(
                    interictal, fs, fs, montage=montage, prewhiten=False)
            else:
                #Get prewhitened data
                _, _, _, seizureFilt, channels = echobase.preprocess(
                    seizure, fs, fs, montage=montage, prewhiten=True)
                _, _, _, interictalFilt, _ = echobase.preprocess(
                    interictal, fs, fs, montage=montage, prewhiten=True)

            ictalStartIndexDS = int(ictalStartIndex * (fsds / fs))
            ictalStopIndexDS = int(ictalStopIndex * (fsds / fs))
            #downsample
            seizureFiltDS = self.downsample(seizureFilt, fs, fsds)
            interictalFiltDS = self.downsample(interictalFilt, fs, fsds)

            data = [
                interictalFiltDS, seizureFiltDS[:ictalStartIndexDS, :],
                seizureFiltDS[ictalStartIndexDS:ictalStopIndexDS + 1, :],
                seizureFiltDS[ictalStopIndexDS + 1:, :]
            ]
            data_FC = []
            if FCtype == "pearson":
                for x in range(4):
                    print(f"\n\n {x}/3")
                    data_FC.append(
                        list(echobase.pearson_wrapper(data[x], fsds)
                             [0]))  #ind at [0] because don't need the p-values
            if FCtype == "crossCorrelation":
                for x in range(4):
                    print(f"\n\n {x}/3")
                    data_FC.append(
                        list(echobase.crossCorrelation_wrapper(data[x], fsds)))
            if FCtype == "coherence":
                for x in range(4):
                    print(f"\n\n {x}/3")
                    data_FC.append(
                        list(echobase.coherence_wrapper(data[x], fsds)))

            FC = [channels, data_FC]
            utils.savePickle([channels, data_FC], fname)
            return FC