def test_hpxgeom_pixel_scales(): geom = HpxGeom.create(nside=8, frame="galactic", axes=[MapAxis.from_edges([0, 2, 3])]) pixel_scales = geom.pixel_scales assert_allclose(pixel_scales, [4] * u.deg)
def test_hpxgeom_solid_angle(): geom = HpxGeom.create(nside=8, frame="galactic", axes=[MapAxis.from_edges([0, 2, 3])]) solid_angle = geom.solid_angle() assert solid_angle.shape == (1, ) assert_allclose(solid_angle.value, 0.016362461737446838)
def test_map_plot_mask(): geom = HpxGeom.create(nside=16) region = CircleSkyRegion(center=SkyCoord("0d", "0d", frame="galactic"), radius=20 * u.deg) mask = geom.region_mask([region]) with mpl_plot_check(): mask.plot_mask()
def test_from_wcs_tiles(): geom = HpxGeom.create(nside=8) wcs_geoms = geom.to_wcs_tiles(nside_tiles=4) wcs_tiles = [Map.from_geom(geom, data=1) for geom in wcs_geoms] m = HpxNDMap.from_wcs_tiles(wcs_tiles=wcs_tiles) assert_allclose(m.data, 1)
def test_map_from_geom(): geom = WcsGeom.create(binsz=1.0, width=10.0) m = Map.from_geom(geom) assert isinstance(m, WcsNDMap) assert m.geom.is_image geom = HpxGeom.create(binsz=1.0, width=10.0) m = Map.from_geom(geom) assert isinstance(m, HpxNDMap) assert m.geom.is_image
def test_hpx_geom_separation(): geom = HpxGeom.create(binsz=0.1, frame="galactic", nest=True) position = SkyCoord(0, 0, unit="deg", frame="galactic") separation = geom.separation(position) assert separation.unit == "deg" assert_allclose(separation.value[0], 45.000049) # Make sure it also works for 2D maps as input separation = geom.to_image().separation(position) assert separation.unit == "deg" assert_allclose(separation.value[0], 45.000049) # make sure works for partial geometry geom = HpxGeom.create(binsz=0.1, frame="galactic", nest=True, region="DISK(0,0,10)") separation = geom.separation(position) assert separation.unit == "deg" assert_allclose(separation.value[0], 9.978725)
def geom_config_hpx(): energy_axis = MapAxis.from_energy_bounds("0.5 TeV", "30 TeV", nbin=3) energy_axis_true = MapAxis.from_energy_bounds( "0.3 TeV", "30 TeV", nbin=3, per_decade=True, name="energy_true" ) geom_hpx = HpxGeom.create( binsz=0.1, frame="galactic", axes=[energy_axis], region="DISK(0, 0, 5.)" ) return {"geom": geom_hpx, "energy_axis_true": energy_axis_true}
def test_hpx_geom_to_binsz(): geom = HpxGeom.create(nside=32, frame="galactic", nest=True) geom_new = geom.to_binsz(1 * u.deg) assert geom_new.nside[0] == 64 assert geom_new.frame == "galactic" assert geom_new.nest geom = HpxGeom.create(nside=32, frame="galactic", nest=True, region="DISK(110.,75.,10.)") geom_new = geom.to_binsz(1 * u.deg) assert geom_new.nside[0] == 64 center = geom_new.center_skydir.galactic assert_allclose(center.l.deg, 110) assert_allclose(center.b.deg, 75)
def test_hpx_nd_map_to_nside(): axis = MapAxis.from_edges([1, 2, 3], name="test-1") geom = HpxGeom.create(nside=64, axes=[axis]) m = HpxNDMap(geom, unit="m2") m.data += 1 m2 = m.to_nside(nside=32) assert_allclose(m2.data, 4) m3 = m.to_nside(nside=128) assert_allclose(m3.data, 0.25)
def test_smooth(kernel): axes = [ MapAxis(np.logspace(0.0, 3.0, 3), interp="log"), MapAxis(np.logspace(1.0, 3.0, 4), interp="lin"), ] geom_nest = HpxGeom.create(nside=256, nest=False, frame="galactic", axes=axes) geom_ring = HpxGeom.create(nside=256, nest=True, frame="galactic", axes=axes) m_nest = HpxNDMap(geom_nest, data=np.ones(geom_nest.data_shape), unit="m2") m_ring = HpxNDMap(geom_ring, data=np.ones(geom_ring.data_shape), unit="m2") desired_nest = m_nest.data.sum() desired_ring = m_ring.data.sum() smoothed_nest = m_nest.smooth(0.2 * u.deg, kernel) smoothed_ring = m_ring.smooth(0.2 * u.deg, kernel) actual_nest = smoothed_nest.data.sum() assert_allclose(actual_nest, desired_nest) assert smoothed_nest.data.dtype == float actual_ring = smoothed_ring.data.sum() assert_allclose(actual_ring, desired_ring) assert smoothed_ring.data.dtype == float # with pytest.raises(NotImplementedError): cutout = m_nest.cutout(position=(0, 0), width=15 * u.deg) smoothed_cutout = cutout.smooth(0.1 * u.deg, kernel) actual_cutout = cutout.data.sum() desired_cutout = smoothed_cutout.data.sum() assert_allclose(actual_cutout, desired_cutout, rtol=0.01) with pytest.raises(ValueError): m_nest.smooth(0.2 * u.deg, "box")
def test_hpx_geom_cutout(): geom = HpxGeom.create(nside=8, frame="galactic", axes=[MapAxis.from_edges([0, 2, 3])]) cutout = geom.cutout(position=SkyCoord("0d", "0d"), width=30 * u.deg) assert cutout.nside == 8 assert cutout.data_shape == (2, 14) assert cutout.data_shape_axes == (2, 1) center = cutout.center_skydir.icrs assert_allclose(center.ra.deg, 0, atol=1e-8) assert_allclose(center.dec.deg, 0, atol=1e-8)
def test_convolve_wcs(nest): energy = MapAxis.from_bounds(1, 100, unit='TeV', nbin=2, name='energy') nside = 256 hpx_geom = HpxGeom.create(nside=nside, axes=[energy], region='DISK(0,0,2.5)', nest=nest) hpx_map = Map.from_geom(hpx_geom) hpx_map.set_by_coord((0, 0, [2, 90]), 1) wcs_geom = WcsGeom.create(width=5, binsz=0.04, axes=[energy]) kernel = PSFKernel.from_gauss(wcs_geom, 0.4 * u.deg) convolved_map = hpx_map.convolve_wcs(kernel) assert_allclose(convolved_map.data.sum(), 2, rtol=0.001)
def test_hpx_geom_to_wcs_tiles(): geom = HpxGeom.create(nside=8, frame="galactic", axes=[MapAxis.from_edges([0, 2, 3])]) tiles = geom.to_wcs_tiles(nside_tiles=2) assert len(tiles) == 48 assert tiles[0].projection == "TAN" assert_allclose(tiles[0].width, [[43.974226], [43.974226]] * u.deg) tiles = geom.to_wcs_tiles(nside_tiles=4) assert len(tiles) == 192 assert tiles[0].projection == "TAN" assert_allclose(tiles[0].width, [[21.987113], [21.987113]] * u.deg)
def test_hpx_geom_region_mask(): geom = HpxGeom.create(nside=256, region="DISK(0.,0.,5.)") circle = CircleSkyRegion(center=SkyCoord("0d", "0d"), radius=3 * u.deg) mask = geom.region_mask(circle) assert_allclose(mask.data.sum(), 534) assert mask.geom.nside == 256 solid_angle = (mask.data * geom.solid_angle()).sum() assert_allclose(solid_angle, 2 * np.pi * (1 - np.cos(3 * u.deg)) * u.sr, rtol=0.01)
def test_coadd_unit(): geom = HpxGeom.create(nside=128) m1 = HpxNDMap(geom, unit="m2") m2 = HpxNDMap(geom, unit="cm2") idx = geom.get_idx() weights = u.Quantity(np.ones_like(idx[0]), unit="cm2") m1.fill_by_idx(idx, weights=weights) assert_allclose(m1.data, 0.0001) weights = u.Quantity(np.ones_like(idx[0]), unit="m2") m1.fill_by_idx(idx, weights=weights) m1.coadd(m2) assert_allclose(m1.data, 1.0001)
def test_hpxndmap_resample_axis(): axis_1 = MapAxis.from_edges([1, 2, 3, 4, 5], name="test-1") axis_2 = MapAxis.from_edges([1, 2, 3, 4], name="test-2") geom = HpxGeom.create(nside=16, axes=[axis_1, axis_2]) m = HpxNDMap(geom, unit="m2") m.data += 1 new_axis = MapAxis.from_edges([2, 3, 5], name="test-1") m2 = m.resample_axis(axis=new_axis) assert m2.data.shape == (3, 2, 3072) assert_allclose(m2.data[0, :, 0], [1, 2]) # Test without all interval covered new_axis = MapAxis.from_edges([1.7, 4], name="test-1") m3 = m.resample_axis(axis=new_axis) assert m3.data.shape == (3, 1, 3072) assert_allclose(m3.data, 2)
def test_wcsndmap_reproject_allsky_car(): geom = WcsGeom.create(binsz=10.0, proj="CAR", coordsys="CEL") m = WcsNDMap(geom) coords = m.geom.get_coord() m.set_by_coord(coords, coords[0].value) geom0 = WcsGeom.create(binsz=1.0, proj="CAR", coordsys="CEL", skydir=(180.0, 0.0), width=30.0) m0 = m.reproject(geom0, order=1) coords0 = m0.geom.get_coord() assert_allclose(m0.get_by_coord(coords0), coords0[0].value) geom1 = HpxGeom.create(binsz=5.0, coordsys="CEL") m1 = m.reproject(geom1, order=1) coords1 = m1.geom.get_coord() m = (coords1[0] > 10) & (coords1[0] < 350) assert_allclose(m1.get_by_coord((coords1[0][m], coords1[1][m])), coords1[0][m])
def test_map_reproject_hpx_to_wcs(): axis = MapAxis.from_bounds(1.0, 10.0, 3, interp="log", name="energy", node_type="center") geom_wcs = WcsGeom.create(skydir=(0, 0), npix=(11, 11), binsz=10, axes=[axis], coordsys="GAL") geom_hpx = HpxGeom.create(binsz=10, coordsys="GAL", axes=[axis]) data = np.arange(3 * 768).reshape(geom_hpx.data_shape) m = HpxNDMap(geom_hpx, data=data) m_r = m.reproject(geom_wcs) actual = m_r.get_by_coord({ "lon": 0, "lat": 0, "energy": [1.0, 3.16227766, 10.0] }) assert_allclose(actual, [287.5, 1055.5, 1823.5], rtol=1e-3)
def test_interp_to_geom(): energy = MapAxis.from_energy_bounds("1 TeV", "300 TeV", nbin=5, name="energy") energy_target = MapAxis.from_energy_bounds("1 TeV", "300 TeV", nbin=7, name="energy") value = 30 coords = { "skycoord": SkyCoord("0 deg", "0 deg"), "energy": energy_target.center[3] } # WcsNDMap geom_wcs = WcsGeom.create(npix=(5, 3), proj="CAR", binsz=60, axes=[energy], skydir=(0, 0)) wcs_map = Map.from_geom(geom_wcs, unit="") wcs_map.data = value * np.ones(wcs_map.data.shape) wcs_geom_target = WcsGeom.create(skydir=(0, 0), width=(10, 10), binsz=0.1 * u.deg, axes=[energy_target]) interp_wcs_map = wcs_map.interp_to_geom(wcs_geom_target, method="linear") assert_allclose(interp_wcs_map.get_by_coord(coords)[0], value, atol=1e-7) assert isinstance(interp_wcs_map, WcsNDMap) assert interp_wcs_map.geom == wcs_geom_target # HpxNDMap geom_hpx = HpxGeom.create(binsz=60, axes=[energy], skydir=(0, 0)) hpx_map = Map.from_geom(geom_hpx, unit="") hpx_map.data = value * np.ones(hpx_map.data.shape) hpx_geom_target = HpxGeom.create(skydir=(0, 0), width=10, binsz=0.1 * u.deg, axes=[energy_target]) interp_hpx_map = hpx_map.interp_to_geom(hpx_geom_target) assert_allclose(interp_hpx_map.get_by_coord(coords)[0], value, atol=1e-7) assert isinstance(interp_hpx_map, HpxNDMap) assert interp_hpx_map.geom == hpx_geom_target # Preserving the counts geom_initial = WcsGeom.create( skydir=(20, 20), width=(5, 5), binsz=0.2 * u.deg, ) test_map = Map.from_geom(geom_initial, unit="") test_map.data = value * np.ones(test_map.data.shape) geom_target = WcsGeom.create( skydir=(20, 20), width=(5, 5), binsz=0.1 * u.deg, ) new_map = test_map.interp_to_geom(geom_target, preserve_counts=True) assert np.floor(np.sum(new_map.data)) == np.sum(test_map.data)
def test_hpx_get_hpxregion_size(): geom = HpxGeom.create(nside=128, region="DISK(110.,75.,2.)") assert_allclose(geom.width, 2.0 * u.deg)