Skip to content

Instantly share code, notes, and snippets.

@clayh53
clayh53 / jekyll-add-image-title.html
Last active March 12, 2022 21:35
This little Jekyll include surrounds Markdown-generated img tags with figure and figcaption tags and takes the title from the Markdown image tag
<script>
/* === routine to wrap img tags in figure tag and follow it with figcaption
This script collects all the image tags on a page. The routine loops through all
the image tags, and creates two new elements - a <figure> and a <figcaption>.
It then tests if the class is "not-figure", and skips to the next img tag if it
contains this class. The routine then extracts the class or classes from the image tag
and transfers them to the figure tag, since in Kramdown markdown parsing, an
image Markdown written like ![image alt name](image_url,"image title"){:.classname 1 .classname 2}
will attach the classes to the image when they are needed in the figure
*/
begin = lag+1 # position where predictions start
df['predict_cum'] = df['predicted_death'][lag+1:].cumsum()
df['predict_cum'] = df['predict_cum'] + current_death_total
fig,ax = plt.subplots(figsize=(12,6))
plt.grid(axis='both',linestyle=':',linewidth='1')
sns.set()
sns.set_style("whitegrid")
ax.plot(df.date,df['deathIncrease'].rolling(window=rolling_avg).mean(),color='red',label='14 day moving average new daily infections')
df['cumulative_deaths'] = df['deathIncrease'].cumsum(axis=0)
current_death_total = df.at[df.index[-1]+lag+1,'cumulative_deaths'] # picks out the last actual death cumulative total
print(current_death_total)
fig,ax = plt.subplots(figsize=(12,6))
plt.grid(axis='both',linestyle=':',linewidth='1')
sns.set()
sns.set_style("whitegrid")
ax.plot(df.date,df['deathIncrease'].rolling(window=rolling_avg).mean(),color='red',label='14 day moving average new daily infections')
ax.plot(df.date, df['predicted_death'].rolling(window=rolling_avg).mean(), color="blue",linestyle=":",label='14 day moving average new daily deaths')
from sklearn.metrics import r2_score
# create columns of 14 day smoothed data
start_R_calc = 192 # index value for Aug 1 2020
end_R_calc = 316 # index for last 'real' data day at time of writing
df['deathIncrease-14daysmoothed'] = df['deathIncrease'].rolling(window=rolling_avg).mean()
df['predicted_Death-14daysmoothed'] = df['predicted_death'].rolling(window=rolling_avg).mean()
model = df[start_R_calc:end_R_calc]['deathIncrease-14daysmoothed']
prediction = df[start_R_calc:end_R_calc]['predicted_Death-14daysmoothed']
R2 = r2_score(model,prediction)
df = df.append(pd.DataFrame({'date': pd.date_range(start=df.date.iloc[-1], periods=-lag, freq='D', closed='right')}))
df = df.reset_index() # resets index after what is essentially appending another dataframe
df['avg_death_factor'] = [death_factor] * len(df.index) # copies value to entire column
df['predicted_death'] = df['positiveIncrease'].shift(-lag) * df['avg_death_factor'] # predicts future deaths from lagged initial cases
# Plot it
fig,ax = plt.subplots(figsize=(12,6))
plt.grid(axis='both',linestyle=':',linewidth='1')
fig,ax = plt.subplots(figsize=(12,6))
ax.plot(df.date,df['deathIncrease'].rolling(window=rolling_avg).mean(),color='red',label='14 day moving average new daily infections')
sns.set()
sns.set_style("whitegrid")
ax.plot(df.date, df['predicted_death'].shift(-lag).rolling(window=rolling_avg).mean(), color="blue",linestyle=":",label='14 day moving average predicted new daily deaths')
plt.xlim([start_date,end_date]) # moves the prediction forward in time to match the lag as a check
leg = ax.legend(loc='center', frameon=False,bbox_to_anchor=(0.5, -0.17))
leg2=ax2.legend(loc='center', frameon=False,bbox_to_anchor=(0.49, -0.22))
# calculate an average death rate from lagged data
avg_death_from_positive_cases = df["death_prediction_scalar"].rolling(window=rolling_avg).mean()
stable_period = 45
start = lag - stable_period # calculates the index from end of dataframe to start the calculation
death_factor = avg_death_from_positive_cases[start:lag].mean() # average of smoothed data
# copy death scalar to an entire column
df['avg_death_factor'] = [death_factor] * len(df.index) # copies value to entire column
df['predicted_death'] = df['positiveIncrease'] * df['avg_death_factor']
# plot a zoomed in look at data since June 1
fig,ax = plt.subplots(figsize=(12,5))
plt.xlim([start_date,end_date])
plt.ylim(0,0.05)
sns.set()
sns.set_style("whitegrid")
ax.plot(df.date,df['death_prediction_scalar'],color='red')
ax.plot(df.date, df["death_prediction_scalar"].rolling(window=rolling_avg).mean(), color="blue")
# calculate relationship between lagged new daily infections and current daily deaths
df["death_prediction_scalar"] = df["deathIncrease"].shift(lag)/df["positiveIncrease"] # lags deaths back in time by the lag value for this calculation
fig,ax = plt.subplots(figsize=(12,5))
sns.set()
sns.set_style("whitegrid")
ax.plot(df.date,df['death_prediction_scalar'],color='red')
ax.plot(df.date, df["death_prediction_scalar"].rolling(window=rolling_avg).mean(), color="blue")
lag = -21 # number of days from current day that prediction factor occurs
fig,ax = plt.subplots(figsize=(12,6))
ax.plot(df.date,df['positiveIncrease'].rolling(window=rolling_avg).mean(),color='red',label='14 day moving average new daily infections')
ax.set_ylabel('Daily new infections')
ax2=ax.twinx()
ax2.plot(df.date, df["deathIncrease"].shift(lag).rolling(window=rolling_avg).mean(), color="blue",label='14 day moving average new daily deaths')
ax2.grid(False) # turn off grid for second Y axis
ax2.set_ylabel('Daily new deaths')
leg = ax.legend(loc='center', frameon=False,bbox_to_anchor=(0.5, -0.10))
leg2=ax2.legend(loc='center', frameon=False,bbox_to_anchor=(0.49, -0.15))