def test_set_ode(self): unit = PhaseUnit("01Jan2020", "01Feb2020", 1000) unit.set_ode(model=SIR, tau=720, rho=0.2) assert issubclass(unit.model, SIR) summary_dict = unit.to_dict() assert summary_dict[Term.TAU] == 720 assert summary_dict[Term.ODE] == SIR.NAME assert summary_dict["rho"] == 0.2 assert summary_dict["sigma"] is None
def test_estimate_with_fixed(self, jhu_data, population_data, country): # Dataset population = population_data.value(country) record_df = jhu_data.subset(country, population=population) # Setting unit = PhaseUnit("27May2020", "27Jun2020", population) unit.set_ode(model=SIR, tau=360, rho=0.01) # Parameter estimation unit.estimate(record_df=record_df) assert unit.tau == unit.to_dict()[Term.TAU] == 360 assert unit.to_dict()["rho"] == 0.01
def test_simulate_y0_direct(self, jhu_data, population_data, country): # Dataset population = population_data.value(country) # Parameter setting unit = PhaseUnit("27May2020", "27Jun2020", population) unit.set_ode(model=SIR, tau=240, rho=0.006, sigma=0.011) # Simulation y0_dict = { "Susceptible": 126512455, "Infected": 1806, "Fatal or Recovered": 14839 } sim_df = unit.simulate(y0_dict=y0_dict) assert set(sim_df.columns) == set(Term.NLOC_COLUMNS)
def test_estimate(self, jhu_data, population_data, country): # Dataset population = population_data.value(country) record_df = jhu_data.subset(country, population=population) # Parameter estimation unit = PhaseUnit("27May2020", "27Jun2020", population) with pytest.raises(NameError): unit.estimate() unit.set_ode(model=SIR) with pytest.raises(ValueError): unit.estimate() unit.record_df = record_df assert set(unit.record_df.columns) == set(Term.SUB_COLUMNS) unit.estimate(timeout=20) # Check results assert isinstance(unit.estimator, Estimator) assert set(SIR.PARAMETERS).issubset(unit.to_dict()) assert set(SIR.DAY_PARAMETERS).issubset(unit.to_dict()) assert None not in unit.to_dict().values()
def test_simulate(self, jhu_data, population_data, country): # Dataset population = population_data.value(country) record_df = jhu_data.subset(country, population=population) # Parameter setting unit = PhaseUnit("27May2020", "27Jun2020", population) with pytest.raises(UnExecutedError): unit3 = PhaseUnit("27May2020", "27Jun2020", population) unit3.set_ode(model=SIR, tau=240, rho=0.006) unit3.simulate() unit.set_ode(model=SIR, tau=240, rho=0.006, sigma=0.011) summary_dict = unit.to_dict() assert summary_dict[Term.RT] == 0.55 assert summary_dict["1/beta [day]"] == 27 assert summary_dict["1/gamma [day]"] == 15 # Set initial values unit.set_y0(record_df) # Simulation sim_df = unit.simulate() assert set(sim_df.columns) == set(Term.NLOC_COLUMNS)
def test_replace(self, jhu_data, population_data, country): # Setting population = population_data.value(country) series = PhaseSeries("01Apr2020", "01Aug2020", population) series.add(end_date="01May2020") series.add(end_date="01Jun2020") series.add(end_date="01Jul2020") series.add() # Replace one old phase with one new phase unit_old = series.unit("0th") unit_new = PhaseUnit( unit_old.start_date, unit_old.end_date, population ) unit_new.set_ode(tau=360) series.replace("0th", unit_new) assert series.unit("0th") == unit_new # Replace one old phase with two new phases unit_old = series.unit("1st") change_date = Term.date_change(unit_old.end_date, days=-7) unit_pre = PhaseUnit( unit_old.start_date, Term.yesterday(change_date), population) unit_pre.set_ode(tau=360) unit_fol = PhaseUnit(change_date, unit_old.end_date, population) unit_fol.set_ode(tau=360) series.replaces(phase="1st", new_list=[unit_pre, unit_fol]) print(series.unit("1st"), unit_pre) assert series.unit("1st") == unit_pre assert series.unit("2nd") == unit_fol # TypeError of new_list with pytest.raises(TypeError): series.replaces(phase="3rd", new_list=[unit_pre, Term]) # ValueError with tense with pytest.raises(ValueError): future_unit = PhaseUnit("01Sep2020", "01Dec2020", population) series.replaces(phase="3rd", new_list=[future_unit]) # Add phase without deletion of any phases new1 = PhaseUnit("02Aug2020", "01Sep2020", population) new2 = PhaseUnit("02Sep2020", "01Oct2020", population) series.replaces(phase=None, new_list=[new1, new2]) assert series.unit("last") == new2
def test_set_tau(self): unit = PhaseUnit("01Jan2020", "01Feb2020", 1000) unit.set_ode(model=None, tau=240, rho=0.006) assert unit.tau == 240 assert "rho" not in unit.to_dict()