Exemple #1
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)
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
Exemple #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)
Exemple #4
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,
    )
Exemple #5
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,
        )
Exemple #6
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
Exemple #7
0
def test_survey_not_list():
    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(TypeError):
        draw_generator = CatsimGenerator(
            catalog,
            sampling_function,
            3,
            stamp_size=stamp_size,
            batch_size=batch_size,
            cpus=cpus,
            add_noise=add_noise,
        )
        draw_output = next(draw_generator)  # noqa: F841
Exemple #8
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