lisphilar / covid19-sir

CovsirPhy: Python library for COVID-19 analysis with phase-dependent SIR-derived ODE models.
https://lisphilar.github.io/covid19-sir/
Apache License 2.0
109 stars 44 forks source link

[Fix] snl.estimate_accuracy() showing error without snl.trend() #895

Closed AnujTiwari closed 3 years ago

AnujTiwari commented 3 years ago

Summary

When we run "snl.estimate_accuracy()" without snl.trend(), such as in case of manually adding change points and estiamting SIR-F values. CovSirPhy shows an error message Please` execute ODEHandler.add() in advance

Codes

loader = cs.DataLoader(update_interval=None) loader.read_csv("Barbour.csv", parse_dates=["date"], dayfirst=False) loader.local

loader.assign(country="US").local.columns

loader.lock( date="date", country="country", province="county", confirmed="confirmed", fatal="fatal", recovered="recovered", population="population", ) loader.locked

jhu_data = loader.jhu() snl = cs.Scenario(country="US", province="Barbour") snl.register(jhu_data) snl.records(variables="CIFR");

current_date = snl.today pd.to_datetime(current_date) fourteen_days_before = pd.to_datetime(current_date) - timedelta(days=14) fourteen_days_before

snl.clear(include_past=True) snl.add(end_date=fourteen_days_before) snl.add(end_date=current_date) snl.summary()

snl.estimate(cs.SIRF) snl.summary()

snl.simulate()

Outputs

UnExecutedError: Please execute ODEHandler.add() in advance.

Environment

lisphilar commented 3 years ago

Memo: I saved the data as "input/Barbour.csv" in my PC and raised the error with the following codes.

from datetime import timedelta
import pandas as pd
import numpy as np
import covsirphy as cs

loader = cs.DataLoader(update_interval=None)
loader.read_csv("input/Barbour.csv", parse_dates=["date"], dayfirst=False)
loader.assign(country="US")
loader.lock(
    date="date", country="country", province="county",
    confirmed="confirmed", fatal="fatal", recovered="recovered",
    population="population",
)
jhu_data = loader.jhu()
snl = cs.Scenario(country="US", province="Barbour")
snl.register(jhu_data)
snl.records(variables="CIFR")
current_date = snl.today
fourteen_days_before = pd.to_datetime(current_date) - timedelta(days=14)
snl.clear(include_past=True)
snl.add(end_date=fourteen_days_before)
snl.add(end_date=current_date)
snl.estimate(cs.SIRF)
snl.summary()
snl.simulate()

Details of the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../covid19-sir/covsirphy/analysis/scenario.py", line 833, in simulate
    sim_df = tracker.simulate().set_index(self.DATE).loc[start:end, variables]
  File ".../covid19-sir/covsirphy/analysis/phase_tracker.py", line 376, in simulate
    sim_df = handler.simulate()
  File ".../covid19-sir/covsirphy/ode/ode_handler.py", line 91, in simulate
    raise UnExecutedError("ODEHandler.add()")
covsirphy.util.error.UnExecutedError: Please execute ODEHandler.add() in advance.
lisphilar commented 3 years ago

Memo: Internal variable PhaseTracker._track_df is a dataframe with time index (Date) and many columns, including estimated parameter values and estimated DAY parameter values (1/beta [day] etc.). Simulation needs parameter values saved in this dataframe.

At the previous version 2.21.0-lambda-fu1, all rows which have any NA cells are ignored by .dropna(). When DAY parameters are NAs, this implementation lead to empty data for simulation unexpectedly and raised the error covsirphy.util.error.UnExecutedError: Please execute ODEHandler.add() in advance.

With pull request #896, only rows which have NAs as parameter values will be ignored. NAs of parameter values are not acceptable in simulation, but NAs of DAY parameter values are acceptable in simulation because DAY parameter values are not used for simulation.

lisphilar commented 3 years ago

Dear @AnujTiwari , I think this was solved with #896 (version 2.21.0-mu) and we can use Scenario.simulate() and Scenario.estimate_accuracy() method as expected. Please confirm this. https://gist.github.com/lisphilar/1816d3677a836de61452d443207e9e47

AnujTiwari commented 3 years ago

Hi Lisphilar, Thanks for the update. I confirm that this function works well.

lisphilar commented 3 years ago

Thank you, I will close this issue.