def run_models(self, refs, which): growths = self.get_growths(refs, which) R0s = [] ms = [] ms_bad = [] ms_good = [] for st_, (g, _) in growths.iterrows(): params = covid19.params(region=st_) R0 = min(formulas.R0_from_K("SEAIR", params, K=np.log(g)), 2.5) R0s.append(R0) m = models.SEAIR(disease=covid19, region=st_, R0=R0) m.set_data(info.get_seair_curves_for_region(st_)) base = m.copy() m.run(120) ms.append(m.clinical.overflow_model()) m = base.copy(R0=2.74) m.run(120) ms_bad.append(m.clinical.overflow_model()) m = base.copy(R0=1.0) m.run(120) ms_good.append(m.clinical.overflow_model()) return map(tuple, [ms_good, ms, ms_bad])
def _test_params(self): p = covid19.params() # Aliases assert p.Qs == p.prob_symptoms assert p.Qsv == p.prob_severe assert p.Qcr == p.prob_critical # Transforms assert p.gamma == 1 / p.infectious_period assert p.sigma == 1 / p.incubation_period
import numpy as np import pandas as pd import mundi from pydemic import fitting as fit, models from pydemic.diseases import covid19 from pydemic_ui import st region = mundi.region(st.text_input("Region", value="BR")) params = covid19.params(region=region) cases = covid19.epidemic_curve(region) def fit_infectious(df, gamma=params.gamma, fmt="{}-smooth", **kwargs): return pd.DataFrame( { fmt.format(key): _infectious_curve(col, gamma, **kwargs) for key, col in df.items() }, index=df.index, ) def _infectious_curve(col, gamma, **kwargs): cases = fit.infectious_curve(col, gamma, **kwargs) cases *= col[-1] / cases.sum() return cases def seir_curves(cases, params, **kwargs): for param in ["gamma", "sigma", "prob_symptoms", "rho", "R0"]:
def show(self): curves = self.get_epidemic_curves(self.user_inputs['states'].index).fillna(0) # Acc cases ax = curves.iloc[-30:, self.user_inputs['idx']::2].plot(legend=False, grid=True) curves[self.user_inputs['loc'], self.user_inputs['which']].iloc[-30:].plot(ax=ax, legend=False, grid=True) st.pyplot() # Daily cases # ax = curves.iloc[-30:, self.user_inputs['idx']::2] # curves[self.user_inputs['loc'], self.user_inputs['which']] \ # .iloc[-30:] \ # .diff() \ # .plot(ax=ax, legend=False, grid=True) # st.pyplot() # # Growth factors growths = self.get_growths(self.user_inputs['states'].index, self.user_inputs['which']) ci = pd.DataFrame({"low": growths["value"] - growths["std"], "std": growths["std"]}) st.header("Growth factor +/- error") ci.plot.bar(width=0.9, ylim=(0.8, 2), stacked=True, grid=True) plt.plot(self.user_inputs['states'].index, [1] * len(self.user_inputs['states']), "k--") st.pyplot() # # Duplication times st.header("Duplication time") (np.log(2) / np.log(growths["value"])).plot.bar(grid=True, ylim=(0, 30)) st.pyplot() # R0 st.header("R0") params = covid19.params(region=self.user_inputs['loc']) ( np.log(growths["value"]) .apply(lambda K: formulas.R0_from_K("SEAIR", params, K=K)) .plot.bar(width=0.9, grid=True, ylim=(0, 4)) ) st.pyplot() ms_good, ms_keep, ms_bad = self.run_models(self.user_inputs['states'].index, self.user_inputs['which']) # ICU overflow for ms, msg in [ (ms_keep, "keep trends"), (ms_bad, "no distancing"), (ms_good, "more distancing"), ]: st.header(f"Deaths and ICU overflow ({msg})") deaths = pd.DataFrame({m.region.id: m["deaths:dates"] for m in ms}) deaths.plot(legend=False, color="0.5") deaths.sum(1).plot(grid=True) deaths[self.user_inputs['loc']].plot(legend=True, grid=True) st.cards({"Total de mortes": fmt(deaths.iloc[-1].sum())}) st.pyplot() data = {} for m in ms: overflow = m.results["dates.icu_overflow"] if overflow: data[m.region.id] = (overflow - pd.to_datetime(today())).days if data: data = pd.Series(data) data.plot.bar(width=0.9, grid=True) st.pyplot()