forked from hcilabusf/language_learning_app_hci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredictionHolder.py
146 lines (122 loc) · 5.86 KB
/
PredictionHolder.py
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
import time
""" A class that holds all of the predictions that are collected from the Matlab PC """
predictionsList = []
startTime = -1;
currentTrial = "";
"""
A function that adds a new prediction
"""
def addPred (pred):
pred.time = int(round(time.time() * 1000)) # Get current time in milliseconds and assign it to pred.time
if currentTrial == "":
pred.trueClass = currentTrial;
predictionsList.append(pred);
"""
A function that returns prediction list
"""
def getPredList():
return predictionsList
def printPredList():
if len(predictionsList) > 0:
print('[')
for pred in predictionsList:
print('{} {} {}, '.format(pred.pred, pred.predClassOne, pred.predClassTwo))
print(']\n')
"""
A function that returns average prediciton for low cognitive workload for a specific window size (By Points).
It starts from the last index till the first one and counts 20 elements from last index
if the list doesn't containt 20 elements then the function returns -1
"""
def getAvgPredLowByPoints(windowSize):
global predictionsList
avgPred = 0.0
if windowSize > len(predictionsList): # if predictionlist size less than window size return -1
return -1.0
# Otherwise, return the average of the moving range size
else:
stopIndex = len(predictionsList) - windowSize # start from last index and subtract window size to get stop index
sum = 0
index = len(predictionsList) -1 # get last index in the list
while index > stopIndex: # the loop starts from last to first index
pred = predictionsList[index]
sum += pred.predClassTwo
index -= 1
avgPred = sum/windowSize
return avgPred
"""
A function that returns average prediciton for high cognitive workload for a specific window size (By Points).
It starts from the last index till the first one and counts 20 elements from last index
if the list doesn't containt 20 elements then the function returns -1
"""
def getAvgPredHighByPoints(windowSize):
global predictionsList
avgPred = 0.0
if windowSize > len(predictionsList): # if predictionlist size less than window size return -1
return -1.0
# Otherwise, return the average of the moving range size
else:
stopIndex = len(predictionsList) - windowSize # start from last index and subtract window size to get stop index
sum = 0
index = len(predictionsList) -1 # get last index in the list
while index > stopIndex: # the loop starts from last to first index
pred = predictionsList[index]
sum += pred.predClassOne
index -= 1
avgPred = sum/windowSize
return avgPred
"""
A function that returns average prediciton for low cognitive workload for a specific time in seconds.
It first gets the current time in milliseconds then it starts from the last index to the first.
It gets the prediction from the list at each index and gets its time, then it calculates the time difference between
the current time and the precition time (when it was added). If the time diff is less than time window, then the prediction was added between time window.
Continue checking till a time difference greater than time window is found so the loop breaks because it means that this element is older than
time window and all previous elements will also be older.
Example: if element was at index "X" then from 0 to X, all the elements are older because predictions are appended so as index increases,
new elements are added.
"""
def getAvgPredLowByTime(timeWindow):
global predictionsList
avgPred = 0.0
currentTime = int(round(time.time() * 1000)) # Get current time in milliseconds
count = 1 # counts number of pred calculated in loop because we don't know how many pred would be used in the loop
stopIndex = 0
sum = 0
index = len(predictionsList) -1
while index > stopIndex:
pred = predictionsList[index]
time_diff = (currentTime - pred.time)/1000 # get the difference then convert it to seconds
if time_diff > timeWindow: # if the difference between the prediction's added time and current time is greater than timeWindow
break
sum += pred.predClassTwo
index -= 1
count += 1
avgPred = sum/count
return avgPred
"""
A function that returns average prediciton for high cognitive workload for a specific time in seconds.
It first gets the current time in milliseconds then it starts from the last index to the first.
It gets the prediction from the list at each index and gets its time, then it calculates the time difference between
the current time and the precition time (when it was added). If the time diff is less than time window, then the prediction was added between time window.
Continue checking till a time difference greater than time window is found so the loop breaks because it means that this element is older than
time window and all previous elements will also be older.
Example: if element was at index "X" then from 0 to X, all the elements are older because predictions are appended so as index increases,
new elements are added.
"""
def getAvgPredHighByTime(timeWindow):
global predictionsList
avgPred = 0.0
currentTime = int(round(time.time() * 1000)) # Get current time in milliseconds
count = 1 # counts number of pred calculated in loop because we don't know how many pred would be used in the loop
stopIndex = 0
sum = 0
index = len(predictionsList) -1
while index > stopIndex:
pred = predictionsList[index]
time_diff = (currentTime - pred.time)/1000 # get the difference then convert it to seconds
if time_diff > timeWindow: # if the difference between the prediction's added time and current time is greater than timeWindow
break
sum += pred.predClassOne
index -= 1
count += 1
avgPred = sum/count
return avgPred