Skip to content

Commit

Permalink
Merge pull request #563 from PieterGit/201707_autotune2
Browse files Browse the repository at this point in the history
autotune fixes (e.g. support for tokenbased authentication for autotune)
  • Loading branch information
scottleibrand committed Jul 23, 2017
2 parents 80d8989 + 81fdd8a commit de73077
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
7 changes: 5 additions & 2 deletions bin/ns-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ $self host <NIGHTSCOUT_HOST|localhost:1337> <entries.json> [QUERY] [stdout|-]
EOF
}

CURL_AUTH=""

# use token authentication if the user has a token set in their API_SECRET environment variable
if [[ "${API_SECRET,,}" =~ "token=" ]]; then
if [[ -z ${QUERY} ]]; then
Expand All @@ -32,6 +34,7 @@ if [[ "${API_SECRET,,}" =~ "token=" ]]; then
fi
else
REPORT_ENDPOINT=$NIGHTSCOUT_HOST/api/v1/${REPORT}'?'${QUERY}
CURL_AUTH='-H "api-secret: ${API_SECRET}"'
fi

case $1 in
Expand All @@ -54,7 +57,7 @@ case $1 in
fi
test -z "$NIGHTSCOUT_HOST" && usage && exit 1;

curl ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT
curl ${CURL_AUTH} ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT

;;
type)
Expand All @@ -66,7 +69,7 @@ case $1 in
;;
*)
test -z "$NIGHTSCOUT_HOST" && usage && exit 1;
curl ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT
curl ${CURL_AUTH} ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT
;;
esac

Expand Down
25 changes: 16 additions & 9 deletions bin/oref0-autotune-export-to-xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def sortedFilenames():
parser = argparse.ArgumentParser(description='Export oref0 autotune files to Microsoft Excel')
parser.add_argument('-d', '--dir', help='autotune directory', default='.')
parser.add_argument('-o', '--output', help='default autotune.xlsx', default='autotune.xlsx')
parser.add_argument('--version', action='version', version='%(prog)s 0.0.3-dev')
parser.add_argument('--version', action='version', version='%(prog)s 0.0.4-dev')
args = parser.parse_args()

# change to openaps directory
Expand All @@ -172,13 +172,20 @@ def sortedFilenames():
f=open(filename, 'r')
print "Adding %s to Excel" % filename
j=json.load(f)
basalProfile=j['basalprofile']
isfProfile=j['isfProfile']['sensitivities']
expandedBasal=expandProfile(basalProfile, 'rate', 'minutes')
expandedIsf=expandProfile(isfProfile, 'sensitivity', 'offset')
write_timebased_profile(worksheetBasal, row, expandedBasal, excel_2decimals_format)
write_timebased_profile(worksheetIsf, row, expandedIsf, excel_integer_format)
write_profile(worksheetProfile, row, j, excel_integer_format)
row=row+1
try:
basalProfile=j['basalprofile']
isfProfile=j['isfProfile']['sensitivities']
expandedBasal=expandProfile(basalProfile, 'rate', 'minutes')
expandedIsf=expandProfile(isfProfile, 'sensitivity', 'offset')
write_timebased_profile(worksheetBasal, row, expandedBasal, excel_2decimals_format)
write_timebased_profile(worksheetIsf, row, expandedIsf, excel_integer_format)
write_profile(worksheetProfile, row, j, excel_integer_format)
row=row+1
except Exception as e:
if j.has_key('error'):
print "Skipping file. Error: %s " % j['error']
else:
print "Skipping file. Exception: %s" % e

workbook.close()
print "Written %d lines to Excel" % row
13 changes: 8 additions & 5 deletions bin/oref0-autotune-recommends-report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#
# Example usage: ~/src/oref0/bin/oref0-autotune-recommends-report.sh <OpenAPS Loop Directory Path>

# fix problems with locales with printf output
LC_NUMERIC=en_US.UTF-8

die() {
echo "$@"
exit 1
Expand Down Expand Up @@ -65,16 +68,16 @@ printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Parameter"
printf "%s\n" "-------------------------------------" >> $report_file

# Print ISF, CSF and Carb Ratio Recommendations
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "ISF" $isf_current $isf_new >> $report_file
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" $csf_current $csf_new >> $report_file
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" "n/a" $csf_new >> $report_file
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" $carb_ratio_current $carb_ratio_new >> $report_file
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "Carb Ratio [g]" $carb_ratio_current $carb_ratio_new >> $report_file

# Print Basal Profile Recommendations
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Basal Profile" "" "" >> $report_file
printf "%-${parameter_width}s| %-${data_width}s|\n" "Basal Profile [unit/hour]" "-" >> $report_file

# Build time_list array of H:M in 30 minute increments to mirror pump basal schedule
time_list=()
Expand Down
36 changes: 16 additions & 20 deletions bin/oref0-autotune.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ TERMINAL_LOGGING=true
RECOMMENDS_REPORT=true
UNKNOWN_OPTION=""

if [ -n "${API_SECRET_READ}" ]; then
HASHED_API_SECRET_READ=`echo -n ${API_SECRET_READ}|sha1sum|cut -f1 -d '-'|cut -f1 -d ' '`
if [ -n "${API_SECRET_READ}" ]; then
echo "WARNING: API_SECRET_READ is deprecated starting with oref 0.6.x. The Nightscout authentication information is now used from the API_SECRET environment variable"
fi

if [[ -z "$API_SECRET" ]]; then
echo "ERROR: 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
Expand Down Expand Up @@ -134,12 +139,12 @@ fi

# Get profile for testing copied to home directory. "openaps" is my loop directory name.
cd $directory && mkdir -p autotune
cp settings/pumpprofile.json autotune/profile.pump.json
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.
if [[ `uname` == 'Darwin' ]] ; then
cp settings/pumpprofile.json settings/profile.json
cp settings/pumpprofile.json settings/profile.json || die "Cannot copy settings/pumpprofile.json"
else
cp -up settings/pumpprofile.json settings/profile.json
cp -up settings/pumpprofile.json settings/profile.json || die "Cannot copy settings/pumpprofile.json"
fi
# If a previous valid settings/autotune.json exists, use that; otherwise start from settings/profile.json
cp settings/autotune.json autotune/profile.json && cat autotune/profile.json | json | grep -q start || cp autotune/profile.pump.json autotune/profile.json
Expand All @@ -156,13 +161,9 @@ fi
echo "Grabbing NIGHTSCOUT treatments.json for date range..."

# Get Nightscout carb and insulin Treatments
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
if [ -n "${HASHED_API_SECRET_READ}" ]; then
curl ${CURL_FLAGS} -H "api-secret: ${HASHED_API_SECRET_READ}" -s $url > ns-treatments.json || die "Couldn't download ns-treatments.json"
else
curl ${CURL_FLAGS} -s $url > ns-treatments.json || die "Couldn't download ns-treatments.json"
fi
query="find\[created_at\]\[\$gte\]=`date --date="$START_DATE -4 hours" -Iminutes`&find\[created_at\]\[\$lte\]=`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
Expand All @@ -183,14 +184,9 @@ echo "Grabbing NIGHTSCOUT entries/sgv.json for date range..."
# Get Nightscout BG (sgv.json) Entries
for i in "${date_list[@]}"
do
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
if [ -n "${HASHED_API_SECRET_READ}" ]; then
curl ${CURL_FLAGS} -H "api-secret: ${HASHED_API_SECRET_READ}" -s $url > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
else
curl ${CURL_FLAGS} -s $url > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
fi

query="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 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

Expand Down
1 change: 0 additions & 1 deletion bin/oref0-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
fi

# install decocare with setuptools since 0.0.31 (with the 6.4U/h fix) isn't published properly to pypi

sudo easy_install -U decocare || die "Can't easy_install decocare"

mkdir -p $HOME/src/
Expand Down

0 comments on commit de73077

Please sign in to comment.