Skip to content

Commit

Permalink
Added license, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jxx123 committed Dec 31, 2017
1 parent c6e8367 commit 9e069d7
Show file tree
Hide file tree
Showing 48 changed files with 136 additions and 14,725 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Compiled python modules.
*.pyc

# Simulation results folders
/examples/results/

# Setuptools distribution folder.
/dist/

Expand Down
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 Jinyu Xie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions examples/apply_customized_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from simglucose.simulation.user_interface import simulate
from simglucose.controller.base import Controller, Action


class MyController(Controller):
def __init__(self, init_state):
self.state = init_state

def policy(self, observation, reward, done, **info):
'''
Every controller must have this implementation!
----
Inputs:
observation - a namedtuple defined in simglucose.simulation.env. It has
CHO and CGM two entries.
reward - current reward returned by environment
done - True, game over. False, game continues
info - additional information as key word arguments,
simglucose.simulation.env.T1DSimEnv returns patient_name
and sample_time
----
Output:
action - a namedtuple defined at the beginning of this file. The
controller action contains two entries: basal, bolus
'''
self.state = observation
action = Action(basal=0, bolus=0)
return action


ctrller = MyController(0)
simulate(controller=ctrller)
2 changes: 2 additions & 0 deletions examples/run_user_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from simglucose.simulation.user_interface import simulate
simulate()
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(name='simglucose',
version='0.1',
description='A Type-1 Diabetes simulator as an OpenAI gym environment',
description='A Type-1 Diabetes Simulator as a Reinforcement Learning Environment',
url='',
author='Jinyu Xie',
author_email='[email protected]',
Expand All @@ -14,6 +14,7 @@
'numpy',
'scipy',
'matplotlib',
'pathos'
],
include_package_data=True,
zip_safe=False)
11 changes: 6 additions & 5 deletions simglucose/controller/basal_bolus_ctrller.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ def __init__(self, target=140):
PATIENT_PARA_FILE)
self.target = target

def policy(self, observation, env):
# sample_time = kwargs.get('sample_time', 1)
# pname = kwargs.get('patient_name')
def policy(self, observation, reward, done, **kwargs):
sample_time = kwargs.get('sample_time', 1)
pname = kwargs.get('patient_name')

action = self._bb_policy(
env.patient.name,
pname,
observation.CHO,
observation.CGM,
env.sample_time)
sample_time)
return action

def _bb_policy(self, name, meal, glucose, env_sample_time):
Expand Down
20 changes: 18 additions & 2 deletions simglucose/controller/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ class Controller(object):
def __init__(self, init_state):
self.state = init_state

def policy(self, observation, env):
def policy(self, observation, reward, done, **info):
'''
Every controller must have this implementation!
----
Inputs:
observation - a namedtuple defined in simglucose.simulation.env. It has
CHO and CGM two entries.
reward - current reward returned by environment
done - True, game over. False, game continues
info - additional information as key word arguments,
simglucose.simulation.env.T1DSimEnv returns patient_name
and sample_time
----
Output:
action - a namedtuple defined at the beginning of this file. The
controller action contains two entries: basal, bolus
'''
self.state = observation
action = self.state[0]
action = Action(basal=0, bolus=0)
return action
21 changes: 18 additions & 3 deletions simglucose/simulation/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from simglucose.patient.t1dpatient import Action
from simglucose.analysis.risk import risk_index
import pandas as pd
from datetime import datetime
from datetime import timedelta
import logging
from collections import namedtuple
Expand All @@ -14,8 +13,20 @@
from rllab.spaces import Box
except ImportError:
print('You could use rllab features, if you have rllab module.')
_Step = namedtuple("Step",
["observation", "reward", "done", "info"])

def Step(observation, reward, done, **kwargs):
"""
Convenience method creating a namedtuple with the results of the
environment.step method.
Put extra diagnostic info in the kwargs
"""
return _Step(observation, reward, done, kwargs)

rllab = False
Step = namedtuple("Step", ["observation", "reward", "done", "info"])
Step = namedtuple("Step",
["observation", "reward", "done", "info"])

Observation = namedtuple('Observation', ['CHO', 'CGM'])
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -106,7 +117,11 @@ def step(self, action):
reward = - np.log(risk)
done = BG < 70 or BG > 350
obs = Observation(CHO=CHO, CGM=CGM)
return Step(observation=obs, reward=reward, done=done)
return Step(observation=obs,
reward=reward,
done=done,
sample_time=self.sample_time,
patient_name=self.patient.name)

def reset(self):
self.patient.reset()
Expand Down

This file was deleted.

Loading

0 comments on commit 9e069d7

Please sign in to comment.