| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
from collections import OrderedDict |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
import pytest |
| 7 |
from matplotlib import pyplot as pl |
| 8 |
from matplotlib.testing.decorators import image_comparison |
| 9 |
|
| 10 |
import corner |
| 11 |
|
| 12 |
|
| 13 |
def _run_corner( |
| 14 |
pandas=False, |
| 15 |
arviz=False, |
| 16 |
N=10000, |
| 17 |
seed=1234, |
| 18 |
ndim=3, |
| 19 |
factor=None, |
| 20 |
exp_data=False, |
| 21 |
**kwargs, |
| 22 |
): |
| 23 |
np.random.seed(seed) |
| 24 |
data1 = np.random.randn(ndim * 4 * N // 5).reshape([4 * N // 5, ndim]) |
| 25 |
data2 = 5 * np.random.rand(ndim)[None, :] + np.random.randn( |
| 26 |
ndim * N // 5 |
| 27 |
).reshape([N // 5, ndim]) |
| 28 |
data = np.vstack([data1, data2]) |
| 29 |
if factor is not None: |
| 30 |
data[:, 0] *= factor |
| 31 |
data[:, 1] /= factor |
| 32 |
if exp_data: |
| 33 |
data = 10**data |
| 34 |
if pandas: |
| 35 |
pd = pytest.importorskip("pandas") |
| 36 |
data = pd.DataFrame.from_dict( |
| 37 |
OrderedDict(zip(map("d{0}".format, range(ndim)), data.T)) |
| 38 |
) |
| 39 |
elif arviz: |
| 40 |
az = pytest.importorskip("arviz") |
| 41 |
data = az.from_dict( |
| 42 |
posterior={"x": data[None]}, |
| 43 |
sample_stats={"diverging": data[None, :, 0] < 0.0}, |
| 44 |
) |
| 45 |
kwargs["truths"] = {"x": np.random.randn(ndim)} |
| 46 |
|
| 47 |
fig = corner.corner(data, **kwargs) |
| 48 |
return fig |
| 49 |
|
| 50 |
|
| 51 |
@image_comparison( |
| 52 |
baseline_images=["basic"], remove_text=True, extensions=["png"] |
| 53 |
) |
| 54 |
def test_basic(): |
| 55 |
_run_corner() |
| 56 |
|
| 57 |
|
| 58 |
@image_comparison( |
| 59 |
baseline_images=["basic_log"], remove_text=True, extensions=["png"] |
| 60 |
) |
| 61 |
def test_basic_log(): |
| 62 |
_run_corner(exp_data=True, axes_scale="log") |
| 63 |
|
| 64 |
|
| 65 |
@image_comparison( |
| 66 |
baseline_images=["basic_log_x2_only"], remove_text=True, extensions=["png"] |
| 67 |
) |
| 68 |
def test_basic_log_x2_only(): |
| 69 |
_run_corner(exp_data=True, axes_scale=["linear", "log", "linear"]) |
| 70 |
|
| 71 |
|
| 72 |
@image_comparison(baseline_images=["labels"], extensions=["png"]) |
| 73 |
def test_labels(): |
| 74 |
_run_corner(labels=["a", "b", "c"]) |
| 75 |
|
| 76 |
|
| 77 |
@image_comparison( |
| 78 |
baseline_images=["quantiles"], remove_text=True, extensions=["png"] |
| 79 |
) |
| 80 |
def test_quantiles(): |
| 81 |
_run_corner(quantiles=[0.16, 0.5, 0.84]) |
| 82 |
|
| 83 |
|
| 84 |
@image_comparison( |
| 85 |
baseline_images=["quantiles_log"], remove_text=True, extensions=["png"] |
| 86 |
) |
| 87 |
def test_quantiles_log(): |
| 88 |
_run_corner(exp_data=True, axes_scale="log", quantiles=[0.16, 0.5, 0.84]) |
| 89 |
|
| 90 |
|
| 91 |
@image_comparison( |
| 92 |
baseline_images=["title_quantiles"], remove_text=False, extensions=["png"] |
| 93 |
) |
| 94 |
def test_title_quantiles(): |
| 95 |
_run_corner( |
| 96 |
quantiles=[0.16, 0.5, 0.84], |
| 97 |
title_quantiles=[0.05, 0.5, 0.95], |
| 98 |
show_titles=True, |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
@image_comparison( |
| 103 |
baseline_images=["title_quantiles_default"], |
| 104 |
remove_text=False, |
| 105 |
extensions=["png"], |
| 106 |
) |
| 107 |
def test_title_quantiles_default(): |
| 108 |
_run_corner(quantiles=[0.16, 0.5, 0.84], show_titles=True) |
| 109 |
|
| 110 |
|
| 111 |
@image_comparison( |
| 112 |
baseline_images=["title_quantiles_raises"], |
| 113 |
remove_text=False, |
| 114 |
extensions=["png"], |
| 115 |
) |
| 116 |
def test_title_quantiles_raises(): |
| 117 |
with pytest.raises(ValueError): |
| 118 |
_run_corner(quantiles=[0.05, 0.16, 0.5, 0.84, 0.95], show_titles=True) |
| 119 |
|
| 120 |
# This one shouldn't raise since show_titles isn't provided |
| 121 |
_run_corner(quantiles=[0.05, 0.16, 0.5, 0.84, 0.95]) |
| 122 |
|
| 123 |
|
| 124 |
@image_comparison( |
| 125 |
baseline_images=["color"], remove_text=True, extensions=["png"] |
| 126 |
) |
| 127 |
def test_color(): |
| 128 |
_run_corner(color="g") |
| 129 |
|
| 130 |
|
| 131 |
@image_comparison( |
| 132 |
baseline_images=["color_filled"], remove_text=True, extensions=["png"] |
| 133 |
) |
| 134 |
def test_color_filled(): |
| 135 |
_run_corner(color="g", fill_contours=True) |
| 136 |
|
| 137 |
|
| 138 |
@image_comparison( |
| 139 |
baseline_images=["overplot"], remove_text=True, extensions=["png"] |
| 140 |
) |
| 141 |
def test_overplot(): |
| 142 |
fig = _run_corner(N=15000, color="g", fill_contours=True) |
| 143 |
_run_corner( |
| 144 |
N=5000, factor=0.5, seed=15, color="b", fig=fig, fill_contours=True |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
@image_comparison( |
| 149 |
baseline_images=["overplot_log"], remove_text=True, extensions=["png"] |
| 150 |
) |
| 151 |
def test_overplot_log(): |
| 152 |
fig = _run_corner( |
| 153 |
N=15000, |
| 154 |
exp_data=True, |
| 155 |
axes_scale="log", |
| 156 |
color="g", |
| 157 |
fill_contours=True, |
| 158 |
) |
| 159 |
_run_corner( |
| 160 |
N=5000, |
| 161 |
factor=0.5, |
| 162 |
seed=15, |
| 163 |
exp_data=True, |
| 164 |
axes_scale="log", |
| 165 |
color="b", |
| 166 |
fig=fig, |
| 167 |
fill_contours=True, |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
@image_comparison( |
| 172 |
baseline_images=["bins"], remove_text=True, extensions=["png"] |
| 173 |
) |
| 174 |
def test_bins(): |
| 175 |
_run_corner(bins=25) |
| 176 |
|
| 177 |
|
| 178 |
@image_comparison( |
| 179 |
baseline_images=["bins_log"], remove_text=True, extensions=["png"] |
| 180 |
) |
| 181 |
def test_bins_log(): |
| 182 |
_run_corner(exp_data=True, axes_scale="log", bins=25) |
| 183 |
|
| 184 |
|
| 185 |
@image_comparison( |
| 186 |
baseline_images=["smooth"], remove_text=True, extensions=["png"] |
| 187 |
) |
| 188 |
def test_smooth(): |
| 189 |
pytest.importorskip("scipy") |
| 190 |
_run_corner(bins=50, smooth=1.0) |
| 191 |
|
| 192 |
|
| 193 |
@image_comparison( |
| 194 |
baseline_images=["smooth_log"], remove_text=True, extensions=["png"] |
| 195 |
) |
| 196 |
def test_smooth_log(): |
| 197 |
pytest.importorskip("scipy") |
| 198 |
_run_corner(exp_data=True, axes_scale="log", bins=50, smooth=1.0) |
| 199 |
|
| 200 |
|
| 201 |
@image_comparison( |
| 202 |
baseline_images=["smooth1d"], remove_text=True, extensions=["png"] |
| 203 |
) |
| 204 |
def test_smooth1d(): |
| 205 |
pytest.importorskip("scipy") |
| 206 |
_run_corner(bins=50, smooth=1.0, smooth1d=1.0) |
| 207 |
|
| 208 |
|
| 209 |
@image_comparison( |
| 210 |
baseline_images=["smooth1d_log"], remove_text=True, extensions=["png"] |
| 211 |
) |
| 212 |
def test_smooth1d_log(): |
| 213 |
pytest.importorskip("scipy") |
| 214 |
_run_corner( |
| 215 |
exp_data=True, axes_scale="log", bins=50, smooth=1.0, smooth1d=1.0 |
| 216 |
) |
| 217 |
|
| 218 |
|
| 219 |
@image_comparison(baseline_images=["titles1"], extensions=["png"]) |
| 220 |
def test_titles1(): |
| 221 |
_run_corner(show_titles=True) |
| 222 |
|
| 223 |
|
| 224 |
@image_comparison(baseline_images=["titles2"], extensions=["png"]) |
| 225 |
def test_titles2(): |
| 226 |
_run_corner(show_titles=True, title_fmt=None, labels=["a", "b", "c"]) |
| 227 |
|
| 228 |
|
| 229 |
@image_comparison( |
| 230 |
baseline_images=["top_ticks"], remove_text=True, extensions=["png"] |
| 231 |
) |
| 232 |
def test_top_ticks(): |
| 233 |
_run_corner(top_ticks=True) |
| 234 |
|
| 235 |
|
| 236 |
@image_comparison(baseline_images=["pandas"], extensions=["png"]) |
| 237 |
def test_pandas(): |
| 238 |
_run_corner(pandas=True) |
| 239 |
|
| 240 |
|
| 241 |
@image_comparison( |
| 242 |
baseline_images=["truths"], remove_text=True, extensions=["png"] |
| 243 |
) |
| 244 |
def test_truths(): |
| 245 |
_run_corner(truths=[0.0, None, 0.15]) |
| 246 |
|
| 247 |
|
| 248 |
@image_comparison( |
| 249 |
baseline_images=["reverse_truths"], remove_text=True, extensions=["png"] |
| 250 |
) |
| 251 |
def test_reverse_truths(): |
| 252 |
_run_corner(truths=[0.0, None, 0.15], reverse=True) |
| 253 |
|
| 254 |
|
| 255 |
@image_comparison( |
| 256 |
baseline_images=["no_fill_contours"], remove_text=True, extensions=["png"] |
| 257 |
) |
| 258 |
def test_no_fill_contours(): |
| 259 |
_run_corner(no_fill_contours=True) |
| 260 |
|
| 261 |
|
| 262 |
@image_comparison( |
| 263 |
baseline_images=["tight"], remove_text=True, extensions=["png"] |
| 264 |
) |
| 265 |
def test_tight(): |
| 266 |
_run_corner(ret=True) |
| 267 |
pl.tight_layout() |
| 268 |
|
| 269 |
|
| 270 |
@image_comparison( |
| 271 |
baseline_images=["reverse"], remove_text=True, extensions=["png"] |
| 272 |
) |
| 273 |
def test_reverse(): |
| 274 |
_run_corner(ndim=2, range=[(4, -4), (-5, 5)]) |
| 275 |
|
| 276 |
|
| 277 |
@image_comparison( |
| 278 |
baseline_images=["reverse_log"], remove_text=True, extensions=["png"] |
| 279 |
) |
| 280 |
def test_reverse_log(): |
| 281 |
_run_corner( |
| 282 |
ndim=2, |
| 283 |
exp_data=True, |
| 284 |
axes_scale="log", |
| 285 |
range=[(1e4, 1e-4), (1e-5, 1e5)], |
| 286 |
) |
| 287 |
|
| 288 |
|
| 289 |
@image_comparison( |
| 290 |
baseline_images=["extended_overplotting"], |
| 291 |
remove_text=True, |
| 292 |
extensions=["png"], |
| 293 |
) |
| 294 |
def test_extended_overplotting(): |
| 295 |
# Test overplotting a more complex plot |
| 296 |
labels = [r"$\theta_1$", r"$\theta_2$", r"$\theta_3$", r"$\theta_4$"] |
| 297 |
|
| 298 |
figure = _run_corner(ndim=4, reverse=False, labels=labels) |
| 299 |
|
| 300 |
# Set same results: |
| 301 |
ndim, nsamples = 4, 10000 |
| 302 |
np.random.seed(1234) |
| 303 |
|
| 304 |
data1 = np.random.randn(ndim * 4 * nsamples // 5).reshape( |
| 305 |
[4 * nsamples // 5, ndim] |
| 306 |
) |
| 307 |
mean = 4 * np.random.rand(ndim) |
| 308 |
|
| 309 |
value1 = mean |
| 310 |
# This is the empirical mean of the sample: |
| 311 |
value2 = np.mean(data1, axis=0) |
| 312 |
|
| 313 |
corner.overplot_lines(figure, value1, color="C1", reverse=False) |
| 314 |
corner.overplot_points( |
| 315 |
figure, value1[None], marker="s", color="C1", reverse=False |
| 316 |
) |
| 317 |
corner.overplot_lines(figure, value2, color="C2", reverse=False) |
| 318 |
corner.overplot_points( |
| 319 |
figure, value2[None], marker="s", color="C2", reverse=False |
| 320 |
) |
| 321 |
|
| 322 |
|
| 323 |
@image_comparison( |
| 324 |
baseline_images=["reverse_overplotting"], |
| 325 |
remove_text=True, |
| 326 |
extensions=["png"], |
| 327 |
) |
| 328 |
def test_reverse_overplotting(): |
| 329 |
# Test overplotting with a reversed plot |
| 330 |
labels = [r"$\theta_1$", r"$\theta_2$", r"$\theta_3$", r"$\theta_4$"] |
| 331 |
|
| 332 |
figure = _run_corner(ndim=4, reverse=True, labels=labels) |
| 333 |
|
| 334 |
# Set same results: |
| 335 |
ndim, nsamples = 4, 10000 |
| 336 |
np.random.seed(1234) |
| 337 |
|
| 338 |
data1 = np.random.randn(ndim * 4 * nsamples // 5).reshape( |
| 339 |
[4 * nsamples // 5, ndim] |
| 340 |
) |
| 341 |
mean = 4 * np.random.rand(ndim) |
| 342 |
|
| 343 |
value1 = mean |
| 344 |
value2 = np.mean(data1, axis=0) |
| 345 |
|
| 346 |
corner.overplot_lines(figure, value1, color="C1", reverse=True) |
| 347 |
corner.overplot_points( |
| 348 |
figure, value1[None], marker="s", color="C1", reverse=True |
| 349 |
) |
| 350 |
corner.overplot_lines(figure, value2, color="C2", reverse=True) |
| 351 |
corner.overplot_points( |
| 352 |
figure, value2[None], marker="s", color="C2", reverse=True |
| 353 |
) |
| 354 |
|
| 355 |
|
| 356 |
@image_comparison( |
| 357 |
baseline_images=["hist_bin_factor"], remove_text=True, extensions=["png"] |
| 358 |
) |
| 359 |
def test_hist_bin_factor(): |
| 360 |
_run_corner(hist_bin_factor=4) |
| 361 |
|
| 362 |
|
| 363 |
@image_comparison( |
| 364 |
baseline_images=["hist_bin_factor_log"], |
| 365 |
remove_text=True, |
| 366 |
extensions=["png"], |
| 367 |
) |
| 368 |
def test_hist_bin_factor_log(): |
| 369 |
_run_corner(exp_data=True, axes_scale="log", hist_bin_factor=4) |
| 370 |
|
| 371 |
|
| 372 |
@image_comparison(baseline_images=["arviz"], extensions=["png"]) |
| 373 |
def test_arviz(): |
| 374 |
_run_corner(arviz=True) |
| 375 |
|
| 376 |
|
| 377 |
@image_comparison( |
| 378 |
baseline_images=["range_fig_arg"], remove_text=True, extensions=["png"] |
| 379 |
) |
| 380 |
def test_range_fig_arg(): |
| 381 |
fig = pl.figure() |
| 382 |
ranges = [(-1.1, 1), 0.8, (-1, 1)] |
| 383 |
_run_corner(N=100_000, range=ranges, fig=fig) |