-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSCHAD_CLA.py
More file actions
73 lines (65 loc) · 2.89 KB
/
USCHAD_CLA.py
File metadata and controls
73 lines (65 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
from scipy.stats import zscore
from pandas import read_csv
from scipy.io import loadmat
import time
from datetime import datetime
from tqdm import tqdm
import sys, os
from experimentsetup import *
from preprocessingandrecords import *
from architecturefns import *
if __name__ == "__main__":
args = sys.argv
# PARAMETERS
CYCLELEN = int(args[1])
NUMPAT = int(args[2])
DISTFN = args[3] #"PHASEINV"/"PRECIS" or "DTW"
DICTYPE = args[4] # YEH, RAND, SYSRAND
SENSOR = 1 # y acc; there are six total: xyz acc, xyz gyro
exp = Experiment(DISTFN, [NUMPAT, CYCLELEN], algyield=True, multivariate=False)
activitymap = {1: "Walking Forward", 2: "Walking Left", 3: "Walking Right", 4: "Walking Upstairs", 5: "Walking Downstairs", 6: "Running Forward", 7: "Jumping Up", 8: "Sitting", 9: "Standing", 10: "Sleeping", 11: "Elevator Up", 12: "Elevator Down"}
omitactivities=["a8t","a9t","a10"]
main_dir = 'USC-HAD/'
i = 0
dataset = []
labels = []
lens = []
for (root,dirs,files) in os.walk(main_dir):
for d in dirs:
for (subj_root,subj_dirs,subj_files) in os.walk(root+d):
for f in subj_files:
if f[:3] not in omitactivities: # omit sedentary
filedata = loadmat(main_dir+d+"/"+f)
filedata = np.asarray(filedata['sensor_readings'])
filedata = filedata[:,SENSOR]
filedata = np.transpose(filedata)
label = f[1:-6]
lens.append(len(filedata))
dataset.append(zscore(filedata, nan_policy='omit', axis=0))
labels.append(label)
print("FINISHED PROCESSING DATA")
RUNLOOP = 1
if DICTYPE=="RAND":
RUNLOOP=10
for ROUNDS in tqdm(range(RUNLOOP),desc="Build Dictionaries, S={}, L={}, {} {}:".format(NUMPAT,CYCLELEN, DICTYPE, DISTFN)):
use_dicts = []
for ts_idx in tqdm(range(len(dataset))):
ts = dataset[ts_idx]
d = None
if DICTYPE=="RAND":
d, _ = randdict(ts, NUMPAT, CYCLELEN, N_SENSORS=1)
elif DICTYPE=="SYSRAND":
d, _ = sysranddict(ts, NUMPAT, CYCLELEN, N_SENSORS=1)
elif DICTYPE=="YEH":
d, _ = exp.make_exemplar(ts)
use_dicts.append(d)
STARTTIME = time.time()
distmat = exp.distmat_from_dicts(use_dicts)
ENDTIME = time.time()
print("FINISHED MAKING DISTMAT: {}, {}, {}, {}".format(NUMPAT, CYCLELEN, DISTFN, DICTYPE))
accrate, errors = CLA_inference(distmat,labels,len(use_dicts))
print("activity classification accuracy: {}".format(accrate))
log = open("logofresults.txt", "a")
log.write("{}, {}, {}, {}, {}, {}, {}\n".format(datetime.now(),DICTYPE,"PRECIS",NUMPAT,CYCLELEN,ENDTIME-STARTTIME, accrate))
log.close()