Example #1
0
def test_LogScale_inverse():
    tiny = np.finfo(np.float32).tiny
    log_scale = LogScale()
    values = np.array([1, 1e-5, 1e-40])
    log_values = log_scale(values)
    assert_allclose(log_values, np.array([0, np.log(1e-5), np.log(tiny)]))
    inv_values = log_scale.inverse(log_values)
    assert_allclose(inv_values, np.array([1, 1e-5, 0]))
Example #2
0
def test_maps_read_write(map_dictionary):
    maps = Maps(**map_dictionary)
    maps.write("test.fits", overwrite=True)
    new_maps = Maps.read("test.fits")

    assert new_maps.geom == maps.geom
    assert len(new_maps) == 2
    assert_allclose(new_maps["map1"].data, 1)
    assert_allclose(new_maps["map2"].data, 2)
Example #3
0
def test_maps_region():
    axis = MapAxis.from_edges([1, 2, 3, 4], name="axis", unit="cm")
    map1 = RegionNDMap.create(region=None, axes=[axis])
    map1.data = 1
    map2 = RegionNDMap.create(region=None, axes=[axis])

    maps = Maps(map1=map1, map2=map2)

    assert len(maps) == 2
    assert_allclose(maps["map1"], 1)
Example #4
0
def test_maps_wrong_addition(map_dictionary):
    maps = Maps(**map_dictionary)

    # Test pop method
    some_map = maps.pop("map2")
    assert len(maps) == 1
    assert_allclose(some_map.data, 2)

    # Test incorrect map addition
    with pytest.raises(ValueError):
        maps["map3"] = maps["map1"].sum_over_axes()
Example #5
0
def test_slice_time_axis(time_intervals):
    axis = TimeMapAxis(time_intervals["t_min"], time_intervals["t_max"],
                       time_intervals["t_ref"])

    new_axis = axis.slice([2, 6, 9])
    squashed = axis.squash()

    assert new_axis.nbin == 3
    assert_allclose(squashed.time_max[0].mjd, 58937.041667)
    assert squashed.nbin == 1
    assert_allclose(squashed.time_max[0].mjd, 58937.041667)
Example #6
0
def test_time_range_jyear():
    cfg = """
    observations:
        datastore: $GAMMAPY_DATA/hess-dl3-dr1
        obs_ids: [23523, 23526]
        obs_time: {
            start: [J2004.92654346, J2004.92658453, J2004.92663655], 
            stop: [J2004.92658453, J2004.92663655, J2004.92670773]
            }
    """
    config = AnalysisConfig.from_yaml(cfg)

    assert_allclose(config.observations.obs_time.start.mjd,
                    [53343.92, 53343.935, 53343.954])
Example #7
0
def test_time_range_iso():
    cfg = """
    observations:
        datastore: $GAMMAPY_DATA/hess-dl3-dr1
        obs_ids: [23523, 23526]
        obs_time: {
            start: [2004-12-04 22:04:48.000, 2004-12-04 22:26:24.000, 2004-12-04 22:53:45.600], 
            stop: [2004-12-04 22:26:24.000, 2004-12-04 22:53:45.600, 2004-12-04 23:31:12.000]
            }
    """
    config = AnalysisConfig.from_yaml(cfg)

    assert_allclose(config.observations.obs_time.start.mjd,
                    [53343.92, 53343.935, 53343.954])
Example #8
0
def test_coord_to_idx_time_axis(time_intervals):
    tmin = time_intervals["t_min"]
    tmax = time_intervals["t_max"]
    tref = time_intervals["t_ref"]
    axis = TimeMapAxis(tmin, tmax, tref, name="time")

    time = Time(58927.020833333336, format="mjd")

    times = axis.time_mid
    times[::2] += 1 * u.h
    times = times.insert(0, tref - [1, 2] * u.yr)

    idx = axis.coord_to_idx(time)
    indices = axis.coord_to_idx(times)

    pix = axis.coord_to_pix(time)
    pixels = axis.coord_to_pix(times)

    assert idx == 0
    assert_allclose(indices[1::2], [-1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
    assert_allclose(indices[::2], -1)
    assert_allclose(pix, 0.5)
    assert_allclose(
        pixels[1::2],
        [-1, 1.5, 3.5, 5.5, 7.5, 9.5, 11.5, 13.5, 15.5, 17.5, 19.5])
Example #9
0
def test_from_table_time_axis():
    t0 = Time("2006-02-12", scale='utc')
    t_min = np.linspace(0, 10, 10) * u.d
    t_max = t_min + 12 * u.h

    table = Table()
    table["TIME_MIN"] = t_min
    table["TIME_MAX"] = t_max
    table.meta.update(time_ref_to_dict(t0))
    table.meta["AXCOLS1"] = "TIME_MIN,TIME_MAX"

    axis = TimeMapAxis.from_table(table, format="gadf")

    assert axis.nbin == 10
    assert_allclose(axis.time_mid[0].mjd, 53778.25)
Example #10
0
def test_time_axis(time_intervals):
    axis = TimeMapAxis(time_intervals["t_min"], time_intervals["t_max"],
                       time_intervals["t_ref"])

    axis_copy = axis.copy()

    assert axis.nbin == 20
    assert axis.name == "time"
    assert axis.node_type == "intervals"

    assert_allclose(axis.time_delta.to_value("min"), 60)
    assert_allclose(axis.time_mid[0].mjd, 58927.020833333336)

    assert "time" in axis.__str__()
    assert "20" in axis.__str__()

    with pytest.raises(ValueError):
        axis.assert_name("bad")

    assert axis_copy == axis
Example #11
0
def test_from_time_edges_time_axis():
    t0 = Time("2020-03-19")
    t_min = t0 + np.linspace(0, 10, 20) * u.d
    t_max = t_min + 1 * u.h

    axis = TimeMapAxis.from_time_edges(t_min, t_max)
    axis_h = TimeMapAxis.from_time_edges(t_min, t_max, unit='h')

    assert axis.nbin == 20
    assert axis.name == "time"
    assert_time_allclose(axis.reference_time, t0)
    assert_allclose(axis.time_delta.to_value("min"), 60)
    assert_allclose(axis.time_mid[0].mjd, 58927.020833333336)
    assert_allclose(axis_h.time_delta.to_value("h"), 1)
    assert_allclose(axis_h.time_mid[0].mjd, 58927.020833333336)
    assert axis == axis_h
Example #12
0
def test_single_interval_time_axis(time_interval):
    axis = TimeMapAxis(time_interval["t_min"], time_interval["t_max"],
                       time_interval["t_ref"])
    coord = Time(58933, format="mjd") + u.Quantity([1.5, 3.5, 10], unit="d")
    pix = axis.coord_to_pix(coord)

    assert axis.nbin == 1
    assert_allclose(axis.time_delta.to_value("d"), 10)
    assert_allclose(axis.time_mid[0].mjd, 58933)
    assert_allclose(pix, [0.65, 0.85, -1.0])
Example #13
0
def test_maps(map_dictionary):
    maps = Maps(**map_dictionary)

    maps["map3"] = maps["map1"].copy()

    assert maps.geom.npix[0] == 10
    assert len(maps) == 3
    assert_allclose(maps["map1"].data, 1)
    assert_allclose(maps["map2"].data, 2)
    assert_allclose(maps["map3"].data, 1)
    assert "map3" in maps.__str__()
Example #14
0
def test_slice_squash_time_axis(time_intervals):
    axis = TimeMapAxis(time_intervals["t_min"], time_intervals["t_max"],
                       time_intervals["t_ref"])
    axis_squash = axis.squash()
    axis_slice = axis.slice(slice(1, 5))

    assert axis_squash.nbin == 1
    assert_allclose(axis_squash.time_min[0].mjd, 58927)
    assert_allclose(axis_squash.time_delta.to_value("d"), 10.04166666)
    assert axis_slice.nbin == 4
    assert_allclose(axis_slice.time_delta.to_value("d")[0], 0.04166666666)
    assert axis_squash != axis_slice