Пример #1
0
 def test_scenario_with_model_change(self):
     # Instance to save population values
     population_data = PopulationData(filename=None)
     # Set tau value and start date of records
     example_data = ExampleData(tau=1440, start_date="01Jan2020")
     # Preset of SIR-F parameters
     preset_dict = SIRF.EXAMPLE["param_dict"]
     # Create dataset from 01Jan2020 to 31Jan2020
     area = {"country": "Theoretical"}
     example_data.add(SIRF, step_n=30, **area)
     # Register population value
     population_data.update(SIRF.EXAMPLE["population"], **area)
     # Create Scenario class
     snl = Scenario(example_data, population_data, tau=1440, **area)
     # Set 0th phase from 02Jan2020 to 31Jan2020 with preset parameter values
     snl.clear(include_past=True)
     snl.add(end_date="31Jan2020", model=SIRF, **preset_dict)
     # Add main scenario
     snl.add(end_date="31Dec2020", name="Main")
     # Add lockdown scenario
     rho_lock = snl.get("rho", phase="0th") / 2
     snl.add(end_date="31Dec2020", name="Lockdown", rho=rho_lock)
     # Add medicine scenario
     kappa_med = snl.get("kappa", phase="0th") / 2
     sigma_med = snl.get("sigma", phase="0th") * 2
     snl.add(end_date="31Dec2020",
             name="Medicine",
             kappa=kappa_med,
             sigma=sigma_med)
     # Add vaccine scenario
     snl.add(end_date="31Dec2020", name="Vaccine", model=SIRFV, omega=0.001)
     # Summarize
     snl.summary()
     # Compare scenarios
     snl.describe(y0_dict={"Vaccinated": 0})
Пример #2
0
 def test_edit_series(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     # Add and clear
     assert snl.summary().empty
     snl.add(end_date="05May2020")
     snl.add(days=20)
     snl.add()
     snl.add(end_date="01Sep2020")
     assert len(snl["Main"]) == 4
     snl.clear(include_past=True)
     snl.add(end_date="01Sep2020", name="New")
     assert len(snl["New"]) == 2
     # Delete
     snl.delete(name="Main")
     assert len(snl["Main"]) == 0
     with pytest.raises(TypeError):
         snl.delete(phases="1st", name="New")
     snl.delete(phases=["1st"], name="New")
     assert len(snl["New"]) == 1
     snl.delete(name="New")
     with pytest.raises(KeyError):
         assert len(snl["New"]) == 1
Пример #3
0
 def test_scenario(self):
     area = {"country": "Theoretical"}
     # Set-up example dataset (from 01Jan2020 to 31Jan2020)
     example_data = ExampleData(tau=1440, start_date="01Jan2020")
     example_data.add(SIRF, step_n=30, **area)
     # Population value
     population_data = PopulationData(filename=None)
     population_data.update(SIRF.EXAMPLE["population"], **area)
     # Set-up Scenario instance
     snl = Scenario(tau=1440, **area)
     snl.register(example_data, population_data)
     # Check records
     record_df = snl.records(variables="CFR")
     assert set(record_df.columns) == set(
         [Term.DATE, Term.C, Term.F, Term.R])
     # Add a past phase to 31Jan2020 with parameter values
     snl.add(model=SIRF, **SIRF.EXAMPLE["param_dict"])
     # Check summary
     df = snl.summary()
     assert not df.empty
     assert len(df) == 1
     assert Term.RT in df
     # Main scenario
     snl.add(end_date="31Dec2020", name="Main")
     assert snl.get(Term.RT, phase="last", name="Main") == 2.50
     # Lockdown scenario
     snl.clear(name="Lockdown")
     rho_lock = snl.get("rho", phase="0th") * 0.5
     snl.add(end_date="31Dec2020", name="Lockdown", rho=rho_lock)
     assert snl.get(Term.RT, phase="last", name="Lockdown") == 1.25
     # Medicine scenario
     snl.clear(name="Medicine")
     kappa_med = snl.get("kappa", phase="0th") * 0.5
     sigma_med = snl.get("sigma", phase="0th") * 2
     snl.add(end_date="31Dec2020",
             name="Medicine",
             kappa=kappa_med,
             sigma=sigma_med)
     assert snl.get(Term.RT, phase="last", name="Medicine") == 1.31
     # Add vaccine scenario
     snl.clear(name="Vaccine")
     rho_vac = snl.get("rho", phase="0th") * 0.8
     kappa_vac = snl.get("kappa", phase="0th") * 0.6
     sigma_vac = snl.get("sigma", phase="0th") * 1.2
     snl.add(end_date="31Dec2020",
             name="Vaccine",
             rho=rho_vac,
             kappa=kappa_vac,
             sigma=sigma_vac)
     assert snl.get(Term.RT, phase="last", name="Vaccine") == 1.72
     # Description
     snl.describe()
     # History
     snl.history("Rt")
     snl.history("rho")
     snl.history("Infected")
     snl.history_rate(name="Medicine")
     snl.simulate(name="Vaccine")
Пример #4
0
 def test_analysis(self, jhu_data, population_data):
     scenario = Scenario(jhu_data, population_data, country="Italy")
     with pytest.raises(KeyError):
         scenario.simulate(name="Main", show_figure=False)
     with pytest.raises(ValueError):
         scenario.estimate(model=SIRF)
     # S-R trend analysis
     scenario.trend(show_figure=False)
     warnings.filterwarnings("ignore", category=UserWarning)
     scenario.trend(show_figure=True)
     # Parameter estimation of SIR-F model
     with pytest.raises(ValueError):
         scenario.param_history(targets=["Rt"], show_figure=False)
     with pytest.raises(ValueError):
         scenario.estimate(model=SIRF, tau=1440)
     scenario.estimate(model=SIRF)
     # History of estimation
     scenario.estimate_history(phase="1st")
     with pytest.raises(KeyError):
         scenario.estimate_history(phase="0th")
     # Accuracy of estimation
     scenario.estimate_accuracy(phase="1st")
     with pytest.raises(KeyError):
         scenario.estimate_accuracy(phase="0th")
     # Prediction
     scenario.add(name="Main", days=100)
     scenario.simulate(name="Main", show_figure=False)
     scenario.simulate(name="Main", show_figure=True)
     scenario.param_history(targets=["Rt"], show_figure=False)
     scenario.param_history(targets=["Rt"], divide_by_first=False)
     scenario.param_history(targets=["Rt"], show_box_plot=False)
     with pytest.raises(KeyError):
         scenario.param_history(targets=["Rt", "Value"])
     with pytest.raises(KeyError):
         scenario.param_history(targets=["Rt"], box_plot=False)
     # New scenario
     sigma_new = scenario.get("sigma", phase="last") * 2
     with pytest.raises(KeyError):
         scenario.get("value")
     warnings.filterwarnings("ignore", category=DeprecationWarning)
     scenario.add_phase(name="New medicines", days=100, sigma=sigma_new)
     # Summarize scenarios
     summary_df = scenario.summary()
     assert isinstance(summary_df, pd.DataFrame)
     desc_df = scenario.describe()
     assert isinstance(desc_df, pd.DataFrame)
     # Estimation errors
     with pytest.raises(TypeError):
         scenario.estimate(SIRF, phases="1st")
     with pytest.raises(KeyError):
         scenario.estimate(SIRF, phases=["100th"])
Пример #5
0
 def test_add_past_phases(self, jhu_data, population_data):
     scenario = Scenario(jhu_data, population_data, country="India")
     scenario.delete()
     # Phase series
     scenario.clear(name="Medicine")
     scenario.add(days=100)
     scenario.delete(name="Medicine")
     with pytest.raises(TypeError):
         scenario.delete(phase="0th")
     with pytest.raises(TypeError):
         scenario.summary(columns="Population")
     with pytest.raises(KeyError):
         scenario.summary(columns=["Value"])
     # Range of past phases
     scenario.first_date = "01Mar2020"
     scenario.first_date
     scenario.last_date = "16Jul2020"
     scenario.last_date
     with pytest.raises(ValueError):
         scenario.first_date = "01Aug2020"
     with pytest.raises(ValueError):
         scenario.last_date = "01Feb2020"
     # With trend analysis
     scenario.trend(set_phases=True)
     with pytest.raises(ValueError):
         scenario.trend(set_phases=False, n_points=3)
     scenario.combine(phases=["3rd", "4th"])
     scenario.separate(date="30May2020", phase="1st")
     scenario.delete(phases=["1st"])
     scenario.trend(set_phases=False)
     trend_df = scenario.summary()
     assert len(trend_df) == 4
     # add scenarios one by one
     scenario.clear(include_past=True)
     scenario.add(end_date="29May2020")
     scenario.add(end_date="05Jun2020").delete(phases=["0th"])
     scenario.add(end_date="15Jun2020")
     scenario.add(end_date="04Jul2020")
     scenario.add()
     one_df = scenario.summary()
     assert len(one_df) == 4
     # With 0th phase
     scenario.use_0th = True
     scenario.trend(set_phases=False, include_init_phase=True)
     scenario.use_0th = False
     scenario.trend(set_phases=True, include_init_phase=True)
     scenario.delete(phases=["0th"])
     assert len(scenario.summary()) == 5
     with pytest.raises(TypeError):
         scenario.delete(phases="1st")
Пример #6
0
 def test_simulate(self, jhu_data, population_data, country):
     warnings.simplefilter("ignore", category=UserWarning)
     warnings.simplefilter("ignore", category=DeprecationWarning)
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01May2020"
     with pytest.raises(ValueError):
         snl.simulate()
     with pytest.raises(ValueError):
         snl.track()
     snl.trend(show_figure=False)
     # Parameter estimation
     with pytest.raises(ValueError):
         # Deprecated
         snl.param_history(["rho"])
     all_phases = snl.summary().index.tolist()
     snl.disable(all_phases[:-2])
     with pytest.raises(NameError):
         snl.simulate()
     snl.estimate(SIRF, timeout=5, timeout_iteration=5)
     # Simulation
     snl.simulate(variables=[Term.C, Term.CI, Term.F, Term.R])
     snl.simulate(phases=all_phases[-2:])
     # Parameter history (Deprecated)
     snl.param_history([Term.RT], divide_by_first=False)
     snl.param_history(["rho"])
     snl.param_history(["rho"], show_figure=False)
     snl.param_history(["rho"], show_box_plot=False)
     with pytest.raises(KeyError):
         snl.param_history(["feeling"])
     # Comparison of scenarios
     snl.describe()
     snl.track()
     snl.history(target="Rt")
     snl.history(target="sigma")
     snl.history(target="rho", show_figure=False)
     snl.history(target="Infected", phases=all_phases[-2:])
     with pytest.raises(KeyError):
         snl.history(target="temperature")
     # Change rate of parameters
     snl.history_rate(name="Main")
     snl.history_rate(
         name="Main", params=["theta", "kappa"], show_figure=False)
     with pytest.raises(TypeError):
         snl.history_rate(params="", name="Main")
     # Add new scenario
     snl.add(end_date="01Sep2020", name="New")
     snl.describe()
Пример #7
0
 def test_adjust_end(self, jhu_data, population_data, country):
     # Setting
     scenario = Scenario(country=country)
     scenario.register(jhu_data, population_data)
     scenario.timepoints(first_date="01Dec2020", today="01Feb2021")
     # Main scenario
     scenario.add(end_date="01Apr2021", name="Main")
     # New scenario
     scenario.clear(name="New", include_past=True)
     scenario.add(end_date="01Jan2021", name="New")
     # Adjust end date
     scenario.adjust_end()
     # Check output
     assert scenario.get(Term.END, phase="last", name="Main") == "01Apr2021"
     assert scenario.get(Term.END, phase="last", name="New") == "01Apr2021"
Пример #8
0
 def test_simulate(self, jhu_data, population_data, country):
     warnings.simplefilter("ignore", category=UserWarning)
     warnings.simplefilter("ignore", category=DeprecationWarning)
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     snl.trend(show_figure=False)
     # Parameter estimation
     with pytest.raises(ValueError):
         # Deprecated
         snl.param_history(["rho"])
     snl.estimate(SIR)
     # Simulation
     snl.simulate()
     # Parameter history (Deprecated)
     snl.param_history([Term.RT], divide_by_first=False)
     snl.param_history(["rho"])
     snl.param_history(["rho"], show_figure=False)
     snl.param_history(["rho"], show_box_plot=False)
     with pytest.raises(KeyError):
         snl.param_history(["feeling"])
     # Comparison of scenarios
     snl.describe()
     snl.history(target="Rt")
     snl.history(target="sigma")
     snl.history(target="Infected")
     with pytest.raises(KeyError):
         snl.history(target="temperature")
     # Change rate of parameters
     snl.history_rate(name="Main")
     with pytest.raises(TypeError):
         snl.history_rate(params="", name="Main")
     # Add new scenario
     snl.add(end_date="01Sep2020", name="New")
     snl.describe()