示例#1
0
    def online_estimation(self, predictions):
        # -- get sample --
        sample = self.eeg_dmg.get_short_sample(self.constants.METHOD)   
     
        # -- artifact removal --
        sample = eawica(sample,self.constants)
        # -- compute features --
#        feature = compute_online_features(sample,self.constants, self.numFeatures)
        feature = compute_online_features(sample,self.constants)
        # -- load train data --
        self.training_data = np.load(self.path_allFeatures)
        
        # -- feature smoothing --
        aux = np.vstack((self.training_data,feature[:self.training_data.shape[1]]))
        # -- feature scaling --
        aux  = self.quantile_scaler.transform( aux )
        
        
        for i in range(aux.shape[1]):
            aux[:,i] = smoothing(aux[:,i])
        feature = aux[-1,:]

        # -- estimate emotion --
#        probabilities = self.model.predict_proba(feature[self.selected_features_indx].reshape(1,-1))
        probabilities = self.model.predict_proba(feature.reshape(1,-1))
        predictions[:] = probabilities.squeeze().tolist()
        
        print('after processing predictions: ', predictions[:])
示例#2
0
#% CLASSIFICATION BY EMOTION FOR A SPECIFIC SUBJECT
from FEATURES import feature_smoothing
from sklearn.preprocessing import QuantileTransformer

qt_arousal = QuantileTransformer(output_distribution='normal')
all_arousal_features_normalized = pd.DataFrame(
    qt_arousal.fit_transform(all_arousal_features),
    columns=list(all_arousal_features.keys()))
qt_valence = QuantileTransformer(output_distribution='normal')
all_valence_features_normalized = pd.DataFrame(
    qt_valence.fit_transform(all_valence_features),
    columns=list(all_valence_features.keys()))

# -- smoothing the feature space -- #
for i in range(all_arousal_labels.shape[1]):
    all_arousal_features_normalized.iloc[:, i] = feature_smoothing.smoothing(
        all_arousal_features_normalized.iloc[:, i])
    all_valence_features_normalized.iloc[:, i] = feature_smoothing.smoothing(
        all_valence_features_normalized.iloc[:, i])

np.sum(np.isnan(all_arousal_features_normalized))
all_arousal_features_normalized = all_arousal_features_normalized.drop(
    ['sdsd'], axis=1)

print(all_arousal_features_normalized.shape)
print(all_valence_features_normalized.shape)

#%########## DATASET SMOOTHING #######################
from CLASSIFIERS.models_trainer import model_trainer
#from FILMS_ANALYSIS_ESCENAS.utils_general import get_aleatory_k_trials_out

model_trainer_valence = model_trainer('valence')
示例#3
0
    def run(self):
        # -- set paths --
        user = '******'
        path_training = './data/DANI/' + user + '.csv'
        path_labels = './data/DANI/labels_' + user + '.csv'
        path_best_model = './RESULTS/DANI/best_model.npy'
        path_best_model_furnitures = './RESULTS/DANI/best_model_furnitures.npy'
        path_final_model = './RESULTS/DANI/final_model.npy'
        path_final_model_furnitures = './RESULTS/DANI/final_model_furnitures.npy'
        path_allFeatures = './RESULTS/DANI/allFeatures.npy'
        path_allLabels = './RESULTS/DANI/allLabels.npy'
        path_quantile_scaler = './RESULTS/DANI/quantile_scaler.pkl'
        ############  LOAD TRAINING DATA #############################
        self.log.update_text('Data loading')
        dataframe = io.open_csvFile(path_training)
        labels_byTrial = io.open_csvFile(path_labels)
        self.log.update_text('Data have been loaded')
        dataframe = dataframe.iloc[8:, :]
        ############## COMPUTE FEATURES ###################################
        numSamples = int(dataframe.shape[0] / self.constants.CHANNELS)
        # -- init training data
        features = []
        labels = []
        anterior = 0
        contador = 0
        self.log.update_text('Start computing features')
        for i in range(numSamples):
            # -- indexing training data
            ini = i * self.constants.CHANNELS
            end = ini + self.constants.CHANNELS

            if anterior == dataframe['trial'].iloc[ini] and contador > 2:
                sample = dataframe.iloc[ini:end, 3:]
                sample = eawica(np.asarray(sample), self.constants)
                features.append(
                    compute_online_features(sample, self.constants,
                                            self.numFeatures))
                labels.append(labels_byTrial['label'].iloc[
                    dataframe['trial'].iloc[i * self.constants.CHANNELS]])
                contador += 1
            elif anterior == dataframe['trial'].iloc[ini] and contador <= 2:
                contador += 1
            else:
                anterior = dataframe['trial'].iloc[ini]
                contador = 0

        features = np.asarray(features)
        labels = np.asarray(labels)

        self.log.update_text('Features have been computed')

        ####### DATASET PREPROCESSING #######################
        self.log.update_text('Features preprocessing')
        from sklearn.preprocessing import QuantileTransformer
        Quantile_scaler = QuantileTransformer(output_distribution='normal')
        features = Quantile_scaler.fit_transform(features)

        np.save(path_allFeatures, features)
        np.save(path_allLabels, labels)
        joblib.dump(Quantile_scaler, path_quantile_scaler)  #

        ########## DATASET SMOOTHING #######################
        from FEATURES.feature_smoothing import smoothing
        features = smoothing(features)

        ######## TRAIN AND TEST SETS SPLIT ##############
        self.log.update_text('Train test split')
        from FEATURES.features_train_test_split import ByTrials_train_test_split
        X_train, y_train, X_test, y_test = ByTrials_train_test_split(
            features, labels, self.numTrials, self.numTestTrials)

        ########## FEATURE SELECTION AND MODEL TRAINING ##############
        self.log.update_text('Model trainning')

        label_names = ['POS', 'NEU', 'NEG']
        best_model = {
            'model': [],
            'name': [],
            'predictions': [],
            'score': 0,
            'selected_features': [],
            'report': []
        }
        for i in range(1, self.numFeatures):
            # -- feature selection -- #
            select_features = i
            _, selected_features_indx = rfe_selection(X_train, y_train,
                                                      select_features)
            # -- model training -- #
            classifiers, names, predictions, scores_list = models_trainer.classify(
                X_train[:, selected_features_indx, ], y_train,
                X_test[:, selected_features_indx], y_test)
            winner = np.asarray(scores_list).argmax()
            if scores_list[winner] > best_model['score']:
                best_model['model'] = classifiers[winner]
                best_model['name'] = names[winner]
                best_model['predictions'] = predictions[winner]
                best_model['score'] = scores_list[winner]
                best_model['selected_features'] = selected_features_indx
                best_model['report'] = models_trainer.get_report(
                    classifiers[winner], X_test[:, selected_features_indx],
                    y_test, label_names)
                # -- logging report --
                self.log.update_text('Report: ' + best_model['report'])
                # save the model to disk
                pickle.dump(best_model['model'], open(path_best_model, 'wb'))
                # -- save furnitures to disk --
                self.log.update_text('Saving furnitures')
                np.save(path_best_model_furnitures, best_model)

        model, name = models_trainer.train_models(
            features[:, best_model['selected_features']], labels,
            best_model['name'])
        final_model = {'model': [], 'name': [], 'selected_features': []}
        final_model['model'] = model
        final_model['name'] = name
        final_model['selected_features'] = best_model['selected_features']
        self.log.update_text('Saving sklearn model')
        pickle.dump(final_model['model'], open(path_final_model, 'wb'))
        np.save(path_final_model_furnitures, final_model)
        self.log.update_text('Training finished!')
示例#4
0
# -- load machine-learning model --
model = pickle.load(open(path_model, 'rb'))
# -- load furnitures --
training_data = np.load(path_training_data)
best_model_furnitures = np.load(path_best_model_furnitures)
selected_features_indx = best_model_furnitures.item().get('selected_features')

print('paso')
# -- get sample --
sample = eeg_dmg.get_short_sample(constants.METHOD)
# -- compute features --
feature = compute_online_features(sample, constants, numFeatures)
# -- feature smoothing --
training_data = np.vstack((training_data, feature))
from FEATURES.feature_smoothing import smoothing
training_data = smoothing(training_data)
# -- feature scaling --
from sklearn.preprocessing import QuantileTransformer, MinMaxScaler
Quantile_scaler = QuantileTransformer(output_distribution='normal')
training_data = Quantile_scaler.fit_transform(training_data)
MinMax_scaler = MinMaxScaler()
training_data = MinMax_scaler.fit_transform(training_data)
feature = training_data[-1, :]
# -- estimate emotion --
probabilities = model.predict_proba(feature[selected_features_indx].reshape(
    1, -1))
predictions[:] = probabilities.squeeze().tolist()


def append_to_store(self):
    allPredictions.append(predictions[:])
示例#5
0
np.save(path_allFeatures, features)
np.save(path_allLabels, labels)

features = np.load(path_allFeatures)

####### DATASET PREPROCESSING #######################
from sklearn.preprocessing import QuantileTransformer
Quantile_scaler = QuantileTransformer(output_distribution='normal')
features = Quantile_scaler.fit_transform(features)

########## DATASET SMOOTHING #######################
from FEATURES.feature_smoothing import smoothing
for i in range(features.shape[1]):
    try:
        features[:, i] = smoothing(features[:, i])
    except:
        print(i)

import matplotlib.pyplot as plt
plt.imshow(features.T)
######## TRAIN AND TEST SETS SPLIT ##############
from FEATURES.features_train_test_split import ByTrials_train_test_split
X_train, y_train, X_test, y_test = ByTrials_train_test_split(
    features, labels, numTrials, numTestTrials)

########## FEATURE SELECTION AND MODEL TRAINING ##############
label_names = ['POS', 'NEU', 'NEG']
best_model = {
    'model': [],
    'name': [],