Exemple #1
0
 def simulate(self, simulation: Simulation):
     timeline = np.arange(0, simulation.days, simulation.step)
     odeModel = self.__buildOdeModelFunction()
     preconditions, initialConditions, variables = self.__preprocessVariables(
         simulation)
     result = SimulationResult(list(self.context.compartments.keys()),
                               timeline)
     if simulation.iterate:
         it = simulation.iterate
         result.paramValues = np.linspace(it.start, it.end, it.intervals)
         result.param = it.key
         tsimulation = simulation.copy()
         for value in result.paramValues:
             tsimulation.params[it.key] = value
             tvariables = variables[:]
             tpreconditions = preconditions.copy()
             self.__validatePreconditions(tpreconditions,
                                          tsimulation.params)
             result.frames.append(
                 self.__singleSimulate(
                     odeModel,
                     initialConditions,
                     tvariables,
                     tsimulation.params,
                     timeline,
                 ))
     else:
         self.__validatePreconditions(preconditions, simulation.params)
         result.frames.append(
             self.__singleSimulate(odeModel, initialConditions, variables,
                                   simulation.params, timeline))
     return result
Exemple #2
0
def test_process_observables(simulation_schema):
    model = Model(**simulation_schema("models/SIR-HL.json"))
    simSIR = {
        "step": 1,
        "days": 365,
        "initial_conditions": {
            "Sl": 599800,
            "Sh": 399800,
            "Il": 200,
            "Ih": 200,
            "Rl": 0,
            "Rh": 0,
        },
        "params": {
            "p": 0.1818,
            "gamma": 0.0714,
            "H": 10,
            "L": 1,
            "Noh": 400000,
            "Nol": 600000,
        },
    }
    simulation = Simulation(**simSIR)
    context = ModelContext(model)
    sim = Simulator(context)
    result = sim.simulate(simulation)
    computeExtraColumns(context, result)
    assert True
Exemple #3
0
def test_sim_precondition_error_animate(simulation_schema):
    model = Model(**simulation_schema("models/SIR.json"))
    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": 999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 0,
            "gamma": 0.0714
        },
        "iterate": {
            "key": "beta",
            "intervals": 10,
            "start": 10,
            "end": -10
        },
    }
    simulation = Simulation(**simSIR)
    sim = Simulator(ModelContext(model))
    with raises(SimulatorError) as e:
        sim.simulate(simulation)
    assert e.value.args[1] == "Precondition not satisisfied: beta >= 0"
Exemple #4
0
def test_sim_model_cannot_solve_preconditions(simulation_schema):
    modelData = simulation_schema("models/SIR.json")
    modelData["preconditions"] = [{"predicate": "(omega + N) > 2"}]

    model = Model(**modelData)
    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": 999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 1,
            "gamma": 0.0714
        },
    }
    simulation = Simulation(**simSIR)
    sim = Simulator(ModelContext(model))
    with raises(SimulatorError) as e:
        sim.simulate(simulation)
    assert (
        e.value.args[1] ==
        "Cannot solve precondition [(omega + N) > 2]: omega + 1000000.0 > 2")
Exemple #5
0
def test_sim_model_cannot_solve_symbols(simulation_schema):
    modelData = simulation_schema("models/SIR.json")
    modelData["expressions"] = [
        {
            "name": "N",
            "value": "S+1"
        },
        {
            "name": "S",
            "value": "N+1"
        },
    ]

    model = Model(**modelData)
    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": 999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 1,
            "gamma": 0.0714
        },
    }
    simulation = Simulation(**simSIR)
    context = ModelContext(model)
    sim = Simulator(context)
    with raises(SimulatorError) as e:
        sim.simulate(simulation)
    assert e.value.args[1] == "Deep recursion exceeded unfolding expressions."
def test_schema_simulation_invalid_initial_conditions():
    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": -999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 1,
            "gamma": 0.0714
        },
    }
    with raises(ValidationError):
        Simulation(**simSIR)
Exemple #7
0
def test_sim_missing_compartment(simulation_schema):
    model = Model(**simulation_schema("models/SIR.json"))

    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": 999600,
            "I": 400
        },
        "params": {
            "beta": 1,
            "gamma": 0.0714
        },
    }
    simulation = Simulation(**simSIR)
    sim = Simulator(ModelContext(model))
    with raises(SimulatorError) as e:
        sim.simulate(simulation)
    assert e.value.args[1] == "Missing initialization for compartment 'R'"
Exemple #8
0
def test_sim_missing_parameter(simulation_schema):
    model = Model(**simulation_schema("models/SIR.json"))

    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": 999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 1
        },
    }
    simulation = Simulation(**simSIR)
    sim = Simulator(ModelContext(model))
    with raises(SimulatorError) as e:
        sim.simulate(simulation)
    assert e.value.args[1] == "Missing parameter 'gamma'"
def test_schema_simulation_iterate_interval_negative():
    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": -999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 1,
            "gamma": 0.0714
        },
        "iterate": {
            "key": "beta",
            "intervals": -10,
            "start": 0,
            "end": 10
        },
    }
    with raises(ValidationError):
        Simulation(**simSIR)
Exemple #10
0
def test_sim_basic(simulation_schema):
    model = Model(**simulation_schema("models/SIR.json"))
    expectedt = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0]
    expectedData = [
        [
            999600.0,
            957254.704,
            193974.687,
            5789.445,
            408.662,
            63.507,
            17.250,
            6.928,
            3.659,
            2.341,
        ],
        [
            400.0,
            39654.697,
            688955.907,
            626406.400,
            442512.697,
            309930.717,
            216918.056,
            151801.651,
            106229.290,
            74337.418,
        ],
        [
            0.0,
            3090.598,
            117069.405,
            367804.154,
            557078.640,
            690005.774,
            783064.692,
            848191.419,
            893767.049,
            925660.240,
        ],
    ]

    simSIR = {
        "step": 5,
        "days": 50,
        "initial_conditions": {
            "S": 999600,
            "I": 400,
            "R": 0
        },
        "params": {
            "beta": 1,
            "gamma": 0.0714
        },
    }
    simulation = Simulation(**simSIR)
    ModelContext(model)
    sim = Simulator(ModelContext(model))
    result = sim.simulate(simulation)

    assert result.compartments == ["S", "I", "R"]
    assert len(result.timeline) == 10
    assert any(lambda t: expectedt[t] == approx(result.timeline[t], 0.001)
               for t in range(10))
    assert len(result.frames) == 1
    assert len(result.frames[0][0]) == 3
    assert any(
        lambda t: expectedData[0][t] == approx(result.frames[0][0][t], 0.001)
        for t in range(10))
    assert any(
        lambda t: expectedData[1][t] == approx(result.frames[0][1][t], 0.001)
        for t in range(10))
    assert any(
        lambda t: expectedData[2][t] == approx(result.frames[0][2][t], 0.001)
        for t in range(10))