@@ -687,40 +687,41 @@ def mapping_matrix_from(
687687 return mat [:, :S ]
688688
689689
690- @numba_util .jit ()
691690def 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
0 commit comments