New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prep for eventual 0.6.0 dev to master release #650

Merged
merged 146 commits into from Oct 5, 2017
Commits
Jump to file or symbol
Failed to load files and symbols.
+2,593 −3,227
Diff settings

Always

Just for now

View
@@ -22,7 +22,7 @@
var generate = require('oref0/lib/autotune-prep');
function usage ( ) {
console.error('usage: ', process.argv.slice(0, 2), '<pumphistory.json> <profile.json> <glucose.json> [carbhistory.json] [autotune/glucose.json]');
console.error('usage: ', process.argv.slice(0, 2), '<pumphistory.json> <profile.json> <glucose.json> [pumpprofile.json] [carbhistory.json] [autotune/glucose.json]');
}
if (!module.parent) {
@@ -33,8 +33,9 @@ if (!module.parent) {
}
var profile_input = process.argv.slice(3, 4).pop();
var glucose_input = process.argv.slice(4, 5).pop();
var carb_input = process.argv.slice(5, 6).pop()
var prepped_glucose_input = process.argv.slice(6, 7).pop()
var pumpprofile_input = process.argv.slice(5, 6).pop()
var carb_input = process.argv.slice(6, 7).pop()
var prepped_glucose_input = process.argv.slice(7, 8).pop()
if ( !pumphistory_input || !profile_input || !glucose_input ) {
usage( );
@@ -51,10 +52,23 @@ if (!module.parent) {
return console.error("Could not parse input data: ", e);
}
var pumpprofile_data = { };
if (typeof pumpprofile_input != 'undefined') {
try {
pumpprofile_data = JSON.parse(fs.readFileSync(pumpprofile_input, 'utf8'));
} catch (e) {
console.error("Warning: could not parse "+pumpprofile_input);
}
}
// disallow impossibly low carbRatios due to bad decoding
if ( typeof(profile_data.carb_ratio) == 'undefined' || profile_data.carb_ratio < 2 ) {
console.log('{ "carbs": 0, "mealCOB": 0, "reason": "carb_ratio ' + profile_data.carb_ratio + ' out of bounds" }');
return console.error("Error: carb_ratio " + profile_data.carb_ratio + " out of bounds");
if ( typeof(pumpprofile_data.carb_ratio) == 'undefined' || pumpprofile_data.carb_ratio < 2 ) {
console.log('{ "carbs": 0, "mealCOB": 0, "reason": "carb_ratios ' + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + ' out of bounds" }');
return console.error("Error: carb_ratios " + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + " out of bounds");
} else {
profile_data.carb_ratio = pumpprofile_data.carb_ratio;
}
}
try {
@@ -64,20 +64,20 @@ csf_new=$(cat $directory/autotune/profile.json | jq '.csf')
carb_ratio_new=$(cat $directory/autotune/profile.json | jq '.carb_ratio')
# Print Header Info
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Parameter" "Current" "Autotune" >> $report_file
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Parameter" "Pump" "Autotune" >> $report_file
printf "%s\n" "-------------------------------------" >> $report_file
# Print ISF, CSF and Carb Ratio Recommendations
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "ISF [mg/dL/U]" $isf_current $isf_new >> $report_file
if [ $csf_current != null ]; then
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "CSF [mg/dL/g]" $csf_current $csf_new >> $report_file
else
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}.3f\n" "CSF [mg/dL/g]" "n/a" $csf_new >> $report_file
fi
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "Carb Ratio [g]" $carb_ratio_current $carb_ratio_new >> $report_file
# if [ $csf_current != null ]; then
# printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "CSF [mg/dL/g]" $csf_current $csf_new >> $report_file
# else
# printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}.3f\n" "CSF [mg/dL/g]" "n/a" $csf_new >> $report_file
# fi
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "Carb Ratio[g/U]" $carb_ratio_current $carb_ratio_new >> $report_file
# Print Basal Profile Recommendations
printf "%-${parameter_width}s| %-${data_width}s|\n" "Basal Profile [unit/hour]" "-" >> $report_file
printf "%-${parameter_width}s| %-${data_width}s|\n" "Basals [U/hr]" "-" >> $report_file
# Build time_list array of H:M in 30 minute increments to mirror pump basal schedule
time_list=()
View
@@ -1,7 +1,7 @@
#!/bin/bash
# This script sets up an easy test environment for autotune, allowing the user to vary parameters
# like start/end date and number of runs.
# like start/end date.
#
# Required Inputs:
# DIR, (--dir=<OpenAPS Directory>)
@@ -10,8 +10,6 @@
# Optional Inputs:
# END_DATE, (--end-date=<YYYY-MM-DD>)
# if no end date supplied, assume we want a months worth or until day before current day
# NUMBER_OF_RUNS (--runs=<integer, number of runs desired>)
# if no number of runs designated, then default to 5
# EXPORT_EXCEL (--xlsx=<filenameofexcel>)
# export to excel. Disabled by default
# TERMINAL_LOGGING (--log <true/false(true)>
@@ -39,7 +37,8 @@ DIR=""
NIGHTSCOUT_HOST=""
START_DATE=""
END_DATE=""
NUMBER_OF_RUNS=1 # Default to a single run if not otherwise specified
START_DAYS_AGO=1 # Default to yesterday if not otherwise specified
END_DAYS_AGO=1 # Default to yesterday if not otherwise specified
EXPORT_EXCEL="" # Default is to not export to Microsoft Excel
TERMINAL_LOGGING=true
RECOMMENDS_REPORT=true
@@ -50,8 +49,8 @@ if [ -n "${API_SECRET_READ}" ]; then
fi
if [[ -z "$API_SECRET" ]]; then
echo "ERROR: API_SECRET is not set when calling oref0-autotune.sh"
exit 1
echo "Warning: API_SECRET is not set when calling oref0-autotune.sh"
# exit 1
fi
# If we are running OS X, we need to use a different version
@@ -96,8 +95,12 @@ case $i in
END_DATE=`date --date="$END_DATE" +%Y-%m-%d`
shift # past argument=value
;;
-r=*|--runs=*)
NUMBER_OF_RUNS="${i#*=}"
-t=*|--start-days-ago=*)
START_DAYS_AGO="${i#*=}"
shift # past argument=value
;;
-d=*|--end-days-ago=*)
END_DAYS_AGO="${i#*=}"
shift # past argument=value
;;
-x=*|--xlsx=*)
@@ -116,28 +119,31 @@ case $i in
esac
done
# remove any trailing / from NIGHTSCOUT_HOST
NIGHTSCOUT_HOST=$(echo $NIGHTSCOUT_HOST | sed 's/\/$//g')
if [[ -z "$DIR" || -z "$NIGHTSCOUT_HOST" ]]; then
echo "Usage: oref0-autotune <--dir=myopenaps_directory> <--ns-host=https://mynightscout.azurewebsites.net> [--start-date=YYYY-MM-DD] [--end-date=YYYY-MM-DD] [--runs=number_of_runs] [--xlsx=autotune.xlsx] [--log=(true)|false]"
echo "Usage: oref0-autotune <--dir=myopenaps_directory> <--ns-host=https://mynightscout.azurewebsites.net> [--start-days-ago=number_of_days] [--end-days-ago=number_of_days] [--start-date=YYYY-MM-DD] [--end-date=YYYY-MM-DD] [--xlsx=autotune.xlsx] [--log=(true)|false]"
exit 1
fi
if [[ -z "$START_DATE" ]]; then
# Default start date of yesterday
START_DATE=`date --date="1 day ago" +%Y-%m-%d`
START_DATE=`date --date="$START_DAYS_AGO days ago" +%Y-%m-%d`
fi
if [[ -z "$END_DATE" ]]; then
# Default end-date as this morning at midnight in order to not get partial day samples for now
# (ISF/CSF adjustments are still single values across each day)
END_DATE=`date --date="1 day ago" +%Y-%m-%d`
END_DATE=`date --date="$END_DAYS_AGO days ago" +%Y-%m-%d`
fi
if [[ -z "$UNKNOWN_OPTION" ]] ; then # everything is ok
echo "Running oref0-autotune --dir=$DIR --ns-host=$NIGHTSCOUT_HOST --start-date=$START_DATE --runs=$NUMBER_OF_RUNS --end-date=$END_DATE"
echo "Running oref0-autotune --dir=$DIR --ns-host=$NIGHTSCOUT_HOST --start-date=$START_DATE --end-date=$END_DATE"
else
echo "Unknown options. Exiting"
exit 1
fi
# Get profile for testing copied to home directory. "openaps" is my loop directory name.
# Get profile for testing copied to home directory.
cd $directory && mkdir -p autotune
cp settings/pumpprofile.json autotune/profile.pump.json || die "Cannot copy settings/pumpprofile.json"
# This allows manual users to be able to run autotune by simply creating a settings/pumpprofile.json file.
@@ -157,15 +163,6 @@ if [[ $TERMINAL_LOGGING = "true" ]]; then
exec &> >(tee -a autotune.$(date +%Y-%m-%d-%H%M%S).log)
fi
# Pull Nightscout Data
echo "Grabbing NIGHTSCOUT treatments.json for date range..."
# Get Nightscout carb and insulin Treatments
query="find%5Bcreated_at%5D%5B%24gte%5D=`date --date="$START_DATE -4 hours" -Iminutes`&find%5Bcreated_at%5D%5B%24lte%5D=`date --date="$END_DATE +1 days" -Iminutes`"
echo Query: $NIGHTSCOUT_HOST/$query
ns-get host $NIGHTSCOUT_HOST treatments.json $query > ns-treatments.json || die "Couldn't download ns-treatments.json"
ls -la ns-treatments.json || die "No ns-treatments.json downloaded"
# Build date list for autotune iteration
date_list=()
date=$START_DATE;
@@ -179,76 +176,76 @@ do
fi
done
echo "Grabbing NIGHTSCOUT entries/sgv.json for date range..."
echo "Grabbing NIGHTSCOUT treatments.json and entries/sgv.json for date range..."
# Get Nightscout BG (sgv.json) Entries
for i in "${date_list[@]}"
do
query="find%5Bdate%5D%5B%24gte%5D=`(date -d $i +%s | tr -d '\n'; echo 000)`&find%5Bdate%5D%5B%24lte%5D=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
echo Query: $NIGHTSCOUT_HOST $query
ns-get host $NIGHTSCOUT_HOST entries/sgv.json $query > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
ls -la ns-entries.$i.json || die "No ns-entries.$i.json downloaded"
done
echo "Running $NUMBER_OF_RUNS runs from $START_DATE to $END_DATE"
sleep 2
# Do iterative runs over date range, save autotune.json (prepped data) and input/output
# profile.json
# Loop 1: Run 1 to Number of Runs specified by user or by default (1)
for run_number in $(seq 1 $NUMBER_OF_RUNS)
do
# Loop 2: Iterate through Date Range
for i in "${date_list[@]}"
do
cp profile.json profile.$run_number.$i.json
query="find%5Bdate%5D%5B%24gte%5D=`(date -d $i +%s | tr -d '\n'; echo 000)`&find%5Bdate%5D%5B%24lte%5D=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
echo Query: $NIGHTSCOUT_HOST $query
ns-get host $NIGHTSCOUT_HOST entries/sgv.json $query > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
ls -la ns-entries.$i.json || die "No ns-entries.$i.json downloaded"
# Get Nightscout carb and insulin Treatments
# echo $i $START_DATE;
#query="find%5Bdate%5D%5B%24gte%5D=`(date -d $i +%s | tr -d'\n'; echo 000)`&find%5Bdate%5D%5B%24lte%5D=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
# to capture UTC-dated treatments, we need to capture an extra 12h on either side, plus the DIA lookback
# 18h = 12h for timezones + 6h for DIA; 36h = 24h for end-of-day + 12h for timezones
query="find%5Bcreated_at%5D%5B%24gte%5D=`date --date="$i -18 hours" -Iminutes`&find%5Bcreated_at%5D%5B%24lte%5D=`date --date="$i +36 hours" -Iminutes`"
echo Query: $NIGHTSCOUT_HOST/$query
ns-get host $NIGHTSCOUT_HOST treatments.json $query > ns-treatments.$i.json || die "Couldn't download ns-treatments.$i.json"
ls -la ns-treatments.$i.json || die "No ns-treatments.$i.json downloaded"
# Do iterative runs over date range, save autotune.json (prepped data) and input/output profile.json
cp profile.json profile.$i.json
# Autotune Prep (required args, <pumphistory.json> <profile.json> <glucose.json>), output prepped glucose
# data or <autotune/glucose.json> below
echo "oref0-autotune-prep ns-treatments.json profile.json ns-entries.$i.json > autotune.$run_number.$i.json"
oref0-autotune-prep ns-treatments.json profile.json ns-entries.$i.json > autotune.$run_number.$i.json \
|| die "Could not run oref0-autotune-prep ns-treatments.json profile.json ns-entries.$i.json"
echo "oref0-autotune-prep ns-treatments.$i.json profile.json ns-entries.$i.json profile.pump.json > autotune.$i.json"
oref0-autotune-prep ns-treatments.$i.json profile.json ns-entries.$i.json profile.pump.json > autotune.$i.json \
|| die "Could not run oref0-autotune-prep ns-treatments.$i.json profile.json ns-entries.$i.json"
# Autotune (required args, <autotune/glucose.json> <autotune/autotune.json> <settings/profile.json>),
# output autotuned profile or what will be used as <autotune/autotune.json> in the next iteration
echo "oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json > newprofile.$run_number.$i.json"
if ! oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json > newprofile.$run_number.$i.json; then
echo "oref0-autotune-core autotune.$i.json profile.json profile.pump.json > newprofile.$i.json"
if ! oref0-autotune-core autotune.$i.json profile.json profile.pump.json > newprofile.$i.json; then
if cat profile.json | jq --exit-status .carb_ratio==null; then
echo "ERROR: profile.json contains null carb_ratio: using profile.pump.json"
cp profile.pump.json profile.json
exit
else
die "Could not run oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json"
die "Could not run oref0-autotune-core autotune.$i.json profile.json profile.pump.json"
fi
else
# Copy tuned profile produced by autotune to profile.json for use with next day of data
cp newprofile.$run_number.$i.json profile.json
cp newprofile.$i.json profile.json
fi
done # End Date Range Iteration
done # End Number of Runs Loop
if ! [[ -z "$EXPORT_EXCEL" ]]; then
echo Exporting to $EXPORT_EXCEL
oref0-autotune-export-to-xlsx --dir $DIR --output $EXPORT_EXCEL
fi
if ! [[ -z "$EXPORT_EXCEL" ]]; then
echo Exporting to $EXPORT_EXCEL
oref0-autotune-export-to-xlsx --dir $DIR --output $EXPORT_EXCEL
fi
# Create Summary Report of Autotune Recommendations and display in the terminal
if [[ $RECOMMENDS_REPORT == "true" ]]; then
# Set the report file name, so we can let the user know where it is and cat
# it to the screen
report_file=$directory/autotune/autotune_recommendations.log
# Create Summary Report of Autotune Recommendations and display in the terminal
if [[ $RECOMMENDS_REPORT == "true" ]]; then
# Set the report file name, so we can let the user know where it is and cat
# it to the screen
report_file=$directory/autotune/autotune_recommendations.log
echo
echo "Autotune pump profile recommendations:"
echo "---------------------------------------------------------"
echo
echo "Autotune pump profile recommendations:"
echo "---------------------------------------------------------"
# Let the user know where the Autotune Recommendations are logged
echo "Recommendations Log File: $report_file"
echo
# Let the user know where the Autotune Recommendations are logged
echo "Recommendations Log File: $report_file"
echo
# Run the Autotune Recommends Report
oref0-autotune-recommends-report $directory
# Run the Autotune Recommends Report
oref0-autotune-recommends-report $directory
# Go ahead and echo autotune_recommendations.log to the terminal, minus blank lines
cat $report_file | egrep -v "\| *\| *$"
fi
# Go ahead and echo autotune_recommendations.log to the terminal, minus blank lines
cat $report_file | egrep -v "\| *\| *$"
fi
done # End Date Range Iteration
@@ -17,7 +17,7 @@
var basal = require('oref0/lib/profile/basal');
var get_iob = require('oref0/lib/iob');
var detect = require('oref0/lib/determine-basal/cob-autosens');
var detect = require('oref0/lib/determine-basal/autosens');
if (!module.parent) {
var detectsensitivity = init();
@@ -27,9 +27,10 @@ if (!module.parent) {
var isf_input = process.argv.slice(4, 5).pop()
var basalprofile_input = process.argv.slice(5, 6).pop()
var profile_input = process.argv.slice(6, 7).pop();
var carb_input = process.argv.slice(7, 8).pop()
if (!glucose_input || !pumphistory_input || !profile_input) {
console.error('usage: ', process.argv.slice(0, 2), '<glucose.json> <pumphistory.json> <insulin_sensitivities.json> <basal_profile.json> <profile.json>');
console.error('usage: ', process.argv.slice(0, 2), '<glucose.json> <pumphistory.json> <insulin_sensitivities.json> <basal_profile.json> <profile.json> [carbhistory.json]');
process.exit(1);
}
@@ -62,6 +63,15 @@ if (!module.parent) {
}
var basalprofile = require(cwd + '/' + basalprofile_input);
var carb_data = { };
if (typeof carb_input != 'undefined') {
try {
carb_data = JSON.parse(fs.readFileSync(carb_input, 'utf8'));
} catch (e) {
console.error("Warning: could not parse "+carb_input);
}
}
var iob_inputs = {
history: pumphistory_data
, profile: profile
@@ -73,13 +83,29 @@ if (!module.parent) {
var detection_inputs = {
iob_inputs: iob_inputs
, glucose_data: glucose_data
, basalprofile: basalprofile
//, clock: clock_data
, carbs: carb_data
, glucose_data: glucose_data
, basalprofile: basalprofile
//, clock: clock_data
};
// calculate sensitivity using 8h of non-exluded data
detection_inputs.deviations = 96;
detect(detection_inputs);
ratio8h = ratio;
newisf8h = newisf;
// calculate sensitivity using all non-exluded data (up to 24h)
detection_inputs.deviations = 288;
detect(detection_inputs);
ratio24h = ratio;
newisf24h = newisf;
if ( ratio8h < ratio24h ) {
console.error("Using 8h autosens ratio of",ratio8h,"(ISF",newisf8h+")");
} else {
console.error("Using 24h autosens ratio of",ratio24h,"(ISF",newisf24h+")");
}
var lowestRatio = Math.min(ratio8h, ratio24h);
var sensAdj = {
"ratio": ratio
"ratio": lowestRatio
}
return console.log(JSON.stringify(sensAdj));
View
@@ -99,7 +99,7 @@ if (!module.parent) {
, glucose: glucose_data
};
var dia_carbs = generate(inputs);
console.log(JSON.stringify(dia_carbs));
var recentCarbs = generate(inputs);
console.log(JSON.stringify(recentCarbs));
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.