Skip to content

Commit a92d828

Browse files
Jammy2211Jammy2211
authored andcommitted
convert mapped_to_source_via_mapping_matrix_from to numpy
1 parent 0b424c4 commit a92d828

3 files changed

Lines changed: 34 additions & 33 deletions

File tree

autoarray/inversion/pixelization/mappers/abstract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ def mapped_to_source_from(self, array: Array2D) -> np.ndarray:
388388
source domain in order to compute their average values.
389389
"""
390390
return mapper_util.mapped_to_source_via_mapping_matrix_from(
391-
mapping_matrix=self.mapping_matrix,
392-
array_slim=np.array(array.slim),
391+
mapping_matrix=np.array(self.mapping_matrix),
392+
array_slim=array.slim,
393393
)
394394

395395
def extent_from(

autoarray/inversion/pixelization/mappers/mapper_util.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -687,40 +687,41 @@ def mapping_matrix_from(
687687
return mat[:, :S]
688688

689689

690-
@numba_util.jit()
691690
def mapped_to_source_via_mapping_matrix_from(
692691
mapping_matrix: np.ndarray, array_slim: np.ndarray
693692
) -> np.ndarray:
694693
"""
695-
Map a masked 2d image in the image domain to the source domain and sum up all mappings on the source-pixels.
696-
697-
For example, suppose we have an image and a mapper. We can map every image-pixel to its corresponding mapper's
698-
source pixel and sum the values based on these mappings.
699-
700-
This will produce something similar to a `reconstruction`, albeit it bypasses the linear algebra / inversion.
701-
702-
Parameters
703-
----------
704-
mapping_matrix
705-
The matrix representing the blurred mappings between sub-grid pixels and pixelization pixels.
706-
array_slim
707-
The masked 2D array of values in its slim representation (e.g. the image data) which are mapped to the
708-
source domain in order to compute their average values.
709-
"""
710-
711-
mapped_to_source = np.zeros(mapping_matrix.shape[1])
712-
713-
source_pixel_count = np.zeros(mapping_matrix.shape[1])
714-
715-
for i in range(mapping_matrix.shape[0]):
716-
for j in range(mapping_matrix.shape[1]):
717-
if mapping_matrix[i, j] > 0:
718-
mapped_to_source[j] += array_slim[i] * mapping_matrix[i, j]
719-
source_pixel_count[j] += 1
720-
721-
for j in range(mapping_matrix.shape[1]):
722-
if source_pixel_count[j] > 0:
723-
mapped_to_source[j] /= source_pixel_count[j]
694+
Map a masked 2D image (in slim form) into the source plane by summing and averaging
695+
each image-pixel's contribution to its mapped source-pixels.
696+
697+
Each row i of `mapping_matrix` describes how image-pixel i is distributed (with
698+
weights) across the source-pixels j. `array_slim[i]` is then multiplied by those
699+
weights and summed over i to give each source-pixel’s total mapped value; finally,
700+
we divide by the number of nonzero contributions to form an average.
701+
702+
Parameters
703+
----------
704+
mapping_matrix : ndarray of shape (M, N)
705+
mapping_matrix[i, j] ≥ 0 is the weight by which image-pixel i contributes to
706+
source-pixel j. Zero means “no contribution.”
707+
array_slim : ndarray of shape (M,)
708+
The slimmed image values for each image-pixel i.
709+
710+
Returns
711+
-------
712+
mapped_to_source : ndarray of shape (N,)
713+
The averaged, mapped values on each of the N source-pixels.
714+
"""
715+
# weighted sums: sum over i of array_slim[i] * mapping_matrix[i, j]
716+
# ==> vector‐matrix multiply: (1×M) dot (M×N) → (N,)
717+
mapped_to_source = array_slim @ mapping_matrix
718+
719+
# count how many nonzero contributions each source-pixel j received
720+
counts = np.count_nonzero(mapping_matrix > 0.0, axis=0)
721+
722+
# avoid division by zero: only divide where counts > 0
723+
nonzero = counts > 0
724+
mapped_to_source[nonzero] /= counts[nonzero]
724725

725726
return mapped_to_source
726727

test_autoarray/inversion/pixelization/mappers/test_abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test__mapped_to_source_from(grid_2d_7x7):
223223

224224
mapped_to_source_util = aa.util.mapper.mapped_to_source_via_mapping_matrix_from(
225225
mapping_matrix=np.array(mapper.mapping_matrix),
226-
array_slim=np.array(array_slim),
226+
array_slim=array_slim,
227227
)
228228

229229
mapped_to_source_mapper = mapper.mapped_to_source_from(array=array_slim)

0 commit comments

Comments
 (0)