-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from Jammy2211/feature/image_mesh_updates
Feature/image mesh updates
- Loading branch information
Showing
7 changed files
with
155 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
autoarray/inversion/pixelization/image_mesh/abstract_weighted.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from typing import Optional | ||
|
||
import numpy as np | ||
|
||
from autoarray.inversion.pixelization.image_mesh.abstract import AbstractImageMesh | ||
from autoarray.structures.grids.uniform_2d import Grid2D | ||
from autoarray.structures.grids.irregular_2d import Grid2DIrregular | ||
|
||
|
||
class AbstractImageMeshWeighted(AbstractImageMesh): | ||
def __init__( | ||
self, | ||
pixels=10.0, | ||
weight_floor=0.0, | ||
weight_power=0.0, | ||
): | ||
""" | ||
An abstract image mesh, which is used by pixelizations to determine the (y,x) mesh coordinates from an adapt | ||
image. | ||
Parameters | ||
---------- | ||
pixels | ||
The total number of pixels in the image mesh and drawn from the Hilbert curve. | ||
weight_floor | ||
The minimum weight value in the weight map, which allows more pixels to be drawn from the lower weight | ||
regions of the adapt image. | ||
weight_power | ||
The power the weight values are raised too, which allows more pixels to be drawn from the higher weight | ||
regions of the adapt image. | ||
""" | ||
|
||
super().__init__() | ||
|
||
self.pixels = pixels | ||
self.weight_floor = weight_floor | ||
self.weight_power = weight_power | ||
|
||
@property | ||
def uses_adapt_images(self) -> bool: | ||
return True | ||
|
||
def weight_map_from(self, adapt_data: np.ndarray): | ||
""" | ||
Returns the weight-map used by the image-mesh to compute the mesh pixel centres. | ||
This is computed from an input adapt data, which is an image representing the data which the KMeans | ||
clustering algorithm is applied too. This could be the image data itself, or a model fit which | ||
only has certain features. | ||
The ``weight_floor`` and ``weight_power`` attributes of the class are used to scale the weight map, which | ||
gives the model flexibility in how it adapts the pixelization to the image data. | ||
Parameters | ||
---------- | ||
adapt_data | ||
A image which represents one or more components in the masked 2D data in the image-plane. | ||
Returns | ||
------- | ||
The weight map which is used to adapt the Delaunay pixels in the image-plane to components in the data. | ||
""" | ||
|
||
weight_map = np.abs(adapt_data) / np.max(adapt_data) | ||
weight_map += self.weight_floor | ||
weight_map[weight_map > 1.0] = 1.0 | ||
|
||
weight_map = weight_map ** self.weight_power | ||
|
||
return weight_map | ||
|
||
def image_plane_mesh_grid_from( | ||
self, grid: Grid2D, adapt_data: Optional[np.ndarray] = None | ||
) -> Grid2DIrregular: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
test_autoarray/inversion/pixelization/image_mesh/test_abstract_weighted.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import autoarray as aa | ||
|
||
|
||
def test__weight_map_from(): | ||
adapt_data = np.array([-1.0, 1.0, 3.0]) | ||
|
||
pixelization = aa.image_mesh.KMeans(pixels=5, weight_floor=0.0, weight_power=1.0) | ||
|
||
weight_map = pixelization.weight_map_from(adapt_data=adapt_data) | ||
|
||
assert weight_map == pytest.approx([0.33333, 0.33333, 1.0], 1.0e-4) | ||
|
||
pixelization = aa.image_mesh.KMeans(pixels=5, weight_floor=0.0, weight_power=2.0) | ||
|
||
weight_map = pixelization.weight_map_from(adapt_data=adapt_data) | ||
|
||
assert weight_map == pytest.approx([0.11111, 0.11111, 1.0], 1.0e-4) | ||
|
||
pixelization = aa.image_mesh.KMeans(pixels=5, weight_floor=1.0, weight_power=1.0) | ||
|
||
weight_map = pixelization.weight_map_from(adapt_data=adapt_data) | ||
|
||
assert weight_map == pytest.approx([1.0, 1.0, 1.0], 1.0e-4) |
23 changes: 0 additions & 23 deletions
23
test_autoarray/inversion/pixelization/image_mesh/test_hilbert.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters