-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathal.py
More file actions
779 lines (656 loc) · 26.9 KB
/
Copy pathal.py
File metadata and controls
779 lines (656 loc) · 26.9 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#!/usr/bin/python
# python al.py <option>+
#
# Performs activity learning on the given data file and outputs either the
# learned model, the results of a cross-validation test, or annotated data.
# Written by Diane J. Cook, Washington State University.
# Copyright (c) 2020. Washington State University (WSU). All rights reserved.
# Code and data may not be used or distributed without permission from WSU.
# python al.py --mode PARTITION --algorithm mlp --epochs 10 --data data --irregular_threshold 0.4
import datetime
import gzip
import os
import sys
import warnings
from datetime import datetime
from datetime import timedelta
import joblib
import numpy as np
from sklearn.cluster import MiniBatchKMeans
from sklearn.decomposition import PCA
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import train_test_split
import config
# --- new for gradient learners -----------------
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import numpy as np
cf = config.Config()
np.set_printoptions(threshold=sys.maxsize)
warnings.filterwarnings('ignore')
## ADD
# al.py
# Map specific activities to who to call
ACTIVITY_ALERT = {
# Nurse-alerts (medical/care tasks)
'Personal_Hygiene': 'nurse',
'Morning_Meds': 'nurse',
'Evening_Meds': 'nurse',
'Sleep_Out_Of_Bed': 'nurse', # patient got up at odd time
'Bathe': 'nurse',
'Leave_Home': 'nurse', # unexpected exit
'Step_Out': 'nurse', # wandering outside
# Police-alerts (security/intrusion)
'Enter_Home': 'police', # unexpected entry
}
class MultiClassMLP(nn.Module):
def __init__(self, in_features, num_classes):
super().__init__()
self.fc1 = nn.Linear(in_features, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(128, num_classes)
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x # no softmax here (handled by CrossEntropyLoss)
def loss(self, y_hat, y):
criterion = nn.CrossEntropyLoss()
return criterion(y_hat, y.long())
class _LinearNet(nn.Module):
def __init__(self, in_features, bias=True): # shared base
super().__init__()
self.linear = nn.Linear(in_features, 1, bias=bias)
def forward(self, x):
return self.linear(x).squeeze(1)
class Adaline(_LinearNet):
# binary classification – assumes labels 0/1
def loss(self, y_hat, y):
return torch.mean((y - y_hat) ** 2) # Adaline uses squared error
class LinReg(_LinearNet):
# regression – y can be real valued
def loss(self, y_hat, y):
return torch.mean((y - y_hat) ** 2)
# --- inside _train_gradient() ---
def _train_gradient(model, X, y, *, epochs=50, lr=1e-3, log=False):
"""
Generic trainer for Adaline / LinReg.
- Standardises every feature column (mean 0, std 1)
- Uses full-batch SGD with learning-rate lr
- Optionally prints loss (and accuracy for Adaline) each epoch
"""
# ---------- scale inputs ----------
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X)
X = scaler.transform(X)
# ---------- tensors ----------
X = torch.tensor(X, dtype=torch.float32)
y = torch.tensor(y, dtype=torch.float32)
# ---------- optimiser ----------
opt = torch.optim.SGD(model.parameters(), lr=lr)
for epoch in range(epochs):
opt.zero_grad()
out = model(X)
loss = model.loss(out, y)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
opt.step()
if log:
if isinstance(model, Adaline): # binary acc.
acc = ((out >= 0.5) == y).float().mean().item()
print(f"Epoch {epoch+1:3d}/{epochs}: "
f"loss={loss.item():.6f} acc={acc:.3f}")
else:
print(f"Epoch {epoch+1:3d}/{epochs}: "
f"loss={loss.item():.6f}")
# return both objects so you can reuse them at prediction time
return model, scaler
def _fit_model(x, y):
if cf.algorithm == 'rf':
cf.clf.fit(x, y)
elif cf.algorithm == 'mlp':
model = MultiClassMLP(in_features=len(x[0]), num_classes=cf.num_activities)
cf.clf, cf.scaler = _train_gradient(model,
x, y,
epochs=cf.epochs,
lr=0.001,
log=cf.logepochs)
else:
raise ValueError(f"Unknown algorithm {cf.algorithm}")
def get_datetime(date, time):
""" Convert a pair of date and time strings to a datetime structure.
"""
dtstr = date + ' ' + time
try:
dt = datetime.strptime(dtstr, "%Y-%m-%d %H:%M:%S.%f")
except:
dt = datetime.strptime(dtstr, "%Y-%m-%d %H:%M:%S")
return dt
def compute_seconds(dt):
""" Compute the number of seconds that have elapsed past midnight
for the current datetime structure.
"""
seconds = dt - dt.replace(hour=0, minute=0, second=0)
return int(seconds.total_seconds())
def find_sensor(sensorname):
""" Return the index of a specific sensor name in the list of sensors.
"""
try:
i = cf.sensornames.index(sensorname)
return i
except:
print("Could not find sensor ", sensorname)
return -1
def find_activity(aname):
""" Return the index of a specific activity name in the list of activities.
If the specified activity is not found in the list, add the new name to the
list.
"""
try:
i = cf.activitynames.index(aname)
return i
except:
if cf.mode == "TEST":
print("Could not find activity ", aname)
return -1
else:
cf.activitynames.append(aname)
cf.num_activities += 1
return cf.num_activities - 1
def read_data():
""" Read the data file containing timestamped sensor readings, segment
into non-overlapping windows, and extract features for each window.
"""
first_event = True
cf.current_timestamp = get_datetime("2001-01-01", "00:00:00.00000")
datafile = open(cf.data_filename, "r")
count = 0
for line in datafile:
words = line.split() # Split line into words separated by spaces
date = words[0]
stime = words[1]
sensorid = words[2]
newsensorid = words[3]
sensorstatus = words[4]
alabel = words[5]
dt = get_datetime(date, stime)
cf.current_seconds_of_day = compute_seconds(dt)
cf.day_of_week = dt.weekday()
previousTimestamp = cf.current_timestamp
cf.current_timestamp = dt
snum1 = find_sensor(sensorid)
timediff = cf.current_timestamp - previousTimestamp
# reset the sensor times and the window
if first_event or timediff.days < 0 or timediff.days > 1:
for i in range(cf.num_sensors):
cf.sensortimes[i] = cf.current_timestamp - timedelta(days=1)
first_event = False
cf.wincnt = 0
if sensorstatus == "ON" and cf.dstype[snum1] == 'n':
cf.dstype[snum1] = 'm'
cf.sensortimes[snum1] = cf.current_timestamp # last time sensor fired
end = False
tempdata = np.zeros(cf.num_features)
if alabel != "Other_Activity" or not cf.ignore_other:
end, tempdata = compute_feature(dt, sensorid, newsensorid)
if end: # End of window reached, add feature vector
if alabel == "Other_Activity" and cf.cluster_other:
cf.labels.append(-1)
else:
cf.labels.append(find_activity(alabel))
cf.data.append(tempdata)
count += 1
cf.timestamps.append(dt) # record the timestamp of this window
datafile.close()
if cf.mode != "TEST":
if cf.cluster_other:
cluster_other_class()
# ---------- feature-vector debug option ----------
if cf.featuredump and len(cf.data) > 0:
print('First feature vector:\n', cf.data[0])
sys.exit(0)
# -------------------------------------------------
def cluster_other_class():
""" Cluster the Other_Activity class into subclasses.
"""
carray = []
for i in range(len(cf.labels)):
if cf.labels[i] == -1: # Other activity
carray.append(cf.data[i])
kmeans = MiniBatchKMeans(n_clusters=cf.num_clusters).fit(carray)
clabels = kmeans.labels_
newlabels = kmeans.predict(carray) # actual number of resulting clusters
nc = len(set(newlabels))
cf.num_clusters = nc
for i in range(cf.num_clusters):
cf.activitynames.append('cluster_' + str(i))
index = 0 # assign Other_Activity labels new subclasses
for i in range(len(cf.labels)):
if cf.labels[i] == -1: # Other activity
cf.labels[i] = newlabels[index] + cf.num_activities
index += 1
with gzip.GzipFile(cf.model_path + 'clusters.gz', 'wb') as f:
joblib.dump(kmeans, f) # save clusters to file
# joblib.dump(kmeans, cf.model_path + 'clusters.pkl') # save clusters to file
cf.num_activities += cf.num_clusters
def assign_features(wsize, prevwin1, prevwin2, sensorid1, lastlocation,
lastmotionlocation, complexity, numtransitions, numdistinctsensors):
""" Assign values to features indices in the feature vector.
"""
tempdata = np.zeros(cf.num_features)
# Attribute 0..2: time of last sensor event in window
tempdata[0] = cf.windata[cf.max_window - 1][1] / 3600 # hour of day
tempdata[1] = cf.windata[cf.max_window - 1][1] # seconds of day
tempdata[2] = cf.windata[cf.max_window - 1][2] # day of week
# Attribute 3: time duration of window in seconds
time1 = cf.windata[cf.max_window - 1][1] # most recent sensor event
time2 = cf.windata[cf.max_window - wsize][1] # first sensor event in window
if time1 < time2:
duration = time1 + (cf.seconds_in_a_day - time2)
else:
duration = time1 - time2
tempdata[3] = duration # window duration
timehalf = cf.windata[int(cf.max_window - (wsize / 2))][1] # halfway point
if time1 < time2:
duration = time1 + (cf.seconds_in_a_day - time2)
else:
duration = time1 - time2
if timehalf < time2:
halfduration = timehalf + (cf.seconds_in_a_day - time2)
else:
halfduration = timehalf - time2
if duration == 0.0:
activitychange = 0.0
else:
activitychange = float(halfduration) / float(duration)
# Attribute 4: time since last sensor event
time2 = cf.windata[cf.max_window - 2][1]
if time1 < time2:
duration = time1 + (cf.seconds_in_a_day - time2)
else:
duration = time1 - time2
tempdata[4] = duration
# Attribute 5..6: dominant sensors from previous windows
tempdata[5] = prevwin1
tempdata[6] = prevwin2
# Attribute 7: last sensor id in window
tempdata[7] = find_sensor(sensorid1)
# Attribute 8: last location in window
tempdata[8] = lastlocation
# Attribute 9: last motion location in window
tempdata[9] = lastmotionlocation
# Attribute 10: complexity (entropy of sensor counts)
tempdata[10] = complexity
# Attribute 11: activity change (activity change between window halves)
tempdata[11] = activitychange
# Attribute 12: number of transitions between areas in window
tempdata[12] = numtransitions
# Attribute 13: number of distinct sensors in window
# tempdata[13] = numdistinctsensors
tempdata[13] = 0
return tempdata
def compute_feature(dt, sensorid1, sensorid2):
""" Compute the feature vector for each window-size sequence of sensor events.
The features (listed by index) are:
0: time of the last sensor event in window (hour)
1: time of the last sensor event in window (seconds)
2: day of the week for the last sensor event in window
3: window size in time duration
4: time since last sensor event
5: dominant sensor for previous window
6: dominant sensor two windows back
7: last sensor event in window
8: last sensor location in window
9: last motion sensor location in window
10: complexity of window (entropy calculated from sensor counts)
11: change in activity level between two halves of window
12: number of transitions between areas in window
13: number of distinct sensors in window
14 - num_sensors+13: counts for each sensor
num_sensors+14 - 2*num_sensors+13: time since sensor last fired (<= SECSINDAY)
"""
lastlocation = -1
lastmotionlocation = -1
prevwin1 = prevwin2 = complexity = maxcount = 0
numtransitions = numdistinctsensors = 0
cf.windata[cf.wincnt][0] = find_sensor(sensorid1)
cf.windata[cf.wincnt][1] = cf.current_seconds_of_day
cf.windata[cf.wincnt][2] = cf.day_of_week
if cf.wincnt < (cf.max_window - 1): # not reached end of window
cf.wincnt += 1
return False, None
else: # reached end of window
wsize = cf.max_window
scount = np.zeros(cf.num_sensors, dtype=np.int)
# Determine the dominant sensor for this window
# count the number of transitions between areas in this window
for i in range(cf.max_window - 1, cf.max_window - (wsize + 1), -1):
scount[cf.windata[i][0]] += 1
id = cf.windata[i][0]
if lastlocation == -1:
lastlocation = id
if (lastmotionlocation == -1) and (cf.dstype[id] == 'm'):
lastmotionlocation = id
if i < cf.max_window - 1: # check for transition
id2 = cf.windata[i + 1][0]
if id != id2:
if (cf.dstype[id] == 'm') and (cf.dstype[id2] == 'm'):
numtransitions += 1
for i in range(cf.num_sensors):
if scount[i] > 1:
ent = float(scount[i]) / float(wsize)
ent *= np.log2(ent)
complexity -= float(ent)
numdistinctsensors += 1
if np.mod(cf.numwin, cf.max_window) == 0:
prevwin2 = prevwin1
prevwin1 = cf.dominant
cf.dominant = 0
for i in range(cf.num_sensors):
if scount[i] > maxcount:
maxcount = scount[i]
cf.dominant = i
tempdata = assign_features(wsize, prevwin1, prevwin2, sensorid1,
lastlocation, lastmotionlocation, complexity, numtransitions,
numdistinctsensors)
# Attributes num_set_features..(num_sensors+(num_set_features-1))
weight = 1
for i in range(cf.max_window - 1, cf.max_window - (wsize + 1), -1):
tempdata[cf.windata[i][0] + cf.num_set_features] += 1 * weight
weight += cf.weightinc
# Attributes num_sensors+num_set_features..
# (2*num_sensors+(num_set_features-1)) time since sensor fired
for i in range(cf.num_sensors):
difftime = cf.current_timestamp - cf.sensortimes[i]
# There is a large gap in time or shift backward in time
if difftime.total_seconds() < 0 or (difftime.days > 0):
tempdata[cf.num_set_features + cf.num_sensors + i] = cf.seconds_in_a_day
else:
tempdata[cf.num_set_features + cf.num_sensors + i] = difftime.total_seconds()
for i in range(cf.max_window - 1):
cf.windata[i][0] = cf.windata[i + 1][0]
cf.windata[i][1] = cf.windata[i + 1][1]
cf.windata[i][2] = cf.windata[i + 1][2]
cf.numwin += 1
if cf.no_overlap and cf.mode != "ANNOTATE":
cf.wincnt = 0
return True, tempdata
def save_params():
""" Save parameters to a file that will accompany a learned and saved model.
"""
modelfilename = os.path.join(cf.model_path, cf.model_name + '.config')
modelfile = open(modelfilename, "w")
modelfile.write('python al.py --sensors \"[')
for i in range(cf.num_sensors): # sensor names
modelfile.write(cf.sensornames[i])
if i < (cf.num_sensors - 1):
modelfile.write(",")
modelfile.write(']\" ')
modelfile.write('--activities \"[')
for i in range(cf.num_activities): # activity names
modelfile.write(cf.activitynames[i])
if i < (cf.num_activities - 1):
modelfile.write(",")
modelfile.write(']\" ')
modelfile.write("--mode TEST ")
if cf.cluster_other:
modelfile.write("--clusterother ")
elif cf.ignore_other:
modelfile.write("ignoreother ")
if cf.model_name != "model":
modelfile.write('model ' + cf.model_name)
modelfile.write("<datafilename>\n")
modelfile.close()
modelfilename = os.path.join(cf.model_path, cf.model_name + '.pkl.gz')
with gzip.GzipFile(modelfilename, 'wb') as f:
joblib.dump(cf.clf, f) # save model to file
def _predict(model, X):
if isinstance(model, nn.Module):
with torch.no_grad():
X_scaled = cf.scaler.transform(X)
X_tensor = torch.tensor(X_scaled, dtype=torch.float32)
outputs = model(X_tensor)
return torch.argmax(outputs, dim=1).numpy()
return model.predict(X)
def report_results(xtest, ytest):
""" Collect and report predictive accuracy of trained model. """
numright = total = 0
newlabels = _predict(cf.clf, xtest)
any_irregular_found = False
for i, pred in enumerate(newlabels):
act_name = cf.activitynames[pred]
if act_name in ACTIVITY_ALERT:
# Compute confidence
if hasattr(cf.clf, 'predict_proba'):
confidence = cf.clf.predict_proba([xtest[i]])[0][pred]
elif isinstance(cf.clf, nn.Module):
with torch.no_grad():
X_tensor = torch.tensor(cf.scaler.transform([xtest[i]]), dtype=torch.float32)
logits = cf.clf(X_tensor)
probs = torch.softmax(logits, dim=1).numpy()[0]
confidence = probs[pred]
else:
confidence = 1.0
if confidence < cf.irregular_threshold:
who = ACTIVITY_ALERT[act_name]
print(f"[ALERT] Irregular “{act_name}” in sample {i} (conf={confidence:.2f}) – calling {who}!")
any_irregular_found = True
# ✅ Move this outside the loop
if not any_irregular_found:
print("[INFO] No irregular activities detected.")
# Evaluation / Accuracy Metrics
if cf.filter_other:
matrix = np.zeros((cf.num_activities, cf.num_activities), dtype=int)
for i in range(len(ytest)):
if ytest[i] != -1:
matrix[ytest[i]][newlabels[i]] += 1
total += 1
if newlabels[i] == ytest[i]:
numright += 1
else:
matrix = confusion_matrix(ytest, newlabels, labels=cf.activitynames)
for i in range(len(ytest)):
if not cf.cluster_other:
if newlabels[i] == ytest[i]:
numright += 1
else:
if newlabels[i] > (cf.num_activities - (cf.num_clusters + 1)):
if ytest[i] == -1 or ytest[i] > (cf.num_activities - (cf.num_clusters + 1)):
numright += 1
elif newlabels[i] == ytest[i]:
numright += 1
total = len(ytest)
print('activities', cf.activitynames)
print('matrix\n', matrix)
print('numright', numright, 'total', total)
print(classification_report(ytest, newlabels))
accuracy = float(numright) / float(total)
return accuracy
def leave_one_out(files):
""" Perform leave-one-subject-out testing. Assume each subject is represented
by a specified file.
"""
results = []
for datafilename in files:
print(datafilename)
cf.data_filename = datafilename
read_data()
xtest = cf.data
ytest = cf.labels
k = len(cf.data[0])
xtrain = np.empty((0, k), dtype=float)
ytrain = np.empty((0), dtype=int)
for otherfilename in files:
if otherfilename != datafilename:
cf.data = []
cf.labels = []
cf.data_filename = otherfilename
read_data()
xtrain = np.append(xtrain, cf.data, axis=0)
ytrain = np.append(ytrain, cf.labels)
_fit_model(xtrain, ytrain)
results.append(report_results(xtest, ytest))
print('results', results)
def train_model():
results = []
algo = cf.algorithm
if cf.add_pca:
pca = PCA(n_components=50)
pca_data = pca.fit_transform(cf.data)
cf.data = np.append(cf.data, pca_data, axis=1)
if cf.mode == "TRAIN":
if algo == 'rf':
_fit_model(cf.data, cf.labels)
elif algo in ('adaline', 'linreg'):
Model = Adaline if algo == 'adaline' else LinReg
model = Model(in_features=len(cf.data[0]))
model, scaler = _train_gradient(model, cf.data, cf.labels,
epochs=cf.epochs,
lr=0.01,
log=cf.logepochs)
cf.clf = model # fix here
else:
raise ValueError(f'Unknown algorithm {algo}')
elif cf.mode == "CV":
for i in range(3):
xtrain, xtest, ytrain, ytest = train_test_split(cf.data,
cf.labels,
test_size=0.33,
random_state=i)
_fit_model(xtrain, ytrain)
results.append(report_results(xtest, ytest))
print('results', results)
elif cf.mode == "PARTITION":
dlength = len(cf.data)
splitpoint = int((2 * dlength) / 3)
ttrain = cf.timestamps[:splitpoint]
ttest = cf.timestamps[splitpoint:]
xtrain = cf.data[:splitpoint]
ytrain = cf.labels[:splitpoint]
xtest = cf.data[splitpoint:]
ytest = cf.labels[splitpoint:]
_fit_model(xtrain, ytrain)
print("results", report_results(xtest, ytest))
plot_irregular_events(xtest, ttest, cf.clf, cf.irregular_threshold)
# call plotting function
def plot_irregular_events(xtest, ttest, clf, threshold):
irreg_times = []
for x, dt in zip(xtest, ttest):
pred = _predict(clf, [x])[0]
act_name = cf.activitynames[pred]
if act_name in ACTIVITY_ALERT:
if hasattr(clf, 'predict_proba'):
conf = clf.predict_proba([x])[0][pred]
elif isinstance(clf, nn.Module):
with torch.no_grad():
x_tensor = torch.tensor(cf.scaler.transform([x]), dtype=torch.float32)
logits = clf(x_tensor)
probs = torch.softmax(logits, dim=1).numpy()[0]
conf = probs[pred]
else:
conf = 1.0
if conf < threshold:
irreg_times.append(dt)
if not irreg_times:
print("[PLOT] No irregular events detected.")
return
hours = [t.hour + t.minute / 60.0 for t in irreg_times]
weekdays = [t.weekday() for t in irreg_times]
plt.figure(figsize=(10, 6))
scatter = plt.scatter(hours, weekdays, c=weekdays, cmap='tab10')
plt.colorbar(scatter, ticks=range(7), label='Weekday')
plt.xticks(range(0, 25, 2))
plt.yticks(range(7), ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
plt.xlabel("Hour of Day")
plt.ylabel("Day of Week")
plt.title("Irregular Alert Times")
plt.grid(True)
plt.tight_layout()
plt.show()
def test_model():
read_data()
modelfilename = os.path.join(cf.model_path, cf.model_name + '.pkl.gz')
with open(modelfilename, 'rb') as f:
cf.clf = joblib.load(f)
print('accuracy ', report_results(cf.data, cf.labels))
def annotate_data(filename):
""" Add activity labels to an input file containing sensor readings.
"""
datafile = open(filename, "r")
date = ""
stime = ""
modelfilename = os.path.join(cf.model_path, cf.model_name + '.pkl.gz')
with open(modelfilename, 'rb') as f:
cf.clf = joblib.load(f)
outputfilename = "./data.al"
outputfile = open(outputfilename, "w")
cf.current_timestamp = get_datetime("2001-01-01", "00:00:00.00000")
first_event = True
fulldata = []
for line in datafile:
words = line.split() # Split line into words on delimiter " "
date = words[0]
stime = words[1]
sensorid = words[2]
newsensorid = words[3]
sensorstatus = words[4]
dt = get_datetime(date, stime)
cf.current_seconds_of_day = compute_seconds(dt)
cf.day_of_week = dt.weekday()
previousTimestamp = cf.current_timestamp
cf.current_timestamp = dt
snum1 = find_sensor(sensorid)
timediff = cf.current_timestamp - previousTimestamp
if first_event == True or timediff.days < 0 or timediff.days > 1:
for i in range(cf.num_sensors):
cf.sensortimes[i] = cf.current_timestamp - timedelta(days=1)
first_event = False
if sensorstatus == "ON" and cf.dstype[snum1] == 'n':
cf.dstype[snum1] = 'm'
cf.sensortimes[snum1] = cf.current_timestamp # last time sensor fired
end, tempdata = compute_feature(dt, sensorid, newsensorid)
if end: # End of window reached, add feature vector
fulldata.append(tempdata)
predict_alabel = _predict(cf.clf, fulldata)
datafile.close()
datafile = open(filename, "r")
linenum = 0
for line in datafile:
words = line.split() # Split line into words on delimiter " "
date = words[0]
stime = words[1]
sensorid = words[2]
newsensorid = words[3]
sensorstatus = words[4]
if linenum < cf.max_window:
aname = "Other_Activity"
else:
aname = cf.activitynames[predict_alabel[linenum - cf.max_window]]
if aname.startswith("cluster_"):
aname = "Other_Activity"
outstr = date + " " + stime + " " + sensorid + " " + newsensorid + " "
outstr += sensorstatus + " " + aname + "\n"
outputfile.write(outstr)
linenum += 1
outputfile.close()
def main(args):
files = cf.set_parameters()
cf.num_features = cf.num_set_features + (2 * cf.num_sensors)
if cf.mode == "TEST":
test_model()
elif cf.mode == "ANNOTATE":
annotate_data(cf.data_filename)
else: # TRAIN, CV, WRITE
if not os.path.exists(cf.model_path): # create directory to store model file
os.makedirs(cf.model_path)
if cf.mode == "LOO":
leave_one_out(files)
else:
read_data()
print("finished reading data")
train_model()
if cf.mode == "TRAIN":
save_params()
if __name__ == "__main__":
main(sys.argv)