Skip to content

Commit

Permalink
remove OverSamplingDataset
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Dec 20, 2024
1 parent d963ffa commit 90cfb65
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 51 deletions.
1 change: 0 additions & 1 deletion autogalaxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from autoarray.dataset.imaging.dataset import Imaging # noqa
from autoarray.dataset.interferometer.dataset import Interferometer # noqa
from autoarray.dataset.dataset_model import DatasetModel
from autoarray.dataset.over_sampling import OverSamplingDataset
from autoarray.inversion.inversion.mapper_valued import MapperValued
from autoarray.inversion.pixelization import mesh # noqa
from autoarray.inversion import regularization as reg # noqa
Expand Down
33 changes: 23 additions & 10 deletions autogalaxy/aggregator/imaging/imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,40 @@ def _imaging_from(
except AttributeError:
psf = None

try:
over_sampling_uniform = aa.Array2D.from_primary_hdu(primary_hdu=fit.value(name="dataset.over_sampling_size_lp")).native
except AttributeError:
over_sampling_uniform = 1
try:
over_sampling_pixelization = aa.Array2D.from_primary_hdu(primary_hdu=fit.value(name="dataset.over_sampling_size_pixelization")).native
except AttributeError:
over_sampling_pixelization = 1

dataset = aa.Imaging(
data=data,
noise_map=noise_map,
psf=psf,
over_sampling=aa.OverSamplingDataset(lp=over_sampling_uniform, pixelization=over_sampling_pixelization),
check_noise_map=False,
)

mask = aa.Mask2D.from_primary_hdu(primary_hdu=fit.value(name="dataset.mask"))

dataset = dataset.apply_mask(mask=mask)

try:
over_sample_size_lp = aa.Array2D.from_primary_hdu(
primary_hdu=fit.value(name="dataset.over_sample_size_lp")
).native
over_sample_size_lp = over_sample_size_lp.apply_mask(mask=mask)
except AttributeError:
over_sample_size_lp = 1

try:
over_sample_size_pixelization = aa.Array2D.from_primary_hdu(
primary_hdu=fit.value(name="dataset.over_sample_size_pixelization")
).native
over_sample_size_pixelization = over_sample_size_pixelization.apply_mask(
mask=mask
)
except AttributeError:
over_sample_size_pixelization = 1

dataset = dataset.apply_over_sampling(
over_sample_size_lp=over_sample_size_lp,
over_sample_size_pixelization=over_sample_size_pixelization,
)

dataset_list.append(dataset)

return dataset_list
Expand Down
4 changes: 2 additions & 2 deletions autogalaxy/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ def save_attributes(self, paths: af.DirectoryPaths):
prefix="dataset",
)
paths.save_fits(
name="over_sampling_size_lp",
name="over_sample_size_lp",
hdu=self.dataset.grids.lp.over_sample_size.native.hdu_for_output,
prefix="dataset",
)
paths.save_fits(
name="over_sampling_size_pixelization",
name="over_sample_size_pixelization",
hdu=self.dataset.grids.pixelization.over_sample_size.native.hdu_for_output,
prefix="dataset",
)
Expand Down
4 changes: 2 additions & 2 deletions autogalaxy/config/visualize/plots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ dataset: # Settings for plots of all datasets
data: false # Plot the individual data of every dataset?
noise_map: false # Plot the individual noise-map of every dataset?
signal_to_noise_map: false # Plot the individual signal-to-noise-map of every dataset?
over_sampling: false # Plot the over-sampling sub-size, used to evaluate light profiles, of every dataset?
over_sampling_pixelization: false # Plot the over-sampling sub-size, used to evaluate pixelizations, of every dataset?
over_sample_size_lp: false # Plot the over-sampling size, used to evaluate light profiles, of every dataset?
over_sample_size_pixelization: false # Plot the over-sampling size, used to evaluate pixelizations, of every dataset?
imaging: # Settings for plots of imaging datasets (e.g. ImagingPlotter).
psf: false
fit: # Settings for plots of all fits (e.g. FitImagingPlotter, FitInterferometerPlotter).
Expand Down
5 changes: 0 additions & 5 deletions autogalaxy/ellipse/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ def save_attributes(self, paths: af.DirectoryPaths):
hdu=self.dataset.mask.hdu_for_output,
prefix="dataset",
)
paths.save_json(
name="over_sampling",
object_dict=to_dict(self.dataset.over_sampling),
prefix="dataset",
)

def profile_log_likelihood_function(
self, instance: af.ModelInstance, paths: Optional[af.DirectoryPaths] = None
Expand Down
5 changes: 2 additions & 3 deletions autogalaxy/galaxy/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,8 @@ def traced_grid_2d_from(self, grid: aa.type.Grid2DLike) -> aa.type.Grid2DLike:
values=grid - self.deflections_yx_2d_from(grid=grid),
mask=grid.mask,
over_sample_size=grid.over_sample_size,
over_sampled=grid.over_sampled - self.deflections_yx_2d_from(
grid=grid.over_sampled
)
over_sampled=grid.over_sampled
- self.deflections_yx_2d_from(grid=grid.over_sampled),
)

return grid - self.deflections_yx_2d_from(grid=grid)
Expand Down
4 changes: 2 additions & 2 deletions autogalaxy/imaging/model/plotter_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def should_plot(name):
noise_map=should_plot("noise_map"),
psf=should_plot("psf"),
signal_to_noise_map=should_plot("signal_to_noise_map"),
over_sampling=should_plot("over_sampling"),
over_sampling_pixelization=should_plot("over_sampling_pixelization"),
over_sample_size_lp=should_plot("over_sample_size_lp"),
over_sample_size_pixelization=should_plot("over_sample_size_pixelization"),
)

mat_plot_2d = self.mat_plot_2d_from(subfolders="")
Expand Down
6 changes: 5 additions & 1 deletion autogalaxy/imaging/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def via_galaxies_from(
grid=grid, psf_shape_2d=self.psf.shape_native
)

dataset = self.via_image_from(image=image)
over_sample_size = grid.over_sample_size.resized_from(
new_shape=image.shape_native, mask_pad_value=1
)

dataset = self.via_image_from(image=image, over_sample_size=over_sample_size)

return dataset.trimmed_after_convolution_from(
kernel_shape=self.psf.shape_native
Expand Down
43 changes: 32 additions & 11 deletions autogalaxy/quantity/dataset_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def __init__(
self,
data: Union[aa.Array2D, aa.VectorYX2D],
noise_map: Union[aa.Array2D, aa.VectorYX2D],
over_sampling: Optional[aa.OverSamplingDataset] = aa.OverSamplingDataset(),
over_sample_size_lp: Union[int, aa.Array2D] = 4,
over_sample_size_pixelization: Union[int, aa.Array2D] = 4,
):
"""
A quantity dataset, which represents a derived quantity of a light profile, mass profile, galaxy or galaxies
Expand Down Expand Up @@ -57,10 +58,13 @@ def __init__(
An array describing the RMS standard deviation error in each pixel used for computing quantities like the
chi-squared in a fit, which is often chosen in an arbitrary way for a quantity dataset given the quantities
are not observed using real astronomical instruments.
over_sampling
The over sampling schemes which divide the grids into sub grids of smaller pixels within their host image
pixels when using the grid to evaluate a function (e.g. images) to better approximate the 2D line integral
This class controls over sampling for all the different grids (e.g. `grid`, `grids.pixelization).
over_sample_size_lp
The over sampling scheme size, which divides the grid into a sub grid of smaller pixels when computing
values (e.g. images) from the grid to approximate the 2D line integral of the amount of light that falls
into each pixel.
over_sample_size_pixelization
How over sampling is performed for the grid which is associated with a pixelization, which is therefore
passed into the calculations performed in the `inversion` module.
"""
if data.shape != noise_map.shape:
if data.shape[0:-1] == noise_map.shape[0:]:
Expand All @@ -76,15 +80,17 @@ def __init__(
super().__init__(
data=data,
noise_map=noise_map,
over_sampling=over_sampling,
over_sample_size_lp=over_sample_size_lp,
over_sample_size_pixelization=over_sample_size_pixelization,
)

@classmethod
def via_signal_to_noise_map(
cls,
data: Union[aa.Array2D, aa.VectorYX2D],
signal_to_noise_map: Union[aa.Array2D],
over_sampling=None,
over_sample_size_lp: Union[int, aa.Array2D] = 4,
over_sample_size_pixelization: Union[int, aa.Array2D] = 4,
):
"""
Represents a derived quantity of a light profile, mass profile, galaxy or galaxies as a dataset that can be
Expand All @@ -100,6 +106,13 @@ def via_signal_to_noise_map(
The data of the quantity (e.g. 2D convergence, 2D potential, 2D deflections) that is fitted.
signal_to_noise_map
The 2D signal to noise map of the quantity's data.
over_sample_size_lp
The over sampling scheme size, which divides the grid into a sub grid of smaller pixels when computing
values (e.g. images) from the grid to approximate the 2D line integral of the amount of light that falls
into each pixel.
over_sample_size_pixelization
How over sampling is performed for the grid which is associated with a pixelization, which is therefore
passed into the calculations performed in the `inversion` module.
"""
try:
noise_map = data / signal_to_noise_map
Expand All @@ -117,7 +130,8 @@ def via_signal_to_noise_map(
return DatasetQuantity(
data=data,
noise_map=noise_map,
over_sampling=over_sampling,
over_sample_size_lp=over_sample_size_lp,
over_sample_size_pixelization=over_sample_size_pixelization,
)

@property
Expand All @@ -133,7 +147,8 @@ def y(self) -> "DatasetQuantity":
return DatasetQuantity(
data=self.data.y,
noise_map=self.noise_map.y,
over_sampling=self.over_sampling,
over_sample_size_lp=self.over_sample_size_lp,
over_sample_size_pixelization=self.over_sample_size_pixelization,
)

@property
Expand All @@ -149,7 +164,8 @@ def x(self) -> "DatasetQuantity":
return DatasetQuantity(
data=self.data.x,
noise_map=self.noise_map.x,
over_sampling=self.over_sampling,
over_sample_size_lp=self.over_sample_size_lp,
over_sample_size_pixelization=self.over_sample_size_pixelization,
)

def apply_mask(self, mask: aa.Mask2D) -> "DatasetQuantity":
Expand All @@ -172,11 +188,16 @@ def apply_mask(self, mask: aa.Mask2D) -> "DatasetQuantity":

data = self.data.apply_mask(mask=mask)
noise_map = self.noise_map.apply_mask(mask=mask)
over_sample_size_lp = self.over_sample_size_lp.apply_mask(mask=mask)
over_sample_size_pixelization = self.over_sample_size_pixelization.apply_mask(
mask=mask
)

dataset = DatasetQuantity(
data=data,
noise_map=noise_map,
over_sampling=self.over_sampling,
over_sample_size_lp=over_sample_size_lp,
over_sample_size_pixelization=over_sample_size_pixelization,
)

dataset.unmasked = unmasked_dataset
Expand Down
10 changes: 4 additions & 6 deletions test_autogalaxy/aggregator/imaging/test_aggregator_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ def test__dataset_generator_from_aggregator__analysis_has_single_dataset(
data=image_7x7,
psf=psf_3x3,
noise_map=noise_map_7x7,
over_sampling=ag.OverSamplingDataset(
lp=5,
pixelization=3,
),
over_sample_size_lp=5,
over_sample_size_pixelization=3,
)

masked_imaging_7x7 = imaging.apply_mask(mask=mask_2d_7x7)
Expand All @@ -34,8 +32,8 @@ def test__dataset_generator_from_aggregator__analysis_has_single_dataset(

for dataset_list in dataset_gen:
assert (dataset_list[0].data == masked_imaging_7x7.data).all()
assert dataset_list[0].grids.lp.over_sample_size[0] == 5
assert dataset_list[0].grids.pixelization.over_sample_size[0] == 3
assert dataset_list[0].grids.over_sample_size_lp.slim[0] == 5
assert dataset_list[0].grids.over_sample_size_pixelization.slim[0] == 3

clean(database_file=database_file)

Expand Down
6 changes: 2 additions & 4 deletions test_autogalaxy/imaging/test_simulate_and_fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test__perfect_fit__chi_squared_0():
noise_map_path=path.join(file_path, "noise_map.fits"),
psf_path=path.join(file_path, "psf.fits"),
pixel_scales=0.2,
over_sampling=ag.OverSamplingDataset(lp=1),
over_sample_size_lp=1,
)

mask = ag.Mask2D.circular(
Expand Down Expand Up @@ -170,9 +170,7 @@ def test__simulate_imaging_data_and_fit__linear_light_profiles_agree_with_standa
)

masked_dataset = dataset.apply_mask(mask=mask)
masked_dataset = masked_dataset.apply_over_sampling(
over_sampling=ag.OverSamplingDataset(lp=1)
)
masked_dataset = masked_dataset.apply_over_sampling(over_sample_size_lp=1)

fit = ag.FitImaging(dataset=masked_dataset, galaxies=[galaxy])

Expand Down
2 changes: 1 addition & 1 deletion test_autogalaxy/imaging/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test__simulator__via_galaxies_from():
assert dataset.shape_native == (20, 20)
assert dataset.data.native[0, 0] != imaging_via_image.data.native[0, 0]
assert dataset.data.native[10, 10] == imaging_via_image.data.native[10, 10]
assert (dataset.psf == imaging_via_image.psf).all()
assert dataset.psf == pytest.approx(imaging_via_image.psf, 1.0e-4)
assert (dataset.noise_map == imaging_via_image.noise_map).all()


Expand Down
2 changes: 0 additions & 2 deletions test_autogalaxy/operate/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def test__unmasked_blurred_image_2d_from():
assert unmasked_blurred_image_2d == pytest.approx(image_2d_manual, 1.0e-4)



def test__visibilities_from_grid_and_transformer(grid_2d_7x7, transformer_7x7_7):
lp = ag.lp.Sersic(intensity=1.0)
lp_visibilities = lp.visibilities_from(
Expand Down Expand Up @@ -277,7 +276,6 @@ def test__unmasked_blurred_image_2d_list_from():
)



def test__visibilities_list_from(grid_2d_7x7, transformer_7x7_7):
lp_0 = ag.lp.Sersic(intensity=1.0)
lp_1 = ag.lp.Sersic(intensity=2.0)
Expand Down
2 changes: 1 addition & 1 deletion test_autogalaxy/quantity/test_dataset_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test__grid(
noise_map=ag.Array2D.full(
fill_value=2.0, shape_native=(7, 7), pixel_scales=1.0
),
over_sampling=ag.OverSamplingDataset(lp=4),
over_sample_size_lp=4,
)

dataset = dataset_quantity.apply_mask(mask=mask_2d_7x7)
Expand Down

0 comments on commit 90cfb65

Please sign in to comment.