-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.py
283 lines (239 loc) · 11.3 KB
/
Program.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
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
import colorama
import pymongo
import DumbDiagnoser
import Common
from sklearn.neighbors import LSHForest
from Common import Constants, CreateFeatureVector
featureVectorFactor = [10.45935922250565026, 13211.087958630917, 2551.4811742410716, 921.5007903683774, 398.02065999138813, 112.54104110334723, 133.8470938825284, 650.0264814360967, 162.64549063052917, 160.09765706586313]
peopleFactor = [100, 1, 100, 10, 10, 10, 10, 100]
loadPreviousResults = False
ChartsNeighbors = 5
ChartsCandidates = 10
ChartsEstimators = 10
PeopleNeighbors = 3
PeopleCandidates = 10
PeopleEstimators = 10
client = pymongo.MongoClient(Constants.LocalHost, Constants.MongoPort)
TrainingSetSize = 70
def Main():
trainingSet, people = LoadTrainingSet()
# Uncomment when running from console:
# colorama.init()
if loadPreviousResults:
previouslyLearnedVectors, previouslyLearnedPeople = LoadPreviouslyLearnedResults()
trainingSet.extend(previouslyLearnedVectors)
people.extend(previouslyLearnedPeople)
else:
client.drop_database(Constants.PreviousResultsDb)
chartsForest = LSHForest(n_neighbors = ChartsNeighbors, n_estimators = ChartsEstimators, n_candidates = ChartsCandidates)
chartsForest.fit(trainingSet)
peopleForest = LSHForest(n_neighbors = PeopleNeighbors, n_estimators = PeopleEstimators, n_candidates = PeopleCandidates)
peopleForest.fit(people)
while True:
try:
featureVector, person = GetNewInput()
ShowCurrentPatient(person)
warnings = DumbDiagnoser.GetDumbDiagnosis(featureVector, person)
diagnosis, closestChartsPeople = Diagnose(chartsForest, featureVector)
closestPeople = GetClosestPeople(peopleForest, person)
ShowWarnings(warnings)
ShowResults(diagnosis, closestChartsPeople, closestPeople)
Learn(chartsForest, featureVector, peopleForest, person, diagnosis)
except EOFError:
print('Exiting')
client.close()
break
except NoSuchRecordException as details:
print(details)
finally:
print
def ShowWarnings(warnings):
print(colorama.Fore.YELLOW + colorama.Style.BRIGHT + 'Warnings ' + colorama.Style.RESET_ALL + '(based on raw personal information and ECG chart only):')
for warning in warnings:
print('\t' + warning)
def ShowCurrentPatient(person):
print('You are:')
print(PersonToDescription(person, False))
def ShowResults(diagnosis, closestChartsPeople, closestPeople):
ShowClosestChartsPeople(closestChartsPeople)
print
print(colorama.Fore.RED + colorama.Style.BRIGHT + 'Your suggested diagnosis' + colorama.Style.RESET_ALL + ': ' + colorama.Style.BRIGHT + diagnosis + colorama.Style.RESET_ALL)
print
ShowClosestPeople(closestPeople)
def ShowClosestPeople(closestPeople):
print('People who have the closest personal information are: ')
for person in closestPeople:
result = PersonToDescription(person, True)
print(result)
def ShowClosestChartsPeople(closestChartsPeople):
print('People who have the closest charts are: ')
for person in closestChartsPeople:
result = PersonToDescription(person, True)
print(result)
def Learn(chartsForest, featureVector, peopleForest, person, diagnosis):
print('Learning you and your chart...')
person[Constants.Diagnosis] = [diagnosis]
vectorizedChartToAdd = Common.VectorizeFeatureVector(featureVector._asdict())
factorizedChartToAdd = Common.FactorizeVectors([vectorizedChartToAdd], featureVectorFactor)
chartsForest.partial_fit(factorizedChartToAdd)
vectorizedPersonToAdd = VectorizePerson(person)
factorizedPersonToAdd = Common.FactorizeVectors([vectorizedPersonToAdd], peopleFactor)
peopleForest.partial_fit(factorizedPersonToAdd)
previousResultsDb = client.get_database(Constants.PreviousResultsDb)
vectorsCollection = previousResultsDb.get_collection(Constants.FeatureVectors)
peopleCollection = previousResultsDb.get_collection(Constants.People)
vectorsCollection.insert_one(featureVector._asdict())
# Allow adding a record and a person that are in the already learned collection
# by deleting the "_id" key, which will be created automatically upon re-insertion
idFieldName = '_id'
if idFieldName in person:
del person[idFieldName]
peopleCollection.insert_one(person)
def PersonToDescription(person, shouldPrintDiagnosis):
bmi = person[Constants.BMI]
sysBP = person[Constants.SystolicBP]
diBP = person[Constants.DiastolicBP]
if person[Constants.Gender] == 1:
gender = 'woman'
else:
gender = 'man'
if (bmi < 18.5):
weightStatus = 'underweight'
elif (25 < bmi <= 30):
weightStatus = 'overweight'
elif (bmi > 30):
weightStatus = 'obese'
else:
weightStatus = 'normal weight'
result = 'A ' + str(person['Age']) + ' years old ' + weightStatus + ' ' + gender
if (person[Constants.Smoking] == 1):
result += ', smoking'
else:
result += ', non-smoker'
result += ', physical activity score: ' + str(person['Sport'])
if (sysBP < 90 & diBP < 60):
bpStatus = 'low blood pressure'
elif ((120 < sysBP < 140) & (diBP < 90)) | ((80 < diBP < 90) & (sysBP < 140)):
bpStatus = 'pre-high blood pressure'
elif (sysBP > 140 | diBP > 90):
bpStatus = 'high blood pressure'
else:
bpStatus = 'normal blood pressure'
result += ', ' + bpStatus
if (person[Constants.familyHistory] == 1):
history = ', with '
else:
history = ', no '
result += history + 'family history of cardiovascular diseases'
if (shouldPrintDiagnosis == True):
result += '.\n' + 'Diagnosis' + ': ' + colorama.Style.BRIGHT + (', ').join(person['Diagnosis']) + colorama.Style.RESET_ALL
return result
def GetNewInput():
print('Enter a new record to diagnose.')
db, record = raw_input('Enter DB and record: ').split()
person = GetPersonFromRecord(db, record)
featureVector = CreateFeatureVectorFromDatabase(record, db)
return featureVector, person
def CreateFeatureVectorFromDatabase(record, dbName):
db = client.get_database(dbName)
collection = db.get_collection(record)
return CreateFeatureVector(collection, dbName, 30)
def Diagnose(chartsForest, featureVector):
factorized = Common.FactorizeVectors([Common.VectorizeFeatureVector(featureVector._asdict())], featureVectorFactor)
distances, indices = chartsForest.kneighbors(factorized, n_neighbors=ChartsNeighbors)
closestChartsPeople = GetPeopleByIndices(indices[0])
predicted = GetPrediction(distances[0], closestChartsPeople)
return predicted, closestChartsPeople
def GetPeopleByIndices(indices):
people = list()
for index in indices:
if index < TrainingSetSize:
# The person is in the original training set
cursor = client.get_database(Constants.TrainingSetDbName).get_collection(Constants.People).find()
else:
# The person is in the previously learned set
index = index - TrainingSetSize
cursor = client.get_database(Constants.PreviousResultsDb).get_collection(Constants.People).find()
if (index > 0):
cursor = cursor.skip(int(index))
person = cursor.next()
people.append(person)
return people
def GetPrediction(distances, closestChartsPeople):
# Suggest a diagnosis for a patient, based on his neighbors and their distances.
# The most common diagnosis wins. If there are more than one with the same appearances among the neighbors,
# the diagnosis which its sum of distances is the lowest wins
histogram = dict()
sumOfDistances = dict()
for personAndDistance in zip(closestChartsPeople, distances):
for diagnosis in personAndDistance[0][Constants.Diagnosis]:
if histogram.has_key(diagnosis):
histogram[diagnosis] += 1
sumOfDistances[diagnosis] += personAndDistance[1]
else:
histogram[diagnosis] = 1
sumOfDistances[diagnosis] = personAndDistance[1]
best = histogram.keys()[0]
for key in histogram.keys():
if (key != best):
if (histogram[key] > histogram[best]):
best = key
elif (histogram[key] == histogram[best]) & (sumOfDistances[key] < sumOfDistances[best]):
best = key
return best
def GetClosestPeople(peopleForest, person):
factorized = Common.FactorizeVectors([VectorizePerson(person)], peopleFactor)
distances, indices = peopleForest.kneighbors(factorized, n_neighbors=PeopleNeighbors)
closestPeople = GetPeopleByIndices(indices[0])
return closestPeople
def GetPersonFromRecord(db, record):
peopleDb = client.get_database(Constants.People)
peopleCollection = peopleDb.get_collection(Constants.People)
personToRecordCollection = peopleDb.get_collection(Constants.PersonToRecordCollection)
personToRecord = personToRecordCollection.find_one({Constants.Record: record, Constants.Database: db})
if personToRecord is None:
raise NoSuchRecordException('No such record as ' + record + ' in DB: ' + db)
id = personToRecord[Constants.ID]
person = peopleCollection.find_one({Constants.ID: id})
return person
def LoadTrainingSet():
trainingSetDb = client.get_database(Constants.TrainingSetDbName)
trainingSetCollection = trainingSetDb.get_collection(Constants.TrainingSetCollectionName)
peopleTrainingSetCollection = trainingSetDb.get_collection(Constants.People)
vectorizedFeatureVectors = VectorizeTrainingSet(trainingSetCollection.find())
vectorizedPeople = VectorizePeopleTrainingSet(peopleTrainingSetCollection.find())
factorizedFeatureVectors = Common.FactorizeVectors(vectorizedFeatureVectors, featureVectorFactor)
factorizedPeople = Common.FactorizeVectors(vectorizedPeople, peopleFactor)
return factorizedFeatureVectors, factorizedPeople
def VectorizeTrainingSet(trainingSet):
result = [Common.VectorizeFeatureVector(featureVector) for featureVector in trainingSet]
return result
def VectorizePeopleTrainingSet(people):
result = [VectorizePerson(person) for person in people]
return result
def VectorizePerson(person):
return [
person[Constants.Age],
person[Constants.Gender],
person[Constants.Smoking],
person[Constants.familyHistory],
person[Constants.Sport],
person[Constants.SystolicBP],
person[Constants.DiastolicBP],
person[Constants.BMI]
]
def LoadPreviouslyLearnedResults():
previousResultsDb = client.get_database(Constants.PreviousResultsDb)
vectorsCollection = previousResultsDb.get_collection(Constants.FeatureVectors)
peopleCollection = previousResultsDb.get_collection(Constants.People)
vectorizedFeatureVectors = VectorizeTrainingSet(vectorsCollection.find())
vectorizedPeople = VectorizePeopleTrainingSet(peopleCollection.find())
factorizedFeatureVectors = Common.FactorizeVectors(vectorizedFeatureVectors, featureVectorFactor)
factorizedPeople = Common.FactorizeVectors(vectorizedPeople, peopleFactor)
return factorizedFeatureVectors, factorizedPeople
class NoSuchRecordException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
Main()