from __future__ import print_function # Python version of oref0-autotune.sh # Original bash code: scottleibrand, pietergit, beached, danamlewis # This script sets up an easy test environment for autotune, allowing the user to vary parameters # like start/end date and number of runs. # # Required Inputs: # DIR, (--dir=) # NIGHTSCOUT_HOST, (--ns-host=) # Optional Inputs: # END_DATE, (--end-date=) # if no end date supplied, assume we want a months worth or until day before current day # NUMBER_OF_RUNS (--runs=) # if no number of runs designated, then default to 5 # EXPORT_EXCEL (--xlsx=) # export to excel. Disabled by default # TERMINAL_LOGGING (--log # logs terminal output to autotune..log in the autotune directory, default to true import argparse import requests import datetime import os, errno import logging import pytz from subprocess import call import shutil import six def get_input_arguments(): parser = argparse.ArgumentParser(description='Autotune') # Required # NOTE: As the code runs right now, this directory needs to exist and as well as the subfolders: autotune, settings parser.add_argument('--dir', '-d', type=str, required=True, help='(--dir=)') parser.add_argument('--ns-host', '-n', type=str, required=True, metavar='NIGHTSCOUT_HOST', help='(--ns-host= ), output prepped glucose # data or below # oref0-autotune-prep ns-treatments.json profile.json ns-entries.$DATE.json > autotune.$RUN_NUMBER.$DATE.json ns_treatments = os.path.join(autotune_directory, 'ns-treatments.json') profile = os.path.join(autotune_directory, 'profile.json') pump_profile = os.path.join(autotune_directory, "pumpprofile.json") ns_entries = os.path.join(autotune_directory, 'ns-entries.{date}.json'.format(date=date.strftime("%Y-%m-%d"))) # autotune.$RUN_NUMBER.$DATE.json autotune_run_filename = os.path.join(autotune_directory, 'autotune.{run_number}.{date}.json' .format(run_number=run_number, date=date.strftime("%Y-%m-%d"))) autotune_prep = 'oref0-autotune-prep {ns_treatments} {profile} {ns_entries} {pump_profile} --output-file {autotune_run_filename}'.format(ns_treatments=ns_treatments, profile=profile, ns_entries=ns_entries, pump_profile=pump_profile, autotune_run_filename=autotune_run_filename) logging.info('Running {script}'.format(script=autotune_prep)) call(autotune_prep, stdout=FNULL, shell=True) logging.info('Writing output to {filename}'.format(filename=autotune_run_filename)) # Autotune (required args, ), # output autotuned profile or what will be used as in the next iteration # oref0-autotune-core autotune.$RUN_NUMBER.$DATE.json profile.json profile.pump.json > newprofile.$RUN_NUMBER.$DATE.json # oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json > newprofile.$RUN_NUMBER.$DATE.json profile_pump = os.path.join(autotune_directory, 'profile.pump.json') # newprofile.$RUN_NUMBER.$DATE.json newprofile_run_filename = os.path.join(autotune_directory, 'newprofile.{run_number}.{date}.json' .format(run_number=run_number, date=date.strftime("%Y-%m-%d"))) autotune_core = 'oref0-autotune-core {autotune_run} {profile} {profile_pump} --output-file {newprofile_run_filename}'.format(profile=profile, profile_pump = profile_pump, autotune_run=autotune_run_filename, newprofile_run_filename=newprofile_run_filename) logging.info('Running {script}'.format(script=autotune_core)) call(autotune_core, stdout=FNULL, shell=True) logging.info('Writing output to {filename}'.format(filename=newprofile_run_filename)) # Copy tuned profile produced by autotune to profile.json for use with next day of data # cp newprofile.$RUN_NUMBER.$DATE.json profile.json shutil.copy(os.path.join(autotune_directory, 'newprofile.{run_number}.{date}.json'.format(run_number=run_number, date=date.strftime("%Y-%m-%d"))), os.path.join(autotune_directory, 'profile.json')) def export_to_excel(output_directory, output_excel_filename): autotune_export_to_xlsx = 'oref0-autotune-export-to-xlsx --dir {0} --output {1}'.format(output_directory, output_excel_filename) call(autotune_export_to_xlsx, shell=True) def create_summary_report_and_display_results(output_directory): print() print("Autotune pump profile recommendations:") print("---------------------------------------------------------") report_file = os.path.join(output_directory, 'autotune', 'autotune_recommendations.log') autotune_recommends_report = 'oref0-autotune-recommends-report {0}'.format(output_directory) call(autotune_recommends_report, shell=True) print("Recommendations Log File: {0}".format(report_file)) # Go ahead and echo autotune_recommendations.log to the terminal, minus blank lines # cat $report_file | egrep -v "\| *\| *$" call(['cat {0} | egrep -v "\| *\| *$"'.format(report_file)], shell=True) if __name__ == "__main__": # Set log level for this app to DEBUG. logging.basicConfig(level=logging.DEBUG) # Supress non-essential logs (below WARNING) from requests module. logging.getLogger("requests").setLevel(logging.WARNING) args = get_input_arguments() directory, nightscout_host, start_date, end_date, number_of_runs, export_excel, recommends_report = assign_args_to_variables(args) # TODO: Convert Nightscout profile to OpenAPS profile format. #get_nightscout_profile(NIGHTSCOUT_HOST, DIR) get_openaps_profile(directory) get_nightscout_carb_and_insulin_treatments(nightscout_host, start_date, end_date, directory) get_nightscout_bg_entries(nightscout_host, start_date, end_date, directory) run_autotune(start_date, end_date, number_of_runs, directory) if export_excel: export_to_excel(directory, export_excel) if recommends_report: create_summary_report_and_display_results(directory)