Skip to content

Commit

Permalink
Merge branch 'autotune' into pip-install
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Jan 9, 2017
2 parents 683259d + 2eb48c9 commit 4e6f25d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
10 changes: 8 additions & 2 deletions bin/oref0-autotune.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ fi
echo "Grabbing NIGHTSCOUT treatments.json for date range..."

# Get Nightscout carb and insulin Treatments
curl "$NIGHTSCOUT_HOST/api/v1/treatments.json?find\[created_at\]\[\$gte\]=`date -d $START_DATE -Iminutes`&\[\$lte\]=`date -d $END_DATE -Iminutes`" > ns-treatments.json
url="$NIGHTSCOUT_HOST/api/v1/treatments.json?find\[created_at\]\[\$gte\]=`date --date="$START_DATE -4 hours" -Iminutes`&find\[created_at\]\[\$lte\]=`date --date="$END_DATE +1 days" -Iminutes`"
echo $url
curl -s $url > ns-treatments.json
ls -la ns-treatments.json

# Build date list for autotune iteration
date_list=()
Expand All @@ -149,7 +152,10 @@ echo "Grabbing NIGHTSCOUT entries/sgv.json for date range..."
# Get Nightscout BG (sgv.json) Entries
for i in "${date_list[@]}"
do
curl "$NIGHTSCOUT_HOST/api/v1/entries/sgv.json?find\[date\]\[\$gte\]=`(date -d $i +%s | tr -d '\n'; echo 000)`&find\[date\]\[\$lte\]=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000" > ns-entries.$i.json
url="$NIGHTSCOUT_HOST/api/v1/entries/sgv.json?find\[date\]\[\$gte\]=`(date -d $i +%s | tr -d '\n'; echo 000)`&find\[date\]\[\$lte\]=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
echo $url
curl -s $url > ns-entries.$i.json
ls -la ns-entries.$i.json
done

echo "Running $NUMBER_OF_RUNS runs from $START_DATE to $END_DATE"
Expand Down
4 changes: 2 additions & 2 deletions bin/oref0-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ else
openaps device add pump mmeowlink subg_rfspy $ttyport $serial $radio_locale || die "Can't add pump"
openaps alias add wait-for-silence '! bash -c "(mmeowlink-any-pump-comms.py --port '$ttyport' --wait-for 1 | grep -q comms && echo -n Radio ok, || openaps mmtune) && echo -n \" Listening: \"; for i in $(seq 1 100); do echo -n .; mmeowlink-any-pump-comms.py --port '$ttyport' --wait-for 30 2>/dev/null | egrep -v subg | egrep No && break; done"'
openaps alias add wait-for-long-silence '! bash -c "echo -n \"Listening: \"; for i in $(seq 1 200); do echo -n .; mmeowlink-any-pump-comms.py --port '$ttyport' --wait-for 45 2>/dev/null | egrep -v subg | egrep No && break; done"'
if [[ ${radio_locale,,} =~ "WW" ]]; then
if [[ ${radio_locale,,} =~ "ww" ]]; then
# add subg-ww-radio-parameters script to mmtune for WW pump. See https://github.com/oskarpearson/mmeowlink/issues/51 or https://github.com/oskarpearson/mmeowlink/wiki/Non-USA-pump-settings for details
sed -i"" 's/^\(mmtune.*\); \(echo -n .*mmtune:\)/\1; echo -n subg-ww-radio-parameters:; \/usr\/local\/bin\/oref0-subg-ww-radio-parameters-timeout; \2/g' openaps.ini

Expand Down Expand Up @@ -567,7 +567,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
(crontab -l; crontab -l | grep -q "cd $directory && ps aux | grep -v grep | grep -q 'openaps monitor-xdrip'" || echo "* * * * * cd $directory && ps aux | grep -v grep | grep -q 'openaps monitor-xdrip' || ( date; openaps monitor-xdrip) | tee -a /var/log/openaps/xdrip-loop.log; cp -up $directory/xdrip/glucose.json $directory/monitor/glucose.json") | crontab -
(crontab -l; crontab -l | grep -q "xDripAPS.py" || echo "@reboot python $HOME/.xDripAPS/xDripAPS.py") | crontab -
elif [[ $ENABLE =~ dexusb ]]; then
(crontab -l; crontab -l | grep -q "@reboot .*dexusb-cgm" || echo "@reboot /usr/bin/python -u /usr/local/bin/oref0-dexusb-cgm-loop >> /var/log/openaps/cgm-dexusb-loop.log 2>&1" ) | crontab -
(crontab -l; crontab -l | grep -q "@reboot .*dexusb-cgm" || echo "@reboot cd $directory && /usr/bin/python -u /usr/local/bin/oref0-dexusb-cgm-loop >> /var/log/openaps/cgm-dexusb-loop.log 2>&1" ) | crontab -
elif ! [[ ${CGM,,} =~ "mdt" ]]; then # use nightscout for cgm
(crontab -l; crontab -l | grep -q "cd $directory && ps aux | grep -v grep | grep -q 'openaps get-bg'" || echo "* * * * * cd $directory && ps aux | grep -v grep | grep -q 'openaps get-bg' || ( date; openaps get-bg ; cat cgm/glucose.json | json -a sgv dateString | head -1 ) | tee -a /var/log/openaps/cgm-loop.log") | crontab -
fi
Expand Down
32 changes: 18 additions & 14 deletions lib/autotune/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ function tuneAllTheThings (inputs) {
var ratios = [];
var count = 0;
for (var i=0; i < ISFGlucose.length; ++i) {
deviation = parseFloat(ISFGlucose[i].deviation);
deviations.push(deviation);
BGI = parseFloat(ISFGlucose[i].BGI);
BGIs.push(BGI);
avgDelta = parseFloat(ISFGlucose[i].avgDelta);
avgDeltas.push(avgDelta);
ratio = 1 + deviation / BGI;
//console.error("Deviation:",deviation,"BGI:",BGI,"avgDelta:",avgDelta,"ratio:",ratio);
ratios.push(ratio);
count++;
deviation = parseFloat(ISFGlucose[i].deviation);
deviations.push(deviation);
BGI = parseFloat(ISFGlucose[i].BGI);
BGIs.push(BGI);
avgDelta = parseFloat(ISFGlucose[i].avgDelta);
avgDeltas.push(avgDelta);
ratio = 1 + deviation / BGI;
//console.error("Deviation:",deviation,"BGI:",BGI,"avgDelta:",avgDelta,"ratio:",ratio);
ratios.push(ratio);
count++;
}
avgDeltas.sort(function(a, b){return a-b});
BGIs.sort(function(a, b){return a-b});
Expand All @@ -161,9 +161,13 @@ function tuneAllTheThings (inputs) {
p50deviation = percentile(deviations, 0.50);
p50BGI = percentile(BGIs, 0.50);
p50ratios = Math.round( percentile(ratios, 0.50) * 1000)/1000;

// calculate what adjustments to ISF would have been necessary to bring median deviation to zero
fullNewISF = ISF * p50ratios;
if (count < 5) {
// leave ISF unchanged if fewer than 5 ISF data points
fullNewISF = ISF;
} else {
// calculate what adjustments to ISF would have been necessary to bring median deviation to zero
fullNewISF = ISF * p50ratios;
}
fullNewISF = Math.round( fullNewISF * 1000 ) / 1000;
// and apply 10% of that adjustment
var newISF = ( 0.9 * ISF ) + ( 0.1 * fullNewISF );
Expand Down Expand Up @@ -254,7 +258,7 @@ function tuneAllTheThings (inputs) {
isfProfile.sensitivities[0].sensitivity = ISF;
autotuneOutput.isfProfile = isfProfile;
autotuneOutput.sens = ISF;
autotuneOutput.CSF = CSF;
autotuneOutput.csf = CSF;
carbRatio = ISF / CSF;
carbRatio = Math.round( carbRatio * 1000 ) / 1000;
autotuneOutput.carb_ratio = carbRatio;
Expand Down

0 comments on commit 4e6f25d

Please sign in to comment.