|
| 1 | +# Copyright 2020 The PyMC Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import numpy as np |
| 17 | +import pytest |
| 18 | + |
| 19 | +from arviz import from_pymc3 |
| 20 | + |
| 21 | +import pymc3 as pm |
| 22 | + |
| 23 | +from pymc3.backends.ndarray import point_list_to_multitrace |
| 24 | +from pymc3.plots import plot_posterior_predictive_glm |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("inferencedata", [True, False]) |
| 28 | +def test_plot_posterior_predictive_glm_defaults(inferencedata): |
| 29 | + with pm.Model() as model: |
| 30 | + pm.Normal("x") |
| 31 | + pm.Normal("Intercept") |
| 32 | + trace = point_list_to_multitrace([{"x": np.array([1]), "Intercept": np.array([1])}], model) |
| 33 | + if inferencedata: |
| 34 | + trace = from_pymc3(trace, model=model) |
| 35 | + _, ax = plt.subplots() |
| 36 | + plot_posterior_predictive_glm(trace, samples=1) |
| 37 | + lines = ax.get_lines() |
| 38 | + expected_xvalues = np.linspace(0, 1, 100) |
| 39 | + expected_yvalues = np.linspace(1, 2, 100) |
| 40 | + for line in lines: |
| 41 | + x_axis, y_axis = line.get_data() |
| 42 | + np.testing.assert_array_equal(x_axis, expected_xvalues) |
| 43 | + np.testing.assert_array_equal(y_axis, expected_yvalues) |
| 44 | + assert line.get_lw() == 0.2 |
| 45 | + assert line.get_c() == "k" |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.parametrize("inferencedata", [True, False]) |
| 49 | +def test_plot_posterior_predictive_glm_non_defaults(inferencedata): |
| 50 | + with pm.Model() as model: |
| 51 | + pm.Normal("x") |
| 52 | + pm.Normal("Intercept") |
| 53 | + trace = point_list_to_multitrace([{"x": np.array([1]), "Intercept": np.array([1])}], model) |
| 54 | + if inferencedata: |
| 55 | + trace = from_pymc3(trace, model=model) |
| 56 | + _, ax = plt.subplots() |
| 57 | + plot_posterior_predictive_glm( |
| 58 | + trace, samples=1, lm=lambda x, _: x, eval=np.linspace(0, 1, 10), lw=0.3, c="b" |
| 59 | + ) |
| 60 | + lines = ax.get_lines() |
| 61 | + expected_xvalues = np.linspace(0, 1, 10) |
| 62 | + expected_yvalues = np.linspace(0, 1, 10) |
| 63 | + for line in lines: |
| 64 | + x_axis, y_axis = line.get_data() |
| 65 | + np.testing.assert_array_equal(x_axis, expected_xvalues) |
| 66 | + np.testing.assert_array_equal(y_axis, expected_yvalues) |
| 67 | + assert line.get_lw() == 0.3 |
| 68 | + assert line.get_c() == "b" |
0 commit comments