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..2a8751a48 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, @@ -136,6 +137,23 @@ def test_plotly_draws_data(self, model_and_forecast): np.testing.assert_array_equal(traces[trace_name].y, data[col]) +class TestPlotPlotly: + def test_draws_forecast(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(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"]) + np.testing.assert_array_equal(traces["Trend"].y, fcst["trend"]) + # With a threshold of 0, every changepoint is marked. + changepoint_traces = [trace for trace in fig.data if trace.mode == "markers" and not trace.name] + assert len(changepoint_traces) == 1 + np.testing.assert_array_equal(pd.to_datetime(changepoint_traces[0].x), m.changepoints) + + class TestSeasonalityPlotData: def test_matches_seasonal_components(self, model_and_forecast): m, _ = model_and_forecast