def _load_test_data_from_file(self, patient, filename, preprocess=True):
        """
        Loading single file test data
        :param patient:
        :param filename:
        :return:
        """
        assert ( filename.find('test'))
        print "\nLoading test data for " + patient + filename
        eeg_data_tmp = EEGData(filename)
        eeg_data = eeg_data_tmp.get_instances()
        assert len(eeg_data) == 1
        eeg_data = eeg_data[0]

        fs = eeg_data.sample_rate
        # preprocessing
        data = eeg_data.eeg_data

        params = self.params
        params['fs']=fs

        ### comment if no preprocessing
        if preprocess==True:
            eeg_data.eeg_data = preprocessing.preprocess_multichannel_data(data,params)
        x = self.feature_extractor.extract(eeg_data)
        self.features_test.append(np.hstack(x))
    def load_train_data_from_file(patient_name, filename, params=None):
        """
        Loading single file training data
        filename: full path to .mat file 
        return: (EEG data Instance, y_seizure, y_early)
        """
        #print "\nLoading train data for " + patient_name + filename
        eeg_data_tmp = EEGData(filename)
        # a list of Instance's
        eeg_data = eeg_data_tmp.get_instances()
        assert len(eeg_data) == 1

        eeg_data = eeg_data[0]
        # eeg_data is now an Instance
        # determine labels based on filename
        if filename.find('interictal') > -1:
            y_seizure = 0
            y_early = 0
        elif eeg_data.latency < 15:
            y_seizure = 1
            y_early = 1
        else:
            y_seizure = 1
            y_early = 0

        fs = eeg_data.sample_rate

        # preprocessing
        data = eeg_data.eeg_data
        params['fs'] = fs

        eeg_data.eeg_data = preprocessing.preprocess_multichannel_data(
            data, params)
        return (eeg_data, y_seizure, y_early)
Exemplo n.º 3
0
    def _load_test_data_from_file(self, patient, filename, preprocess=True):
        """
        Loading single file test data
        :param patient:
        :param filename:
        :return:
        """
        assert (filename.find('test'))
        print("\nLoading test data for " + patient + filename)
        eeg_data_tmp = EEGData(filename)
        eeg_data = eeg_data_tmp.get_instances()
        assert len(eeg_data) == 1
        eeg_data = eeg_data[0]

        fs = eeg_data.sample_rate
        # preprocessing
        data = eeg_data.eeg_data

        params = self.params
        params['fs'] = fs

        ### comment if no preprocessing
        if preprocess == True:
            eeg_data.eeg_data = preprocessing.preprocess_multichannel_data(
                data, params)
        x = self.feature_extractor.extract(eeg_data)
        self.features_test.append(np.hstack(x))
Exemplo n.º 4
0
    def _load_training_data_from_file(self,
                                      patient,
                                      filename,
                                      preprocess=True):
        """
        Loading single file training data
        :param patient:
        :param filename:
        :return:
        """
        #print "\nLoading train data for " + patient + filename
        eeg_data_tmp = EEGData(filename)
        # a list of Instance's
        eeg_data = eeg_data_tmp.get_instances()
        assert len(eeg_data) == 1
        # eeg_data is now an Instance

        eeg_data = eeg_data[0]
        if filename.find('interictal') > -1:
            y_seizure = 0
            y_early = 0
        elif eeg_data.latency < 15:
            y_seizure = 1
            y_early = 1
        else:
            y_seizure = 1
            y_early = 0

        fs = eeg_data.sample_rate

        # preprocessing
        data = eeg_data.eeg_data

        params = self.params
        params['fs'] = fs
        ### comment if no preprocessing
        if preprocess == True:
            eeg_data.eeg_data = preprocessing.preprocess_multichannel_data(
                data, params)
        ###
        x = self.feature_extractor.extract(eeg_data)
        self.features_train.append(np.hstack(x))
        self.type_labels.append(y_seizure)
        self.early_labels.append(y_early)
    def load_test_data_from_file(patient_name, filename, params=None):
        """
        Loading single file test data
        :return: EEG data Instance (no labels returned)
        """
        assert (filename.find('test'))
        #print "\nLoading test data for " + patient_name + filename
        eeg_data_tmp = EEGData(filename)
        eeg_data = eeg_data_tmp.get_instances()
        assert len(eeg_data) == 1
        # an Instance
        eeg_data = eeg_data[0]
        fs = eeg_data.sample_rate
        data = eeg_data.eeg_data
        params['fs'] = fs

        eeg_data.eeg_data = preprocessing.preprocess_multichannel_data(
            data, params)
        return eeg_data
    def _load_training_data_from_file(self, patient, filename, preprocess=True):
        """
        Loading single file training data
        :param patient:
        :param filename:
        :return:
        """
        #print "\nLoading train data for " + patient + filename
        eeg_data_tmp = EEGData(filename)
        # a list of Instance's
        eeg_data = eeg_data_tmp.get_instances()
        assert len(eeg_data) == 1
        # eeg_data is now an Instance

        eeg_data = eeg_data[0]
        if filename.find('interictal') > -1:
            y_seizure=0
            y_early=0
        elif eeg_data.latency < 15:
            y_seizure=1
            y_early=1
        else:
            y_seizure=1
            y_early=0

        fs = eeg_data.sample_rate

        # preprocessing
        data = eeg_data.eeg_data

        params = self.params
        params['fs']=fs
        ### comment if no preprocessing
        if preprocess==True:
            eeg_data.eeg_data = preprocessing.preprocess_multichannel_data(data,params)
        ###
        x = self.feature_extractor.extract(eeg_data)
        self.features_train.append(np.hstack(x))
        self.type_labels.append(y_seizure)
        self.early_labels.append(y_early)
Exemplo n.º 7
0
    def test_combination(self, fold=3, max_segments=-1):
        """
        Test the predictors using features given by each feature_extractor 
        in feature_extractors on the data specified by the patient argument.

        :param max_segments: maximum segments to load. -1 to use the number of 
        total segments available. Otherwise, all segments (ictal and interictal)
        will be randomly subsampled without replacement. 

        return: an instance of FeaturesPredictsTable         """

        loader = SubjectEEGData(self._patient,
                                self._data_path,
                                use_cache=True,
                                max_train_segments=max_segments)

        # a list of (Instance, y_seizure, y_early)'s
        train_data = loader.get_train_data()
        fs = train_data[0][0].sample_rate

        # preprocessing.
        #params = {'fs':fs,
        #  'anti_alias_cutoff': 100.,
        ##  'anti_alias_width': 30.,
        #  'anti_alias_attenuation' : 40,
        #  'elec_noise_width' :3.,
        #  'elec_noise_attenuation' : 60.0,
        #  'elec_noise_cutoff' : [49.,51.]}
        # list of preprocessed tuples
        params = self.params
        params['fs'] = fs
        #        for (x, y_seizure, y_early) in train_data:
        #            x.eeg_data = preprocessing.preprocess_multichannel_data(x.eeg_data, self.params)

        #train_data2 = []
        #for (x, y_seizure, y_early) in train_data:
        #    x.eeg_data = preprocessing.preprocess_multichannel_data(x.eeg_data, params)
        #    train_data2.append(((x, y_seizure, y_early)))
        #train_data =train_data2

        # pre-extract features
        features = [
        ]  # list of feature tuples. list length = len(self._feature_extractors)
        Y_seizure = np.array(
            [y_seizure for (x, y_seizure, y_early) in train_data])
        Y_early = np.array([y_early for (x, y_seizure, y_early) in train_data])
        skf_seizure = cross_validation.StratifiedKFold(Y_seizure, n_folds=fold)
        skf_early = cross_validation.StratifiedKFold(Y_early, n_folds=fold)
        for i, feature_extractor in enumerate(self._feature_extractors):
            #print 'Extracting features with %s'%str(feature_extractor)
            #Xlist = [feature_extractor.extract(x) for  (x, y_seizure, y_early)
            #        in train_data]
            Xlist = []
            for (x, y_seizure, y_early) in train_data:
                #print '---------'
                #print x.eeg_data.shape
                params['fs'] = x.sample_rate
                x.eeg_data = preprocessing.preprocess_multichannel_data(
                    x.eeg_data, params)
                feat = feature_extractor.extract(x)
                #print x.eeg_data.shape
                #print feat.shape
                Xlist.append(feat)
            #Tracer()()
            # Xlist = list of ndarray's
            #print len(Xlist), Xlist[0].shape,len(Xlist[0])
            n = len(Xlist)
            #d = len(Xlist[0])
            d = Xlist[0].shape[0]
            # make 2d numpy array
            #print n,d
            X = np.zeros((n, d))
            #print X.shape
            for i in range(len(Xlist)):
                #print Xlist[i].shape, X[i, :].shape
                X[i, :] = Xlist[i].T

            # chunk data for cross validation
            # construct a list of 2d numpy arrays to be fed to XValidation
            # tr_I = train index, te_I = test index
            X_seizure = []
            y_seizure = []

            #Tracer()()
            for tr_I, te_I in skf_seizure:
                X_seizure.append(X[tr_I, :])
                y_seizure.append(Y_seizure[tr_I])
            X_early = []
            y_early = []
            for tr_I, te_I in skf_early:
                X_early.append(X[tr_I, :])
                y_early.append(Y_early[tr_I])
            features.append((X_seizure, y_seizure, X_early, y_early))

        T = CVFeaturesPredictorsTester.test_all_combinations(
            features, self._feature_extractors, self._predictors)
        return T
    def test_combination(self, fold=3, max_segments=-1):
        """
        Test the predictors using features given by each feature_extractor 
        in feature_extractors on the data specified by the patient argument.

        :param max_segments: maximum segments to load. -1 to use the number of 
        total segments available. Otherwise, all segments (ictal and interictal)
        will be randomly subsampled without replacement. 

        return: an instance of FeaturesPredictsTable         """

        loader = SubjectEEGData(self._patient, self._data_path, use_cache=True, 
                max_train_segments=max_segments)

        # a list of (Instance, y_seizure, y_early)'s
        train_data = loader.get_train_data()
        fs = train_data[0][0].sample_rate

        # preprocessing. 
        #params = {'fs':fs,
        #  'anti_alias_cutoff': 100.,
        ##  'anti_alias_width': 30.,
        #  'anti_alias_attenuation' : 40,
        #  'elec_noise_width' :3.,
        #  'elec_noise_attenuation' : 60.0,
        #  'elec_noise_cutoff' : [49.,51.]}
        # list of preprocessed tuples
        params = self.params
        params['fs']=fs
#        for (x, y_seizure, y_early) in train_data:
#            x.eeg_data = preprocessing.preprocess_multichannel_data(x.eeg_data, self.params)

        #train_data2 = []
        #for (x, y_seizure, y_early) in train_data:
        #    x.eeg_data = preprocessing.preprocess_multichannel_data(x.eeg_data, params)
        #    train_data2.append(((x, y_seizure, y_early)))
        #train_data =train_data2

        # pre-extract features 
        features = [] # list of feature tuples. list length = len(self._feature_extractors)
        Y_seizure = np.array([y_seizure for (x, y_seizure, y_early) in train_data])
        Y_early = np.array([y_early for (x, y_seizure, y_early) in train_data])
        skf_seizure = cross_validation.StratifiedKFold(Y_seizure, n_folds=fold)
        skf_early = cross_validation.StratifiedKFold(Y_early, n_folds=fold)
        for i, feature_extractor in enumerate(self._feature_extractors):
            #print 'Extracting features with %s'%str(feature_extractor)
            #Xlist = [feature_extractor.extract(x) for  (x, y_seizure, y_early)
            #        in train_data]
            Xlist = []
            for (x, y_seizure, y_early) in train_data:
                #print '---------'
                #print x.eeg_data.shape
                params['fs']=x.sample_rate
                x.eeg_data = preprocessing.preprocess_multichannel_data(x.eeg_data, params)
                feat =feature_extractor.extract(x)
                #print x.eeg_data.shape
                #print feat.shape
                Xlist.append(feat)
            #Tracer()()
            # Xlist = list of ndarray's
            #print len(Xlist), Xlist[0].shape,len(Xlist[0])
            n = len(Xlist)
            #d = len(Xlist[0])
            d = Xlist[0].shape[0]
            # make 2d numpy array
            #print n,d
            X = np.zeros((n, d))
            #print X.shape
            for i in xrange(len(Xlist)):
                #print Xlist[i].shape, X[i, :].shape
                X[i, :] = Xlist[i].T

            # chunk data for cross validation
            # construct a list of 2d numpy arrays to be fed to XValidation
            # tr_I = train index, te_I = test index
            X_seizure = []
            y_seizure = []

            #Tracer()()
            for tr_I, te_I in skf_seizure:
                X_seizure.append(X[tr_I, :])
                y_seizure.append(Y_seizure[tr_I])
            X_early = []
            y_early = []
            for tr_I, te_I in skf_early:
                X_early.append(X[tr_I, :])
                y_early.append(Y_early[tr_I])
            features.append( (X_seizure, y_seizure, X_early, y_early) )

        T = CVFeaturesPredictorsTester.test_all_combinations(features, 
                self._feature_extractors, self._predictors)
        return T