jxx123 / simglucose

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

Report function does not work #42

Open yujiadeng opened 2 years ago

yujiadeng commented 2 years ago

Hi, I just installed the package from pip. I was running the tutorial and failed to get the visualizations.

Reproduce:

from simglucose.simulation.env import T1DSimEnv
from simglucose.controller.basal_bolus_ctrller import BBController
from simglucose.sensor.cgm import CGMSensor
from simglucose.actuator.pump import InsulinPump
from simglucose.patient.t1dpatient import T1DPatient
from simglucose.simulation.scenario_gen import RandomScenario
from simglucose.simulation.scenario import CustomScenario
from simglucose.simulation.sim_engine import SimObj, sim, batch_sim
from datetime import timedelta
from datetime import datetime

# specify start_time as the beginning of today
now = datetime.now()
start_time = datetime.combine(now.date(), datetime.min.time())

# --------- Create Random Scenario --------------
# Specify results saving path
path = './results'

# Create a simulation environment
patient = T1DPatient.withName('adolescent#001')
sensor = CGMSensor.withName('Dexcom', seed=1)
pump = InsulinPump.withName('Insulet')
scenario = RandomScenario(start_time=start_time, seed=1)
env = T1DSimEnv(patient, sensor, pump, scenario)

# Create a controller
controller = BBController()

# Put them together to create a simulation object
s1 = SimObj(env, controller, timedelta(days=1), animate=False, path=path)
results1 = sim(s1)
print(results1)

from simglucose.analysis.report import report
import pandas as pd
from pathlib import Path

result_filename = './results/adolescent#001.csv'
df = pd.read_csv(result_filename, index_col=0)
report(df)

I found all errors raised in the plot function is regarding to BG which is defined by BG = df.unstack(level=0).BG. I tried both single day/ >1 days and looks the plot functions always raise error. What shape should BG be?

jxx123 commented 2 years ago

This might be a dependency issue. Mine is good. I feel there is some updates in pandas causing this issue, but I agree the report function is written in quite a messy way. I will need to install the latest pandas and debug more.

yihuicai commented 1 year ago

I also met a similar issue when generating report, seems the numpy calculations returns an object which object is not compatible with DataFrame object in pandas library:

Traceback (most recent call last): File "/home/alan/simglucose/sim_origin.py", line 6, in <module> simulate(start_time=datetime.combine(datetime.now().date(), datetime.min.time()),controller=BBController(),cgm_seed=123412,animate=False,parallel=True) File "/home/alan/simglucose/simglucose/simulation/user_interface.py", line 383, in simulate results, ri_per_hour, zone_stats, figs, axes = report(df, save_path) File "/home/alan/simglucose/simglucose/analysis/report.py", line 251, in report ri_per_hour, ri_mean, fig_ri, ax5 = risk_index_trace(BG, visualize=False) File "/home/alan/simglucose/simglucose/analysis/report.py", line 102, in risk_index_trace fBG_df = pd.concat(fBG, axis=1).transpose() File "/home/alan/.local/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 372, in concat op = _Concatenator( File "/home/alan/.local/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 462, in __init__ raise TypeError(msg) TypeError: cannot concatenate object of type '<class 'numpy.float64'>'; only Series and DataFrame objs are valid

My dependencies are:

pandas: 2.0.2 numpy: 1.24.3 Python: 3.10.6