-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgen_random_data.js
More file actions
66 lines (60 loc) · 2.93 KB
/
gen_random_data.js
File metadata and controls
66 lines (60 loc) · 2.93 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
// Use this script to generate random data for the app
// Run this script with the command:
// node --max-old-space-size=32768 gen_random_data.js
console.log('Generating random data')
const fs = require('fs');
const snomed_terms = require('./data/snomed_terms.json');
// Returns a random integer between min (inclusive) and max (inclusive).
function random_int(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const sex_id2code = ['Male', 'Female', 'Unknown']
const eth_id2code = ['Asian', 'Black', 'White', 'Mixed', 'Other', 'Unknown'];
let ptt2age = {};
let ptt2sex = {};
let ptt2eth = {};
let ptt2dod = {};
let cui2ptt_pos = {};
let cui2ptt_tsp = {};
let ptt_num = 100000;
let max_ptt = 1000; // max. number of ptt a term can have
let max_age = 100;
let die_pct = 10; // percentage of died ptt = 1 / die_pct
// generate ptt_num random patient data
for (let i=0;i<ptt_num;i++) {
ptt2age[i] = random_int(0,max_age);
ptt2sex[i] = sex_id2code[random_int(0,sex_id2code.length-1)];
ptt2eth[i] = eth_id2code[random_int(0,eth_id2code.length-1)];
ptt2dod[i] = random_int(0,die_pct) == 0 ? random_int(Math.floor(Date.now()/1000) - (60*60*24*365*10), Math.floor(Date.now()/1000)) : 0;
if (i%100000 == 0) console.log('ptt:', i, `${Math.floor((i/ptt_num)*100)}%`);
}
// for each snomed terms, generate some random mention data
for (let i=0;i<snomed_terms.length;i++) {
if (snomed_terms[i]['str'].search('(disorder)')==-1 && snomed_terms[i]['str'].search('(finding)')==-1 &&
snomed_terms[i]['str'].search('(procedure)')==-1 && snomed_terms[i]['str'].search('(substance)')==-1)
continue;
let picked = {};
cui2ptt_pos[i] = {};
cui2ptt_tsp[i] = {};
for (let j=0;j<random_int(0,max_ptt);j++) {
let ptt = random_int(0, ptt_num-1);
while (picked[ptt]) ptt = random_int(0, ptt_num-1);
picked[ptt] = true;
cui2ptt_pos[i][ptt] = random_int(1,100);
cui2ptt_tsp[i][ptt] = random_int(Math.floor(Date.now()/1000) - (60*60*24*365*10), Math.floor(Date.now()/1000));
}
if (i%100000 == 0) console.log('men:', i, `${Math.floor((i/snomed_terms.length)*100)}%`);
}
// write to files
console.log('Writing to files...')
fs.writeFileSync('data/ptt2age.json', JSON.stringify(ptt2age));
fs.writeFileSync('data/ptt2sex.json', JSON.stringify(ptt2sex));
fs.writeFileSync('data/ptt2eth.json', JSON.stringify(ptt2eth));
fs.writeFileSync('data/ptt2dod.json', JSON.stringify(ptt2dod));
const pos_out = fs.createWriteStream('data/cui2ptt_pos.jsonl', {flags: 'w'});
const tsp_out = fs.createWriteStream('data/cui2ptt_tsp.jsonl', {flags: 'w'});
Object.keys(cui2ptt_pos).forEach( k => { pos_out.write(`{"${snomed_terms[k]['cui']}":` + JSON.stringify(cui2ptt_pos[k]) + '}\n'); });
Object.keys(cui2ptt_tsp).forEach( k => { tsp_out.write(`{"${snomed_terms[k]['cui']}":` + JSON.stringify(cui2ptt_tsp[k]) + '}\n'); });
console.log('Finished generating random data')