def btnPreprocessPushed(self):	# ASKS FOR THE DATA TO BE PROCESSED AND CALLS TO THE PIPELINE FUNCTIONS
		
		import global_variables
		import filter_files as filt
		import feature_extraction as feat
		import svm_classification as classif
		import plots

		import online_predict_test as onl_pred

		mainRoot = tk.Tk()
		mainRoot.withdraw()
		mainRoot.update()
		files_to_process = tkfiledialog.askopenfilenames(parent =mainRoot, initialdir = "./data",title = "Select file(s)",filetypes = (("txt files","*.txt"),("all files","*.*")))
		
		print("\nSelected Files: ", files_to_process, "\n")
		if files_to_process:
			global_variables.set_files_to_process(files_to_process)
			mainRoot.destroy()

# Processing
			trial_hor, trial_ver, trial_lab = filt.filter()
			
# Feature Extraction
			features = []
			features = feat.features(trial_hor, trial_ver)

# Classification
			classif.classification(features, trial_lab)

#	PLOTTING FUNCTIONS (FOR TESTING PURPOSES)

#			plots.feature3d_plot(features, trial_lab)
#			plots.feature2d_plot(features, trial_lab)
#			plots.trial_plot(trial_hor, trial_ver, trial_lab)
#			plots.derivative_signal(trial_hor, trial_ver, trial_lab)
#			plots.derivative_plots(trial_hor, trial_ver, trial_lab)
#			plots.feature_plot(features, trial_lab)
#			plots.movement_plot(trial_hor, trial_ver, trial_lab, "'Up'")
			
			saveRoot = tk.Tk()
			saveRoot.withdraw()
			saveRoot.update()
			save_wdw = SaveWindow(saveRoot)
			saveRoot.mainloop()
Beispiel #2
0
def online_calculations(online_data_hor, online_data_ver):
    """
# FUNCTION:	 	online_calculations(online_data_hor, online_data_ver)
# INPUT: 		Horizontal and Vertical data
# OUTPUT: 		None
# DESCRIPTION:	Performs all the desired calculations over the data 
#				and predicts the label
# AUTHOR:		Jayro Martinez Cervero
	"""

    samples_per_trial = global_variables.get_samples_per_trial()
    lab = global_variables.get_lab()
    model_name = global_variables.get_model()
    model = pickle.load(open(model_name, 'rb'))

    # Pre-processing
    filtered_hor = filt.online_filter(online_data_hor)
    filtered_ver = filt.online_filter(online_data_ver)

    # Feature Extraction
    features = feat.features([filtered_hor], [filtered_ver])

    # Check if there's a movement or not
    probs = model.predict_proba(features)

    lab.sort()
    max_pos = int(np.where(probs[0] == np.max(probs[0]))[0])
    movement = lab[max_pos]

    print("\n********************************************")

    if np.max(probs) > 0.5:
        print(lab)
        print(np.max(probs))
        print("MOVEMENT DETECTED: ", movement)

    file_name = global_variables.get_online_data_file()
    new_file_name = "./data/" + file_name + ".txt"
    file = open(new_file_name, 'a')
    str_write = str([online_data_hor, online_data_ver]) + "\n"
    file.write(str_write)
    file.close()
Beispiel #3
0
 def read_flute(self):
     sigs = []
     i = 1
     if (self.flute):
         print('start flute feature extraction')
         os.chdir(os.getcwd() + '\\flute')
         for path in os.listdir():
             if (i % 100 == 0):
                 print("flute : ", i, "/", np.size(os.listdir()))
             # y, sr = lr.load(path)
             signal = sig()
             audio = mpe.AudioFileClip(path)
             signal.y = np.mean(audio.to_soundarray(buffersize=200), 1)
             signal.sr = audio.fps  # sampling rate
             feat = feature_extraction.features(signal.y, signal.sr)
             feat_vec = feat.get_feature_vectors()
             self.X.append(feat_vec.tolist())
             self.Y.append(2)  #2 for flute
             sigs.append(signal)
             i = i + 1
         os.chdir("..")
     return sigs
def menu():

    os.system('clear')

    # We show the main menu
    print("*******************************************************")
    print("*******************************************************")
    print("*********                                     *********")
    print("***       WELCOME TO OUR FANTASTIC EOG PROGRAM      ***")
    print("*********                                     *********")
    print("*******************************************************")
    print("*******************************************************")
    print("\nYou can perform the following actions:\n")
    print("	1. Acquire New Data")
    print("	2. Pre-process Data, Extract Features & Classify")
    print("	3. Online Classification")
    print("	0. EXIT\n")

    option = input("What would you like to do?: ")

    if option == '1':  # Acquire new data

        os.system('clear')
        files_in_data = [x for x in os.listdir('./data') if x.endswith(".txt")]
        files_in_data = natsorted(files_in_data)
        print("Select the file to save the new data: \n")
        print("	 0 : Create a New File")
        for i in range(0, len(files_in_data)):
            print("	", i + 1, ": Add to File ", files_in_data[i])
        target_file = int(input("\nYour selection: "))
        if target_file == 0:
            os.system('clear')
            file_name = input(
                "Please write the name of the file (without extension): ")
            global_variables.set_save_file(file_name + ".txt")
        elif target_file <= len(files_in_data):
            file_name = files_in_data[target_file - 1]
            global_variables.set_save_file(file_name)
        else:
            os.system('clear')
            print("Wrong Option, sorry.")
            time.sleep(2)
            menu()


# Connection, Data Acquisition & Data Storage
        con.connect()

    elif option == '2':  # Pre-process data, extract features & classify

        os.system('clear')
        all_files = []
        files_to_process = []
        all_files = [x for x in os.listdir('./data') if x.endswith(".txt")]
        all_files = natsorted(all_files)
        print("Select the file/s to pre-process\n")

        for i in range(0, len(all_files)):
            print("	", i, " : ", all_files[i])

        selected_files = input(
            "\nSelect desired files (you can use '-' to select ranges and/or ',' to select multiple datasets): "
        )
        selected_files = selected_files.split(',')
        aux = []

        for j in range(0, len(selected_files)):
            aux = selected_files[j].split('-')

            for k in range(int(aux[0]), int(aux[-1]) + 1):

                files_to_process.append("./data/" + all_files[k])

        global_variables.set_files_to_process(files_to_process)

        time.sleep(1)
        os.system('clear')

        # Data Load, Filtering & Trial Split
        print("Pre-processing & extracting features from files : ",
              files_to_process)

        trial_hor, trial_ver, trial_lab = filt.filter()

        # Feature Extraction
        features = []
        features = feat.features(trial_hor, trial_ver)

        print(
            "\nPre-processing & Feature extraction done. Now we are going to perform classification."
        )

        # Classification
        classif.classification(features, trial_lab)

        save = input("\nDo you want to save the model?? [y/n]: ")
        if save == 'y' or save == 'Y':
            model_name = input("Write model name: ")
            model_file_name = "./models/" + model_name + ".sav"
            model = global_variables.get_model()
            pickle.dump(model, open(model_file_name, 'wb'))
            print("Model saved in " + model_file_name)

        time.sleep(3)
        menu()

    elif option == '3':  # Online classification

        models_list = []
        models_list = [x for x in os.listdir('./models') if x.endswith(".sav")]
        models_list = natsorted(models_list)
        os.system('clear')
        print("Available models:\n")

        for l in range(0, len(models_list)):
            print("	", l, " : ", models_list[l])

        model = input("\nSelect the model for classification: ")
        global_variables.set_model(models_list[int(model)])
        os.system('clear')
        print("Online classification started. \nModel selected: ",
              global_variables.get_model(), "\n")

        # Online Classification
        online.online_classif()

    elif option == '0':  # EXIT
        os.system('clear')
        print("###############################")
        print("##     SEE YOU SOON!!!!!!    ##")
        print("###############################")
        time.sleep(3)
        os.system('clear')
        sys.exit()

    else:  # Wrong option
        print("\nYou selected a wrong option. ARE YOU KIDDING ME?")
        time.sleep(2)
        menu()