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 __init__( self, max_number, wld_catalog_name, stamp_size, pixel_scale, shift=None, group_id=None, seed=DEFAULT_SEED, ): """Blends are defined from *groups* of galaxies from a CatSim-like catalog. Note: the pre-run WLD images are not used here. We only use the pre-run catalog (in i band) to identify galaxies that belong to a group. Args: max_number (int): Same as in SamplingFunction wld_catalog_name: File path to a pre-analyzed WLD Catsim catalog stamp_size (int): Size of the generated stamps pixel_scale (float): pixel scale of the survey, in arcseconds per pixel shift (list): List containing shifts to apply (useful to avoid randomization) group_id (list): List containing which group_ids to analyze (avoid randomization) seed (int): Seed to initialize randomness for reproducibility. """ super().__init__(max_number, seed) self.wld_catalog = CatsimCatalog.from_file( wld_catalog_name).get_raw_catalog() self.stamp_size = stamp_size self.pixel_scale = pixel_scale self.shift = shift self.group_id = group_id
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)
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
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, )
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)
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
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, )
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
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
def test_verbose(): """Testing the verbose option""" CatsimCatalog.from_file(CATALOG_PATH, verbose=True) CosmosCatalog.from_file(COSMOS_CATALOG_PATHS, verbose=True, exclusion_level="none")
def test_reading_catsim_catalog(): """Returns the catsim catalog""" catsim_catalog = CatsimCatalog.from_file(CATALOG_PATH) return catsim_catalog