def risk_diff(BG_last_hour): if len(BG_last_hour) < 2: return 0 else: _, _, risk_current = risk_index([BG_last_hour[-1]], 1) _, _, risk_prev = risk_index([BG_last_hour[-2]], 1) return risk_prev - risk_current
def step(self, action, reward_fun=neg_risk): ''' action is a namedtuple with keys: basal, bolus ''' CHO = 0.0 insulin = 0.0 BG = 0.0 CGM = 0.0 # This loop runs for n minutes in patients life # n is determined by the sample time, which is in turn defined by # the quality of sensor and the interval between each measurement for _ in range(int(self.sample_time)): # Compute moving average as the sample measurements tmp_CHO, tmp_insulin, tmp_BG, tmp_CGM = self.mini_step(action) CHO += tmp_CHO / self.sample_time insulin += tmp_insulin / self.sample_time BG += tmp_BG / self.sample_time CGM += tmp_CGM / self.sample_time # Compute risk index horizon = 1 LBGI, HBGI, risk = risk_index([BG], horizon) # Record current action self.CHO_hist.append(CHO) self.insulin_hist.append(insulin) # Record next observation self.time_hist.append(self.time) self.BG_hist.append(BG) self.CGM_hist.append(CGM) self.risk_hist.append(risk) self.LBGI_hist.append(LBGI) self.HBGI_hist.append(HBGI) # Compute reward, and decide whether game is over window_size = int(60 / self.sample_time) BG_last_hour = self.CGM_hist[-window_size:] reward = reward_fun(BG_last_hour) # Stopping criteria # done = BG < 70 or BG > 350 # If the BG level go out of control done = self.patient.t >= 1440 # At the end of the day (24hrs = 1440 mins) obs = Observation(CGM=CGM) return Step( observation=obs, reward=reward, done=done, sample_time=self.sample_time, patient_name=self.patient.name, meal=CHO, patient_state=self.patient.state)
def _reset(self): self.sample_time = self.sensor.sample_time self.viewer = None BG = self.patient.observation.Gsub horizon = 1 LBGI, HBGI, risk = risk_index([BG], horizon) CGM = self.sensor.measure(self.patient) self.time_hist = [self.scenario.start_time] self.BG_hist = [BG] self.CGM_hist = [CGM] self.risk_hist = [risk] self.LBGI_hist = [LBGI] self.HBGI_hist = [HBGI] self.CHO_hist = [] self.insulin_hist = []
def neg_risk(BG_last_hour): if len(BG_last_hour) < 2: return 0 else: _, _, risk_current = risk_index([BG_last_hour[-1]], 1) return - risk_current