Exemplo n.º 1
0
def test_psf():
    get_psf(
        mirror_diameter=8.36,
        effective_area=32.4,
        filt_wavelength=7528.51,
        fwhm=0.748,
        atmospheric_model="Moffat",
    )
    get_psf(
        mirror_diameter=8.36,
        effective_area=32.4,
        filt_wavelength=7528.51,
        fwhm=0.748,
        atmospheric_model=None,
    )
    with pytest.raises(NotImplementedError) as excinfo:
        get_psf(
            mirror_diameter=8.36,
            effective_area=32.4,
            filt_wavelength=7528.51,
            fwhm=0.748,
            atmospheric_model="Layered",
        )

    assert "atmospheric model request" in str(excinfo.value)

    with pytest.raises(RuntimeError) as excinfo:
        get_psf(mirror_diameter=1,
                effective_area=4,
                filt_wavelength=7528.51,
                fwhm=0.748)

    assert "Incompatible effective-area and mirror-diameter values." in str(
        excinfo.value)

    with pytest.raises(RuntimeError) as excinfo:
        get_psf(
            mirror_diameter=0,
            effective_area=0,
            filt_wavelength=7528.51,
            fwhm=0.748,
            atmospheric_model=None,
        )

    assert "Neither the atmospheric nor the optical PSF components are defined." in str(
        excinfo.value)

    get_psf(mirror_diameter=0,
            effective_area=0,
            filt_wavelength=7528.51,
            fwhm=0.748)

    get_psf_from_file("tests/example_psf", get_surveys("Rubin"))
    get_psf_from_file("tests/multi_psf", get_surveys("Rubin"))
Exemplo n.º 2
0
def test_sampling_too_much_objects():
    # FAILING
    CATALOG_PATH = "data/sample_input_catalog.fits"

    class TestSamplingFunction(SamplingFunction):
        def __call__(self, table, **kwargs):
            return table[:self.max_number + 1]

        @property
        def compatible_catalogs(self):
            return "CatsimCatalog", "CosmosCatalog"

    with pytest.raises(ValueError) as excinfo:
        stamp_size = 24.0
        batch_size = 8
        cpus = 1
        add_noise = "all"

        catalog = CatsimCatalog.from_file(CATALOG_PATH)
        sampling_function = TestSamplingFunction(max_number=5)
        draw_generator = CatsimGenerator(
            catalog,
            sampling_function,
            get_surveys("Rubin"),
            stamp_size=stamp_size,
            batch_size=batch_size,
            cpus=cpus,
            add_noise=add_noise,
        )
        draw_output = next(draw_generator)  # noqa: F841

    assert "Number of objects per blend must be less than max_number" in str(
        excinfo.value)
Exemplo n.º 3
0
def test_sampling_incompatible_catalog():
    class TestSamplingFunction(SamplingFunction):
        def __call__(self, table, **kwargs):
            pass

        @property
        def compatible_catalogs(self):
            return "CosmosCatalog"

    with pytest.raises(AttributeError) as excinfo:
        stamp_size = 24.0
        batch_size = 8
        cpus = 1
        add_noise = "all"

        catalog = CatsimCatalog.from_file(CATALOG_PATH)
        sampling_function = TestSamplingFunction(max_number=5)
        draw_generator = CatsimGenerator(
            catalog,
            sampling_function,
            get_surveys("Rubin"),
            stamp_size=stamp_size,
            batch_size=batch_size,
            cpus=cpus,
            add_noise=add_noise,
        )
        draw_output = next(draw_generator)  # noqa: F841

    assert "Your catalog and sampling functions are not compatible with each other." in str(
        excinfo.value)
Exemplo n.º 4
0
def get_meas_results(meas_function, cpus=1, measure_kwargs=None):
    """Returns draw generator with group sampling function"""

    catalog_name = data_dir / "sample_input_catalog.fits"
    stamp_size = 24
    survey = get_surveys("Rubin")
    shifts = [[-0.3, 1.2]]
    indexes = [[1]]
    catalog = CatsimCatalog.from_file(catalog_name)
    draw_blend_generator = CatsimGenerator(
        catalog,
        DefaultSampling(seed=TEST_SEED),
        [survey],
        shifts=shifts,
        indexes=indexes,
        stamp_size=stamp_size,
        batch_size=1,
        seed=TEST_SEED,
    )
    meas_generator = MeasureGenerator(meas_function,
                                      draw_blend_generator,
                                      cpus=cpus,
                                      measure_kwargs=measure_kwargs)
    blend_results, results = next(meas_generator)
    wcs = blend_results["wcs"]
    x, y = wcs.world_to_pixel_values(shifts[0][0] / 3600, shifts[0][1] / 3600)
    target = np.array([x.item(), y.item()])
    return target, results
Exemplo n.º 5
0
def test_multiresolution(mock_show):
    catalog_name = data_dir / "sample_input_catalog.fits"

    stamp_size = 24.0
    batch_size = 8
    cpus = 1
    add_noise = "all"
    surveys = get_surveys(["Rubin", "HSC"])

    catalog = CatsimCatalog.from_file(catalog_name)
    sampling_function = DefaultSampling(stamp_size=stamp_size)
    draw_blend_generator = CatsimGenerator(
        catalog,
        sampling_function,
        surveys,
        stamp_size=stamp_size,
        batch_size=batch_size,
        cpus=cpus,
        add_noise=add_noise,
    )

    meas_generator = MeasureGenerator(sep_measure,
                                      draw_blend_generator,
                                      cpus=cpus)
    metrics_generator = MetricsGenerator(
        meas_generator,
        target_meas={"ellipticity": meas_ksb_ellipticity},
        meas_band_num=(2, 1))
    blend_results, measure_results, metrics_results = next(metrics_generator)

    assert "Rubin" in blend_results["blend_list"].keys(
    ), "Both surveys get well defined outputs"
    assert "HSC" in blend_results["blend_list"].keys(
    ), "Both surveys get well defined outputs"
    assert blend_results["blend_images"]["Rubin"][0].shape[-1] == int(
        24.0 / 0.2), "Rubin survey should have a pixel scale of 0.2"
    assert blend_results["blend_images"]["HSC"][0].shape[-1] == int(
        24.0 / 0.167), "HSC survey should have a pixel scale of 0.167"
    assert ("Rubin" in measure_results["catalog"]["sep_measure"].keys()
            ), "Both surveys get well defined outputs"
    assert ("HSC" in measure_results["catalog"]["sep_measure"].keys()
            ), "Both surveys get well defined outputs"
    assert ("Rubin" in metrics_results["galaxy_summary"]["sep_measure"].keys()
            ), "Both surveys get well defined outputs"
    assert ("HSC" in metrics_results["galaxy_summary"]["sep_measure"].keys()
            ), "Both surveys get well defined outputs"

    plot_metrics_summary(
        metrics_results,
        target_meas_keys=["ellipticity0"],
        target_meas_limits=[[-1, 1]],
        interactive=False,
    )
    plot_metrics_summary(
        metrics_results,
        target_meas_keys=["ellipticity0"],
        target_meas_limits=[[-1, 1]],
        interactive=True,
    )
Exemplo n.º 6
0
def test_incompatible_catalogs():
    stamp_size = 24.0
    batch_size = 8
    cpus = 1
    add_noise = True

    catalog = CatsimCatalog.from_file(CATALOG_PATH)
    sampling_function = DefaultSampling(stamp_size=stamp_size)
    with pytest.raises(ValueError):
        # Wrong generator
        draw_generator = CosmosGenerator(  # noqa: F841
            catalog,
            sampling_function,
            get_surveys("Rubin"),
            stamp_size=stamp_size,
            batch_size=batch_size,
            cpus=cpus,
            add_noise=add_noise,
        )
    with pytest.raises(ValueError):
        # Missing filter
        draw_generator = CatsimGenerator(  # noqa: F841
            catalog,
            sampling_function,
            get_surveys("HST"),
            stamp_size=stamp_size,
            batch_size=batch_size,
            cpus=cpus,
            add_noise=add_noise,
        )

    catalog = CosmosCatalog.from_file(COSMOS_CATALOG_PATHS,
                                      exclusion_level="none")
    with pytest.raises(ValueError):
        draw_generator = CatsimGenerator(  # noqa: F841
            catalog,
            sampling_function,
            get_surveys("Rubin"),
            stamp_size=stamp_size,
            batch_size=batch_size,
            cpus=cpus,
            add_noise=add_noise,
        )
Exemplo n.º 7
0
def test_source_not_visible():
    filt = Filter(
        name="u",
        psf=get_psf(
            mirror_diameter=8.36,
            effective_area=32.4,
            filt_wavelength=3592.13,
            fwhm=0.859,
        ),
        sky_brightness=22.9,
        exp_time=1680,
        zeropoint=9.16,
        extinction=0.451,
    )
    catalog = CatsimCatalog.from_file(CATALOG_PATH)
    with pytest.raises(SourceNotVisible):
        gal = get_catsim_galaxy(  # noqa: F841
            catalog.table[0], filt, get_surveys("Rubin"), True, True, True)
Exemplo n.º 8
0
def get_group_sampling_draw_generator(batch_size=3):
    """Returns draw generator with group sampling function"""
    wld_catalog_name = data_dir / "sample_group_catalog.fits"
    catalog_name = data_dir / "sample_group_input_catalog.fits"

    max_number = 10
    stamp_size = 24
    survey = get_surveys("Rubin")
    pixel_scale = 0.2
    shift = [0.8, -0.7]
    catalog = CatsimCatalog.from_file(catalog_name)
    sampling_function = GroupSamplingNumbered(
        max_number, wld_catalog_name, stamp_size, pixel_scale, shift=shift, seed=TEST_SEED
    )
    draw_blend_generator = CatsimGenerator(
        catalog, sampling_function, [survey], batch_size=batch_size, seed=TEST_SEED
    )
    return draw_blend_generator
Exemplo n.º 9
0
def get_draw_generator(
    batch_size=8,
    cpus=1,
    add_noise="all",
    fixed_parameters=False,
    sampling_function=None,
):
    """Returns a btk.draw_blends generator for default parameters"""
    catalog_name = "data/sample_input_catalog.fits"
    stamp_size = 24.0
    if fixed_parameters:
        shifts = [
            [[-0.3, 1.2], [-1.6, -1.7]],
            [[-1.1, -2.1], [1.4, 1.8]],
            [[-1.8, -0.8], [-0.6, 2.2]],
            [[-2.0, -0.7], [-2.2, 1.9]],
            [[1.1, -1.5], [0.1, -2.3]],
            [[-2.3, 1.9], [0.4, -1.9]],
            [[2.0, -2.0], [2.0, 0.1]],
            [[0.2, 2.4], [-1.8, -2.0]],
        ]
        indexes = [[4, 5], [9, 1], [9, 2], [0, 2], [3, 8], [0, 7], [10, 2],
                   [0, 10]]
    else:
        shifts = None
        indexes = None
    catalog = btk.catalog.CatsimCatalog.from_file(catalog_name)
    if sampling_function is None:
        sampling_function = btk.sampling_functions.DefaultSampling(
            stamp_size=stamp_size, seed=TEST_SEED)
    draw_generator = btk.draw_blends.CatsimGenerator(
        catalog,
        sampling_function,
        get_surveys("Rubin"),
        batch_size=batch_size,
        stamp_size=stamp_size,
        shifts=shifts,
        indexes=indexes,
        cpus=cpus,
        add_noise=add_noise,
        verbose=True,
        seed=TEST_SEED,
    )
    return draw_generator
Exemplo n.º 10
0
def test_cosmos_ext_galaxies():
    stamp_size = 24.0
    batch_size = 2
    catalog = CosmosCatalog.from_file(COSMOS_EXT_CATALOG_PATHS, exclusion_level="none")
    sampling_function = DefaultSampling(stamp_size=stamp_size)
    HST = get_surveys("HST")

    draw_generator = CosmosGenerator(
        catalog,
        sampling_function,
        HST,
        batch_size=batch_size,
        stamp_size=stamp_size,
        cpus=1,
        add_noise="all",
        verbose=True,
    )

    _ = next(draw_generator)
Exemplo n.º 11
0
def test_cosmos_galaxies_parametric():
    stamp_size = 24.0
    batch_size = 2
    catalog = CosmosCatalog.from_file(COSMOS_CATALOG_PATHS)
    sampling_function = DefaultSampling(stamp_size=stamp_size)
    HST = get_surveys("HST")

    draw_generator = CosmosGenerator(
        catalog,
        sampling_function,
        HST,
        batch_size=batch_size,
        stamp_size=stamp_size,
        cpus=1,
        add_noise="all",
        verbose=True,
        gal_type="parametric",
    )

    _ = next(draw_generator)
Exemplo n.º 12
0
def get_metrics_generator(
    meas_function,
    cpus=1,
    measure_kwargs=None,
):
    """Returns draw generator with group sampling function"""
    catalog_name = "data/sample_input_catalog.fits"
    stamp_size = 24
    survey = get_surveys("Rubin")
    shifts = [
        [[-0.3, 1.2], [-1.6, -1.7]],
        [[-1.1, -2.1], [1.4, 1.8]],
        [[-1.8, -0.8], [-0.6, 2.2]],
        [[-2.0, -0.7], [-2.2, 1.9]],
        [[1.1, -1.5], [0.1, -2.3]],
        [[-2.3, 1.9], [0.4, -1.9]],
        [[2.0, -2.0], [2.0, 0.1]],
        [[0.2, 2.4], [-1.8, -2.0]],
    ]
    indexes = [[4, 5], [9, 1], [9, 2], [0, 2], [3, 8], [0, 7], [10, 2],
               [0, 10]]
    catalog = CatsimCatalog.from_file(catalog_name)
    draw_blend_generator = CatsimGenerator(
        catalog,
        DefaultSampling(seed=TEST_SEED),
        [survey],
        shifts=shifts,
        indexes=indexes,
        stamp_size=stamp_size,
        seed=TEST_SEED,
    )
    meas_generator = MeasureGenerator(meas_function,
                                      draw_blend_generator,
                                      cpus=cpus,
                                      measure_kwargs=measure_kwargs)
    metrics_generator = MetricsGenerator(
        meas_generator,
        target_meas={"ellipticity": meas_ksb_ellipticity},
    )
    return metrics_generator