Пример #1
0
 def test_start_record_range(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     # Test
     snl.first_date = "01Apr2020"
     assert snl.first_date == "01Apr2020"
     snl.last_date = "01May2020"
     assert snl.last_date == "01May2020"
     with pytest.raises(ValueError):
         snl.first_date = "01Jan2019"
     with pytest.raises(ValueError):
         tomorrow = Term.tomorrow(datetime.now().strftime(Term.DATE_FORMAT))
         snl.last_date = tomorrow
Пример #2
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")
Пример #3
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
Пример #4
0
 def test_estimate(self, jhu_data, population_data, country):
     warnings.simplefilter("ignore", category=UserWarning)
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     with pytest.raises(ValueError):
         snl.estimate(SIR)
     snl.trend(include_init_phase=True, show_figure=False)
     snl.disable(phases=["0th"])
     with pytest.raises(AttributeError):
         snl.estimate_history(phase="1th")
     # Parameter estimation
     with pytest.raises(KeyError):
         snl.estimate(SIR, phases=["30th"])
     with pytest.raises(ValueError):
         snl.estimate(model=SIR, tau=1440)
     snl.enable(phases=["0th"])
     with pytest.raises(TypeError):
         snl.estimate(model=SIR, phases="1st")
     with pytest.raises(ValueError):
         snl.estimate(model=SIR, phases=["0th"])
     snl.clear(include_past=True)
     snl.trend(show_figure=False)
     snl.estimate(SIR)
     # Estimation history
     snl.estimate_history(phase="1st")
     # Estimation accuracy
     snl.estimate_accuracy(phase="1st")
     # Get a value
     snl.get(Term.RT)
     with pytest.raises(KeyError):
         snl.get("feeling")
Пример #5
0
 def test_summary(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     snl.trend(show_figure=False)
     # One scenario
     assert set(snl.summary().columns) == set(
         [Term.TENSE, Term.START, Term.END, Term.N])
     # Show two scenarios
     snl.clear(name="New")
     cols = snl.summary().reset_index().columns
     assert set([Term.SERIES, Term.PHASE]).issubset(set(cols))
     # Show selected scenario
     cols_sel = snl.summary(name="New").reset_index().columns
     assert not set([Term.SERIES, Term.PHASE]).issubset(set(cols_sel))
     # Columns to show
     show_cols = [Term.N, Term.START]
     assert set(snl.summary(columns=show_cols).columns) == set(show_cols)
     with pytest.raises(TypeError):
         snl.summary(columns=Term.N)
     with pytest.raises(KeyError):
         snl.summary(columns=[Term.N, "Temperature"])
     # To markdown
     snl.summary().to_markdown()
Пример #6
0
 def test_estimate(self, jhu_data, population_data, country):
     warnings.simplefilter("ignore", category=UserWarning)
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     with pytest.raises(ValueError):
         snl.estimate(SIR)
     snl.trend(show_figure=False)
     with pytest.raises(AttributeError):
         snl.estimate_history(phase="last")
     # Parameter estimation
     with pytest.raises(KeyError):
         snl.estimate(SIR, phases=["30th"])
     with pytest.raises(ValueError):
         snl.estimate(model=SIR, tau=1440)
     snl.estimate(SIR, timeout=1, timeout_iteration=1)
     # Estimation history
     snl.estimate_history(phase="last")
     # Estimation accuracy
     snl.estimate_accuracy(phase="last")
     # Get a value
     snl.get(Term.RT)
     with pytest.raises(KeyError):
         snl.get("feeling")
Пример #7
0
 def test_trend(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     # Deprecated
     warnings.simplefilter("error")
     with pytest.raises(DeprecationWarning):
         snl.trend(include_init_phase=False, show_figure=False)
     warnings.simplefilter("ignore")
     snl.trend(include_init_phase=False, show_figure=False)
     # S-R trend analysis
     snl.trend(show_figure=False)
     assert snl["Main"]
     with pytest.raises(ValueError):
         snl.trend(show_figure=False, n_points=3)
     # Disable/enable
     length = len(snl["Main"])
     snl.disable(phases=["0th"], name="Main")
     assert len(snl["Main"]) == length - 1
     snl.enable(phases=["0th"], name="Main")
     assert len(snl["Main"]) == length
     with pytest.raises(TypeError):
         snl.enable(phases="0th", name="Main")
     with pytest.raises(TypeError):
         snl.disable(phases="1st", name="Main")
Пример #8
0
 def test_retrospective(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "19Oct2020"
     snl.trend(show_figure=False)
     # Retrospective analysis
     snl.retrospective(
         "01Sep2020", model=SIR, control="Main", target="Retrospective")
Пример #9
0
 def test_retrospective(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Jun2020"
     snl.trend(show_figure=False)
     # Retrospective analysis
     snl.retrospective(
         "01May2020", model=SIRF, control="Main", target="Retrospective",
         timeout=1, timeout_iteration=1)
Пример #10
0
 def test_add_phase_dep(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     # Test
     warnings.simplefilter("error")
     with pytest.raises(DeprecationWarning):
         snl.add_phase(end_date="01May2020")
     warnings.simplefilter("ignore")
     snl.add_phase(end_date="01May2020")
Пример #11
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()
Пример #12
0
 def test_edit(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     snl.trend(show_figure=False)
     # Combine
     length = len(snl["Main"])
     snl.combine(["1st", "2nd"])
     n_changed = int(population_data.value(country) * 0.98)
     snl.combine(["2nd", "3rd"], population=n_changed)
     assert len(snl["Main"]) == length - 2
     # Separate
     snl.separate(date="01May2020")
     assert len(snl["Main"]) == length - 1
Пример #13
0
 def test_records(self, jhu_data, population_data, country):
     warnings.simplefilter("ignore", category=UserWarning)
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     # Test
     df = snl.records(show_figure=False)
     assert isinstance(df, pd.DataFrame)
     assert set(df.columns) == set(Term.NLOC_COLUMNS)
     dates = df[Term.DATE]
     assert dates.min() == Term.date_obj(snl.first_date)
     assert dates.max() == Term.date_obj(snl.last_date)
     df2 = snl.records(show_figure=True)
     assert isinstance(df2, pd.DataFrame)
     assert set(df2.columns) == set(Term.NLOC_COLUMNS)
Пример #14
0
 def test_trend(self, jhu_data, population_data, country):
     # Setting
     snl = Scenario(jhu_data, population_data, country)
     snl.first_date = "01Apr2020"
     snl.last_date = "01Aug2020"
     snl.trend(show_figure=False)
     assert snl["Main"]
     with pytest.raises(ValueError):
         snl.trend(show_figure=False, n_points=3)
     # Disable/enable
     length = len(snl["Main"])
     snl.disable(phases=["0th"], name="Main")
     assert len(snl["Main"]) == length - 1
     snl.enable(phases=["0th"], name="Main")
     assert len(snl["Main"]) == length
     with pytest.raises(TypeError):
         snl.enable(phases="0th", name="Main")
     with pytest.raises(TypeError):
         snl.disable(phases="1st", name="Main")
Пример #15
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()