def test_add_phase_with_model(self, jhu_data, population_data, country): # Setting population = population_data.value(country) series = PhaseSeries("01Apr2020", "01Aug2020", population) series.add() # Add future phase with model and tau series.add(end_date="01Sep2020", model=SIR, tau=360) series.add(end_date="01Oct2020") length = len(series) assert series.to_dict()[Term.num2str(length - 2)][Term.ODE] == SIR.NAME assert series.to_dict()[Term.num2str(length - 1)][Term.TAU] == 360 series.add(end_date="01Nov2020", rho=0.006) series.add(end_date="01Dec2020", sigma=0.011) assert series.to_dict()[Term.num2str(length + 1)][Term.RT] == 0.55 assert series.to_dict()[Term.num2str(length + 1)]["1/beta [day]"] == 41
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)
def test_records(self, jhu_data, population_data, country): warnings.simplefilter("ignore", category=UserWarning) # Setting dhl = DataHandler(jhu_data, population_data, country) dhl.init_records() dhl.first_date = "01Apr2020" dhl.last_date = "01Aug2020" # Test df = dhl.records(variables=None, show_figure=False) assert isinstance(df, pd.DataFrame) assert set(df.columns) == set([Term.DATE, Term.CI, Term.F, Term.R]) dates = df[Term.DATE] assert dates.min() == Term.date_obj(dhl.first_date) assert dates.max() == Term.date_obj(dhl.last_date) df2 = dhl.records(variables=["Susceptible"], show_figure=True) assert set(df2.columns) == set([Term.DATE, Term.S])
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_find_with_few_records(self, jhu_data, population_data): population = population_data.value("Italy") min_size = 7 df = jhu_data.subset(country="Italy") start_date = df.loc[df.index[0], Term.DATE].strftime(Term.DATE_FORMAT) end_date = Term.date_change(start_date, days=min_size - 2) sr_df = jhu_data.to_sr(country="Italy", population=population, end_date=end_date) with pytest.raises(ValueError): change_finder = ChangeFinder(sr_df, min_size=min_size) change_finder.run()
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
def test_start_record_range(self, jhu_data, population_data, country): # Setting dhl = DataHandler(jhu_data, population_data, country) dhl.init_records() # Test dhl.first_date = "01Apr2020" assert dhl.first_date == "01Apr2020" dhl.last_date = "01May2020" assert dhl.last_date == "01May2020" with pytest.raises(ValueError): dhl.first_date = "01Jan2019" with pytest.raises(ValueError): tomorrow = Term.tomorrow(datetime.now().strftime(Term.DATE_FORMAT)) dhl.last_date = tomorrow
def test_record_range(self, snl, first_date, last_date, today): snl.timepoints() snl.first_date = first_date snl.last_date = last_date snl.today = today assert snl.first_date == first_date assert snl.last_date == last_date assert snl.today == today with pytest.raises(ValueError): snl.first_date = "01Jan2019" with pytest.raises(ValueError): snl.last_date = Term.tomorrow(datetime.now().strftime(Term.DATE_FORMAT)) # Add a phase to today (01Apr2020) snl.add(name="Main") assert snl.get(Term.END, phase="last", name="Main") == today snl.clear(name="Main", include_past=True)
def test_score(self, snl, metrics): try: snl.delete(name="Score") except KeyError: pass snl.clear(name="Score", template="Main") assert isinstance(snl.score(metrics=metrics, name="Score"), float) # Selected phases df = snl.summary(name="Score") all_phases = df.index.tolist() sel_score = snl.score(phases=all_phases[-2:], name="Score") # Selected past days (when the beginning date is a start date) beginning_date = df.loc[df.index[-2], Term.START] past_days = Term.steps(beginning_date, snl.today, tau=1440) assert snl.score(past_days=past_days, name="Score") == sel_score # Selected past days snl.score(past_days=60, name="Score")
def test_score(self, jhu_data, population_data, country): snl = Scenario(jhu_data, population_data, country, tau=360) snl.trend(show_figure=False) snl.estimate(SIRF, timeout=1, timeout_iteration=1) assert isinstance(snl.score(metrics="RMSLE"), float) # Selected phases df = snl.summary() all_phases = df.index.tolist() sel_score = snl.score(phases=all_phases[-2:]) # Selected past days (when the begging date is a start date) beginning_date = df.loc[df.index[-2], Term.START] past_days = Term.steps(beginning_date, snl.last_date, tau=1440) assert snl.score(past_days=past_days) == sel_score # Selected past days snl.score(past_days=60) with pytest.raises(ValueError): snl.score(phases=["1st"], past_days=60)
def test_score(self, jhu_data, population_data, country): snl = Scenario(jhu_data, population_data, country, tau=360) snl.trend(show_figure=False) snl.estimate(SIRF, timeout=10) metrics_list = ["MAE", "MSE", "MSLE", "RMSE", "RMSLE"] for metrics in metrics_list: score = snl.score(metrics=metrics) assert isinstance(score, float) # Selected phases df = snl.summary() all_phases = df.index.tolist() sel_score = snl.score(phases=all_phases[-2:]) # Selected past days (when the begging date is a start date) beginning_date = df.loc[df.index[-2], Term.START] past_days = Term.steps(beginning_date, snl.last_date, tau=1440) assert snl.score(past_days=past_days) == sel_score # Selected past days snl.score(past_days=60)