This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
401 lines (350 loc) · 16 KB
/
index.ts
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
import * as assert from 'assert';
import * as math from 'mathjs';
import { MathType } from 'mathjs';
import {
IBarbados,
IDictOfStringArray,
IInput,
IMultiplicativeRisks,
IRelativeRisk,
IRiskJson,
ITreeMapData
} from './glaucoma-risk-calculator-engine';
const isNullOrUndefined = o => o == null;
const isNumber = n => !isNullOrUndefined(n) && typeof n === 'number';
export const ethnicities_pretty = (ethnicities: IDictOfStringArray | any): IDictOfStringArray =>
ethnicities.map(study => (study_name => `${study_name}: ${study[study_name].join(', ')}`)(Object.keys(study)[0]));
export const s_col_to_s = (s: string): string => s.slice(0, s.indexOf(':'));
export const in_range = (range: string, num: number): boolean => {
if (range === 'all' || range[0] === '_') return false;
const dash = range.indexOf('-');
if (dash !== -1)
return num >= parseInt(range.slice(0, dash - range.length), 10) && num <= parseInt(range.slice(-dash), 10);
let last = range.slice(-2);
if (!isNaN(parseInt(last[0], 10))) last = last[1];
if (isNaN(parseInt(last, 10))) {
const _rest = parseInt(range, 10);
const operators = Object.freeze({
'>': num > _rest,
'+': num >= _rest,
'>=': num >= _rest,
'=>': num >= _rest
});
if (Object.keys(operators).indexOf(last) === -1)
throw TypeError(`Invalid operation of \`${last}\``);
return operators[last];
}
const op = range.slice(0, 2);
const rest = parseInt(range.slice(2), 10);
if (op === '<=' || op === '=<')
return num <= rest;
else if (op[0] === '<')
return num < parseInt(range.slice(1), 10);
else if (op === '>=' || op === '=>')
return num >= rest;
return range === num as any;
};
export const lowest_range = (ranges: string[]): number =>
ranges.reduce((prevNum: number, currentValue: string): number => {
const curNum: number = parseInt(currentValue, 10);
return curNum < prevNum ? curNum : prevNum;
}, 100);
export const uniq = (a: any[]): any[] => {
const seen = {};
return a.filter(item =>
seen.hasOwnProperty(item) ? false : (seen[item] = true)
).filter(k => k !== undefined);
};
/* tslint:disable:array-type */
export const uniq2 = (arr: {}[]): {}[] => {
const keys = arr.length === 0 ? [] : Object.keys(arr[0]);
const seen = new Map();
arr.forEach((a) => {
const key = keys.map(k => a[k]).join('|');
if (!seen.has(key))
seen.set(key, a);
});
return Array.from(seen.values());
};
export const preprocess_studies = (risk_json: IRiskJson): IRiskJson => {
/* Preprocesses studies in risk_json.
*
* Currently:
* - Sorts ages and assigns a lower bound equal to the next lowest bound (if lower bound not present)
*/
Object.keys(risk_json.studies).forEach(study_name => {
if (risk_json.studies[study_name].hasOwnProperty('age')) {
if (!risk_json.studies[study_name].hasOwnProperty('age_map') || !risk_json.studies[study_name].age_map.size)
risk_json.studies[study_name].age_map = new Map<string, number>();
const sr: string[] = sort_ranges(Object.keys(risk_json.studies[study_name].age));
// console.info('sr[0]', sr[0]);
// Lower bound
if (sr[0][0] !== '<') {
// console.info('LOWER BOUND');
const lt = `<${parseInt(sr[0], 10)}`;
risk_json.studies[study_name].age = Object.assign(
{ [lt]: risk_json.studies[study_name].age[sr[0]] },
risk_json.studies[study_name].age
);
risk_json.studies[study_name].age_map.set(lt, risk_json.studies[study_name].age[sr[0]]);
// console.info(`key=${lt}, val=${risk_json.studies[study_name].age[sr[0]]}`);
}
// Upper bound
if (['>', '+'].indexOf(sr[sr.length - 1][0].slice(-1)) === -1) {
// console.info('UPPER BOUND');
const top_bars = sr.map(r => [parseInt(r.indexOf('-') === -1 ? r : r.split('-')[1], 10), r]).filter(
(n: [number, string]) => !isNaN(n[0])).sort();
const top_bar: [number, string] = top_bars[top_bars.length - 1] as [number, string];
// console.info('top_bars =', top_bars, 'top_bar =', top_bar, ';');
if (['>', '+'].indexOf(top_bar[1].slice(-1)) === -1) {
risk_json.studies[study_name].age[`${top_bar}+`] = risk_json.studies[study_name].age[top_bar[1]];
/*console.info(`risk_json.studies[${study_name}].age_map.set('${top_bar}+',
${risk_json.studies[study_name].age[top_bar[1]]})`);
risk_json.studies[study_name].age_map.set(
`${top_bar}+`, risk_json.studies[study_name].age[top_bar[1]]
);*/
}
}
risk_json.studies[study_name].age_map.set(sr[0], risk_json.studies[study_name].age[sr[0]]);
// console.info('map =', risk_json.studies[study_name].age_map, ';');
}
if (risk_json.studies[study_name].hasOwnProperty('agenda')) {
const all_genders_seen: string[] = uniq(risk_json.studies[study_name].agenda.map(
agenda => agenda.gender
));
let gendersAssigned: number = 0;
all_genders_seen.forEach(gender => {
const sr: string[] = sort_ranges(risk_json.studies[study_name].agenda.filter(agenda =>
agenda.gender === gender
).map(agenda => agenda.age));
if (sr.length) {
// Lower bound
const lowest_bar: string = sr.find(
o => ['=<', '<='].indexOf(o.slice(0, 2)) === -1 && ['<', '>'].indexOf(o[0][0]) === -1
);
gendersAssigned++;
const lt = parseInt(lowest_bar, 10);
assert(!isNaN(lt), `${lowest_bar} unexpectedly pareses to NaN`);
risk_json.studies[study_name].agenda.unshift(
Object.assign({},
risk_json.studies[study_name].agenda.filter(
agenda => agenda.age === lowest_bar && agenda.gender === gender)[0],
{ age: `<${lt}` }
)
);
// Upper bound
const top_bars = sr.map(r => [parseInt(r.indexOf('-') === -1 ? r : r.split('-')[1], 10), r]).filter(
(n: [number, string]) => !isNaN(n[0])).sort();
const top_bar = top_bars[top_bars.length - 1];
if (top_bar)
risk_json.studies[study_name].agenda.push(
risk_json.studies[study_name].agenda
.filter(agenda => agenda.age === top_bar[1] && agenda.gender === gender)
.map(o => Object.assign({}, o, { age: `${top_bar[0]}+` }))
[0]
);
}
});
assert.equal(gendersAssigned, all_genders_seen.length, 'Genders assigned != all genders');
risk_json.studies[study_name].agenda = uniq2(risk_json.studies[study_name].agenda);
}
});
return risk_json;
};
export const sort_ranges = (ranges: string[]): string[] =>
ranges.sort((a: string, b: string): number => {
if (a[0] === '<') return -1;
else if (a[0] === '>') return a[0].charCodeAt(0) - b[0].charCodeAt(0);
else if (isNaN(parseInt(a[0], 10)) || b[0] === '<') return 1;
else if (b[0] === '>' || isNaN(parseInt(b[0], 10))) return -1;
return parseInt(a.split('-')[0], 10) - parseInt(b.split('-')[0], 10);
});
const ensure_map = (k): boolean => {
if (k === 'map') return true;
throw TypeError(`Expected map, got ${k}`);
};
export const risk_from_study = (risk_json: IRiskJson, input: IInput): number => {
if (isNullOrUndefined(risk_json)) throw TypeError('`risk_json` must be defined');
else if (isNullOrUndefined(input)) throw TypeError('`input` must be defined');
preprocess_studies(risk_json);
const study: IBarbados = risk_json.studies[input.study] as IBarbados;
const study_vals = study[study.expr[0].key];
const out = Array.isArray(study_vals) ? study_vals
.filter(o => study.expr[0].filter
.every(k =>
k === 'age' ?
in_range(o.age, input.age) :
(input.hasOwnProperty(k) ?
o[k] === input[k] : true)
)
)[study.expr[0].take > 0 ? study.expr[0].take - 1 : 0]
: study_vals[ensure_map(study.expr[0].type) && Object.keys(study_vals).filter(k =>
in_range(k, input[study.expr[0].key])
)[study.expr[0].take - 1]];
if (!out) throw TypeError('Expected out to match something');
return isNumber(out) ? out : out[study.expr[0].extract];
};
export const familial_risks_from_study = (risk_json: IRiskJson, input: IInput, warn: boolean = true): number[] => {
/* tslint:disable:no-unused-expression no-console */
const study = risk_json.studies[input.study];
const res = [];
if (!study.hasOwnProperty('sibling_pc')) {
warn && console.warn(`Using sibling from ${risk_json.default_family_history.from_study}`);
study['sibling_pc'] = risk_json.default_family_history.sibling_pc;
}
input.sibling && res.push(study['sibling_pc']);
if (!study.hasOwnProperty('parents_pc')) {
warn && console.warn(`Using parents_pc from ${risk_json.default_family_history.from_study}`);
study['parents_pc'] = risk_json.default_family_history.parents_pc;
risk_json.default_family_history.ref.forEach(ref => study.ref.push(ref));
// risk_json.default_family_history.ref.map(study.ref.push.bind(study));
}
input.parent && res.push(study['sibling_pc']);
return res;
};
export const combined_risk = (familial_risks_from_study_l: number[], risk_from_studies: number): MathType =>
math.add(
familial_risks_from_study_l
.map(r => math.multiply(math.divide(r, 100), risk_from_studies))
.reduce(math.add as (p: number, c: number) => number),
risk_from_studies
);
export const risks_from_study = (risk_json: IRiskJson, input: IInput): number[] => {
if (isNullOrUndefined(risk_json)) throw TypeError('`risk_json` must be defined');
else if (isNullOrUndefined(input)) throw TypeError('`input` must be defined');
preprocess_studies(risk_json);
const study: IBarbados = risk_json.studies[input.study] as IBarbados;
const study_vals = study[study.expr[0].key];
const out = Array.isArray(study_vals) ?
study_vals
.filter(o => input.gender ? o.gender === input.gender : true)
.map(o => o[study.expr[0].extract])
: ensure_map(study.expr[0].type)
&& Object
.keys(study_vals)
.filter(k => ['a', '_'].indexOf(k[0]) === -1)
.map(k => study_vals[k]);
if (!out) throw TypeError('Expected out to match something');
return uniq(out);
};
export const place_in_array = (entry: any, a: any[]): number => {
if (isNullOrUndefined(entry)) throw TypeError('`entry` must be defined');
else if (isNullOrUndefined(a)) throw TypeError('`a` must be defined');
const sortedA = a.sort();
for (let i = 0; i < sortedA.length; i++)
if (sortedA[i] === entry) return i;
return -1;
};
export const pos_in_range = (ranges: string[], num: number): number => {
ranges = sort_ranges(ranges);
for (let i = 0; i < ranges.length; i++)
if (in_range(ranges[i], num))
return i;
return -1;
};
export const list_ethnicities = (risk_json: IRiskJson): IDictOfStringArray => {
if (isNullOrUndefined(risk_json)) throw TypeError('`risk_json` must be defined');
return Object
.keys(risk_json.studies)
.map(k => {
return { [k]: risk_json.studies[k].ethnicities };
}) as IDictOfStringArray | any;
};
export const ethnicity2study = (risk_json: IRiskJson): {} => {
const o = {};
Object
.keys(risk_json.studies)
.map(study_name =>
risk_json.studies[study_name].ethnicities.map(ethnicity => ({ [ethnicity]: study_name }))
)
.reduce((a, b) => a.concat(b), [])
.forEach(obj => Object.assign(o, obj));
return o;
};
export const calc_default_multiplicative_risks = (risk_json: IRiskJson,
user: IMultiplicativeRisks): IMultiplicativeRisks => {
return {
age: risk_json.default_multiplicative_risks.age[
Object
.keys(risk_json.default_multiplicative_risks.age)
.filter(range => in_range(range, user.age as number))
[0]
],
myopia: user.myopia ? risk_json.default_multiplicative_risks.myopia.existent : 1,
family_history: user.family_history ? risk_json.default_multiplicative_risks.family_history.existent : 1,
diabetes: user.diabetes ? risk_json.default_multiplicative_risks.diabetes.existent : 1
};
};
export const calc_relative_risk = (risk_json: IRiskJson, input: IInput): IRelativeRisk => {
const has_gender = input.gender != null;
const risk_per_study = has_gender ?
Object
.keys(risk_json.studies)
.map(study_name => ({
[study_name]: risk_json.studies[study_name].agenda == null ?
(age_range => ({
max_prevalence: risk_json.studies[study_name].age[age_range as any],
age: age_range[0]
}))(Object
.keys(risk_json.studies[study_name].age)
.filter(age_range => in_range(age_range, input.age)))
: risk_json.studies[study_name].agenda
.filter(stat => input.gender === stat.gender && in_range(stat.age, input.age))
[0]
}))
.reduce((obj, item) => {
const k = Object.keys(item)[0];
obj[k] = item[k];
return obj;
}, {}) : null;
const relative_risk: { [study: /*Study*/ string]: number }[] = Object
.keys(risk_per_study)
.map(study_name => ({
[study_name]: risk_per_study[study_name][risk_json.studies[study_name].expr[0].extract
] || risk_per_study[study_name][
Object
.keys(risk_per_study[study_name])
.filter(k => typeof risk_per_study[study_name][k] === 'number')
[0]
]
}))
.sort((a, b) => a[Object.keys(a)[0]] > b[Object.keys(b)[0]] as any);
const graphed_rr: ITreeMapData[] = relative_risk.map(atoi => {
const study_name = Object.keys(atoi)[0];
const risk_val = atoi[study_name];
return {
name: risk_json.studies[study_name].ethnicities[0],
size: risk_val, value: risk_val
};
});
return Object.assign({
age: input.age,
study: input.study,
rr: relative_risk,
risk_per_study: risk_per_study as IRelativeRisk['risk_per_study'],
graphed_rr
}, has_gender ? { gender: input.gender } : {});
};
/*
export const get_risk_pc = (pc => ((r => r > 100 ? 100 : r)(fam_risk.reduce((a, b) => a + b, 1) + pc)))(math.multiply(
math.divide(risks.lastIndexOf(risk) + 1, risks.length), 100
));
*/
/*
if (require.main === module) {
import { exists, readFile, writeFile } from 'fs';
exists('./risk.json', fs_exists => {
// tslint:disable:no-console
console.error(`fs_exists = ${fs_exists}`);
writeFile('/tmp/a.txt', `fs_exists = ${fs_exists}`, err => {
if (err) throw err;
readFile('./risk.json', 'utf8', (e, data) => {
if (e) throw e;
// tslint:disable:no-console
console.info(data);
});
// fs_exists && console.info(JSON.stringify(require('./risk'), null, '\t'))
});
});
}
*/