from dataclasses import dataclass
from pymgipsim.InputGeneration.signal import Events
import sys, json, numpy
[docs]
@dataclass(slots=True)
class settings():
""" General, simulator wide settings.
Mirrors the settings field of the scenario JSON file.
Parameters:
sampling_time : Sampling time of the simulation [min].
solver_name : ODE solver name (Euler or RK4)
number_of_subjects : Cohort simulation size.
start_time : Start time of the simulation in %d-%m-%Y %H:%M:%S datetime format.
end_time : End time of the simulation in %d-%m-%Y %H:%M:%S datetime format.
simulator_name : Currently openloop single scale, will be extended with more capabilities.
"""
sampling_time: int
simulator_name: str
solver_name: str
save_directory: str
start_time: int
end_time: int
random_seed: int
random_state: dict
[docs]
@dataclass(slots=True)
class controller():
name: str
parameters: list
[docs]
@dataclass(slots=True)
class model():
""" Model descriptor.
Mirrors the model field of the scenario JSON file.
Parameters:
name : Name of the model (T1DM.Hovorka, T1DM.IVP, T2DM.Jauslin)
parameters : Array of model specific parameter values.
initial_conditions : Array of initial states for the simulation
"""
name: str = None
parameters: list = None
initial_conditions: list = None
[docs]
@dataclass(slots=True)
class mscale():
""" Model descriptor.
Mirrors the model field of the scenario JSON file.
Parameters:
name : Name of the model (T1DM.Hovorka, T1DM.IVP, T2DM.Jauslin)
parameters : Array of model specific parameter values.
initial_conditions : Array of initial states for the simulation
"""
models: list = None
parameters: list = None
[docs]
@dataclass(slots=True)
class demographic_info():
""" Model independent patient information.
Mirrors the demographic info field of the scenario JSON file.
Parameters:
body_weight : Body weights of the patients [kg].
egfr : Glomural filtration rates [mL/min/1.73 m^2 BSA]
basal : Basal insulin rates [U/hr].
height : Height [m].
total_daily_basal : [U]
"""
body_weight_range: list = None
body_weight: list = None
renal_function_category: list = None
egfr: list = None
basal: list = None
height: list = None
total_daily_basal: list = None
carb_insulin_ratio: list = None
resting_heart_rate: list = None
correction_bolus: list = None
HbA1c: list = None
waist_size: list = None
baseline_daily_energy_intake: list = None
baseline_daily_energy_expenditure: list = None
baseline_daily_urinary_glucose_excretion: list = None
[docs]
@dataclass(slots=True)
class patient():
""" Describes the patient/cohort.
Mirrors the patient field of the scenario JSON file.
Parameters:
demographic_info (demographic_info) : Stores the model independent patient information.
model (model) : Model descriptor.
"""
demographic_info: demographic_info = None
model: model = None
mscale: mscale = None
files: list = None
number_of_subjects: int = None
def __post_init__(self):
scenario_module = sys.modules[__name__]
# Casts imported dictionaries to corresponding dataclasses
for attribute in self.__slots__:
if not (attribute == 'files' or attribute == 'number_of_subjects'):
try:
setattr(self, attribute, getattr(scenario_module, attribute)(**getattr(self, attribute)))
except:
setattr(self, attribute, None)
# print("Loaded scenario lacks: " + attribute + " information.")
# print("Loaded inputs lacks: " + attribute + " information.")
[docs]
@dataclass(slots=True)
class scenario():
""" Stores all the necessary information to uniquely define a simulation.
Mirrors the scenario JSON file.
Note:
Undefined/ not required field are None.
Parameters:
settings (settings) : General, simulator wide settings.
input_generation (input_generation) : Defines parameters for random input generation.
inputs (inputs) : Defines the events (start time, magnitude and duration) of specific inputs.
patient (patient) : Describes the simulated virtual cohort.
"""
settings: settings
input_generation: input_generation
inputs: inputs
patient: patient
controller: controller
def __post_init__(self):
scenario_module = sys.modules[__name__]
# Casts imported dictionaries to corresponding dataclasses
for attribute in self.__slots__:
try:
setattr(self, attribute, getattr(scenario_module, attribute)(**getattr(self, attribute)))
except:
setattr(self, attribute, None)
# print("Loaded scenario lacks: " + attribute + " information.")
[docs]
def load_scenario(path):
with open(path, "r") as f: #
loaded_scenario = json.load(f)
f.close()
scenario_instance = scenario(**loaded_scenario)
return scenario_instance
[docs]
def save_scenario(path, scenario):
with open(path, "w") as f:
json.dump(scenario, f, indent=4)