pep8 tweaks.

This commit is contained in:
Isaac Laughlin 2017-03-03 11:42:44 -08:00
parent e081db52e1
commit 9c82c8ed7a

View file

@ -2,7 +2,7 @@
# All rights reserved. # All rights reserved.
# #
# This source code is licensed under the BSD-style license found in the # This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant # LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory. # of patent rights can be found in the PATENTS file in the same directory.
from __future__ import absolute_import from __future__ import absolute_import
@ -36,6 +36,7 @@ except ImportError:
class Prophet(object): class Prophet(object):
# Plotting default color for R/Python consistency # Plotting default color for R/Python consistency
forecast_color = '#0072B2' forecast_color = '#0072B2'
def __init__( def __init__(
self, self,
growth='linear', growth='linear',
@ -180,7 +181,6 @@ class Prophet(object):
else: else:
self.changepoints_t = np.array([0]) # dummy changepoint self.changepoints_t = np.array([0]) # dummy changepoint
def get_changepoint_matrix(self): def get_changepoint_matrix(self):
A = np.zeros((self.history.shape[0], len(self.changepoints_t))) A = np.zeros((self.history.shape[0], len(self.changepoints_t)))
for i, t_i in enumerate(self.changepoints_t): for i, t_i in enumerate(self.changepoints_t):
@ -261,7 +261,6 @@ class Prophet(object):
# This relies pretty importantly on pandas keeping the columns in order. # This relies pretty importantly on pandas keeping the columns in order.
return pd.DataFrame(expanded_holidays) return pd.DataFrame(expanded_holidays)
def make_all_seasonality_features(self, df): def make_all_seasonality_features(self, df):
seasonal_features = [ seasonal_features = [
# Add a column of zeros in case no seasonality is used. # Add a column of zeros in case no seasonality is used.
@ -626,7 +625,8 @@ class Prophet(object):
ax.plot(fcst['ds'].values, fcst['cap'], ls='--', c='k') ax.plot(fcst['ds'].values, fcst['cap'], ls='--', c='k')
if uncertainty: if uncertainty:
ax.fill_between(fcst['ds'].values, fcst['yhat_lower'], ax.fill_between(fcst['ds'].values, fcst['yhat_lower'],
fcst['yhat_upper'], color=self.forecast_color, alpha=0.2) fcst['yhat_upper'], color=self.forecast_color,
alpha=0.2)
ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2) ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
ax.set_xlabel(xlabel) ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel) ax.set_ylabel(ylabel)
@ -655,21 +655,24 @@ class Prophet(object):
('plot_yearly', 'yearly' in fcst)] ('plot_yearly', 'yearly' in fcst)]
components = [(plot, cond) for plot, cond in components if cond] components = [(plot, cond) for plot, cond in components if cond]
npanel = len(components) npanel = len(components)
fig, axes = plt.subplots(npanel, 1, facecolor='w', figsize=(9, 3 * npanel)) fig, axes = plt.subplots(npanel, 1, facecolor='w',
figsize=(9, 3 * npanel))
artists = [] artists = []
for ax, plot in zip(axes, [getattr(self, plot) for plot, _ in components]): for ax, plot in zip(axes,
[getattr(self, plot) for plot, _ in components]):
artists += plot(fcst, ax=ax, uncertainty=uncertainty) artists += plot(fcst, ax=ax, uncertainty=uncertainty)
fig.tight_layout() fig.tight_layout()
return artists return artists
def plot_trend(self, fcst, ax=None, uncertainty=True, **plotargs): def plot_trend(self, fcst, ax=None, uncertainty=True, **plotargs):
artists = [] artists = []
if not ax: if not ax:
ax = fig.add_subplot(111) ax = fig.add_subplot(111)
artists += ax.plot(fcst['ds'].values, fcst['trend'], ls='-', c=self.forecast_color) artists += ax.plot(fcst['ds'].values, fcst['trend'], ls='-',
c=self.forecast_color)
if 'cap' in fcst: if 'cap' in fcst:
artists += ax.plot(fcst['ds'].values, fcst['cap'], ls='--', c='k') artists += ax.plot(fcst['ds'].values, fcst['cap'], ls='--', c='k')
if uncertainty: if uncertainty:
@ -682,7 +685,6 @@ class Prophet(object):
ax.set_ylabel('trend') ax.set_ylabel('trend')
return artists return artists
def plot_holidays(self, fcst, ax=None, uncertainty=True): def plot_holidays(self, fcst, ax=None, uncertainty=True):
artists = [] artists = []
if not ax: if not ax:
@ -694,10 +696,12 @@ class Prophet(object):
# NOTE the above CI calculation is incorrect if holidays overlap # NOTE the above CI calculation is incorrect if holidays overlap
# in time. Since it is just for the visualization we will not # in time. Since it is just for the visualization we will not
# worry about it now. # worry about it now.
artists += ax.plot(fcst['ds'].values, y_holiday, ls='-', c=self.forecast_color) artists += ax.plot(fcst['ds'].values, y_holiday, ls='-',
c=self.forecast_color)
if uncertainty: if uncertainty:
artists += [ax.fill_between(fcst['ds'].values, y_holiday_l, y_holiday_u, artists += [ax.fill_between(fcst['ds'].values,
color=self.forecast_color, alpha=0.2)] y_holiday_l, y_holiday_u,
color=self.forecast_color, alpha=0.2)]
ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2) ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
ax.xaxis.set_major_locator(MaxNLocator(nbins=7)) ax.xaxis.set_major_locator(MaxNLocator(nbins=7))
ax.set_xlabel('ds') ax.set_xlabel('ds')
@ -715,10 +719,12 @@ class Prophet(object):
y_weekly = [df_s.loc[d]['weekly'] for d in days] y_weekly = [df_s.loc[d]['weekly'] for d in days]
y_weekly_l = [df_s.loc[d]['weekly_lower'] for d in days] y_weekly_l = [df_s.loc[d]['weekly_lower'] for d in days]
y_weekly_u = [df_s.loc[d]['weekly_upper'] for d in days] y_weekly_u = [df_s.loc[d]['weekly_upper'] for d in days]
artists += ax.plot(range(len(days)), y_weekly, ls='-', c=self.forecast_color) artists += ax.plot(range(len(days)), y_weekly, ls='-',
c=self.forecast_color)
if uncertainty: if uncertainty:
artists += [ax.fill_between(range(len(days)), y_weekly_l, y_weekly_u, artists += [ax.fill_between(range(len(days)),
color=self.forecast_color, alpha=0.2)] y_weekly_l, y_weekly_u,
color=self.forecast_color, alpha=0.2)]
ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2) ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
ax.set_xticks(range(len(days))) ax.set_xticks(range(len(days)))
ax.set_xticklabels(days) ax.set_xticklabels(days)
@ -734,7 +740,7 @@ class Prophet(object):
df_s['doy'] = df_s['ds'].map(lambda x: x.strftime('2000-%m-%d')) df_s['doy'] = df_s['ds'].map(lambda x: x.strftime('2000-%m-%d'))
df_s = df_s.groupby('doy').first().sort_index() df_s = df_s.groupby('doy').first().sort_index()
artists += ax.plot(pd.to_datetime(df_s.index), df_s['yearly'], ls='-', artists += ax.plot(pd.to_datetime(df_s.index), df_s['yearly'], ls='-',
c=self.forecast_color) c=self.forecast_color)
if uncertainty: if uncertainty:
artists += [ax.fill_between( artists += [ax.fill_between(
pd.to_datetime(df_s.index), df_s['yearly_lower'], pd.to_datetime(df_s.index), df_s['yearly_lower'],
@ -748,5 +754,4 @@ class Prophet(object):
return artists return artists
# fb-block 9 # fb-block 9