dataPath1='/home/ymeng/jade/myo/src/myo_raw/data/work/1' quat1 = np.genfromtxt(os.path.join(dataPath1,'gyro.mat'), delimiter=',') QUAT1 = quat1[6:-6:5, :] QUAT1_smooth = savgol_filter(QUAT1, 31, 3, axis=0) fig = plt.figure() fig.suptitle('Signal 1 & 2 Overlaid with DTW', fontsize=18, fontweight='bold') ax = fig.add_subplot(111) fig.subplots_adjust(top=0.85) #ax.set_title('axes title') ax.set_xlabel('samples', fontsize=16) ax.set_ylabel('ampitude', fontsize=16) signal0 = np.reshape(QUAT0_smooth[:,0], (-1,1)) ax.plot(signal0) #fig1 = plt.figure() #fig1.suptitle('Signal 2', fontsize=18, fontweight='bold') #ax1 = fig1.add_subplot(111) #fig1.subplots_adjust(top=0.85) ##ax.set_title('axes title') #ax1.set_xlabel('samples', fontsize=16) #ax1.set_ylabel('ampitude', fontsize=16) signal1 = np.reshape(QUAT1_smooth[:,0], (-1,1)) ax.plot(signal1, 'r') aligned = align_signal(signal0, signal1, w=5, has_time=False); ax.plot(aligned, 'r--') plt.show()
def build_classifier(samples=1, nClusters=None, ID='user', used=False): if used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": ID } progress = myo_state2.Progress(classifier_pkl='../data/state_classifier.pkl', **args) return for identifier in ('l', 'u'): for i in range(samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i = preprocess(os.path.join('../../data/work/', str(i)), update_extremes=True, identifier=identifier) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i else: imu_i_a = align_signal(imu_demos, imu_i, w=5, has_time=False) imu_demos += imu_i_a EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i n = len(imu_demos) # get average if identifier == 'l': print "\nProcessing myo on lower arm..." emg_demos_l = emg_demos imu_demos_l = imu_demos/samples EMG_MAX_l = EMG_MAX/samples EMG_MIN_l = EMG_MIN/samples GYRO_MAX_l = GYRO_MAX/samples print "EMG_MAX_l", EMG_MAX_l print "EMG_MIN_l", EMG_MIN_l print "GYRO_MAX_l", GYRO_MAX_l print "# data points", len(imu_demos_l) else: print "\nProcessing myo on upper arm..." emg_demos_u = emg_demos imu_demos_u = imu_demos/samples EMG_MAX_u = EMG_MAX/samples EMG_MIN_u = EMG_MIN/samples GYRO_MAX_u = GYRO_MAX/samples print "EMG_MAX_u", EMG_MAX_u print "EMG_MIN_u", EMG_MIN_u print "GYRO_MAX_u", GYRO_MAX_u print "# data points", len(imu_demos_u) N_l = imu_demos_l.shape[0] N_u = imu_demos_u.shape[0] print N_u if N_l < N_u: emg_demos_u = emg_demos_u[0:N_l, :] imu_demos_u = imu_demos_u[0:N_l, :] Time = np.arange(N_l).reshape((N_l,1)) elif N_l > N_u: emg_demos_l = emg_demos_l[0:N_u, :] imu_demos_l = imu_demos_l[0:N_u, :] Time = np.arange(N_u).reshape((N_u,1)) else: Time = np.arange(N_u).reshape((N_u,1)) if os.path.exists('/usr/local/MATLAB'): print "MATLAB FOUND" ## use the following if there is Malab under /usr/local ## GMM and GMR will be performed os.chdir('../matlab') subprocess.call(['matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo('+str(samples)+');exit']) os.chdir('../myo_python') else: print "MATLAB NOT FOUND" ## use the following if there is no Matlab ## simply use the aligned average as expected trajectory np.savetxt('../data/demo_l.dat', imu_demos_l[:, -4:], delimiter=',') np.savetxt('../data/demo_u.dat', imu_demos_u[:, -4:], delimiter=',') emg_demos = np.hstack((emg_demos_l,emg_demos_u)) emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) observations = np.hstack((EMG_WEIGHT*emg_demos_l, imu_demos_l, EMG_WEIGHT*emg_demos_u, imu_demos_u)) timed_observations = np.hstack((TIME_WEIGHT*Time, EMG_WEIGHT*emg_demos_l, imu_demos_l, EMG_WEIGHT*emg_demos_u, imu_demos_u)) if nClusters is None: N = observations.shape[0] low_limit = N/20 # 0.5 states per second high_limit = int(1.5*N/20) # 0.75 states per second scores = {} for n in range(low_limit, high_limit): state_cluster = classifier.SignalCluster(timed_observations, n) score = state_cluster.evaluate(timed_observations, state_cluster.labels) scores[score] = n print '# clusters, score', n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(timed_observations, n_clusters) else: state_cluster = classifier.SignalCluster(timed_observations, nClusters) plt.figure() plt.plot(imu_demos) plt.plot(state_cluster.labels, '*') plt.show(block=False) builder = BuildMDP(actionsData=emg_cluster.labels, statesData=state_cluster.labels) pickle.dump(builder, open('../data/mdp.pkl', 'wb')) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s'+str(item)+'\n') print "expected actions: ", emg_cluster.labels print "expected states: ", state_cluster.labels baseline = evaluate(emg_cluster.labels, state_cluster.labels, builder) print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_cluster.labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) action_classifier = classifier.SignalClassifier(n_neighbors=5) action_classifier.train(emg_demos, emg_cluster.labels, trainingFile=None) pickle.dump(action_classifier, open('../data/action_classifier.pkl', 'wb'))
dataPath1 = "/home/ymeng/jade/myo/src/myo_raw/data/work/1" quat1 = np.genfromtxt(os.path.join(dataPath1, "gyro.mat"), delimiter=",") QUAT1 = quat1[6:-6:5, :] QUAT1_smooth = savgol_filter(QUAT1, 31, 3, axis=0) fig = plt.figure() fig.suptitle("Signal 1 & 2 Overlaid with DTW", fontsize=18, fontweight="bold") ax = fig.add_subplot(111) fig.subplots_adjust(top=0.85) # ax.set_title('axes title') ax.set_xlabel("samples", fontsize=16) ax.set_ylabel("ampitude", fontsize=16) signal0 = np.reshape(QUAT0_smooth[:, 0], (-1, 1)) ax.plot(signal0) # fig1 = plt.figure() # fig1.suptitle('Signal 2', fontsize=18, fontweight='bold') # ax1 = fig1.add_subplot(111) # fig1.subplots_adjust(top=0.85) ##ax.set_title('axes title') # ax1.set_xlabel('samples', fontsize=16) # ax1.set_ylabel('ampitude', fontsize=16) signal1 = np.reshape(QUAT1_smooth[:, 0], (-1, 1)) ax.plot(signal1, "r") aligned = align_signal(signal0, signal1, w=5, has_time=False) ax.plot(aligned, "r--") plt.show()
def build_classifier(samples=1, nClusters=None, ID='user', used=False): if used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": ID} progress = myo_state2.Progress( classifier_pkl='../data/state_classifier.pkl', **args) return has_matlab = os.path.exists('/usr/local/MATLAB') for identifier in ('l', 'u'): for i in range(samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i, states_i = preprocess( os.path.join('../../data/work/', str(i)), update_extremes=True, identifier=identifier) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i emg_data = emg_i imu_data = imu_i state_labels = states_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i Time_a = np.arange(imu_i.shape[0]).reshape((imu_i.shape[0], 1)) ort_data_a = np.hstack((Time_a, imu_i[:, -4:])) else: imu_i_a = align_signal(imu_demos / i, imu_i, w=5, has_time=False) imu_demos += imu_i_a emg_data = np.vstack((emg_data, emg_i)) imu_data = np.vstack((imu_data, imu_i)) EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i state_labels = np.concatenate((state_labels, states_i)) # Used by Matlab function to compute the expected trojectory ort_data_a = np.vstack( (ort_data_a, np.hstack((Time_a, imu_i_a[:, -4:])))) # # Use original data to train state classifier # # aligned data is only used for expected trajectory # Time = np.arange(emg_i.shape[0]).reshape((emg_i.shape[0],1)) # emg_data.append(np.hstack((Time, emg_i))) # # Time = np.arange(imu_i.shape[0]).reshape((imu_i.shape[0],1)) # imu_data.append(np.hstack((Time, imu_i))) n = len(imu_demos) # get average if identifier == 'l': print "\nProcessing myo on lower arm..." emg_demos_l = emg_demos imu_demos_l = imu_demos / samples EMG_MAX_l = EMG_MAX / samples EMG_MIN_l = EMG_MIN / samples GYRO_MAX_l = GYRO_MAX / samples emg_data_l = emg_data imu_data_l = imu_data ort_data_l = ort_data_a print "EMG_MAX_l", EMG_MAX_l print "EMG_MIN_l", EMG_MIN_l print "GYRO_MAX_l", GYRO_MAX_l print "# data points", len(imu_demos_l) else: print "\nProcessing myo on upper arm..." emg_demos_u = emg_demos imu_demos_u = imu_demos / samples EMG_MAX_u = EMG_MAX / samples EMG_MIN_u = EMG_MIN / samples GYRO_MAX_u = GYRO_MAX / samples emg_data_u = emg_data imu_data_u = imu_data ort_data_u = ort_data_a print "EMG_MAX_u", EMG_MAX_u print "EMG_MIN_u", EMG_MIN_u print "GYRO_MAX_u", GYRO_MAX_u print "# data points", len(imu_demos_u) N_l = imu_demos_l.shape[0] N_u = imu_demos_u.shape[0] print N_u if N_l < N_u: emg_demos_u = emg_demos_u[0:N_l, :] imu_demos_u = imu_demos_u[0:N_l, :] Time = np.arange(N_l).reshape((N_l, 1)) elif N_l > N_u: emg_demos_l = emg_demos_l[0:N_u, :] imu_demos_l = imu_demos_l[0:N_u, :] Time = np.arange(N_u).reshape((N_u, 1)) else: Time = np.arange(N_u).reshape((N_u, 1)) if has_matlab: print "MATLAB FOUND" ## use the following if there is Malab under /usr/local ## GMM and GMR will be performed os.chdir('../matlab') #subprocess.call(['matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo('+str(samples)+');exit']) np.savetxt('ort_data_u', ort_data_u, delimiter=',') np.savetxt('ort_data_l', ort_data_l, delimiter=',') subprocess.call([ 'matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo_gmm();exit' ]) os.chdir('../myo_python') else: print "MATLAB NOT FOUND" ## use the following if there is no Matlab ## simply use the aligned average as expected trajectory np.savetxt('../data/demo_l.dat', imu_demos_l[:, -4:], delimiter=',') np.savetxt('../data/demo_u.dat', imu_demos_u[:, -4:], delimiter=',') emg_demos = np.hstack((emg_demos_l, emg_demos_u)) emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) # observations = np.hstack((EMG_WEIGHT*emg_demos_l, imu_demos_l, EMG_WEIGHT*emg_demos_u, imu_demos_u)) # timed_observations = np.hstack((TIME_WEIGHT*Time, EMG_WEIGHT*emg_demos_l, imu_demos_l, EMG_WEIGHT*emg_demos_u, imu_demos_u)) print emg_data_l.shape, imu_data_l.shape, emg_data_u.shape, imu_data_u.shape n_points = min(emg_data_l.shape[0], emg_data_u.shape[0]) state_labels = state_labels[0:n_points] observations = np.hstack( (EMG_WEIGHT * emg_data_l[0:n_points, :], imu_data_l[0:n_points, :], EMG_WEIGHT * emg_data_u[0:n_points, :], imu_data_u[0:n_points, :])) # if nClusters is None: # N = observations.shape[0] # low_limit = N/20 # 0.5 states per second # high_limit = int(1.5*N/20) # 0.75 states per second # scores = {} # for n in range(low_limit, high_limit): # state_cluster = classifier.SignalCluster(timed_observations, n) # score = state_cluster.evaluate(timed_observations, state_cluster.labels) # scores[score] = n # print '# clusters, score', n, score # # max_score = max(scores.keys()) # n_clusters = scores[max_score] # state_cluster = classifier.SignalCluster(timed_observations, n_clusters) # else: # state_cluster = classifier.SignalCluster(timed_observations, nClusters) plt.figure() plt.plot(imu_demos) plt.plot(state_labels, '*') plt.show(block=True) statesData = state_labels[0:len(emg_demos)] valid_states = statesData >= 0 actionsData = emg_cluster.labels[valid_states] statesData = statesData[valid_states] builder = BuildMDP(actionsData=actionsData, statesData=statesData) pickle.dump(builder, open('../data/mdp.pkl', 'wb')) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s' + str(item) + '\n') print "expected actions: ", actionsData print "expected states: ", statesData baseline = evaluate(actionsData, statesData, builder) print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) action_classifier = classifier.SignalClassifier(n_neighbors=5) action_classifier.train(emg_demos, emg_cluster.labels, trainingFile=None) pickle.dump(action_classifier, open('../data/action_classifier.pkl', 'wb'))
def main(): parser = argparse.ArgumentParser(description="Do something.") parser.add_argument('-s', '--samples', default=1, type=int) parser.add_argument('-n', '--clusters', default=None, type=int) parser.add_argument('-i', '--id', default='user', type=str) parser.add_argument('-u', '--used', default=False, action="store_true") # no need to train the model args = parser.parse_args() if args.used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": args.id} progress = myo_state.Progress( classifier_pkl='../data/state_classifier.pkl', **args) return for i in range(args.samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i = preprocess( os.path.join('../../data/work/', str(i)), update_extremes=True) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i else: imu_i_a = align_signal(imu_demos, imu_i, w=5, has_time=False) imu_demos += imu_i_a EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i n = len(imu_demos) Time = np.arange(n).reshape((n, 1)) # get average imu_demos = imu_demos / args.samples EMG_MAX = EMG_MAX / args.samples EMG_MIN = EMG_MIN / args.samples GYRO_MAX = GYRO_MAX / args.samples print "EMG_MAX", EMG_MAX print "EMG_MIN", EMG_MIN print "GYRO_MAX", GYRO_MAX print "# data points", len(imu_demos) # imu = data[:, -4:] np.savetxt('../data/imu_sample.dat', imu_demos, delimiter=',') emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) observations = np.hstack((EMG_WEIGHT * emg_demos, imu_demos)) timed_observations = np.hstack( (TIME_WEIGHT * Time, EMG_WEIGHT * emg_demos, imu_demos)) #print observations.shape if args.clusters is None: N = observations.shape[0] low_limit = N / 20 # 0.5 states per second high_limit = 2 * N / 20 # 1.5 states per second scores = {} for n in range(low_limit, high_limit): state_cluster = classifier.SignalCluster(timed_observations, n) score = state_cluster.evaluate(timed_observations, state_cluster.labels) scores[score] = n print '# clusters, score', n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(timed_observations, n_clusters) else: state_cluster = classifier.SignalCluster(timed_observations, args.clusters) plt.figure() plt.plot(imu_demos) plt.plot(state_cluster.labels, '*') #plt.figure() #plt.plot(emg_demos) np.savetxt("imu_demos.dat", imu_demos, delimiter=',') np.savetxt("emg_demos.dat", emg_demos, delimiter=',') #plt.plot(emg_cluster.labels, 'go') plt.show(block=False) builder = BuildMDP(actionsData=emg_cluster.labels, statesData=state_cluster.labels) pickle.dump(builder, open('../data/mdp.pkl', 'wb')) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s' + str(item) + '\n') print "expected actions: ", emg_cluster.labels print "expected states: ", state_cluster.labels baseline = evaluate(emg_cluster.labels, state_cluster.labels, builder) print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_cluster.labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) action_classifier = classifier.SignalClassifier(n_neighbors=5) action_classifier.train(emg_demos, emg_cluster.labels, trainingFile=None) pickle.dump(action_classifier, open('../data/action_classifier.pkl', 'wb')) args = { "give_prompt": True, "EMG_MAX": EMG_MAX, "EMG_MIN": EMG_MIN, "GYRO_MAX": GYRO_MAX, "mdp": builder, "baseline": baseline, "id": args.id } progress = myo_state.Progress(classifier=state_classifier, **args)
def build_classifier(samples=1, nClusters=None, ID="user", used=False, has_matlab=False): if used: mdp = pickle.load(open("../data/mdp.pkl")) args = {"give_prompt": True, "mdp": mdp, "id": ID} progress = myo_state2.Progress(classifier_pkl="../data/state_classifier.pkl", **args) return if has_matlab is None: has_matlab = os.path.exists("/usr/local/MATLAB") for identifier in ("l", "u"): for i in range(samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i, states_i = preprocess( os.path.join("../../data/work/", str(i)), update_extremes=True, identifier=identifier ) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i # use the first emg sample as demo imu_demos = imu_i emg_data = emg_i imu_data = imu_i state_labels = states_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i Time_a = np.arange(imu_i.shape[0]).reshape((imu_i.shape[0], 1)) ort_data_a = np.hstack((Time_a, imu_i[:, -4:])) emg_data_a = np.hstack((Time_a, emg_i)) else: imu_i_a = align_signal(imu_demos / i, imu_i, w=5, has_time=False) imu_demos += imu_i_a emg_data = np.vstack((emg_data, emg_i)) # concatenate emg signals imu_data = np.vstack((imu_data, imu_i)) EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i state_labels = np.concatenate((state_labels, states_i)) # all labels emg_i_a = align_signal(emg_demos, emg_i, w=5, has_time=False) # Used by Matlab function to compute the expected trojectory # The signals are aligned to the first one # Potentional improvement: align the to the one with medium length. ort_data_a = np.vstack((ort_data_a, np.hstack((Time_a, imu_i_a[:, -4:])))) emg_data_a = np.vstack((emg_data_a, np.hstack((Time_a, emg_i_a)))) n = len(imu_demos) # get average if identifier == "l": print "\nProcessing myo on lower arm..." emg_demos_l = emg_demos imu_demos_l = imu_demos / samples EMG_MAX_l = EMG_MAX / samples EMG_MIN_l = EMG_MIN / samples GYRO_MAX_l = GYRO_MAX / samples emg_data_l = emg_data emg_data_l_timed = emg_data_a # timed, for GMM imu_data_l = imu_data ort_data_l = ort_data_a print "EMG_MAX_l", EMG_MAX_l print "EMG_MIN_l", EMG_MIN_l print "GYRO_MAX_l", GYRO_MAX_l print "# data points", len(imu_demos_l) else: print "\nProcessing myo on upper arm..." emg_demos_u = emg_demos imu_demos_u = imu_demos / samples EMG_MAX_u = EMG_MAX / samples EMG_MIN_u = EMG_MIN / samples GYRO_MAX_u = GYRO_MAX / samples emg_data_u = emg_data emg_data_u_timed = emg_data_a imu_data_u = imu_data ort_data_u = ort_data_a print "EMG_MAX_u", EMG_MAX_u print "EMG_MIN_u", EMG_MIN_u print "GYRO_MAX_u", GYRO_MAX_u print "# data points", len(imu_demos_u) N_l = imu_demos_l.shape[0] N_u = imu_demos_u.shape[0] N_min = min(N_l, N_u) print N_min emg_demos_u = emg_demos_u[0:N_min, :] imu_demos_u = imu_demos_u[0:N_min, :] emg_demos_l = emg_demos_l[0:N_min, :] imu_demos_l = imu_demos_l[0:N_min, :] Time = np.arange(N_min).reshape((N_min, 1)) show_ort_l = np.genfromtxt("../../data/work/0/ort_l.mat", delimiter=",") show_ort_l = show_ort_l[16:-6:2, :] np.savetxt("../data/prompt_l.dat", show_ort_l, delimiter=",") show_ort_u = np.genfromtxt("../../data/work/0/ort_u.mat", delimiter=",") show_ort_u = show_ort_u[16:-6:2, :] np.savetxt("../data/prompt_u.dat", show_ort_u, delimiter=",") if has_matlab: print "MATLAB FOUND" ## use the following if there is Malab under /usr/local ## GMM and GMR will be performed os.chdir("../matlab") np.savetxt("ort_data_u", ort_data_u, delimiter=",") np.savetxt("ort_data_l", ort_data_l, delimiter=",") np.savetxt("emg_data_u", emg_data_u_timed, delimiter=",") np.savetxt("emg_data_l", emg_data_l_timed, delimiter=",") subprocess.call(["matlab", "-nodisplay", "-nojvm", "-nosplash", "-r", "demo_gmm();exit"]) os.chdir("../myo_python") else: print "MATLAB NOT FOUND" ## use the following if there is no Matlab ## simply use the aligned average as expected trajectory np.savetxt("../data/demo_l.dat", imu_demos_l[:, -4:], delimiter=",") np.savetxt("../data/demo_u.dat", imu_demos_u[:, -4:], delimiter=",") np.savetxt("../data/emg_l.dat", emg_demos_l, delimiter=",") np.savetxt("../data/emg_u.dat", emg_demos_u, delimiter=",") emg_demos = np.hstack((emg_demos_l, emg_demos_u)) emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) observations = np.hstack((EMG_WEIGHT * emg_demos_l, imu_demos_l, EMG_WEIGHT * emg_demos_u, imu_demos_u)) if nClusters is None: N = max(state_labels) # number of tagged points print "state labels:", N low_limit = max(2, N / 2) # at least 2 clusters high_limit = N scores = {} for n in range(low_limit, high_limit + 2): state_cluster = classifier.SignalCluster(observations, n) score = state_cluster.evaluate(observations, state_cluster.labels) scores[score] = n print "# clusters, score", n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(observations, n_clusters) else: state_cluster = classifier.SignalCluster(observations, nClusters) plt.figure() plt.plot(show_ort_l) # plt.plot(emg_demos_u) plt.plot(state_cluster.labels, "*") plt.show(block=True) ## Currently we implement the n-gram model without explicitly training the MDP ## So the following code is not used ## ============================================================================ ## build mdp # # indexes = [x for x,item in enumerate(valid_states) if item] # critical points # segments = [] # left = indexes[0] # for i,ind in enumerate(indexes): # if ind-left > 5: # segments.append((left, ind)) # left = ind # print segments # pickle.dump(segments, open('../data/segments.dat', 'w')) # actionsData = emg_cluster.labels[valid_states] # statesData = statesData[valid_states] # builder = BuildMDP(actionsData=statesData, statesData=statesData) # use the same labels # pickle.dump(builder, open('../data/mdp.pkl', 'wb')) # # print "path", builder.path # print "policy", builder.Pi # print "expected actions: ", actionsData ## ============================================================================== print "expected states: ", state_cluster.labels # baseline = evaluate(actionsData, statesData, builder) # print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_cluster.labels, trainingFile=None) baseline = None pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open("../data/state_classifier.pkl", "wb")) state_labels = state_labels[0:N_min] critical_points = observations[state_labels >= 0] task = state_classifier.predict(critical_points) state_sequence = [] with open("../data/state_sequence.dat", "w") as f: for item in task: if item not in state_sequence or item != state_sequence[-1]: state_sequence.append(item) f.write(str(item) + "\n")
def main(): parser = argparse.ArgumentParser(description="Do something.") parser.add_argument('-s', '--samples', default=1, type=int) parser.add_argument('-n', '--clusters', default=None, type=int) parser.add_argument('-i', '--id', default='user', type=str) parser.add_argument('-u', '--used', default=False, action="store_true") # no need to train the model args = parser.parse_args() if args.used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": args.id } progress = myo_state.Progress(classifier_pkl='../data/state_classifier.pkl', **args) return for i in range(args.samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i = preprocess(os.path.join('../../data/work/', str(i)), update_extremes=True) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i else: imu_i_a = align_signal(imu_demos, imu_i, w=5, has_time=False) imu_demos += imu_i_a EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i n = len(imu_demos) Time = np.arange(n).reshape((n,1)) # get average imu_demos = imu_demos/args.samples EMG_MAX = EMG_MAX/args.samples EMG_MIN = EMG_MIN/args.samples GYRO_MAX = GYRO_MAX/args.samples print "EMG_MAX", EMG_MAX print "EMG_MIN", EMG_MIN print "GYRO_MAX", GYRO_MAX print "# data points", len(imu_demos) # imu = data[:, -4:] np.savetxt('../data/imu_sample.dat', imu_demos, delimiter=',') emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) observations = np.hstack((EMG_WEIGHT*emg_demos, imu_demos)) timed_observations = np.hstack((TIME_WEIGHT*Time, EMG_WEIGHT*emg_demos, imu_demos)) #print observations.shape if args.clusters is None: N = observations.shape[0] low_limit = N/20 # 0.5 states per second high_limit = 2*N/20 # 1.5 states per second scores = {} for n in range(low_limit, high_limit): state_cluster = classifier.SignalCluster(timed_observations, n) score = state_cluster.evaluate(timed_observations, state_cluster.labels) scores[score] = n print '# clusters, score', n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(timed_observations, n_clusters) else: state_cluster = classifier.SignalCluster(timed_observations, args.clusters) plt.figure() plt.plot(imu_demos) plt.plot(state_cluster.labels, '*') #plt.figure() #plt.plot(emg_demos) np.savetxt("imu_demos.dat", imu_demos, delimiter=',') np.savetxt("emg_demos.dat", emg_demos, delimiter=',') #plt.plot(emg_cluster.labels, 'go') plt.show(block=False) builder = BuildMDP(actionsData=emg_cluster.labels, statesData=state_cluster.labels) pickle.dump(builder, open('../data/mdp.pkl', 'wb')) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s'+str(item)+'\n') print "expected actions: ", emg_cluster.labels print "expected states: ", state_cluster.labels baseline = evaluate(emg_cluster.labels, state_cluster.labels, builder) print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_cluster.labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) action_classifier = classifier.SignalClassifier(n_neighbors=5) action_classifier.train(emg_demos, emg_cluster.labels, trainingFile=None) pickle.dump(action_classifier, open('../data/action_classifier.pkl', 'wb')) args = {"give_prompt": True, "EMG_MAX": EMG_MAX, "EMG_MIN": EMG_MIN, "GYRO_MAX": GYRO_MAX, "mdp": builder, "baseline": baseline, "id": args.id } progress = myo_state.Progress(classifier=state_classifier, **args)
def build_classifier(samples=1, nClusters=None, ID='user', used=False, has_matlab=False): if used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": ID} progress = myo_state2.Progress( classifier_pkl='../data/state_classifier.pkl', **args) return if has_matlab is None: has_matlab = os.path.exists('/usr/local/MATLAB') for identifier in ('l', 'u'): for i in range(samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i, states_i = preprocess( os.path.join('../../data/work/', str(i)), update_extremes=True, identifier=identifier) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i # use the first emg sample as demo imu_demos = imu_i emg_data = emg_i imu_data = imu_i state_labels = states_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i Time_a = np.arange(imu_i.shape[0]).reshape((imu_i.shape[0], 1)) ort_data_a = np.hstack((Time_a, imu_i[:, -4:])) emg_data_a = np.hstack((Time_a, emg_i)) else: imu_i_a = align_signal(imu_demos / i, imu_i, w=5, has_time=False) imu_demos += imu_i_a emg_data = np.vstack( (emg_data, emg_i)) # concatenate emg signals imu_data = np.vstack((imu_data, imu_i)) EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i state_labels = np.concatenate( (state_labels, states_i)) # all labels emg_i_a = align_signal(emg_demos, emg_i, w=5, has_time=False) # Used by Matlab function to compute the expected trojectory # The signals are aligned to the first one # Potentional improvement: align the to the one with medium length. ort_data_a = np.vstack( (ort_data_a, np.hstack((Time_a, imu_i_a[:, -4:])))) emg_data_a = np.vstack((emg_data_a, np.hstack( (Time_a, emg_i_a)))) n = len(imu_demos) # get average if identifier == 'l': print "\nProcessing myo on lower arm..." emg_demos_l = emg_demos imu_demos_l = imu_demos / samples EMG_MAX_l = EMG_MAX / samples EMG_MIN_l = EMG_MIN / samples GYRO_MAX_l = GYRO_MAX / samples emg_data_l = emg_data emg_data_l_timed = emg_data_a # timed, for GMM imu_data_l = imu_data ort_data_l = ort_data_a print "EMG_MAX_l", EMG_MAX_l print "EMG_MIN_l", EMG_MIN_l print "GYRO_MAX_l", GYRO_MAX_l print "# data points", len(imu_demos_l) else: print "\nProcessing myo on upper arm..." emg_demos_u = emg_demos imu_demos_u = imu_demos / samples EMG_MAX_u = EMG_MAX / samples EMG_MIN_u = EMG_MIN / samples GYRO_MAX_u = GYRO_MAX / samples emg_data_u = emg_data emg_data_u_timed = emg_data_a imu_data_u = imu_data ort_data_u = ort_data_a print "EMG_MAX_u", EMG_MAX_u print "EMG_MIN_u", EMG_MIN_u print "GYRO_MAX_u", GYRO_MAX_u print "# data points", len(imu_demos_u) N_l = imu_demos_l.shape[0] N_u = imu_demos_u.shape[0] N_min = min(N_l, N_u) print N_min emg_demos_u = emg_demos_u[0:N_min, :] imu_demos_u = imu_demos_u[0:N_min, :] emg_demos_l = emg_demos_l[0:N_min, :] imu_demos_l = imu_demos_l[0:N_min, :] Time = np.arange(N_min).reshape((N_min, 1)) show_ort_l = np.genfromtxt('../../data/work/0/ort_l.mat', delimiter=',') show_ort_l = show_ort_l[16:-6:2, :] np.savetxt('../data/prompt_l.dat', show_ort_l, delimiter=',') show_ort_u = np.genfromtxt('../../data/work/0/ort_u.mat', delimiter=',') show_ort_u = show_ort_u[16:-6:2, :] np.savetxt('../data/prompt_u.dat', show_ort_u, delimiter=',') if has_matlab: print "MATLAB FOUND" ## use the following if there is Malab under /usr/local ## GMM and GMR will be performed os.chdir('../matlab') np.savetxt('ort_data_u', ort_data_u, delimiter=',') np.savetxt('ort_data_l', ort_data_l, delimiter=',') np.savetxt('emg_data_u', emg_data_u_timed, delimiter=',') np.savetxt('emg_data_l', emg_data_l_timed, delimiter=',') subprocess.call([ 'matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo_gmm();exit' ]) os.chdir('../myo_python') else: print "MATLAB NOT FOUND" ## use the following if there is no Matlab ## simply use the aligned average as expected trajectory np.savetxt('../data/demo_l.dat', imu_demos_l[:, -4:], delimiter=',') np.savetxt('../data/demo_u.dat', imu_demos_u[:, -4:], delimiter=',') np.savetxt('../data/emg_l.dat', emg_demos_l, delimiter=',') np.savetxt('../data/emg_u.dat', emg_demos_u, delimiter=',') emg_demos = np.hstack((emg_demos_l, emg_demos_u)) emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) observations = np.hstack((EMG_WEIGHT * emg_demos_l, imu_demos_l, EMG_WEIGHT * emg_demos_u, imu_demos_u)) if nClusters is None: N = max(state_labels) # number of tagged points print "state labels:", N low_limit = max(2, N / 2) # at least 2 clusters high_limit = N scores = {} for n in range(low_limit, high_limit + 2): state_cluster = classifier.SignalCluster(observations, n) score = state_cluster.evaluate(observations, state_cluster.labels) scores[score] = n print '# clusters, score', n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(observations, n_clusters) else: state_cluster = classifier.SignalCluster(observations, nClusters) plt.figure() plt.plot(show_ort_l) #plt.plot(emg_demos_u) plt.plot(state_cluster.labels, '*') plt.show(block=True) ## Currently we implement the n-gram model without explicitly training the MDP ## So the following code is not used ## ============================================================================ ## build mdp # # indexes = [x for x,item in enumerate(valid_states) if item] # critical points # segments = [] # left = indexes[0] # for i,ind in enumerate(indexes): # if ind-left > 5: # segments.append((left, ind)) # left = ind # print segments # pickle.dump(segments, open('../data/segments.dat', 'w')) #actionsData = emg_cluster.labels[valid_states] #statesData = statesData[valid_states] # builder = BuildMDP(actionsData=statesData, statesData=statesData) # use the same labels # pickle.dump(builder, open('../data/mdp.pkl', 'wb')) # # print "path", builder.path # print "policy", builder.Pi #print "expected actions: ", actionsData ## ============================================================================== print "expected states: ", state_cluster.labels #baseline = evaluate(actionsData, statesData, builder) #print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_cluster.labels, trainingFile=None) baseline = None pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) state_labels = state_labels[0:N_min] critical_points = observations[state_labels >= 0] task = state_classifier.predict(critical_points) state_sequence = [] with open('../data/state_sequence.dat', 'w') as f: for item in task: if item not in state_sequence or item != state_sequence[-1]: state_sequence.append(item) f.write(str(item) + '\n')
def main(): parser = argparse.ArgumentParser(description="Do something.") parser.add_argument('-s', '--samples', default=1, type=int) parser.add_argument('-n', '--clusters', default=None, type=int) args = parser.parse_args() for i in range(args.samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i = preprocess( os.path.join('../../data/work/', str(i)), update_extremes=True) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i else: imu_i_a = align_signal(imu_demos, imu_i, w=5, has_time=False) imu_demos += imu_i_a EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i # get average imu_demos = imu_demos / args.samples EMG_MAX = EMG_MAX / args.samples EMG_MIN = EMG_MIN / args.samples GYRO_MAX = GYRO_MAX / args.samples print "EMG_MAX", EMG_MAX print "EMG_MIN", EMG_MIN print "GYRO_MAX", GYRO_MAX print "# data points", len(imu_demos) # imu = data[:, -4:] np.savetxt('../data/imu_sample.dat', imu_demos, delimiter=',') emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) N = imu_demos.shape[0] time = np.arange(1, N + 1).reshape(N, 1) / 10.0 # use seconds as feature observations = np.hstack((time, 1 * emg_demos, imu_demos)) #print observations.shape if args.clusters is None: N = observations.shape[0] low_limit = N / 20 # 0.5 states per second high_limit = 2 * N / 20 # 1.5 states per second scores = {} for n in range(low_limit, high_limit): state_cluster = classifier.SignalCluster(observations, n) score = state_cluster.evaluate(observations, state_cluster.labels) scores[score] = n print '# clusters, score', n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(observations, n_clusters) else: state_cluster = classifier.SignalCluster(observations, args.clusters) #plt.plot(emg) plt.figure() plt.plot(imu_demos) plt.plot(state_cluster.labels, 'r*') plt.figure() plt.plot(emg_demos) #np.savetxt("imu_demos.dat", imu_demos, delimiter=',') np.savetxt("emg_demos.dat", emg_demos, delimiter=',') #plt.plot(emg_cluster.labels, 'go') plt.show(block=False) builder = BuildMDP(actionsData=emg_cluster.labels, statesData=state_cluster.labels) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s' + str(item) + '\n') state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations[:, 1:], state_cluster.labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX), open('../data/state_classifier.pkl', 'wb')) args = { "give_prompt": True, "EMG_MAX": EMG_MAX, "EMG_MIN": EMG_MIN, "GYRO_MAX": GYRO_MAX } progress = myo_state.Progress(classifier=state_classifier, **args)
def build_classifier(samples=1, nClusters=None, ID='user', used=False): if used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": ID } progress = myo_state2.Progress(classifier_pkl='../data/state_classifier.pkl', **args) return has_matlab = os.path.exists('/usr/local/MATLAB') for identifier in ('l', 'u'): for i in range(samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i, states_i = preprocess(os.path.join('../../data/work/', str(i)), update_extremes=True, identifier=identifier) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i emg_data = emg_i imu_data = imu_i state_labels = states_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i Time_a = np.arange(imu_i.shape[0]).reshape((imu_i.shape[0],1)) ort_data_a = np.hstack((Time_a, imu_i[:,-4:])) else: imu_i_a = align_signal(imu_demos/i, imu_i, w=5, has_time=False) imu_demos += imu_i_a emg_data = np.vstack((emg_data, emg_i)) imu_data = np.vstack((imu_data, imu_i)) EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i state_labels = np.concatenate((state_labels, states_i)) # Used by Matlab function to compute the expected trojectory ort_data_a = np.vstack( (ort_data_a, np.hstack((Time_a, imu_i_a[:,-4:]))) ) # # Use original data to train state classifier # # aligned data is only used for expected trajectory # Time = np.arange(emg_i.shape[0]).reshape((emg_i.shape[0],1)) # emg_data.append(np.hstack((Time, emg_i))) # # Time = np.arange(imu_i.shape[0]).reshape((imu_i.shape[0],1)) # imu_data.append(np.hstack((Time, imu_i))) n = len(imu_demos) # get average if identifier == 'l': print "\nProcessing myo on lower arm..." emg_demos_l = emg_demos imu_demos_l = imu_demos/samples EMG_MAX_l = EMG_MAX/samples EMG_MIN_l = EMG_MIN/samples GYRO_MAX_l = GYRO_MAX/samples emg_data_l = emg_data imu_data_l = imu_data ort_data_l = ort_data_a print "EMG_MAX_l", EMG_MAX_l print "EMG_MIN_l", EMG_MIN_l print "GYRO_MAX_l", GYRO_MAX_l print "# data points", len(imu_demos_l) else: print "\nProcessing myo on upper arm..." emg_demos_u = emg_demos imu_demos_u = imu_demos/samples EMG_MAX_u = EMG_MAX/samples EMG_MIN_u = EMG_MIN/samples GYRO_MAX_u = GYRO_MAX/samples emg_data_u = emg_data imu_data_u = imu_data ort_data_u = ort_data_a print "EMG_MAX_u", EMG_MAX_u print "EMG_MIN_u", EMG_MIN_u print "GYRO_MAX_u", GYRO_MAX_u print "# data points", len(imu_demos_u) N_l = imu_demos_l.shape[0] N_u = imu_demos_u.shape[0] print N_u if N_l < N_u: emg_demos_u = emg_demos_u[0:N_l, :] imu_demos_u = imu_demos_u[0:N_l, :] Time = np.arange(N_l).reshape((N_l,1)) elif N_l > N_u: emg_demos_l = emg_demos_l[0:N_u, :] imu_demos_l = imu_demos_l[0:N_u, :] Time = np.arange(N_u).reshape((N_u,1)) else: Time = np.arange(N_u).reshape((N_u,1)) if has_matlab: print "MATLAB FOUND" ## use the following if there is Malab under /usr/local ## GMM and GMR will be performed os.chdir('../matlab') #subprocess.call(['matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo('+str(samples)+');exit']) np.savetxt('ort_data_u', ort_data_u, delimiter=',') np.savetxt('ort_data_l', ort_data_l, delimiter=',') subprocess.call(['matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo_gmm();exit']) os.chdir('../myo_python') else: print "MATLAB NOT FOUND" ## use the following if there is no Matlab ## simply use the aligned average as expected trajectory np.savetxt('../data/demo_l.dat', imu_demos_l[:, -4:], delimiter=',') np.savetxt('../data/demo_u.dat', imu_demos_u[:, -4:], delimiter=',') emg_demos = np.hstack((emg_demos_l,emg_demos_u)) emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) # observations = np.hstack((EMG_WEIGHT*emg_demos_l, imu_demos_l, EMG_WEIGHT*emg_demos_u, imu_demos_u)) # timed_observations = np.hstack((TIME_WEIGHT*Time, EMG_WEIGHT*emg_demos_l, imu_demos_l, EMG_WEIGHT*emg_demos_u, imu_demos_u)) print emg_data_l.shape, imu_data_l.shape, emg_data_u.shape, imu_data_u.shape n_points = min(emg_data_l.shape[0], emg_data_u.shape[0]) state_labels = state_labels[0:n_points] observations = np.hstack((EMG_WEIGHT*emg_data_l[0:n_points, :], imu_data_l[0:n_points, :], EMG_WEIGHT*emg_data_u[0:n_points, :], imu_data_u[0:n_points, :])) # if nClusters is None: # N = observations.shape[0] # low_limit = N/20 # 0.5 states per second # high_limit = int(1.5*N/20) # 0.75 states per second # scores = {} # for n in range(low_limit, high_limit): # state_cluster = classifier.SignalCluster(timed_observations, n) # score = state_cluster.evaluate(timed_observations, state_cluster.labels) # scores[score] = n # print '# clusters, score', n, score # # max_score = max(scores.keys()) # n_clusters = scores[max_score] # state_cluster = classifier.SignalCluster(timed_observations, n_clusters) # else: # state_cluster = classifier.SignalCluster(timed_observations, nClusters) plt.figure() plt.plot(imu_demos) plt.plot(state_labels, '*') plt.show(block=True) statesData = state_labels[0:len(emg_demos)] valid_states = statesData>=0 actionsData = emg_cluster.labels[valid_states] statesData = statesData[valid_states] builder = BuildMDP(actionsData=actionsData, statesData=statesData) pickle.dump(builder, open('../data/mdp.pkl', 'wb')) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s'+str(item)+'\n') print "expected actions: ", actionsData print "expected states: ", statesData baseline = evaluate(actionsData, statesData, builder) print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) action_classifier = classifier.SignalClassifier(n_neighbors=5) action_classifier.train(emg_demos, emg_cluster.labels, trainingFile=None) pickle.dump(action_classifier, open('../data/action_classifier.pkl', 'wb'))
def build_classifier(samples=1, nClusters=None, ID='user', used=False): if used: mdp = pickle.load(open('../data/mdp.pkl')) args = {"give_prompt": True, "mdp": mdp, "id": ID} progress = myo_state2.Progress( classifier_pkl='../data/state_classifier.pkl', **args) return for identifier in ('l', 'u'): for i in range(samples): data_i, EMG_max_i, EMG_min_i, GYRO_max_i = preprocess( os.path.join('../../data/work/', str(i)), update_extremes=True, identifier=identifier) emg_i = data_i[:, 1:9] imu_i = data_i[:, 9:] if i == 0: emg_demos = emg_i imu_demos = imu_i EMG_MAX = EMG_max_i EMG_MIN = EMG_min_i GYRO_MAX = GYRO_max_i else: imu_i_a = align_signal(imu_demos, imu_i, w=5, has_time=False) imu_demos += imu_i_a EMG_MAX += EMG_max_i EMG_MIN += EMG_min_i GYRO_MAX += GYRO_max_i n = len(imu_demos) # get average if identifier == 'l': print "\nProcessing myo on lower arm..." emg_demos_l = emg_demos imu_demos_l = imu_demos / samples EMG_MAX_l = EMG_MAX / samples EMG_MIN_l = EMG_MIN / samples GYRO_MAX_l = GYRO_MAX / samples print "EMG_MAX_l", EMG_MAX_l print "EMG_MIN_l", EMG_MIN_l print "GYRO_MAX_l", GYRO_MAX_l print "# data points", len(imu_demos_l) else: print "\nProcessing myo on upper arm..." emg_demos_u = emg_demos imu_demos_u = imu_demos / samples EMG_MAX_u = EMG_MAX / samples EMG_MIN_u = EMG_MIN / samples GYRO_MAX_u = GYRO_MAX / samples print "EMG_MAX_u", EMG_MAX_u print "EMG_MIN_u", EMG_MIN_u print "GYRO_MAX_u", GYRO_MAX_u print "# data points", len(imu_demos_u) N_l = imu_demos_l.shape[0] N_u = imu_demos_u.shape[0] print N_u if N_l < N_u: emg_demos_u = emg_demos_u[0:N_l, :] imu_demos_u = imu_demos_u[0:N_l, :] Time = np.arange(N_l).reshape((N_l, 1)) elif N_l > N_u: emg_demos_l = emg_demos_l[0:N_u, :] imu_demos_l = imu_demos_l[0:N_u, :] Time = np.arange(N_u).reshape((N_u, 1)) else: Time = np.arange(N_u).reshape((N_u, 1)) if os.path.exists('/usr/local/MATLAB'): print "MATLAB FOUND" ## use the following if there is Malab under /usr/local ## GMM and GMR will be performed os.chdir('../matlab') subprocess.call([ 'matlab', '-nodisplay', '-nojvm', '-nosplash', '-r', 'demo(' + str(samples) + ');exit' ]) os.chdir('../myo_python') else: print "MATLAB NOT FOUND" ## use the following if there is no Matlab ## simply use the aligned average as expected trajectory np.savetxt('../data/demo_l.dat', imu_demos_l[:, -4:], delimiter=',') np.savetxt('../data/demo_u.dat', imu_demos_u[:, -4:], delimiter=',') emg_demos = np.hstack((emg_demos_l, emg_demos_u)) emg_cluster = classifier.SignalCluster(emg_demos, n_clusters=8) observations = np.hstack((EMG_WEIGHT * emg_demos_l, imu_demos_l, EMG_WEIGHT * emg_demos_u, imu_demos_u)) timed_observations = np.hstack( (TIME_WEIGHT * Time, EMG_WEIGHT * emg_demos_l, imu_demos_l, EMG_WEIGHT * emg_demos_u, imu_demos_u)) if nClusters is None: N = observations.shape[0] low_limit = N / 20 # 0.5 states per second high_limit = int(1.5 * N / 20) # 0.75 states per second scores = {} for n in range(low_limit, high_limit): state_cluster = classifier.SignalCluster(timed_observations, n) score = state_cluster.evaluate(timed_observations, state_cluster.labels) scores[score] = n print '# clusters, score', n, score max_score = max(scores.keys()) n_clusters = scores[max_score] state_cluster = classifier.SignalCluster(timed_observations, n_clusters) else: state_cluster = classifier.SignalCluster(timed_observations, nClusters) plt.figure() plt.plot(imu_demos) plt.plot(state_cluster.labels, '*') plt.show(block=False) builder = BuildMDP(actionsData=emg_cluster.labels, statesData=state_cluster.labels) pickle.dump(builder, open('../data/mdp.pkl', 'wb')) print "path", builder.path print "policy", builder.Pi with open('../data/state_sequence.dat', 'w') as f: for item in builder.path: f.write('s' + str(item) + '\n') print "expected actions: ", emg_cluster.labels print "expected states: ", state_cluster.labels baseline = evaluate(emg_cluster.labels, state_cluster.labels, builder) print "baseline performance: ", baseline state_classifier = classifier.SignalClassifier(n_neighbors=5) state_classifier.train(observations, state_cluster.labels, trainingFile=None) pickle.dump((state_classifier, EMG_MAX, EMG_MIN, GYRO_MAX, baseline), open('../data/state_classifier.pkl', 'wb')) action_classifier = classifier.SignalClassifier(n_neighbors=5) action_classifier.train(emg_demos, emg_cluster.labels, trainingFile=None) pickle.dump(action_classifier, open('../data/action_classifier.pkl', 'wb'))