-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.py
238 lines (200 loc) · 9.03 KB
/
Common.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
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
from collections import namedtuple
import operator
from numpy import std, mean
from pymongo import MongoClient, ASCENDING
from pyeeg import ap_entropy
class Constants:
Database = "Database"
RecordNumber = "RecordNumber"
AverageHeartbeat = "AverageHeartbeat"
IrregularBeatsPercent = "IrregularBeatsPercent"
AverageBeatChange = "AverageBeatChange"
Irregularity = "Irregularity"
QS = "QS"
QtoR = "QtoR"
StoR = "StoR"
QSTD = "QSTD"
RSTD = "RSTD"
SSTD = "SSTD"
ID = "ID"
Gender = "Gender"
Smoking = "Smoking"
familyHistory = "familyHistory"
Sport = "Sport"
Age = "Age"
SystolicBP = "SystolicBP"
DiastolicBP = "DiastolicBP"
BMI = "BMI"
Diagnosis = "Diagnosis"
Record = "Record"
PreviousResultsDb = 'PreviousResults'
FeatureVectors = 'FeatureVectors'
People = 'People'
TrainingSetDbName = 'TrainingSet'
TrainingSetCollectionName = 'TrainingSetCollection'
LocalHost = "localhost"
PersonToRecordCollection = "PersonToRecord"
Time = "Time"
Value = "Value"
Label = "Label"
LabelNone = "None"
LabelR = "R"
LabelQ = "Q"
LabelS = "S"
Left = "Left"
Right = "Right"
MinPoint = "MinPoint"
MaxPoint = "MaxPoint"
MongoPort = 27017
FeatureVector = namedtuple("FeatureVector", " ".join((Constants.Database, Constants.RecordNumber, Constants.AverageHeartbeat, Constants.IrregularBeatsPercent, Constants.AverageBeatChange, Constants.Irregularity, Constants.QS, Constants.QtoR, Constants.StoR, Constants.QSTD, Constants.RSTD, Constants.SSTD)))
Person = namedtuple("Person", " ".join((Constants.ID, Constants.Gender, Constants.Smoking, Constants.familyHistory, Constants.Sport, Constants.Age, Constants.SystolicBP, Constants.DiastolicBP, Constants.BMI, Constants.Diagnosis)))
SamplingRates = {'nsrdb': 128, 'mitdb': 360, 'afdb': 250, 'svdb': 128, 'cudb': 250}
defaultFactor = [100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
offspringsInGeneration = 10
defaultMutationFactor = 0.1
meanVector = [84.09906486418608, 0.26678132286443434, 0.20721282754655673, 0.970974320025125, 91.95639357742009, 0.3777119473541706, 2.6660855423453014, 285.2128648853569, 328.56882100633914, 520.6623932902744]
def VectorizeFeatureVector(featureVector):
return [
featureVector[Constants.AverageHeartbeat],
featureVector[Constants.IrregularBeatsPercent],
featureVector[Constants.AverageBeatChange],
featureVector[Constants.Irregularity],
featureVector[Constants.QS],
featureVector[Constants.QtoR],
featureVector[Constants.StoR],
featureVector[Constants.QSTD],
featureVector[Constants.RSTD],
featureVector[Constants.SSTD]
]
def FactorizeVectors(vectorized, factorVector):
result = [[(vector[i] - meanVector[i]) * factorVector[i] for i in range(len(vector))] for vector in vectorized]
return result
def GetTrainingSet():
client = MongoClient(Constants.LocalHost, Constants.MongoPort)
featureVectorsDb = client.get_database(Constants.FeatureVectors)
X = list()
for singleCollection in SamplingRates.keys():
collection = featureVectorsDb.get_collection(singleCollection)
limitSize = 15
if singleCollection == 'nsrdb':
limitSize = 10
for item in collection.find().limit(limitSize):
X.append(item)
return X
def GetTestSet():
client = MongoClient(Constants.LocalHost, Constants.MongoPort)
featureVectorsDb = client.get_database(Constants.FeatureVectors)
X = list()
for singleCollection in SamplingRates.keys():
collection = featureVectorsDb.get_collection(singleCollection)
skipSize = 15
if singleCollection == 'nsrdb':
skipSize = 10
for item in collection.find().skip(skipSize):
X.append(item)
return X
def PutTrainingSet(trainingSet):
client = MongoClient(Constants.LocalHost, Constants.MongoPort)
trainingSetDb = client.get_database(Constants.TrainingSetDbName)
trainingSetDb.drop_collection(Constants.TrainingSetCollectionName)
trainingSetCollection = trainingSetDb.get_collection(Constants.TrainingSetCollectionName)
trainingSetCollection.insert_many(trainingSet)
print(trainingSet)
def GetPeopleTrainingSet():
client = MongoClient(Constants.LocalHost, Constants.MongoPort)
trainingSetDb = client.get_database(Constants.TrainingSetDbName)
trainingSetCollection = trainingSetDb.get_collection(Constants.TrainingSetCollectionName)
peopleCollection = client.get_database(Constants.People).get_collection(Constants.People)
personToRecordCollection = client.get_database(Constants.People).get_collection(Constants.PersonToRecordCollection)
result = list()
for featureVector in trainingSetCollection.find():
database = featureVector[Constants.Database]
record = featureVector[Constants.RecordNumber]
id = personToRecordCollection.find_one({Constants.Record: record, Constants.Database: database})[Constants.ID]
person = peopleCollection.find_one({Constants.ID: id})
result.append(person)
return result
def PutPeopleTrainingSet(trainingSet):
client = MongoClient(Constants.LocalHost, Constants.MongoPort)
trainingSetDb = client.get_database(Constants.TrainingSetDbName)
trainingSetDb.drop_collection(Constants.People)
trainingSetCollection = trainingSetDb.get_collection(Constants.People)
trainingSetCollection.insert_many(trainingSet)
# print(trainingSet)
def CreateFeatureVector(collection, dbName, takeFirstMinutes):
limitSamples = SamplingRates[dbName] * 60 * takeFirstMinutes
lastBeat = -1
lastQ = -1
amplitudeSum = {Constants.LabelQ: 0, Constants.LabelR: 0, Constants.LabelS: 0}
labelsCounters = {Constants.LabelQ: 0, Constants.LabelR: 0, Constants.LabelS: 0}
amplitudesLists = {Constants.LabelQ: list(), Constants.LabelR: list(), Constants.LabelS: list()}
heartbeats = list()
valuesHistogram = dict()
sumQS = 0
countQS = 0
for entry in collection.find().sort(Constants.Time, ASCENDING).limit(limitSamples):
label = entry[Constants.Label]
time = entry[Constants.Time]
value = entry[Constants.Value]
if label == Constants.LabelNone:
continue
amplitudeSum[label] += value
labelsCounters[label] += 1
amplitudesLists[label].append(value)
if value in valuesHistogram:
valuesHistogram[value] += 1
else:
valuesHistogram[value] = 1
if label == Constants.LabelR:
if lastBeat > 0:
heartbeats.append(time - lastBeat)
lastBeat = time
elif label == Constants.LabelQ:
lastQ = time
elif label == Constants.LabelS:
if lastQ > 0:
sumQS += time - lastQ
countQS += 1
lastQ = -1
averageQAmplitude = amplitudeSum[Constants.LabelQ] / float(labelsCounters[Constants.LabelQ])
averageRAmplitude = amplitudeSum[Constants.LabelR] / float(labelsCounters[Constants.LabelR])
averageSAmplitude = amplitudeSum[Constants.LabelS] / float(labelsCounters[Constants.LabelS])
# Calculate the baseline by the most common value
baseline = max(valuesHistogram.iteritems(), key=operator.itemgetter(1))[0]
# Convert heartbeats length from a number of samples to actual time
normalizedHeartbeats = [ float(i) / SamplingRates[dbName] for i in heartbeats ]
heartbeatSTD = std(normalizedHeartbeats)
beatChanges = [abs(float(x) - normalizedHeartbeats[i-1])/normalizedHeartbeats[i-1] for i, x in enumerate(normalizedHeartbeats)][1:]
beatChangesCount = len([change for change in beatChanges if float(change) >= 0.1])
totalBeatsCount = len(beatChanges)
result = FeatureVector(
Database = dbName,
RecordNumber = collection.name,
AverageHeartbeat = 60 / mean(normalizedHeartbeats),
IrregularBeatsPercent = float(beatChangesCount) / totalBeatsCount,
AverageBeatChange = mean(beatChanges),
Irregularity = ap_entropy(normalizedHeartbeats, 2, heartbeatSTD*0.2),
QS = (sumQS / float(countQS)) * 1000 / SamplingRates[dbName],
QtoR = abs(averageQAmplitude - baseline) / abs(averageRAmplitude - baseline),
StoR = abs(averageSAmplitude - baseline) / abs(averageRAmplitude - baseline),
QSTD = std(amplitudesLists[Constants.LabelQ]),
RSTD = std(amplitudesLists[Constants.LabelR]),
SSTD = std(amplitudesLists[Constants.LabelS])
)
return result
def GetAverageValues():
client = MongoClient(Constants.LocalHost, Constants.MongoPort)
db = client.get_database(Constants.FeatureVectors)
sumVector = [0] * 10
total = 0
for singleCollection in SamplingRates.keys():
collection = db.get_collection(singleCollection)
for item in collection.find():
vector = VectorizeFeatureVector(item)
total += 1
for i in range(10):
sumVector[i] += vector[i]
print('sums: ' + sumVector.__str__())
for i in range(10):
sumVector[i] /= float(total)
return sumVector