def test_add_entry_flows_post_stratification(): """ Ensure we can add flows after a model is stratified. """ model = CompartmentalModel( times=[0, 5], compartments=["S", "I", "R"], infectious_compartments=["I"] ) assert len(model._flows) == 0 strat = Stratification("location", ["urban", "rural"], ["S", "I", "R"]) model.stratify_with(strat) with pytest.raises(AssertionError): model.add_importation_flow("imports", 10, "S", expected_flow_count=1) assert len(model._flows) == 0 model.add_importation_flow("imports", 10, "S", expected_flow_count=2) assert len(model._flows) == 2 expected_flows = [ ImportFlow("imports", C("S", {"location": "urban"}), 10, []), ImportFlow("imports", C("S", {"location": "rural"}), 10, []), ] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)])
def test_stratify_entry_flows__with_no_explicit_adjustments(): """ Ensure entry flows are stratified correctly when no adjustments are requested. Expect flow to be conserved, split evenly over the new strata. """ model = CompartmentalModel( times=[0, 5], compartments=["S", "I", "R"], infectious_compartments=["I"] ) model.add_importation_flow("imports", 10, "S") expected_flows = [ImportFlow("imports", C("S"), 10)] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)]) strat = Stratification("location", ["urban", "rural"], ["S", "I", "R"]) model.stratify_with(strat) expected_flows = [ ImportFlow("imports", C("S", {"location": "urban"}), 10, [Multiply(0.5)]), ImportFlow("imports", C("S", {"location": "rural"}), 10, [Multiply(0.5)]), ] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)]) strat = Stratification("age", ["young", "old"], ["S", "I", "R"]) model.stratify_with(strat) expected_flows = [ ImportFlow( "imports", C("S", {"location": "urban", "age": "young"}), 10, [Multiply(0.5), Multiply(0.5)], ), ImportFlow( "imports", C("S", {"location": "urban", "age": "old"}), 10, [Multiply(0.5), Multiply(0.5)], ), ImportFlow( "imports", C("S", {"location": "rural", "age": "young"}), 10, [Multiply(0.5), Multiply(0.5)], ), ImportFlow( "imports", C("S", {"location": "rural", "age": "old"}), 10, [Multiply(0.5), Multiply(0.5)], ), ] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)])
def test_add_entry_flows_post_stratification__with_filter(): """ Ensure we can add flows after a model is stratified when a strata filter is applied """ model = CompartmentalModel( times=[0, 5], compartments=["S", "I", "R"], infectious_compartments=["I"] ) assert len(model._flows) == 0 strat = Stratification("location", ["urban", "rural"], ["S", "I", "R"]) model.stratify_with(strat) assert len(model._flows) == 0 model.add_importation_flow( "imports", 10, "S", dest_strata={"location": "urban"}, expected_flow_count=1 ) assert len(model._flows) == 1 expected_flows = [ ImportFlow("imports", C("S", {"location": "urban"}), 10, []), ] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)])
def test_stratify_entry_flows__with_explicit_adjustments(): """ Ensure entry flows are stratified correctly when adjustments are requested. """ model = CompartmentalModel( times=[0, 5], compartments=["S", "I", "R"], infectious_compartments=["I"] ) model.add_importation_flow("imports", 10, "S") expected_flows = [ImportFlow("imports", C("S"), 10)] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)]) strat = Stratification("location", ["urban", "rural"], ["S", "I", "R"]) strat.add_flow_adjustments("imports", {"urban": Multiply(0.9), "rural": None}) model.stratify_with(strat) expected_flows = [ ImportFlow( "imports", C("S", {"location": "urban"}), 10, [Multiply(0.9)], ), ImportFlow( "imports", C("S", {"location": "rural"}), 10, [], ), ] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)]) strat = Stratification("age", ["young", "old"], ["S", "I", "R"]) strat.add_flow_adjustments("imports", {"young": Multiply(0.8), "old": Overwrite(1)}) model.stratify_with(strat) expected_flows = [ ImportFlow( "imports", C("S", {"location": "urban", "age": "young"}), 10, [Multiply(0.9), Multiply(0.8)], ), ImportFlow( "imports", C("S", {"location": "urban", "age": "old"}), 10, [Multiply(0.9), Overwrite(1)], ), ImportFlow( "imports", C("S", {"location": "rural", "age": "young"}), 10, [Multiply(0.8)], ), ImportFlow( "imports", C("S", {"location": "rural", "age": "old"}), 10, [Overwrite(1)], ), ] assert len(expected_flows) == len(model._flows) assert all([a._is_equal(e) for e, a in zip(expected_flows, model._flows)])