diff --git a/examples/run_gymnasium.py b/examples/run_gymnasium.py index c31e9e38..03530ead 100644 --- a/examples/run_gymnasium.py +++ b/examples/run_gymnasium.py @@ -4,7 +4,7 @@ register( id="simglucose/adolescent2-v0", entry_point="simglucose.envs:T1DSimGymnaisumEnv", - max_episode_steps=10, + max_episode_steps=1000, kwargs={"patient_name": "adolescent#002"}, ) diff --git a/setup.py b/setup.py index 9939c187..e44e3db4 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="simglucose", - version="0.2.9", + version="0.2.10", description="A Type-1 Diabetes Simulator as a Reinforcement Learning Environment in OpenAI gym or rllab (python implementation of UVa/Padova Simulator)", url="https://github.com/jxx123/simglucose", author="Jinyu Xie", diff --git a/simglucose/__init__.py b/simglucose/__init__.py index ea4ffe53..e69de29b 100644 --- a/simglucose/__init__.py +++ b/simglucose/__init__.py @@ -1,6 +0,0 @@ -from gym.envs.registration import register - -register( - id='simglucose-v0', - entry_point='simglucose.envs:T1DSimEnv', -) diff --git a/simglucose/envs/simglucose_gym_env.py b/simglucose/envs/simglucose_gym_env.py index aab5e394..0bc8ca4f 100644 --- a/simglucose/envs/simglucose_gym_env.py +++ b/simglucose/envs/simglucose_gym_env.py @@ -12,10 +12,8 @@ from datetime import datetime import gymnasium - PATIENT_PARA_FILE = pkg_resources.resource_filename( - "simglucose", "params/vpatient_params.csv" -) + "simglucose", "params/vpatient_params.csv") class T1DSimEnv(gym.Env): @@ -28,9 +26,11 @@ class T1DSimEnv(gym.Env): SENSOR_HARDWARE = "Dexcom" INSULIN_PUMP_HARDWARE = "Insulet" - def __init__( - self, patient_name=None, custom_scenario=None, reward_fun=None, seed=None - ): + def __init__(self, + patient_name=None, + custom_scenario=None, + reward_fun=None, + seed=None): """ patient_name must be 'adolescent#001' to 'adolescent#010', or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' @@ -79,20 +79,19 @@ def _create_env(self): if isinstance(self.patient_name, list): patient_name = self.np_random.choice(self.patient_name) - patient = T1DPatient.withName(patient_name, random_init_bg=True, seed=seed4) + patient = T1DPatient.withName(patient_name, + random_init_bg=True, + seed=seed4) else: - patient = T1DPatient.withName( - self.patient_name, random_init_bg=True, seed=seed4 - ) + patient = T1DPatient.withName(self.patient_name, + random_init_bg=True, + seed=seed4) if isinstance(self.custom_scenario, list): scenario = self.np_random.choice(self.custom_scenario) else: - scenario = ( - RandomScenario(start_time=start_time, seed=seed3) - if self.custom_scenario is None - else self.custom_scenario - ) + scenario = (RandomScenario(start_time=start_time, seed=seed3) if + self.custom_scenario is None else self.custom_scenario) sensor = CGMSensor.withName(self.SENSOR_HARDWARE, seed=seed2) pump = InsulinPump.withName(self.INSULIN_PUMP_HARDWARE) @@ -109,11 +108,11 @@ def _close(self): @property def action_space(self): ub = self.env.pump._params["max_basal"] - return spaces.Box(low=0, high=ub, shape=(1,)) + return spaces.Box(low=0, high=ub, shape=(1, )) @property def observation_space(self): - return spaces.Box(low=0, high=1000, shape=(1,)) + return spaces.Box(low=0, high=1000, shape=(1, )) @property def max_basal(self): @@ -121,7 +120,7 @@ def max_basal(self): class T1DSimGymnaisumEnv(gymnasium.Env): - metadata = {"render_modes": ["human"]} + metadata = {"render_modes": ["human"], "render_fps": 60} MAX_BG = 1000 def __init__( @@ -140,12 +139,14 @@ def __init__( reward_fun=reward_fun, seed=seed, ) - self.observation_space = gymnasium.spaces.Box( - low=0, high=self.MAX_BG, shape=(1,), dtype=np.float32 - ) - self.action_space = gymnasium.spaces.Box( - low=0, high=self.env.max_basal, shape=(1,), dtype=np.float32 - ) + self.observation_space = gymnasium.spaces.Box(low=0, + high=self.MAX_BG, + shape=(1, ), + dtype=np.float32) + self.action_space = gymnasium.spaces.Box(low=0, + high=self.env.max_basal, + shape=(1, ), + dtype=np.float32) def step(self, action): obs, reward, done, info = self.env.step(action) @@ -159,7 +160,8 @@ def step(self, action): # ) # Once the max_episode_steps is set, the truncated value will be overridden. truncated = False - return np.array([obs.CGM], dtype=np.float32), reward, done, truncated, info + return np.array([obs.CGM], + dtype=np.float32), reward, done, truncated, info def reset(self, seed=None, options=None): super().reset(seed=seed) diff --git a/simglucose/patient/t1dpatient.py b/simglucose/patient/t1dpatient.py index 5c79506f..0cac02c5 100644 --- a/simglucose/patient/t1dpatient.py +++ b/simglucose/patient/t1dpatient.py @@ -246,7 +246,7 @@ def reset(self): Reset the patient state to default intial state ''' if self._init_state is None: - self.init_state = self._params.iloc[2:15] + self.init_state = np.copy(self._params.iloc[2:15].values) else: self.init_state = self._init_state