-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
198 lines (174 loc) · 6.29 KB
/
index.js
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
import fs from 'fs-extra';
import path from 'path';
import _ from 'lodash';
import moment from 'moment';
import program from 'commander';
import promisify from 'es6-promisify';
import template from './profile_template.json';
import translateUtils from './util';
const exec = promisify(require('child_process').exec);
program
.version('0.2.0')
.option('--start-date <startDate>', 'YYYY-MM-DD')
.option('--end-date <endDate>', 'YYYY-MM-DD')
.option('--source <pathToJsonDump>', 'Path to the Tidepool JSON data.')
.option(
'-d, --no-docker',
'Set flag to use globally installed oref0 executables instead of docker image.',
true,
)
.option('-v, --verbose', 'Verbose console logging')
.parse(process.argv);
const jsonPath =
program.source || '../tidepool/command-line-data-tools/test.json';
const data = fs.readJsonSync(jsonPath);
const DATA_PATH = path.resolve('data');
const SETTINGS_PATH = path.join(DATA_PATH, 'settings');
const AUTOTUNE_PATH = path.join(DATA_PATH, 'autotune');
const GLUCOSE_MM = 18.01559;
const START_DATE =
(program.startDate && moment(program.startDate).format('YYYY-MM-DD')) ||
moment()
.subtract(1, 'day')
.format('YYYY-MM-DD');
const END_DATE =
(program.endDate && moment(program.endDate).format('YYYY-MM-DD')) ||
moment()
.subtract(1, 'day')
.format('YYYY-MM-DD');
const DATE_FILTER_START = moment.utc(START_DATE).subtract(4, 'hours');
const DATE_FILTER_END = moment.utc(END_DATE).add(1, 'day');
(async function main() {
await fs.remove(DATA_PATH);
await fs.mkdirp(SETTINGS_PATH);
await fs.mkdirp(AUTOTUNE_PATH);
const sortedData = _.sortBy(data, ['time']);
// profile.json generation
if (program.verbose) {
console.log('Generating profile.json');
}
const pumpSettings = _.last(_.filter(sortedData, { type: 'pumpSettings' }));
const { activeSchedule } = pumpSettings;
const activeBasalSchedule = pumpSettings.basalSchedules[activeSchedule];
const basalProfiles = _.map(activeBasalSchedule, sched => ({
start: moment.utc(sched.start).format('HH:mm:ss'),
minutes: moment.duration(sched.start).asMinutes(),
rate: sched.rate,
}));
let isf;
let carbRatio;
if (pumpSettings.insulinSensitivities) {
isf = translateUtils.getAverageISF(
pumpSettings.insulinSensitivities[activeSchedule],
);
} else {
isf = translateUtils.getAverageISF(pumpSettings.insulinSensitivity);
}
if (pumpSettings.carbRatios) {
carbRatio = translateUtils.getAverageCarbRatio(
pumpSettings.carbRatios[activeSchedule],
);
} else {
carbRatio = translateUtils.getAverageCarbRatio(pumpSettings.carbRatio);
}
const profile = _.cloneDeep(template);
profile.basalprofile = basalProfiles;
profile.isfProfile.sensitivities[0].sensitivity = isf;
profile.carb_ratio = carbRatio;
const profilePath = path.join(SETTINGS_PATH, 'profile.json');
await fs.writeFile(profilePath, JSON.stringify(profile, null, ' '));
await fs.copy(profilePath, path.join(SETTINGS_PATH, 'pumpprofile.json'));
await fs.copy(profilePath, path.join(SETTINGS_PATH, 'autotune.json'));
await fs.copy(profilePath, path.join(AUTOTUNE_PATH, 'profile.pump.json'));
await fs.copy(profilePath, path.join(AUTOTUNE_PATH, 'profile.json'));
// CBG data translation
if (program.verbose) {
console.log('Translating CBG values');
}
const cbgData = _.filter(
sortedData,
datum =>
datum.type === 'cbg' &&
moment.utc(datum.time).isBetween(DATE_FILTER_START, DATE_FILTER_END),
);
const translatedCbgData = _.groupBy(
_.map(cbgData, cbg => ({
glucose: cbg.value * GLUCOSE_MM,
date: cbg.time,
dateString: cbg.time,
})),
cbg => moment.utc(cbg.date).format('YYYY-MM-DD'),
);
_.forOwn(translatedCbgData, async (dateData, date) => {
await fs.writeFile(
path.join(DATA_PATH, `tp-entries-${date}.json`),
JSON.stringify(dateData, null, ' '),
);
});
// treatment history translation
if (program.verbose) {
console.log('Translating pump history events');
}
const historyEvents = _.filter(
sortedData,
datum =>
_.includes(['basal', 'bolus', 'wizard'], datum.type) &&
// non-temp basal's are taken care of by the basal profiles
(datum.type === 'basal' ? datum.deliveryType === 'temp' : true) &&
moment.utc(datum.time).isBetween(DATE_FILTER_START, DATE_FILTER_END),
);
const translatedEvents = _.map(historyEvents, event => {
switch (event.type) {
case 'basal':
return translateUtils.translateBasal(event);
case 'bolus':
return translateUtils.translateBolus(event);
case 'wizard':
return translateUtils.translateWizard(event);
default:
console.error('Unhandled event: ', event);
throw new Error('Unknown event');
}
});
await fs.writeFile(
path.join(DATA_PATH, `tp-treatments.json`),
JSON.stringify(translatedEvents, null, ' '),
);
let DOCKER_CMD = 'docker run -i -v "$(pwd)":/app pazaan/openaps ';
let DOCKER_PREFIX = '/app/';
if (!program.docker) {
DOCKER_CMD = '';
DOCKER_PREFIX = '';
}
// this loop is _very_ similar to what oref0-autotune does
const currentDay = moment(START_DATE);
while (currentDay.isSameOrBefore(END_DATE)) {
const currentDayStr = currentDay.format('YYYY-MM-DD');
if (program.verbose) {
console.log(`Processing ${currentDayStr}`);
}
/* eslint-disable no-await-in-loop */
await fs.copy(
'data/autotune/profile.json',
`data/autotune/profile.${currentDayStr}.json`,
);
await exec(
`${DOCKER_CMD}oref0-autotune-prep ${DOCKER_PREFIX}data/tp-treatments.json ${DOCKER_PREFIX}data/autotune/profile.json ${DOCKER_PREFIX}data/tp-entries-${currentDayStr}.json > data/autotune.${currentDayStr}.json`,
);
await exec(
`${DOCKER_CMD}oref0-autotune-core ${DOCKER_PREFIX}data/autotune.${currentDayStr}.json ${DOCKER_PREFIX}data/autotune/profile.json ${DOCKER_PREFIX}data/autotune/profile.pump.json > data/newprofile.${currentDayStr}.json`,
);
await fs.copy(
`data/newprofile.${currentDayStr}.json`,
'data/autotune/profile.json',
);
await exec(
`${DOCKER_CMD}oref0-autotune-recommends-report ${DOCKER_PREFIX}data`,
);
/* eslint-enable */
currentDay.add(1, 'day');
}
console.log(
await fs.readFile('data/autotune/autotune_recommendations.log', 'utf8'),
);
})();