Browse Source

smooth spline

main
parent
commit
e5be6bdba1
  1. 32
      tornado/tornado.py

32
tornado/tornado.py

@ -5,6 +5,8 @@ import requests
import matplotlib.pyplot as plt
from matplotlib import rc
import json
from scipy import interpolate
import numpy as np
# %% Use LaTeX
rc('text', usetex=True)
@ -24,45 +26,33 @@ x = r.json()
#%%
df = pd.DataFrame(x['timeline'])
df = df.reset_index()
#%% convert index to datetime, messing things up
# df.index = pd.to_datetime(df.index, dayfirst=True)
df.index = pd.to_datetime(df.index)
#df = df.reset_index()
# %%
df['daily_deaths'] = df['deaths'].diff().abs() # dirty trick to prevent negative outliers
df['daily_deaths_avg'] = df['daily_deaths'].rolling(7).mean()
df['death_change'] = df['daily_deaths_avg'].diff()
# %% test polyfit
df.fillna(value=0, inplace=True)
poly = np.polyfit(df['death_change'].values, df['daily_deaths_avg'].values, 10)
poly_tornado = np.poly1d(poly)(df['death_change'].values)
df['tornado'] = poly_tornado
# %% check B spline
# https://github.com/kawache/Python-B-spline-examples
# %% Try to smooth the curve with interpolation
# df['death_change'] = df['death_change'].interpolate(method='cubic')
#df.fillna(value=0, inplace=True)
# %%
#df['death_change'] = df['death_change'].rolling(3).mean()
df = df.resample('4H').asfreq()
# %%
# df.drop(columns=['cases', 'deaths', 'recovered'], inplace=True)
df = df.interpolate(method='spline', order=5)
# %%
df.plot(x='death_change', y='daily_deaths_avg')
# df['daily_deaths_avg'] = df['daily_deaths'].rolling(14).mean()
# df['death_change'] = df['daily_deaths_avg'].diff()
# %%
ax = plt.figure(figsize=cm2inch(15,15), frameon=False)
plt.plot(df['death_change'], df['daily_deaths_avg'])
plt.show()
#%%
#df.plot()
df.plot(x='death_change', y='tornado')
#df.plot(x='death_change', y='tornado')
Loading…
Cancel
Save