Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gempyor plugins for Seeding and InitialConditions, fixes issue #101 🧩 #194

Merged
merged 19 commits into from
Apr 12, 2024

Conversation

jcblemai
Copy link
Collaborator

@jcblemai jcblemai commented Mar 24, 2024

Let the user put tiny (or big) bit of R and and python code to replace a module within gempyor. Solves #101. Documentation here

There are two ways of doing this:

  • monkey patching the methods of the class: smaller syntax but less powerful and possible side effect
  • based on inheritance, as described here. issue is that it depends on cpython implementation

Example: random number of infected between 0 and 10%

in config:

initial_conditions:
  method: plugin
  plugin_file_path: model_input/my_initial_conditions.py

in model_input/my_initial_conditions.py

import gempyor.seeding_ic
import numpy as np

class InitialConditions(gempyor.seeding_ic.InitialConditions):

    def get_from_config(self, sim_id: int, setup) -> np.ndarray:
        y0 = np.zeros((setup.compartments.compartments.shape[0], setup.nsubpops))
        S_idx = setup.compartments.get_comp_idx({"infection_stage":"S"})
        I_idx = setup.compartments.get_comp_idx({"infection_stage":"I"})
        prop_inf = np.random.uniform(low=0,high=.1)
        y0[S_idx, :] = setup.subpop_pop * (1-prop_inf)
        y0[I_idx, :] = setup.subpop_pop * prop_inf

        return y0
    
    def get_from_file(self, sim_id: int, setup) -> np.ndarray:
        return self.draw(sim_id=sim_id, setup=setup)

and from R

import gempyor.seeding_ic
import numpy as np

class InitialConditions(gempyor.seeding_ic.InitialConditions):

    def get_from_config(self, sim_id: int, setup) -> np.ndarray:
        import rpy2.robjects as robjects
        robjects.r.source("path_to_your_Rscript.R", encoding="utf-8")
        y0 = robjects.r["initial_condition_fromR"]

        return y0
    
    def get_from_file(self, sim_id: int, setup) -> np.ndarray:
        return self.draw(sim_id=sim_id, setup=setup)

@jcblemai jcblemai changed the title Gempyor plugins, fix #101 Gempyor plugins for Seeding and InitialConditions, fixes issue #101 Mar 24, 2024
@jcblemai jcblemai changed the title Gempyor plugins for Seeding and InitialConditions, fixes issue #101 Gempyor plugins for Seeding and InitialConditions, fixes issue #101 🧩 Mar 24, 2024
Base automatically changed from breaking-improvments to main April 11, 2024 07:51
@jcblemai jcblemai requested a review from alsnhll April 12, 2024 12:55
@jcblemai jcblemai merged commit 05483be into main Apr 12, 2024
1 check passed
@jcblemai jcblemai deleted the gempyor-plugin branch April 12, 2024 14:40
jcblemai added a commit that referenced this pull request Oct 3, 2024
Gempyor plugins for `Seeding` and `InitialConditions`, fixes issue #101 🧩
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants