<file_sep>import pandas as pd
import numpy as np
import scipy.io as sio
import pickle
import sklearn.svm as svm
from sklearn.externals import joblib 
from sklearn.model_selection import train_test_split,cross_val_score,ShuffleSplit,StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
import sys
import os
from sklearn.metrics import f1_score
from sklearn.linear_model import LinearRegression,Lasso,Ridge
from sklearn import metrics



label = pd.read_csv('../data_labels/CIS-PD_Training_Data_IDs_Labels.csv')
#trainY = np.array(label['dyskinesia'])
# 'on_off', 'dyskinesia', 'tremor'

if __name__ == '__main__':
    i = 0
    while i < len(sys.argv[1:]):
        fn = sys.argv[i+1]
        trainY = np.array(label[sys.argv[i+2]])


        with open(fn, 'rb') as handle:
            all_presentations = pickle.loads(handle.read())
        
        filename,ext = os.path.splitext(fn)
        
        not_nan = ~np.isnan(trainY)
        
        X = all_presentations[1:][not_nan]
        Y = trainY[not_nan]
        D = X.shape[1]
        #Test if concatenating mean value within each patient is helpful
        #X = X[:,:(D//2)]
        seed = np.random.randint(100)
        trainX,testX,trainY,testY = train_test_split(X,Y,random_state = 20,test_size = 0.25)
        #LinearSVC doesn't converge
#        print("seed: ",seed)
#        Lsvc = svm.LinearSVC(class_weight='balanced')
#        Lsvc.fit(trainX,trainY)
#        pred_trainY = Lsvc.predict(trainX)
#        pred_testY = Lsvc.predict(testX)

#        print("LSVC_train accuracy rate: {} F1-score: {}".format(Lsvc.score(trainX,trainY),f1_score(trainY,pred_trainY,average = 'weighted')))
#        print("LSVC_test  accuracy rate: {} F1-score: {}".format(Lsvc.score(testX,testY),f1_score(testY,pred_testY,average = 'weighted')))

#        print(metrics.confusion_matrix(trainY, pred_trainY))
#        print(metrics.classification_report(trainY,pred_trainY, digits=3))
#        print(metrics.confusion_matrix(testY, pred_testY))
#        print(metrics.classification_report(testY,pred_testY, digits=3))

        #Final test its MSE
        # Parameters change according to the labels
        
        svc = svm.SVC(class_weight=None,kernel = 'linear',gamma=0.001,degree=3,C=1)
        svc.fit(trainX,trainY)
        pred_trainY = svc.predict(trainX)
        pred_testY = svc.predict(testX)
        print("SVC_train accuracy rate: {} F1-score: {}".format(svc.score(trainX,trainY),f1_score(trainY,pred_trainY,average = 'weighted')))
        print("SVC_test  accuracy rate: {} F1-score: {}".format(svc.score(testX,testY),f1_score(testY,pred_testY,average = 'weighted')))
        print("MSE:{}".format(np.mean((pred_testY-testY)**2)))
        joblib.dump(svc,filename+'_'+sys.argv[i+2]+'_svc.pkl')

        print(metrics.confusion_matrix(trainY, pred_trainY))
        print(metrics.classification_report(trainY,pred_trainY, digits=3))
        print(metrics.confusion_matrix(testY, pred_testY))
        print(metrics.classification_report(testY,pred_testY, digits=3))
        
        #GridSearchCV
        
        cv = StratifiedShuffleSplit(n_splits=5, test_size=0.25, random_state=0)
        parameters = {'kernel':('linear', 'rbf','poly'),'degree':[3,5,7], 'C':[0.001, 0.01, 0.1, 1, 10],'gamma' : [0.001, 0.01, 0.1, 1],'class_weight':['balanced',None]}
        clf = GridSearchCV(svm.SVC(max_iter = 5000), parameters,cv=cv)
        clf.fit(X,Y)
        
        cv = StratifiedShuffleSplit(n_splits=5, test_size=0.25, random_state=1)
        scores_accuracy = cross_val_score(clf.best_estimator_, X, Y, cv=cv)
        scores_f1 = cross_val_score(clf.best_estimator_,X,Y,cv=cv,scoring='f1_weighted')
        print(sys.argv[i+2])
        print(clf.best_estimator_)
        print("cv mean:",scores_accuracy.mean())
        print("cv std:",scores_accuracy.std())

        i += 2

        
        
        
        
        joblib.dump(clf,filename+'_clf.pkl')
        #joblib.dump(svc, filename+'_svc0410.pkl')



<file_sep># -*- coding: utf-8 -*-
"""
Created on Wed Feb 19 10:33:51 2020

@author: eva
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#0:fully on 4:fully 0ff
label = pd.read_csv('data_labels/CIS-PD_Training_Data_IDs_Labels.csv')

step = pd.DataFrame(np.arange(0,1200.1,0.1),columns = ['step'])
step['step'] = step['step'].map(lambda x:str(round(x,1)))
def data_array(subject_id):
    measurement_id = label[label['subject_id'] == subject_id]['measurement_id'].values
    group = []
    for id in measurement_id:
        m = pd.read_csv('training_data/'+id+'.csv')
        m['step'] = m['Timestamp'].map(lambda x:str(round(x,1)))
        m= pd.concat([m,step],sort=False)        
        m_red = m.groupby('step').mean()
        group.append(m_red[["X","Y","Z"]].values)
    return group
l = label[label['subject_id'] == 1004]['on_off'].values
per1004 = data_array(1004)
per = np.array(per1004)







measurement_id = label[label['subject_id'] == 1004]['measurement_id'].values
l = label[label['subject_id'] == 1004].loc[0][2]

m = pd.read_csv('training_data/'+measurement_id[0]+'.csv')
m.iloc[:10]
m.iloc[-10:]
m.Y.sum()

m.plot('Timestamp','X')
m.plot('Timestamp','Y')
m.plot('Timestamp','Z')

m['seconds'] = m['Timestamp'].map(lambda x:int(x*10)/10)

m.iloc[:10].groupby('seconds').mean()
xx= pd.concat([m,b],sort=False)
np.arange(0,1200,0.01)
xx_red = xx.groupby('seconds').mean()

m_red = m.groupby('seconds').mean()
def plot(df):
    fig,ax = plt.subplots()
    ax.plot(df['Timestamp'],df.X,df['Timestamp'],df.Y,df['Timestamp'],df.Z)
    plt.legend(('X','Y','Z'))
    plt.show()

plot(m_red)


def plot_multi(s=0,n=0):
    fig,axs = plt.subplots(n-s,1)
    t = []
    for i in range(n-s):
        measurement_id = label[label['subject_id'] == 1004].loc[s+i][0]
        m = pd.read_csv('training_data/'+measurement_id+'.csv')
        l = label[label['subject_id'] == 1004].loc[i][2]
        m['seconds'] = m['Timestamp'].map(int)+1
        m = m.groupby('seconds').mean()
        t.extend(m[["X","Y","Z"]].values)
        axs[i].plot(m['Timestamp'],m.X,m['Timestamp'],m.Y,m['Timestamp'],m.Z)
        axs[i].set_title("{}:{}".format('label',l))
        #axs[i].legend(('X','Y','Z'))
    
    
    
    fig.tight_layout()
    #return t        
t = plot_multi(4,7)
t = np.array(t)
plt.plot(range(3600),t[:,0],range(3600),t[:,1],range(3600),t[:,2])



threedee = plt.figure().gca(projection='3d')
threedee.plot(m.Timestamp,m.X,m.Y)
threedee.set_xlabel('X')
threedee.set_ylabel('Y')
threedee.set_zlabel('Z')
plt.show()<file_sep>import pandas as pd
import numpy as np
import scipy.io as sio
import pickle
import sys

label_Train = pd.read_csv('../data_labels/CIS-PD_Training_Data_IDs_Labels.csv')
label_Test = pd.read_csv('../data_labels/CIS-PD_Testing_Data_IDs_Labels.csv')


id_list = label_Train['subject_id'].unique()
c_Train = list(label_Train.groupby('subject_id')['measurement_id'].count())
c_Test  = list(label_Test.groupby('subject_id')['measurement_id'].count())



def count(subject_id,data):
    l = []
    if data == 'Train':
        measurement_id = label_Train[label_Train['subject_id'] == subject_id]['measurement_id'].values
        for id in measurement_id:

            m = pd.read_csv('../training_data/'+id+'.csv')
            m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
            m = m.groupby('step').mean()
            l.append(len(m))

    else:
        measurement_id = label_Test[label_Test['subject_id'] == subject_id]['measurement_id'].values

        for id in measurement_id:
            m = pd.read_csv('../testing_data/'+id+'.csv')
            m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
            m = m.groupby('step').mean()
            l.append(len(m))

    return l


def create_tran(n):
    counter = 0
    #all_tran = {}
    for i,id in enumerate(id_list):
        
        cur = 0
        for data in ["Train","Test"][:n]:
            l = count(id,data)
            for j in l:
                seq = mat["Psi"][0]["stateSeq"][-1]["z"][0][i][0][cur:cur+j]
                cur += j
                cur += 1
                tran = np.zeros((c_state,c_state))

                for ts in range(j-1):
                    tran[seq[ts]-1][seq[ts+1]-1] += 1
                tran = np.where(tran==0,0.1,tran)

                tran = tran/tran.sum(axis = 1)
                all_tran[counter] = tran
                counter += 1
        print(id,"done")

def create_HMM(N,D):
    for i in range(N):
        tran = all_tran[i]
        d,v = np.linalg.eig(tran.T)
        pos_zero = np.argmin(abs(d-1))

        if np.round(d[pos_zero],6) != 1:
            print('{} has no eigenvalue equal to 1'.format(i))

        p = v[pos_zero]
        p /= sum(p)
        all_HMM[i,:D] = p




if __name__ == '__main__':
    i = 0
    while i < len(sys.argv[1:]):
        route = sys.argv[1:][i]
        mat = sio.loadmat('../result/'+str(route)+'/SamplerOutput.mat')
        c_state = (mat["Psi"][0]["F"][-1]).shape[1]
        all_tran = {}
        if sys.argv[1:][i+1] == 'Test':
            create_tran(2)
        else:
            create_tran(1)
            c_Test = [0 for i in c_Test]

        N = len(all_tran)
        D = all_tran[0].shape[1]
        all_HMM = np.zeros((N,2*D))
        create_HMM(N,D)
        print('Total data seq:{}'.format(N))
        print('Length of vectors:{}'.format(2*D))
        test_HMM = np.zeros((1,2*D))
        train_HMM = np.zeros((1,2*D))
        cur = 0
        for l in range(len(c_Train)):
            all_HMM[cur:c_Train[l]+c_Test[l]+cur,D:] = np.tile(all_HMM[cur:c_Train[l]+c_Test[l]+cur,:D].mean(axis = 0),(c_Train[l]+c_Test[l],1))
            train_HMM = np.concatenate((train_HMM,all_HMM[cur:cur+c_Train[l]]))
            cur += c_Train[l]
            test_HMM = np.concatenate((test_HMM,all_HMM[cur:cur+c_Test[l]]))
            cur += c_Test[l]

        with open(str(route).replace('/','_')+'_seq_train_HMM.txt', 'wb') as handle:
            pickle.dump(train_HMM, handle)

        with open(str(route).replace('/','_')+'_seq_test_HMM.txt', 'wb') as handle:
            pickle.dump(test_HMM, handle)


        i += 2
'''
for i in range(len(all_tran)):
    if i % 50 == 0:
        print(i,all_tran[i])
'''
<file_sep># -*- coding: utf-8 -*-
"""
Created on Mon May  4 22:25:12 2020

@author: eva
"""

import pandas as pd
import numpy as np
import scipy.io as sio
import pickle
import sys
from sklearn.externals import joblib 
import os

label_Train = pd.read_csv('data_labels/CIS-PD_Training_Data_IDs_Labels.csv')
label_Test = pd.read_csv('data_labels/CIS-PD_Testing_Data_IDs_Labels.csv')


id_list = label_Train['subject_id'].unique()
c_Train = list(label_Train.groupby('subject_id')['measurement_id'].count())
c_Test  = list(label_Test.groupby('subject_id')['measurement_id'].count())


if __name__ == '__main__':
    fn = sys.argv[1]
    filename,ext = os.path.splitext(fn)
    with open(fn, 'rb') as handle:
        testing_data = pickle.loads(handle.read())
    
    model = joblib.load(sys.argv[2])
    
    a = model.predict(testing_data)
    
    with open("dyskinesia.txt",'wb') as fn:
        pickle.dump(a,fn)


<file_sep># INF552_project

## Introduction

Wearable devices enable us to track human behaviors continuously. Recognizing the connection between continuous sensor data and human actions may help the physicians better understand the conditions of patients. In this report, the Hidden Markov Model is utilized to extract the patterns of continuous accelerometer sequences and the extracted features are proved to be helpful in predicting Parkinson’s Disease patients’ medical states. I directly use the raw data provided and the accuracy rates are around 60% for predicting medication states and classifying severity of tremor and dyskinesia. Additional analysis and data preprocessing on the human sensory data may further recognize the human behavior patterns and increase the classification accuracy. Therefore, it may become feasible for doctors to use sensor data recorded by the portable devices as a support to prescribe the medicine.

## Model

As we were provided with the continuous collection of sensor data from wearable devices, there are more than hundred thousand timestamps in each individual file. Instead of training machine/deep learning on the dataset directly, it may be more practical and efficient to analyze data after extracting some patterns of data. 
I utilized the Hidden Markov Model (HMM) to help discover the potential patterns of human behaviors. Then, embedded the HMM before running classification and regression model on the data.

<file_sep>import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
import scipy.io as sio


label_Train = pd.read_csv('data_labels/CIS-PD_Training_Data_IDs_Labels.csv')
label_Test  = pd.read_csv('data_labels/CIS-PD_Testing_Data_IDs_Labels.csv') 


#Return a single wo minutes data
def id_single_data(subject_id,i):
    measurement_id = label_Train[label_Train['subject_id'] == subject_id]['measurement_id'].values
    id = measurement_id[i]
    m = pd.read_csv('training_data/'+id+'.csv')
    m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
    m = m.groupby('step').mean()
    return m[["X","Y","Z"]].values


#Return an array of each subject_id with or without scaling within each data points
#seperate data by [0,0,0]
def concatData(subject_id,z = False):
    measurement_id = label_Train[label_Train['subject_id'] == subject_id]['measurement_id'].values
    group = []
    for id in measurement_id:
        m = pd.read_csv('training_data/'+id+'.csv')
        m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
        m = m.groupby('step').mean()
        if not z:
            group.extend(m[["X","Y","Z"]].values)
        else:
            group.extend(scale(m[["X","Y","Z"]].values,axis = 0))
        group.append([0,0,0])
    
    measurement_id = label_Test[label_Test['subject_id'] == subject_id]['measurement_id'].values
    
    for id in measurement_id:
        m = pd.read_csv('testing_data/'+id+'.csv')
        m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
        m = m.groupby('step').mean()
        if not z:
            group.extend(m[["X","Y","Z"]].values)
        else:
            group.extend(scale(m[["X","Y","Z"]].values,axis = 0))
        group.append([0,0,0])


    return np.array(group)

#Return an array of each subject with scaling within each person
#First scaling, then add [0,0,0] to split the data
def zscoreAmongperson(subject_id):
    measurement_id = label_Train[label_Train['subject_id'] == subject_id]['measurement_id'].values
    l = []
    group = []
    for id in measurement_id:
        m = pd.read_csv('training_data/'+id+'.csv')
        m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
        m = m.groupby('step').mean()
        l.append(len(m))
        group.extend(m[["X","Y","Z"]].values)
        #group.append([0,0,0])
    measurement_id = label_Test[label_Test['subject_id'] == subject_id]['measurement_id'].values
   
    for id in measurement_id:
        m = pd.read_csv('testing_data/'+id+'.csv')
        m['step'] = m['Timestamp'].map(lambda x:int(x*5)/10)
        m = m.groupby('step').mean()
        l.append(len(m))
        group.extend(m[["X","Y","Z"]].values)
        #group.append([0,0,0])
    
    group = scale(np.array(group),axis = 0)
    cur = 0
    for i in l:
        cur += i
        group = np.insert(group,cur,np.array([[0,0,0]]),axis = 0)
        cur += 1
    
    return np.array(group)

def createArray(l):
    noScale = []
    Scaledatapoints = []
    Scaleperson = []

    for i,id in enumerate(l):

        noScale.append(concatData(id,False))
        np.savetxt('outputA_Total/'+str(id_list[i])+'.csv',noScale[-1])
        #sio.savemat('outputA_Total/'+str(id_list[i])+'.mat',{'data':noScale[-1]})
        Scaledatapoints.append(concatData(id,True))
        #sio.savemat('outputB_Total/'+str(id_list[i])+'.mat',{'data':Scaledatapoints[-1]})
        np.savetxt('outputB_Total/'+str(id_list[i])+'.csv',Scaledatapoints[-1])
        Scaleperson.append(zscoreAmongperson(id))
        #sio.savemat('outputC_Total/'+str(id_list[i])+'.mat',{'data':Scaleperson[-1]})
        np.savetxt('outputC_Total/'+str(id_list[i])+'.csv',Scaleperson[-1])
        print(id," done!")

    return np.array(noScale),np.array(Scaledatapoints),np.array(Scaleperson)



id_list = label_Train['subject_id'].unique()

A,B,C = createArray(id_list)

#np.savetxt(str(id_list[0])+'.csv',A[0])
#np.savetxt(str(id_list[0])+'_B.csv',B[0])
#np.savetxt(str(id_list[0])+'_C.csv',C[0])
#
#
#sio.savemat(str(id_list[0])+'_B.mat', {'mydata': A[0]})
#
#A[0][:10]
#B[0][:10]
#C[0].sum(axis = 0)
<file_sep>import pandas as pd
import numpy as np
import scipy.io as sio
import pickle
import sklearn.svm as svm
from sklearn.externals import joblib 
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV,train_test_split,cross_val_score,ShuffleSplit,StratifiedShuffleSplit

import sys
import os
from sklearn.metrics import f1_score
from sklearn.linear_model import LinearRegression,Lasso,Ridge
from sklearn import metrics



label = pd.read_csv('../data_labels/CIS-PD_Training_Data_IDs_Labels.csv')
#trainY = np.array(label['dyskinesia'])
# 'on_off', 'dyskinesia', 'tremor'

if __name__ == '__main__':
    i = 0
    while i < len(sys.argv[1:]):
        fn = sys.argv[i+1]
        trainY = np.array(label[sys.argv[i+2]])


        with open(fn, 'rb') as handle:
            all_presentations = pickle.loads(handle.read())
        
        filename,ext = os.path.splitext(fn)
        
        not_nan = ~np.isnan(trainY)
        
        X = all_presentations[1:][not_nan]
        Y = trainY[not_nan]
        D = X.shape[1]
        #Test if concatenating mean value within each patient is helpful
        #X = X[:,:(D//2)]
        
        #Final test its MSE
        
        seed = np.random.randint(100)
        trainX,testX,trainY,testY = train_test_split(X,Y,random_state = 20,test_size = 0.25)
        print("seed: ",seed)

        # Parameters change according to the labels
        lasso = Lasso(alpha=0.005,max_iter=5000)
        lasso.fit(trainX,trainY)
        print("lasso_mse:",np.mean((lasso.predict(testX)-testY)**2))


        #GridSearchCV
        
        parameters = {'alpha':[0.005, 0.01, 0.1, 1, 10]}

        lr = LinearRegression()
        cv = StratifiedShuffleSplit(n_splits=5, test_size=0.25, random_state=0)
        
        clf_lasso=GridSearchCV(Lasso(max_iter=5000),parameters,cv=cv)
        clf_ridge=GridSearchCV(Ridge(max_iter=5000),parameters,cv=cv)
        clf_lasso.fit(X,Y)
        clf_ridge.fit(X,Y)      
        
        print(sys.argv[i+2])
        cv = StratifiedShuffleSplit(n_splits=5, test_size=0.25, random_state=1)

        scores_accuracy = cross_val_score(clf_lasso.best_estimator_, X, Y, cv=cv,scoring='neg_mean_squared_error')
        print("lasso")
        print(clf_lasso.best_estimator_)
        print(scores_accuracy.mean())
        print(scores_accuracy.std())
        

        scores_accuracy = cross_val_score(clf_ridge.best_estimator_, X, Y, cv=cv,scoring='neg_mean_squared_error')
        print("ridge")
        print(clf_ridge.best_estimator_)
        print(scores_accuracy.mean())
        print(scores_accuracy.std())


        i += 2
        
