In [1]:
import pandas as pd
import nivapy3 as nivapy
import matplotlib.pyplot as plt
from fbprophet import Prophet
from fbprophet.plot import add_changepoints_to_plot
In [2]:
# Connect to db
eng = nivapy.da.connect()
A quick test of Facebook's Prophet algorithm, as applied to ICPW.
In [3]:
# Select project
prj_grid = nivapy.da.select_resa_projects(eng)
prj_grid
In [4]:
# Select project
prj_df = prj_grid.get_selected_df()
prj_df
Out[4]:
In [5]:
# Get stations for these projects
stn_df = nivapy.da.select_resa_project_stations(prj_df, eng)
print (len(stn_df), 'stations associated with the selected projects.')
stn_df.head()
Out[5]:
In [6]:
# Map
nivapy.spatial.quickmap(stn_df,
popup='station_code',
aerial_imagery=True)
Out[6]:
In [7]:
# Get available parameters
st_dt = '1970-01-01'
end_dt = '2019-01-01'
par_grid = nivapy.da.select_resa_station_parameters(stn_df,
st_dt,
end_dt,
eng)
par_grid
In [8]:
# Select relevant parameters
par_df = par_grid.get_selected_df()
par_df
Out[8]:
In [9]:
# Get chem
wc_df, dup_df = nivapy.da.select_resa_water_chemistry(stn_df,
par_df,
st_dt,
end_dt,
eng,
drop_dups=True,
lod_flags=False)
wc_df.head()
Out[9]:
In [10]:
# Save for later
wc_df.to_csv('icpw_norway_chem.csv', index=False, encoding='utf-8')
In [11]:
# Load saved data
wc_df = pd.read_csv('icpw_norway_chem.csv', encoding='utf-8')
wc_df['sample_date'] = pd.to_datetime(wc_df['sample_date'])
In [12]:
# Choose site
stn = 'BIE01'
# Resample to monthly for chosen site
df = wc_df.query('station_code == @stn')
df.index = df['sample_date']
df = df.resample('M').mean().reset_index()
df.head()
Out[12]:
In [13]:
# Get TOC
toc_df = df[['sample_date', 'TOC_mg C/l']].copy()
toc_df.columns = ['ds', 'y']
toc_df.head()
Out[13]:
In [14]:
# Fit model
m = Prophet()
m.fit(toc_df)
Out[14]:
In [15]:
# Predict the next 10 years, with uncertainty
future = m.make_future_dataframe(periods=120, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
a = add_changepoints_to_plot(fig.gca(), m, fcst)
#plt.savefig(stn + '_TOC.png', dpi=300)
In [16]:
fig2 = m.plot_components(fcst)
In [ ]:
# Get SO4
so4_df = df[['sample_date', 'SO4_mg/l']].copy()
so4_df.columns = ['ds', 'y']
so4_df.head()
In [ ]:
# Fit model
so4_df['cap'] = so4_df['y'].max()
m = Prophet(growth='logistic')
m.fit(so4_df)
In [ ]:
# Predict the next 10 years, with uncertainty
future = m.make_future_dataframe(periods=120, freq='M')
future['cap'] = so4_df['y'].max()
fcst = m.predict(future)
fig = m.plot(fcst)
a = add_changepoints_to_plot(fig.gca(), m, fcst)
plt.savefig(stn + '_SO4.png', dpi=300)
Based on the above plots, SO4 at site 'CA01'
declined steadily until about 2004, then started to decrease more rapidly.