1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
|
import healpy as hp
from healpy.newvisufunc import projview, newprojplot
import matplotlib.pyplot as plt
import numpy as np
import pytest
import os.path
path = os.path.dirname(os.path.realpath(__file__))
@pytest.fixture
def map_data():
return hp.read_map(
os.path.join(
path,
"data",
"wmap_band_iqumap_r9_7yr_W_v4_udgraded32_masked_smoothed10deg_fortran.fits",
)
)
def test_projview_mollweide(map_data):
projview(map_data, coord=["G"], flip="astro", projection_type="mollweide")
def test_projview_mollweide_graticule(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
projection_type="mollweide",
)
def test_projview_mollweide_graticule_labels(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="horizontal",
projection_type="mollweide",
)
def test_projview_mollweide_vertical_cbar(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
projection_type="mollweide",
)
def test_projview_mollweide_extended_cbar(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=45,
projection_type="mollweide",
title="Mollweide projection, astro convention (default)",
)
newprojplot(
theta=np.radians(50), phi=np.radians(60), marker="o", color="r", markersize=10
)
def test_projview_mollweide_geo_convention(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=45,
projection_type="mollweide",
title="Mollweide projection, geo convention",
flip="geo",
phi_convention="clockwise",
)
newprojplot(
theta=np.radians(50), phi=np.radians(60), marker="o", color="r", markersize=10
)
def test_projview_hammer(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="hammer",
title="Hammer projection",
)
def test_projview_aitoff(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="aitoff",
title="Aitoff projection",
)
def test_projview_cart(map_data):
projview(map_data, coord=["G"], projection_type="cart")
def test_projview_cart_labels(map_data):
projview(
map_data, coord=["G"], projection_type="cart", xlabel="xlabel", ylabel="ylabel"
)
def test_projview_cart_graticule(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="horizontal",
projection_type="cart",
title="Cart projection",
)
def test_projview_cart_graticule_vertical_cbar(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
projection_type="cart",
)
def test_projview_cart_lonra_latra(map_data):
projview(
map_data,
coord=["G"],
projection_type="cart",
lonra=[-30, 30],
latra=[-30, 30],
)
def test_projview_lonra_latra_error(map_data):
# Test that both lonra and latra raise error for mollweide (default projection)
with pytest.raises(ValueError):
projview(
map_data,
coord=["G"],
lonra=[-30, 30],
latra=[-30, 30],
)
def test_projview_lambert_lonra_error(map_data):
# Test that lonra raises error for lambert projection
with pytest.raises(ValueError, match="lonra cannot be set for projection_type='lambert'"):
projview(
map_data,
coord=["G"],
projection_type="lambert",
lonra=[-30, 30],
)
def test_projview_lambert_half_sky(map_data):
# Test lambert projection with half sky using latra
projview(
map_data,
coord=["G"],
projection_type="lambert",
latra=[0, 90],
graticule=True,
graticule_labels=True,
title="Lambert half-sky projection",
)
def test_projview_3d(map_data):
projview(
map_data,
coord=["G"],
hold=False,
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="horizontal",
projection_type="3d",
title="3D projection",
)
def test_projview_3d_vertical_cbar(map_data):
projview(
map_data,
coord=["G"],
hold=False,
graticule=True,
graticule_labels=True,
projection_type="3d",
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
cmap="viridis",
)
def test_projview_polar(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
cb_orientation="horizontal",
projection_type="polar",
title="Polar projection",
)
def test_projview_polar_vertical_cbar(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
cb_orientation="vertical",
projection_type="polar",
)
def test_projview_polar_override(map_data):
projview(
map_data,
coord=["G"],
hold=False,
graticule=True,
graticule_labels=True,
flip="astro",
projection_type="polar",
unit="cbar label",
cb_orientation="horizontal",
override_plot_properties={
"cbar_shrink": 0.5,
"cbar_pad": 0.02,
"cbar_label_pad": -35,
"figure_width": 16,
"figure_size_ratio": 0.63,
},
)
def test_projview_hammer_fontsize(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit=r"cbar label $\alpha$",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="hammer",
title="Hammer projection",
fontsize={
"xlabel": 20,
"ylabel": 20,
"xtick_label": 20,
"ytick_label": 20,
"title": 20,
"cbar_label": 20,
"cbar_tick_label": 20,
},
xtick_label_color="r",
ytick_label_color="g",
graticule_color="black",
)
def test_projview_hammer_no_phi_tick_label_shift(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit=r"cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="hammer",
title="Hammer projection",
phi_convention="symmetrical",
)
s = 500
plt.scatter(np.deg2rad(0), np.deg2rad(0), color="r", marker="x", linewidth=10, s=s)
plt.scatter(
np.deg2rad(120), np.deg2rad(0), color="r", marker="x", linewidth=10, s=s
)
plt.scatter(
np.deg2rad(-120), np.deg2rad(0), color="r", marker="x", linewidth=10, s=s
)
plt.scatter(np.deg2rad(0), np.deg2rad(60), color="r", marker="x", linewidth=10, s=s)
plt.scatter(
np.deg2rad(0), np.deg2rad(-60), color="r", marker="x", linewidth=10, s=s
)
def test_projview_hammer_override_axis_tick_labels(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit=r"cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="hammer",
title="Hammer projection",
custom_xtick_labels=["A", "B", "C", "D", "E"],
custom_ytick_labels=["F", "G", "H", "I", "J"],
)
def test_projview_hammer_equatorial(map_data):
projview(
map_data,
coord=["G", "C"],
graticule=True,
graticule_labels=True,
unit=r"cbar label",
xlabel="RA",
ylabel="DEC",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="hammer",
title="Hammer projection",
)
def test_projview_cart_local_azimuth_counterclockwise(map_data):
local_sidereal_time = 18
altitude = -35.206667
rotAngles = [(180 + (local_sidereal_time * 15)) % 360, -(altitude - 90)]
projview(
map_data,
coord=["G", "C"],
graticule=True,
graticule_labels=True,
unit=r"cbar label",
xlabel="azimuth",
ylabel="zenith",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="cart",
title="Cartesian projection",
rot=rotAngles,
fontsize={"xtick_label": 20},
phi_convention="counterclockwise",
)
def test_projview_cart_local_azimuth_clockwise(map_data):
local_sidereal_time = 18
altitude = -35.206667
rotAngles = [(180 + (local_sidereal_time * 15)) % 360, -(altitude - 90)]
projview(
map_data,
coord=["G", "C"],
graticule=True,
graticule_labels=True,
unit=r"cbar label",
xlabel="azimuth",
ylabel="zenith",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="cart",
title="Cartesian projection",
rot=rotAngles,
fontsize={"xtick_label": 20},
phi_convention="clockwise",
)
def test_projview_cart_local_azimuth_symmetrical(map_data):
local_sidereal_time = 18
altitude = -35.206667
rotAngles = [(180 + (local_sidereal_time * 15)) % 360, -(altitude - 90)]
projview(
map_data,
coord=["G", "C"],
graticule=True,
graticule_labels=True,
unit=r"cbar label",
xlabel="azimuth",
ylabel="zenith",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=30,
projection_type="cart",
title="Cartesian projection",
rot=rotAngles,
fontsize={"xtick_label": 20},
phi_convention="symmetrical",
)
def test_projview_return_only_data(map_data):
longitude, latitude, grid_map = projview(
map_data, coord=["G"], return_only_data=True
)
def test_projview_planck_colormap(map_data):
m_scaled = map_data * 3000
projview(
m_scaled,
title="Planck colormap",
cmap="planck",
rlabel=r"A$_{\mathsf{ex. 1}}$",
llabel=r"$Q$",
unit=r"$\mu$K",
fontname="serif",
width=10,
show_tickmarkers=True,
cbar_ticks=[-300, 0, 300],
cb_orientation="vertical",
sub=121,
override_plot_properties={"cbar_tick_direction": "in"},
)
projview(
m_scaled,
title="Planck logarithmic colormap",
cmap="planck_log",
norm="symlog2",
rlabel=r"A$_{\mathrm{ex. 3}}$",
llabel=r"$I$",
unit=r"$\mu$K",
min=-1e3,
max=1e7,
cb_orientation="vertical",
sub=122,
)
plt.tight_layout()
def test_projview_symlog_normalization(map_data):
m_scaled = map_data * 3000
projview(
m_scaled,
title="symlog normalization",
cmap="planck",
norm="symlog",
rlabel=r"A$_{\mathsf{ex. 2}}$",
llabel=r"$Q$",
unit=r"$\mu$K",
cbar_ticks=[-3000, -30, 0, 30, 3000],
remove_mono=True,
show_tickmarkers=True,
sub=121,
override_plot_properties={"cbar_tick_direction": "in"},
norm_dict={"linscale": 0.5},
)
projview(
m_scaled,
title="WMAP colormap",
cmap="wmap",
rlabel=r"A$_{\mathrm{ex. 4}}$",
llabel=r"$I$",
unit=r"$\mu$K",
fontname="serif",
min=-100,
max=100,
sub=122,
)
plt.tight_layout()
def test_projview_cart_bgcolor(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="horizontal",
projection_type="cart",
title="Cart projection with bgcolor",
bgcolor="lightblue",
)
def test_projview_no_colorbar(map_data):
"""Test that colorbar can be hidden with cbar=False"""
projview(
map_data,
coord=["G"],
cbar=False,
projection_type="mollweide",
title="Map without colorbar",
)
# Verify no colorbar was created by checking figure
fig = plt.gcf()
# A figure with colorbar has more than 1 axes
# Without colorbar, should have exactly 1 axes
assert len(fig.axes) == 1
plt.close('all')
def test_projview_none_map_with_cbar():
"""Test that m=None works correctly even with cbar=True (default)"""
# This should not raise an error even though cbar defaults to True
projview(
m=None,
coord=["G"],
projection_type="mollweide",
title="Blank map for overplotting",
)
# Should complete without error
plt.close('all')
def test_projview_none_map_explicit_cbar_false():
"""Test that m=None with explicit cbar=False works"""
projview(
m=None,
coord=["G"],
cbar=False,
projection_type="mollweide",
title="Blank map with explicit cbar=False",
)
plt.close('all')
def test_projview_mollweide_badcolor(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="vertical",
min=-0.05,
max=0.05,
latitude_grid_spacing=45,
projection_type="mollweide",
title="Mollweide projection with badcolor",
badcolor="red",
)
def test_projview_cart_bgcolor(map_data):
projview(
map_data,
coord=["G"],
graticule=True,
graticule_labels=True,
unit="cbar label",
xlabel="longitude",
ylabel="latitude",
cb_orientation="horizontal",
projection_type="cart",
title="Cart projection with bgcolor",
bgcolor="lightblue",
)
def test_projview_colormap_object_preservation(map_data):
"""Test that projview preserves user-modified Colormap object colors"""
# Create a colormap with custom bad/under colors
colormap = plt.get_cmap('viridis').copy()
colormap.set_bad('white')
colormap.set_under('yellow')
# Call projview with the modified colormap
projview(
map_data,
cmap=colormap,
projection_type='hammer'
)
# Get the colormap from the plot
ax = plt.gca()
if hasattr(ax, 'collections') and len(ax.collections) > 0:
plot_cmap = ax.collections[0].get_cmap()
# Verify colors are preserved
bad_is_white = np.allclose(plot_cmap._rgba_bad[:3], [1.0, 1.0, 1.0])
under_is_yellow = np.allclose(plot_cmap._rgba_under[:3], [1.0, 1.0, 0.0])
assert bad_is_white, f"projview should preserve Colormap bad color, got {plot_cmap._rgba_bad}"
assert under_is_yellow, f"projview should preserve Colormap under color, got {plot_cmap._rgba_under}"
|