-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,349 additions
and
1,102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# Compiled python modules. | ||
*.pyc | ||
|
||
# results foler | ||
/results/ | ||
|
||
# Setuptools distribution folder. | ||
/dist/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
|
||
|
||
setup(name='simglucose', | ||
version='0.1.4', | ||
description='A Type-1 Diabetes Simulator as a Reinforcement Learning Environment', | ||
version='0.1.5', | ||
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', | ||
author_email='[email protected]', | ||
|
@@ -14,7 +14,8 @@ | |
'numpy', | ||
'scipy', | ||
'matplotlib', | ||
'pathos' | ||
'pathos', | ||
'gym' | ||
], | ||
include_package_data=True, | ||
zip_safe=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from gym.envs.registration import register | ||
|
||
register( | ||
id='simglucose-v0', | ||
entry_point='simglucose.envs:T1DSimEnv', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from simglucose.envs.simglucose_gym_env import T1DSimEnv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from simglucose.simulation.env import T1DSimEnv as _T1DSimEnv | ||
from simglucose.patient.t1dpatient import T1DPatient | ||
from simglucose.sensor.cgm import CGMSensor | ||
from simglucose.actuator.pump import InsulinPump | ||
from simglucose.simulation.scenario_gen import RandomScenario | ||
from simglucose.controller.base import Action | ||
import pandas as pd | ||
import numpy as np | ||
import pkg_resources | ||
import gym | ||
from gym import error, spaces, utils | ||
from gym.utils import seeding | ||
from datetime import datetime | ||
|
||
PATIENT_PARA_FILE = pkg_resources.resource_filename( | ||
'simglucose', 'params/vpatient_params.csv') | ||
|
||
|
||
class T1DSimEnv(gym.Env): | ||
''' | ||
A wrapper of simglucose.simulation.env.T1DSimEnv to support gym API | ||
''' | ||
metadata = {'render.modes': ['human']} | ||
|
||
def __init__(self, patient_name=None): | ||
''' | ||
patient_name must be 'adolescent#001' to 'adolescent#010', | ||
or 'adult#001' to 'adult#010', or 'child#001' to 'child#010' | ||
''' | ||
if patient_name is None: | ||
# patient_name = self.pick_patient() | ||
|
||
# have to hard code the patient_name, gym has some interesting | ||
# error when choosing the patient | ||
patient_name = 'adolescent#001' | ||
patient = T1DPatient.withName(patient_name) | ||
sensor = CGMSensor.withName('Dexcom') | ||
scenario = RandomScenario(start_time=datetime(2018, 1, 1, 0, 0, 0)) | ||
pump = InsulinPump.withName('Insulet') | ||
self.env = _T1DSimEnv(patient, sensor, pump, scenario) | ||
|
||
@staticmethod | ||
def pick_patient(): | ||
patient_params = pd.read_csv(PATIENT_PARA_FILE) | ||
while True: | ||
print('Select patient:') | ||
for j in range(len(patient_params)): | ||
print('[{0}] {1}'.format(j + 1, patient_params['Name'][j])) | ||
try: | ||
select = int(input('>>> ')) | ||
except ValueError: | ||
print('Please input a number.') | ||
continue | ||
|
||
if select < 1 or select > len(patient_params): | ||
print('Please input 1 to {}'.format(len(patient_params))) | ||
continue | ||
|
||
return select | ||
|
||
def _step(self, action): | ||
# This gym only controls basal insulin | ||
act = Action(basal=action, bolus=0) | ||
return self.env.step(act) | ||
|
||
def _reset(self): | ||
return self.env.reset() | ||
|
||
def _seed(self, seed=None): | ||
rng, 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 | ||
self.env.sensor.seed = seed1 | ||
self.env.scenario.seed = seed2 | ||
return [seed1, seed2] | ||
|
||
def _render(self, mode='human', close=False): | ||
self.env.render(close=close) | ||
|
||
@property | ||
def action_space(self): | ||
ub = self.env.pump._params['max_basal'] | ||
return spaces.Box(low=0, high=ub, shape=(1,)) | ||
|
||
@property | ||
def observation_space(self): | ||
return spaces.Box(low=0, high=np.inf, shape=(1,)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.