diff --git a/python/prophet/plot.py b/python/prophet/plot.py index 3e7efb9d7..02cdd0ff7 100644 --- a/python/prophet/plot.py +++ b/python/prophet/plot.py @@ -761,7 +761,7 @@ def plot_plotly( data = [] # Add actual - assert m.history + assert m.history is not None data.append(go.Scatter( name='Actual', x=m.history['ds'], @@ -826,7 +826,7 @@ def plot_plotly( line=dict(color=trend_color, width=line_width), )) # Add changepoints - assert m.changepoints + assert m.changepoints is not None if changepoints and len(m.changepoints) > 0: signif_changepoints = m.changepoints[ np.abs(np.nanmean(m.params['delta'], axis=0)) >= changepoints_threshold diff --git a/python/prophet/tests/test_plot.py b/python/prophet/tests/test_plot.py index e3df0489d..009c9b132 100644 --- a/python/prophet/tests/test_plot.py +++ b/python/prophet/tests/test_plot.py @@ -15,6 +15,7 @@ get_forecast_component_plotly_props, get_seasonality_plotly_props, plot_forecast_component, + plot_plotly, plot_seasonality, plot_weekly, plot_yearly, @@ -201,3 +202,24 @@ def test_plotly_draws_data(self, model_and_forecast): traces = get_seasonality_plotly_props(m, "weekly")["traces"] np.testing.assert_array_equal(pd.to_datetime(traces[0].x), data["ds"]) np.testing.assert_array_equal(traces[0].y, data["weekly"]) + + +class TestPlotPlotly: + def test_draws_forecast(self, model_and_forecast): + pytest.importorskip("plotly") + m, fcst = model_and_forecast + fig = plot_plotly(m, fcst) + traces = {trace.name: trace for trace in fig.data if trace.name} + np.testing.assert_array_equal(pd.to_datetime(traces["Actual"].x), m.history["ds"]) + np.testing.assert_array_equal(traces["Actual"].y, m.history["y"]) + np.testing.assert_array_equal(pd.to_datetime(traces["Predicted"].x), fcst["ds"]) + np.testing.assert_array_equal(traces["Predicted"].y, fcst["yhat"]) + + def test_draws_trend_and_changepoints(self, model_and_forecast): + pytest.importorskip("plotly") + m, fcst = model_and_forecast + fig = plot_plotly(m, fcst, trend=True, changepoints=True, changepoints_threshold=0) + traces = {trace.name: trace for trace in fig.data if trace.name} + np.testing.assert_array_equal(traces["Trend"].y, fcst["trend"]) + # With a zero threshold every changepoint is marked. + np.testing.assert_array_equal(pd.to_datetime(fig.data[-1].x), m.changepoints)