Skip to content

Commit

Permalink
Good for serialization computing, parallel not ready
Browse files Browse the repository at this point in the history
  • Loading branch information
jxx123 committed Dec 16, 2017
0 parents commit b48b4aa
Show file tree
Hide file tree
Showing 30 changed files with 1,381 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Compiled python modules.
*.pyc

# Setuptools distribution folder.
/dist/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
/*.egg
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include simglucose/params/*.csv
19 changes: 19 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from setuptools import setup


setup(name='simglucose',
version='0.1',
description='A Type-1 Diabetes simulator as an OpenAI gym environment',
url='',
author='Jinyu Xie',
author_email='[email protected]',
license='MIT',
packages=['simglucose'],
install_requires=[
'pandas',
'numpy',
'scipy',
'matplotlib',
],
include_package_data=True,
zip_safe=False)
Empty file added simglucose/__init__.py
Empty file.
Empty file added simglucose/analysis/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions simglucose/analysis/risk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np


def risk_index(BG, horizon):
# BG is in mg/dL
# horizon in samples
BG_to_compute = BG[-horizon:]
fBG = 1.509 * (np.log(BG_to_compute)**1.084 - 5.381)
rl = 10 * fBG[fBG < 0]**2
rh = 10 * fBG[fBG > 0]**2
LBGI = np.nan_to_num(np.mean(rl))
HBGI = np.nan_to_num(np.mean(rh))
RI = LBGI + HBGI
return (LBGI, HBGI, RI)
Empty file.
53 changes: 53 additions & 0 deletions simglucose/controller/basal_bolus_ctrller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from .base import Controller
from .base import Action
import numpy as np
import pandas as pd
import pkg_resources
import logging

logger = logging.getLogger(__name__)
CONTROL_QUEST = pkg_resources.resource_filename(
'simglucose', 'params/Quest.csv')
PATIENT_PARA_FILE = pkg_resources.resource_filename(
'simglucose', 'params/vpatient_params.csv')


class BBController(Controller):
def __init__(self, target=140):
self.quest = pd.read_csv(CONTROL_QUEST)
self.patient_params = pd.read_csv(
PATIENT_PARA_FILE)
self.target = target

def policy(self, observation, **kwargs):
sample_time = kwargs.get('sample_time', 1)
pname = kwargs.get('patient_name')
action = self._bb_policy(
pname, observation.CHO, observation.CGM, sample_time)
return action

def _bb_policy(self, name, meal, glucose, env_sample_time):
if any(self.quest.Name.str.match(name)):
q = self.quest[self.quest.Name.str.match(name)]
params = self.patient_params[self.patient_params.Name.str.match(
name)]
u2ss = np.asscalar(params.u2ss.values)
BW = np.asscalar(params.BW.values)
else:
q = pd.DataFrame([['Average', 1 / 15, 1 / 50, 50, 30]],
columns=['Name', 'CR', 'CF', 'TDI', 'Age'])
u2ss = 1.43
BW = 57.0

basal = u2ss * BW / 6000
if meal > 0:
logger.info('Calculating bolus ...')
logger.debug('glucose = {}'.format(glucose))
bolus = np.asscalar(meal / q.CR.values + (glucose > 150)
* (glucose - self.target) / q.CF.values)
else:
bolus = 0

bolus = bolus / env_sample_time
action = Action(basal=basal, bolus=bolus)
return action
13 changes: 13 additions & 0 deletions simglucose/controller/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from collections import namedtuple

Action = namedtuple('ctrller_action', ['basal', 'bolus'])


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

def policy(self, observation, **kwargs):
self.state = observation
action = self.state[0]
return action
31 changes: 31 additions & 0 deletions simglucose/params/Quest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Name,CR,CF,Age,TDI
adolescent#001,12,15.0360283441,18,36.7339146827
adolescent#002,5,13.1750891807,19,62.031042736
adolescent#003,23,33.5259913553,15,24.2427803659
adolescent#004,14,21.8224838663,17,35.2528665417
adolescent#005,12,20.9084301999,16,34.0047207467
adolescent#006,7,17.6966328214,14,49.5812925714
adolescent#007,8,12.4931832305,16,43.638064
adolescent#008,4,11.9355179763,14,63.3866974592
adolescent#009,21,20.013934775,19,24.0781667718
adolescent#010,14,31.8684843717,17,33.1735076937
adult#001,10,8.77310657487,61,50.416652
adult#002,8,9.21276345633,65,57.86877688
adult#003,9,17.9345522688,27,56.4297186222
adult#004,16,42.6533755134,66,33.8079423727
adult#005,5,8.23126750783,52,68.315922352
adult#006,10,18.21328135,26,61.38880928
adult#007,22,26.1530845971,35,42.0066074109
adult#008,13,12.2505850562,48,42.7787865846
adult#009,5,7.64317583896,68,67.211482912
adult#010,5,10.69260456,68,64.448546656
child#001,25,42.7177301243,9,17.4729287744
child#002,23,36.9153947641,9,18.1778368801
child#003,22,31.0525488428,8,16.0218757795
child#004,25,40.7235040865,12,19.8151389176
child#005,7,33.6312561084,10,40.9345323552
child#006,19,39.9807063565,8,20.2240173973
child#007,8,24.9969862972,9,36.2128538724
child#008,15,30.8823307429,10,21.4944284585
child#009,25,35.3170388027,7,17.3942110251
child#010,18,29.222745246,12,20.6516964887
3 changes: 3 additions & 0 deletions simglucose/params/pump_params.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name,min_bolus,max_bolus,inc_bolus,min_basal,max_basal,inc_basal,sample_time
Cozmo,0.0,75.0,0.05,0.0,35.0,0.05,1.0
Insulet,0.0,30.0,0.05,0.0,30.0,0.05,1.0
4 changes: 4 additions & 0 deletions simglucose/params/sensor_params.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name,PACF,gamma,lambda,delta,xi,sample_time,min,max
Dexcom,0.7,-0.5444,15.9574,1.6898,-5.47,3.0,39.0,600.0
GuardianRT,0.7,-0.5444,15.9574,1.6898,-5.47,5.0,39.0,600.0
Navigator,0.7,-0.5444,15.9574,1.6898,-5.47,1.0,32.0,600.0
Loading

0 comments on commit b48b4aa

Please sign in to comment.