Debian Codegraph @

tests/test_hist2d.py

raw
1 # -*- coding: utf-8 -*-
2
3 import matplotlib
4 import numpy as np
5 import pytest
6 from matplotlib import pyplot as pl
7 from matplotlib.testing.decorators import image_comparison
8
9 import corner
10
11 try:
12 import scipy # noqa
13 except ImportError:
14 scipy_installed = False
15 else:
16 scipy_installed = True
17
18
19 def _run_hist2d(nm, N=50000, seed=1234, **kwargs):
20 # Generate some fake data.
21 np.random.seed(seed)
22 x = np.random.randn(N)
23 y = np.random.randn(N)
24
25 fig, ax = pl.subplots(1, 1, figsize=(8, 8))
26 corner.hist2d(x, y, ax=ax, **kwargs)
27
28
29 @image_comparison(
30 baseline_images=["cutoff"], remove_text=True, extensions=["png"]
31 )
32 def test_cutoff():
33 _run_hist2d("cutoff", range=[(0, 4), (0, 2.5)])
34
35
36 @pytest.mark.skipif(not scipy_installed, reason="requires scipy for smoothing")
37 @image_comparison(
38 baseline_images=["cutoff2"], remove_text=True, extensions=["png"]
39 )
40 def test_cutoff2():
41 _run_hist2d(
42 "cutoff2",
43 range=[(-4, 4), (-0.1, 0.1)],
44 N=100000,
45 fill_contours=True,
46 smooth=1,
47 )
48
49
50 @image_comparison(
51 baseline_images=["basic"], remove_text=True, extensions=["png"]
52 )
53 def test_basic():
54 _run_hist2d("basic")
55
56
57 @image_comparison(
58 baseline_images=["color"], remove_text=True, extensions=["png"]
59 )
60 def test_color():
61 _run_hist2d("color", color="g")
62
63
64 @image_comparison(
65 baseline_images=["backgroundDark"], remove_text=True, extensions=["png"]
66 )
67 def test_backgroundDark():
68 pl.style.use("dark_background")
69 _run_hist2d("backgroundDark")
70 pl.style.use("default")
71
72
73 @image_comparison(
74 baseline_images=["backgroundDark2"], remove_text=True, extensions=["png"]
75 )
76 def test_backgroundDark2():
77 pl.style.use("dark_background")
78 _run_hist2d("backgroundDark2", color="r")
79 pl.style.use("default")
80
81
82 @image_comparison(
83 baseline_images=["backgroundSolarized"],
84 remove_text=True,
85 extensions=["png"],
86 )
87 def test_backgroundSolarized():
88 pl.style.use("Solarize_Light2")
89 _run_hist2d("backgroundSolarized")
90 pl.style.use("default")
91
92
93 @image_comparison(
94 baseline_images=["backgroundColor"], remove_text=True, extensions=["png"]
95 )
96 def test_backgroundColor():
97 pl.style.use("default")
98 matplotlib.rcParams["axes.facecolor"] = "yellow"
99 matplotlib.rcParams["axes.edgecolor"] = "red"
100 matplotlib.rcParams["xtick.color"] = "green"
101 matplotlib.rcParams["ytick.color"] = "blue"
102 _run_hist2d("backgroundColor")
103 pl.style.use("default")
104
105
106 @image_comparison(
107 baseline_images=["levels1"], remove_text=True, extensions=["png"]
108 )
109 def test_levels1():
110 _run_hist2d("levels1", levels=[0.68, 0.95])
111
112
113 @image_comparison(
114 baseline_images=["levels2"], remove_text=True, extensions=["png"]
115 )
116 def test_levels2():
117 _run_hist2d("levels2", levels=[0.5, 0.75])
118
119
120 @image_comparison(
121 baseline_images=["filled"], remove_text=True, extensions=["png"]
122 )
123 def test_filled():
124 _run_hist2d("filled", fill_contours=True)
125
126
127 @image_comparison(
128 baseline_images=["smooth1"], remove_text=True, extensions=["png"]
129 )
130 def test_smooth1():
131 _run_hist2d("smooth1", bins=50)
132
133
134 @pytest.mark.skipif(not scipy_installed, reason="requires scipy for smoothing")
135 @image_comparison(
136 baseline_images=["smooth2"], remove_text=True, extensions=["png"]
137 )
138 def test_smooth2():
139 _run_hist2d("smooth2", bins=50, smooth=(1.0, 1.5))
140
141
142 @pytest.mark.skipif(not scipy_installed, reason="requires scipy for smoothing")
143 @image_comparison(
144 baseline_images=["philsplot"], remove_text=True, extensions=["png"]
145 )
146 def test_philsplot():
147 _run_hist2d(
148 "philsplot",
149 plot_datapoints=False,
150 fill_contours=True,
151 levels=[0.68, 0.95],
152 color="g",
153 bins=50,
154 smooth=1.0,
155 )
156
157
158 @image_comparison(
159 baseline_images=["lowN"], remove_text=True, extensions=["png"]
160 )
161 def test_lowN():
162 _run_hist2d("lowN", N=20)
163
164
165 @image_comparison(
166 baseline_images=["lowNfilled"], remove_text=True, extensions=["png"]
167 )
168 def test_lowNfilled():
169 _run_hist2d("lowNfilled", N=20, fill_contours=True)
170
171
172 @image_comparison(
173 baseline_images=["lowNnofill"], remove_text=True, extensions=["png"]
174 )
175 def test_lowNnofill():
176 _run_hist2d("lowNnofill", N=20, no_fill_contours=True)
177
178
179 def test_infinite_loop():
180 x, y = np.random.rand(2, 1000)
181 with pytest.raises(ValueError):
182 corner.hist2d(x, y, 20, range=[(0, 1), (2, 3)])