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 run_sim_once(simtime, meals, controller, patients): # times run_time = timedelta(hours=simtime) start_time = datetime(2020, 1, 1, 0,0,0) scenario = CustomScenario(start_time = start_time, scenario=meals) envs = build_envs(scenario, start_time, patients) # must deepcopy controllers because they're dynamic controllers = [copy.deepcopy(controller) for _ in range(len(envs))] sim_instances = [SimObj(env, ctr, run_time, animate=False, path='./results') for (env, ctr) in zip(envs, controllers)] # run simulations results = batch_sim(sim_instances, parallel=False) # create dataframe with results from 1 sim return pd.concat(results, keys=[s.env.patient.name for s in sim_instances])
def simulate(sim_time=None, scenario=None, controller=None, start_time=None, save_path=None, animate=None, parallel=None): ''' Main user interface. ---- Inputs: sim_time - a datetime.timedelta object specifying the simulation time. scenario - a simglucose.scenario.Scenario object. Use simglucose.scenario_gen.RandomScenario or simglucose.scenario.CustomScenario to create a scenario object. controller - a simglucose.controller.Controller object. start_time - a datetime.datetime object specifying the simulation start time. save_path - a string representing the directory to save simulation results. animate - switch for animation. True/False. parallel - switch for parallel computing. True/False. ''' if animate is None: while True: select = input('Show animation? (y/n) ') if select == 'y': animate = True break elif select == 'n': animate = False break else: continue if parallel is None: while True: select = input('Use multiple processes? (y/n) ') if select == 'y': parallel = True break elif select == 'n': parallel = False break else: continue if platform.system() == 'Darwin': if animate is True and parallel is True: raise ValueError( """animate and parallel cannot be turned on at the same time in macOS.""" ) if save_path is None: save_path = pick_save_path() sim_instances = create_sim_instance(sim_time=sim_time, scenario=scenario, controller=controller, start_time=start_time, save_path=save_path, animate=animate) results = batch_sim(sim_instances, parallel=parallel) df = pd.concat(results, keys=[s.env.patient.name for s in sim_instances]) results, ri_per_hour, zone_stats, figs, axes = report(df, save_path) return 0
def simulate(sim_time=None, scenario=None, controller=None, patient_names=[], cgm_name=None, cgm_seed=None, insulin_pump_name=None, start_time=None, save_path=None, animate=None, parallel=None): ''' Main user interface. ---- Inputs: sim_time - a datetime.timedelta object specifying the simulation time. scenario - a simglucose.scenario.Scenario object. Use simglucose.scenario_gen.RandomScenario or simglucose.scenario.CustomScenario to create a scenario object. controller - a simglucose.controller.Controller object. start_time - a datetime.datetime object specifying the simulation start time. save_path - a string representing the directory to save simulation results. animate - switch for animation. True/False. parallel - switch for parallel computing. True/False. ''' if animate is None: animate = pick_animate() if parallel is None: parallel = pick_parallel() if platform.system() == 'Darwin' and (animate and parallel): raise ValueError( """animate and parallel cannot be turned on at the same time in macOS.""" ) if save_path is None: save_path = pick_save_path() if sim_time is None: sim_time = timedelta( hours=float(input('Input simulation time (hr): '))) if scenario is None: scenario = pick_scenario(start_time=start_time) if not patient_names: patient_names = pick_patients() if cgm_name is None: cgm_name = pick_cgm_sensor() if cgm_seed is None: cgm_seed = pick_cgm_seed() if insulin_pump_name is None: insulin_pump_name = pick_insulin_pump() if controller is None: controller = pick_controller() def local_build_env(pname): patient = T1DPatient.withName(pname) cgm_sensor = CGMSensor.withName(cgm_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 envs = [local_build_env(p) for p in patient_names] ctrllers = [copy.deepcopy(controller) for _ in range(len(envs))] sim_instances = [ SimObj(e, c, sim_time, animate=animate, path=save_path) for (e, c) in zip(envs, ctrllers) ] results = batch_sim(sim_instances, parallel=parallel) df = pd.concat(results, keys=[s.env.patient.name for s in sim_instances]) results, ri_per_hour, zone_stats, figs, axes = report(df, save_path) return results
print(results1) # --------- 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=1), animate=False, path=path) results2 = sim(s2) print(results2) # --------- batch simulation -------------- # Re-initialize simulation objects s1.reset() s2.reset() # create a list of SimObj, and call batch_sim s = [s1, s2] results = batch_sim(s, parallel=True) print(results)
results1 = sim(s1) print(results1) # --------- Create Custom Scenario -------------- # Create a simulation environment patient = T1DPatient.withName('adolescent#001') sensor = CGMSensor.withName('Navigator', 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=1), animate=False, path=path) results2 = sim(s2) print(results2) # --------- batch simulation -------------- # Re-initialize simulation objects s1.reset() s2.reset() # create a list of SimObj, and call batch_sim s = [s1, s2] results = batch_sim(s, parallel=False) print(results)