-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayesian.js
More file actions
75 lines (60 loc) · 2.44 KB
/
bayesian.js
File metadata and controls
75 lines (60 loc) · 2.44 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
// var queries = require(__dirname+'/queries');
// var q = require('q');
// var database = require(__dirname+'/db_connection')
// var getdata_controller = require(__dirname+'/getData');
var db = require('../../controllers/DatabaseConnection')
var Disease = db.Disease;
var Symptom = db.Symptom;
// Input symptoms by name: ['Pain','Fever']
// Returns: array of score/disease tuple: [{disease: Disease, score: float}]
//TODO: Not case sensitive
async function likelihoodCalculator(input_symptoms) {
//The results array we return later
var results = []
var parent_symptoms = await db.getParentSymptoms(input_symptoms);
// number of input symptoms does not includes superclasses
let number_of_input_symptoms = parseFloat(input_symptoms.length)
var parent_symptoms = await db.getParentSymptoms(input_symptoms);
var parent_symptoms_names = parent_symptoms.map(sym => sym.symptom_name);
for (superclass of parent_symptoms_names) {input_symptoms.push(superclass)};
//We put this in a variable for later use.
//Pull every disease into memory. TODO: Make request a bit finer.
var diseases = await Disease.findAll({include: Symptom})
//We iterate through each disease and calculate their score.
for (var disease of diseases){
// Initial some computational variables
var frequency = 0.0;
var matches = 0.0;
var importance = 1.0; //We don't actually use this at the moment.
var frequency_sum = 0.0;
//Example Symptom object (for easy reference)
// {
// id: 'HP:0000003',
// symptom_name: 'Multicystic kidney dysplasia',
// definition: 'Multicystic dysplasi...',
// Correlation: { disease_orpha: '564', symptom_id: 'HP:0000003', frequency: 0.895 }
// }
for (var symptom of disease.Symptoms) {
frequency_sum += parseFloat(symptom.Correlation.frequency);
// If one of the input symptoms matches this symptom
if (input_symptoms.includes(symptom.symptom_name)) {
matches += 1;
frequency += parseFloat(symptom.Correlation.frequency);
}
//There is no match, so pull parent symptoms and see if they might match.
}
//else if: deal with subclasses
// If there was at least oen symptom match
if (matches != 0) {
let likelihood = (frequency / frequency_sum) * (matches/number_of_input_symptoms);
results.push({
disease: disease,
score: likelihood
})
}
}
return results;
}
module.exports = {
likelihoodCalculator
}