jxx123 / simglucose

A Type-1 Diabetes simulator implemented in Python for Reinforcement Learning purpose
MIT License
232 stars 109 forks source link

Insulin unit error and scenario setting error #71

Open TomatoZ2 opened 9 months ago

TomatoZ2 commented 9 months ago

Insulin unit error

The upper limit of the action space has the wrong unit. According to your code, the insulin unit input by the simulator is U/min, but in fact the upper limit unit of your pump parameters is U/hour.

    def bolus(self, amount):
        bol = bol * self.U2PMOL  # convert from U/min to pmol/min
        bol = np.round(bol / self._params['inc_bolus']) * self._params['inc_bolus']
        bol = bol / self.U2PMOL  # convert from pmol/min to U/min
        bol = min(bol, self._params['max_bolus'])
        bol = max(bol, self._params['min_bolus'])
        return bol

The basal insulin injection rate is usually 1 U/hour, and the bolus is about 10 U/hour. Obviously pump parameters are U/hour, not U/min, which results in the upper limit of the action space being too large and the agent unable to converge. <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">

Name | min_bolus | max_bolus | inc_bolus | min_basal | max_basal | inc_basal | sample_time -- | -- | -- | -- | -- | -- | -- | -- Cozmo | 0 | 75 | 0.05 | 0 | 35 | 0.05 | 1 Insulet | 0 | 30 | 0.05 | 0 | 30 | 0.05 | 1

A simple scaling works.

    def bolus(self, amount):
        bol = amount / 60  # convert from U/hour to U/min
        bol = bol * self.U2PMOL  # convert from U/min to pmol/min
        bol = np.round(bol / self._params['inc_bolus']) * self._params['inc_bolus']
        bol = bol / self.U2PMOL  # convert from pmol/min to U/min
        bol = min(bol, self._params['max_bolus'])
        bol = max(bol, self._params['min_bolus'])
        return bol

Scenario setting error

I use gym to implement simglucose, but the CHO intake is inconsistent with my scenario settings.(Code was modified to customize the termination condition, just ignore it)

start_time = datetime(2018, 1, 1, 6, 0, 0)
scen = [(7, 60), (12, 59), (16, 74), (21, 39)]
scenario = CustomScenario(start_time=start_time, scenario=scen)

def get_env(patient_name, reward_fun=None, done_fun=None, seed=None, custom_scenario=scenario):
    env_name = 'simglucose-' + patient_name[:-4] + patient_name[-3:] + '-v0'
    register(
        id=env_name,
        entry_point='simglucose.envs:T1DSimEnv',
        kwargs={'patient_name': patient_name,
                'custom_scenario': custom_scenario,
                'reward_fun': reward_fun,
                'done_fun': done_fun,
                'seed': seed}
    )
    env = gym.make(env_name)
    return env

No CHO at 7:00. image

jxx123 commented 9 months ago

Thanks for reporting this. I will start investigating this soon.

TomatoZ2 commented 9 months ago

I set start time at 0:00, and the rendering is OK. It seems that start time of the scenario setting does not take effect during simulation, but it has an impact on the coordinate axes. Maybe you should check the start time in CustomScenario.

def get_env(patient_name):
    start_time = datetime(2018, 1, 1, 0, 0, 0)
    scen = [(7, 60), (12, 59), (16, 74)]
    scenario = CustomScenario(start_time=start_time, scenario=scen)
    env_name = 'simglucose-' + patient_name[:-4] + patient_name[-3:] + '-v0'
    print(env_name)
    register(
        id=env_name,
        entry_point='simglucose.envs:T1DSimEnv',
        kwargs={'patient_name': patient_name,
                'cost_fun': cost_fun,
                'custom_scenario': scenario}
    )
    env = safety_gymnasium.make(env_name)
    return env

The figure below shows the correct scenario. image

jxx123 commented 5 months ago

Thank you! Do you mind submitting a Pull Request for this?