For developers

Programatically run simulations

An example is provided in manual_script.py on how programatically defined scenarios can be simulated.

How to add a model?

Defining the model

  • Define the state variables, inputs, constants, parameters, and model equations in the VirtualPatient/Models folder

  • In the Parameters file:
    • Define all model parameters and their vectorization function (as_array).

    • Define generate function that either loads patient files or draws random samples from distributions.

  • In the Inputs file:
    • Define all model inputs and their vectorization function (as_array).

  • In the Model file:
    • Define a unique name in the Model class, same name should be given to the folder.

    • Diff. equations should be defined in the model function.

    • Initial conditions should be defined in the preprocessing function.

    • A from_scenario function has to be defined which passes the inputs, parameters, etc. from the scenario to the model. Unit conversions from the default units to the model specific ones is convenient to do in this function.

  • Define virtual patient parameters in JSON files in the Patients folder.

Add model to the generate functions

  • Add functions that generate the necessary inputs of the model in generate_inputs_main function in the generate_inputs file.

  • Add model parameter generation in generate_virtual_subjects_main function in the generate_subjects file.

Note

If the added model is just an auixiliary model, it can be readily used for simulation. An example is the physical activity -> heart rate model, and the implementation can be found in pymgipsim/InputGeneration/heart_rate_settings.py. If the added model represents the patient, the following steps are needed.

Update VirtualCohort

  • Add the defined model to the virtual patient class in VirtualPatient/VirtualPatient.

Update plots

  • Update plotting function with specifics of the defined model.

Optional: Controller compatibility *

How to add a controller?

Define controller class

  • Define the class similarly to already existing ones in the pymgipsim/Controllers folder.

  • The controller has to have a unique name, same as the folder name.

  • run function of the controller will be called in every sampling time of the simulation with the available data up to that point. The function has to modify inplace the current sample of the input array.

Adding it to the solver

  • Add the controller to the set_controller function in pymgipsim/ModelSolver/singlescale.py.

Update plots

  • Update plotting function with specifics of the defined controller.

PYmGIPsim package

pymgipsim