def test_batch_sim(self): # specify start_time as the beginning of today now = datetime.now() start_time = datetime.combine(now.date(), datetime.min.time()) # --------- Create Random Scenario -------------- # Create a simulation environment patient = T1DPatient.withName('adolescent#001') sensor = CGMSensor.withName('Dexcom', seed=1) pump = InsulinPump.withName('Insulet') scenario = RandomScenario(start_time=start_time, seed=1) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = BBController() # Put them together to create a simulation object s1 = SimObj(env, controller, timedelta( days=2), animate=True, path=save_folder) results1 = sim(s1) # --------- Create Custom Scenario -------------- # Create a simulation environment patient = T1DPatient.withName('adolescent#001') sensor = CGMSensor.withName('Dexcom', seed=1) pump = InsulinPump.withName('Insulet') # custom scenario is a list of tuples (time, meal_size) scen = [(7, 45), (12, 70), (16, 15), (18, 80), (23, 10)] scenario = CustomScenario(start_time=start_time, scenario=scen) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = BBController() # Put them together to create a simulation object s2 = SimObj(env, controller, timedelta( days=2), animate=False, path=save_folder) results2 = sim(s2) # --------- batch simulation -------------- s1.reset() s2.reset() s1.animate = False s = [s1, s2] results_para = batch_sim(s, parallel=True) s1.reset() s2.reset() s = [s1, s2] results_serial = batch_sim(s, parallel=False) assert_frame_equal(results_para[0], results1) assert_frame_equal(results_para[1], results2) for r1, r2 in zip(results_para, results_serial): assert_frame_equal(r1, r2)
def build_env(pname): patient = T1DPatient.withName(pname) sensor = CGMSensor.withName('Dexcom') pump = InsulinPump.withName('Insulet') copied_scenario = copy.deepcopy(scenario) env = T1DSimEnv(patient, sensor, pump, copied_scenario) return env
def local_build_env(pname): patient = T1DPatient.withName(pname) cgm_sensor = CGMSensor.withName(cgm_sensor_name, seed=cgm_seed) insulin_pump = InsulinPump.withName(insulin_pump_name) scen = copy.deepcopy(scenario) env = T1DSimEnv(patient, cgm_sensor, insulin_pump, scen) return env
def __init__(self, patient_name=None, speed=0, oracle=-1, reward_fun=neg_risk, seed=0): ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' self._gym_disable_underscore_compat = True seeds = [0, 0, 0, 0, 0] #self._seed() # These two patients had the most different values for optimal(?) CR and CF. # We simulate a fictitious person whose body values oscillate between these two patient's body values # This ensures the algorithm need to adapt significantly. patient_name_a = 'adolescent#003' patient_name_b = 'adolescent#008' patient_a = T1DPatient.withName(patient_name_a) patient_b = T1DPatient.withName(patient_name_b) # sensor = CGMSensor.withName('Navigator', seed=seeds[1]) # Sample frequency = 1 min sensor = CGMSensor.withName('Dexcom', seed=seed)# seed=seeds[1]) # Sample frequency = 3 min # sensor = CGMSensor.withName('GuardianRT', seed=seeds[1]) # Sample frequency = 5 min pump = InsulinPump.withName('Insulet') hour = 0 #self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) scenario = RandomScenario(start_time=start_time, seed=seed)#, seed=seeds[2]) # scenario = WeightScenario(weight=patient._params.BW, start_time=start_time, seed=seeds[2]) self.env = _NS_T1DSimEnv(patient_a, patient_b, sensor, pump, scenario, speed, oracle) self.reward_fun = reward_fun self.target = 140 self.max_horizon = 1 # Sampling in policy parameter space, not in action space of env. self.oracle = oracle
def test_results_consistency(self): # Test data results_exp = pd.read_csv(TESTDATA_FILENAME, index_col=0) results_exp.index = pd.to_datetime(results_exp.index) # specify start_time as the beginning of today start_time = datetime(2018, 1, 1, 0, 0, 0) # --------- Create Random Scenario -------------- # Create a simulation environment patient = T1DPatient.withName('adolescent#001') sensor = CGMSensor.withName('Dexcom', seed=1) pump = InsulinPump.withName('Insulet') scenario = RandomScenario(start_time=start_time, seed=1) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = BBController() # Put them together to create a simulation object s = SimObj(env, controller, timedelta(days=2), animate=False, path=save_folder) results = sim(s) assert_frame_equal(results, results_exp)
def run_sim_PID_once(pname, runtime, meals, controller_params): ''' Run the simulation a single time on a single patient with the PID controller. Parameters ---------- pname: str patient name runtime: int simulation time, in hours. meals: (timedelta, int) a tuple containing the time of meal (as referenced from simulation start) and the meal size, in grams. targetBG: int the target blood glucose for the controller, in mg/dl lowBG: int the pump suspension glucose for the controller, in mg/dl Returns ------- A pandas dataframe containing the simulation results. axis=0: time, type datetime.datetime axis=1: data category, type str ''' sensor = CGMSensor.withName('Dexcom') pump = InsulinPump.withName('Insulet') scenario = CustomScenario(start_time = datetime(2020, 1, 1, 0,0,0), scenario=meals) obj = SimObj(T1DSimEnv(T1DPatient.withName(pname), sensor, pump, scenario), controller.PIDController(controller_params, pname), timedelta(hours=runtime), animate=False, path=None) return sim(obj)
def _create_env_from_random_state(self): # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(self.np_random.randint(0, 1000)) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 seed4 = seeding.hash_seed(seed3 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) if self.saved_state is not None: seed2=self.saved_state[0] seed3=self.saved_state[1] seed4=self.saved_state[2] hour=self.saved_state[3] print('Using state', seed2, seed3, seed4, hour) start_time = datetime(2021, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name, random_init_bg=True, seed=seed4) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) if self.harrison_benedict: vpatient_params = pd.read_csv(PATIENT_PARA_FILE) self.kind = self.patient_name.split('#')[0] self.bw = vpatient_params.query('Name=="{}"'.format(self.patient_name))['BW'].item() scenario=RandomBalancedScenario(start_time=start_time, seed=seed3, harrison_benedict=self.harrison_benedict, bw=self.bw, kind=self.kind) else: scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) env = _T1DSimEnv(patient, sensor, pump, scenario,noise=self.noise) return env, seed2, seed3, seed4, hour
def __init__(self, patient_name=None, speed=0, oracle=-1, reward_fun=neg_risk, seed=0): ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' self._gym_disable_underscore_compat = True seeds = [0, 0, 0, 0, 0] #self._seed() patient_name_a = 'adolescent#003' patient_name_b = 'adolescent#008' patient_a = T1DPatient.withName(patient_name_a) patient_b = T1DPatient.withName(patient_name_b) # sensor = CGMSensor.withName('Navigator', seed=seeds[1]) # Sample frequency = 1 min sensor = CGMSensor.withName('Dexcom', seed=seed)# seed=seeds[1]) # Sample frequency = 3 min # sensor = CGMSensor.withName('GuardianRT', seed=seeds[1]) # Sample frequency = 5 min pump = InsulinPump.withName('Insulet') hour = 0 #self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) scenario = RandomScenario(start_time=start_time, seed=seed)#, seed=seeds[2]) # scenario = WeightScenario(weight=patient._params.BW, start_time=start_time, seed=seeds[2]) self.env = _NS_T1DSimEnv(patient_a, patient_b, sensor, pump, scenario, speed, oracle) self.reward_fun = reward_fun self.target = 140 self.max_horizon = 1 # Sampling in policy parameter space, not in action space of env. self.oracle = oracle
def __init__(self, patient_name=None, custom_scenario=None, reward_fun=None, animate=False, controller_name='', results_path=None): ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' seeds = self._seed() # have to hard code the patient_name, gym has some interesting # error when choosing the patient if patient_name is None: patient_name = 'adolescent#001' patient = T1DPatient.withName(patient_name) sensor = CGMSensor.withName('Dexcom', seed=seeds[1]) hour = self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) if custom_scenario is None: scenario = RandomScenario(start_time=start_time, seed=seeds[2]) scenario = custom_scenario pump = InsulinPump.withName('Insulet') self.env = _T1DSimEnv(patient, sensor, pump, scenario, controller_name=controller_name, results_path=results_path) self.reward_fun = reward_fun self.animate = animate
def __init__(self, patient_name=None, reward_fun=None): ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' seeds = self._seed() # have to hard code the patient_name, gym has some interesting # error when choosing the patient if patient_name is None: patient_name = 'adolescent#001' patient = T1DPatient.withName(patient_name) sensor = CGMSensor.withName('Dexcom', seed=seeds[1]) hour = self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) # scenario = RandomScenario(start_time=start_time, seed=seeds[2]) # Added a custom scenario with no meals # scen = [(7, 45), (12, 70), (16, 15), (18, 80), (23, 10)] scen = [(0, 0)] scenario = CustomScenario(start_time=start_time, scenario=scen) pump = InsulinPump.withName('Insulet') self.env = _T1DSimEnv(patient, sensor, pump, scenario) self.reward_fun = reward_fun # Added by Jonas -- state space is now 10 * sample_time = 30 minutes long self.state_space_length = 10 self.insulin_history = np.zeros(4)
def run_sim_PID(no_runs, patients, runtime, meals, controller_params): ''' Run the simulation a single time on a list of patients with the PID controller. Parameters ---------- no_runs: int the number of separate simulation runs. patients: list of str a list of patient name strings. Patient name strings can be found in the params/Quest.csv file inside simGlucose. runtime: int simulation time, in hours. meals: (timedelta, int) a tuple containing the time of meal (as referenced from simulation start) and the meal size, in grams. targetBG: int the target blood glucose for the controller, in mg/dl lowBG: int the pump suspension glucose for the controller, in mg/dl Returns ------- A pandas dataframe containing the simulation results. axis=0: time, type datetime.datetime axis=1: MultiIndex level 0: data category, type str level 1: patient id, type str level 2: run number, type int (starts at 1) ''' sensor = CGMSensor.withName('Dexcom') pump = InsulinPump.withName('Insulet') scenario = CustomScenario(start_time = datetime(2020, 1, 1, 0,0,0), scenario=meals) sim_objs = [] keys = [] for run in range(0, no_runs): for pname in patients: sim_objs.append(SimObj(T1DSimEnv(T1DPatient.withName(pname), sensor, pump, copy.deepcopy(scenario)), # because random numbers. controller.PIDController(controller_params, pname), timedelta(hours=runtime), animate=False, path=None)) keys.append((run + 1, pname)) p_start = time.time() print('Running batch simulation of {} items...'.format(len(patients * no_runs))) p = pathos.pools.ProcessPool() results = p.map(sim, sim_objs) print('Simulation took {} seconds.'.format(time.time() - p_start)) return pd.concat(results, axis=1, keys=keys)
def _create_env_from_random_state(self): # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(self.np_random.randint(0, 1000)) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 seed4 = seeding.hash_seed(seed3 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name, random_init_bg=True, seed=seed4) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) env = _T1DSimEnv(patient, sensor, pump, scenario) return env, seed2, seed3, seed4
def pidsim(): for idx,patient in enumerate(patients): patient = T1DPatient.withName(patient) sensor = CGMSensor.withName('Dexcom', seed=1) pump = InsulinPump.withName('Insulet') p,i,d = pidparams[idx] for seed in range (10,20): scenario = RandomScenario(start_time=start_time, seed=randint(10, 99999)) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = FoxPIDController(112.517,kp=p, ki=i, kd=d) # Put them together to create a simulation object s1 = SimObj(env, controller, timedelta(days=10), animate=False, path=path+str(seed)) results1 = sim(s1) print('Complete:',patient.name,'-',seed) print('All done!')
def seed(self, seed=None): print('_seed called') self.np_random, seed1 = seeding.np_random(seed=seed) # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(seed1 + 1) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2021, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) self.env = _T1DSimEnv(patient, sensor, pump, scenario) return [seed1, seed2, seed3]
def __init__(self, patient_name=None, reward_fun=None): ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' seeds = [0, 0, 0, 0, 0] #self._seed() # have to hard code the patient_name, gym has some interesting # error when choosing the patient if patient_name is None: patient_name = 'adolescent#001' print(patient_name) patient = T1DPatient.withName(patient_name) sensor = CGMSensor.withName('Dexcom', seed=seeds[1]) # sensor = CGMSensor.withName('Navigator', seed=seeds[1]) # sensor = CGMSensor.withName('GuardianRT', seed=seeds[1]) hour = 0 #self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) scenario = RandomScenario(start_time=start_time, seed=seeds[2]) pump = InsulinPump.withName('Insulet') self.env = _T1DSimEnv(patient, sensor, pump, scenario) self.reward_fun = reward_fun
def bbsim(): for patient in patients: patient = T1DPatient.withName(patient) sensor = CGMSensor.withName('Dexcom', seed=1) pump = InsulinPump.withName('Insulet') for seed in range(10, 20): scenario = RandomScenario(start_time=start_time, seed=randint(10, 99999)) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = BBController() # Put them together to create a simulation object s1 = SimObj(env, controller, timedelta(days=10), animate=False, path=path + str(seed)) results1 = sim(s1) print('Complete:', patient.name, '-', seed) print('All done!')
def __init__(self, patient_name=None, reward_fun=None, scen=None, start_time=None, sensor_name=None, pump_name=None): #, scenario_tup=None, startTime=None ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' seeds = self._seed() # have to hard code the patient_name, gym has some interesting # error when choosing the patient if patient_name is None: patient_name = 'adolescent#001' if sensor_name is None: sensor_name = 'Dexcom' if pump_name is None: pump_name = 'Insulet' print(patient_name) print(sensor_name) print(pump_name) print(scen) patient = T1DPatient.withName(patient_name) sensor = CGMSensor.withName(sensor_name, seed=seeds[1]) #hour = self.np_random.randint(low=0.0, high=24.0) #start_time = datetime(2018, 1, 1, hour, 0, 0) scenario = CustomScenario( start_time=start_time, scenario=scen ) #RandomScenario(start_time=start_time, seed=seeds[2]) pump = InsulinPump.withName(pump_name) self.env = _T1DSimEnv(patient, sensor, pump, scenario) self.reward_fun = reward_fun
def __init__(self, patient_name=None, reward_fun=None, Initial_Bg=0): ''' patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' ''' seeds = self._seed() # have to hard code the patient_name, gym has some interesting # error when choosing the patient if patient_name is None: patient_name = 'adolescent#001' patient = T1DPatient.withName(patient_name, Initial_Bg) sensor = CGMSensor.withName('GuardianRT', seed=seeds[1]) #Dexcom hour = 20 #self.np_random.randint(low=0.0, high=24.0) start_time = datetime(2018, 1, 1, hour, 0, 0) # scenario = RandomScenario(start_time=start_time, seed=seeds[2]) # custom scenario is a list of tuples (time, meal_size) # scen = [(0,float(Initial_Bg)),(13, 45), (16, 10), (18, 35), (22, 10)]#, (23, 10)] scen = [(13, 45), (16, 10), (18, 35), (22, 10)] #, (23, 10)] scenario = CustomScenario(start_time=start_time, scenario=scen) pump = InsulinPump.withName('Insulet') self.env = _T1DSimEnv(patient, sensor, pump, scenario) self.reward_fun = reward_fun
def _create_env_from_random_state(self): #print('_create_env_from_random_state simglucose extended observation') # Derive a random seed. This gets passed as a uint, but gets # checked as an int elsewhere, so we need to keep it below # 2**31. seed2 = seeding.hash_seed(self.np_random.randint(0, 1000)) % 2**31 seed3 = seeding.hash_seed(seed2 + 1) % 2**31 seed4 = seeding.hash_seed(seed3 + 1) % 2**31 hour = self.np_random.randint(low=0.0, high=24.0) if self.saved_state is not None: seed2=self.saved_state[0] seed3=self.saved_state[1] seed4=self.saved_state[2] hour=self.saved_state[3] print('Using state', seed2, seed3, seed4, hour) start_time = datetime(2021, 1, 1, hour, 0, 0) patient = T1DPatient.withName(self.patient_name, random_init_bg=True, seed=seed4) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) scenario = RandomScenario(start_time=start_time, seed=seed3) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) env = _T1DSimEnvExtendedObs(patient, sensor, pump, scenario,n_samples=self._n_samples, append_action=self.append_action ) self.time_per_step=int(sensor.sample_time) self.time_in_env=0 return env, seed2, seed3, seed4, hour
ki = float(.00000) while ki <= 0.00001: ki +=.000001 print('kp=' + str(ki)) # (kp, ki, kd, target, low) PIDparams = (0.00025, round(ki, 7), 0, 120, 70) dfs = run_sim_PID(n, adults, t, meals, PIDparams) filename = 'dfs/' + 'p_' + str(PIDparams[0]) + ' i_' + str(PIDparams[1]) + ' d_' + str(PIDparams[2]) + ' target_' + str(PIDparams[3]) + '.bz2' dfs.to_pickle(filename) ======= pname = "adult#001" t = 9 meals = [(timedelta(hours=2), 50)] sensor = CGMSensor.withName('Dexcom') pump = InsulinPump.withName('Insulet') scenario = CustomScenario(start_time = datetime(2020, 1, 1, 0,0,0), scenario=meals) keys = [] # forward horizon horizon = 50 controller_params = (140, 80, horizon) obj= SimObj(T1DSimEnv(T1DPatient.withName(pname), sensor, pump, copy.deepcopy(scenario)), # because random numbers. controller.MPCNaive(controller_params, pname), timedelta(hours=t), animate=False, path=None) keys.append((1, pname))
from simglucose.simulation.scenario import CustomScenario from simglucose.simulation.sim_engine import SimObj, sim, batch_sim from datetime import timedelta from datetime import datetime # specify start_time as the beginning of today now = datetime.now() start_time = datetime.combine(now.date(), datetime.min.time()) # --------- Create Random Scenario -------------- # Specify results saving path path = './results' # Create a simulation environment patient = T1DPatient.withName('adolescent#001') sensor = CGMSensor.withName('Dexcom', seed=1) pump = InsulinPump.withName('Insulet') scenario = RandomScenario(start_time=start_time, seed=1) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = BBController() # Put them together to create a simulation object s1 = SimObj(env, controller, timedelta(days=1), animate=False, path=path) results1 = sim(s1) print(results1) # --------- Create Custom Scenario -------------- # Create a simulation environment patient = T1DPatient.withName('adolescent#001')
from simglucose.simulation.scenario import CustomScenario from simglucose.simulation.sim_engine import SimObj, sim, batch_sim from datetime import timedelta from datetime import datetime # specify start_time as the beginning of today now = datetime.now() start_time = datetime.combine(now.date(), datetime.min.time()) # --------- Create Random Scenario -------------- # Specify results saving path path = './results' # Create a simulation environment patient = T1DPatient.withName('adolescent#001') sensor = CGMSensor.withName('Navigator', seed=1) pump = InsulinPump.withName('Insulet') scenario = RandomScenario(start_time=start_time, seed=1) env = T1DSimEnv(patient, sensor, pump, scenario) # Create a controller controller = BBController() # Put them together to create a simulation object s1 = SimObj(env, controller, timedelta(days=1), animate=False, path=path) results1 = sim(s1) print(results1) # --------- Create Custom Scenario -------------- # Create a simulation environment patient = T1DPatient.withName('adolescent#001')