Skip to content

Commit b663f8f

Browse files
authored
2350 - BUG - ena_maps pixel masking broken for push projection (#2352)
* Add debug statements to help find pixel mask binning issue Fix pixel mask bug Add test that catches bug * Scale back overly verbose logging messages * Fix log messages
1 parent 4fed5a9 commit b663f8f

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

imap_processing/ena_maps/ena_maps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ def project_pset_values_to_map( # noqa: PLR0912
845845
raise KeyError(f"Value keys not found in pointing set: {missing_keys}")
846846

847847
if pset_valid_mask is None:
848+
logger.debug("No pset_valid_mask provided, using all pixels as valid.")
848849
pset_valid_mask = np.ones(pointing_set.num_points, dtype=bool)
849850

850851
if index_match_method is IndexMatchMethod.PUSH:
@@ -906,7 +907,7 @@ def project_pset_values_to_map( # noqa: PLR0912
906907
stacked_valid_mask = pset_valid_mask.stack(
907908
{CoordNames.GENERIC_PIXEL.value: pointing_set.spatial_coords}
908909
)
909-
pset_valid_mask_bc, _ = xr.broadcast(data_bc, stacked_valid_mask)
910+
_, pset_valid_mask_bc = xr.broadcast(data_bc, stacked_valid_mask)
910911
pset_valid_mask_values = pset_valid_mask_bc.values
911912
else:
912913
pset_valid_mask_values = pset_valid_mask

imap_processing/hi/hi_l2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,18 @@ def generate_hi_map(
156156
pset_valid_mask = None # Default to no mask (full spin)
157157
if descriptor.spin_phase == "ram":
158158
pset_valid_mask = pset.data["ram_mask"]
159+
logger.debug(
160+
f"Using ram mask with shape: {pset_valid_mask.shape} "
161+
f"containing {np.prod(pset_valid_mask.shape)} pixels,"
162+
f"{np.sum(pset_valid_mask.values)} of which are True."
163+
)
159164
elif descriptor.spin_phase == "anti":
160165
pset_valid_mask = ~pset.data["ram_mask"]
166+
logger.debug(
167+
f"Using anti-ram mask with shape: {pset_valid_mask.shape} "
168+
f"containing {np.prod(pset_valid_mask.shape)} pixels,"
169+
f"{np.sum(pset_valid_mask.values)} of which are True."
170+
)
161171

162172
# Project (bin) the PSET variables into the map pixels
163173
output_map.project_pset_values_to_map(

imap_processing/tests/ena_maps/test_ena_maps.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,11 @@ def test_project_pset_with_dataarray_mask_push(self, mock_frame_transform_az_el)
704704

705705
rect_pset = self.rectangular_psets[0]
706706

707+
# Set all counts to a uniform non-zero value
708+
rect_pset.data["counts"] = xr.full_like(
709+
rect_pset.data["counts"], fill_value=10.0
710+
)
711+
707712
# Create a DataArray mask matching the pset spatial dimensions
708713
valid_mask = xr.DataArray(
709714
np.ones(rect_pset.data["counts"].shape[2:], dtype=bool),
@@ -713,7 +718,8 @@ def test_project_pset_with_dataarray_mask_push(self, mock_frame_transform_az_el)
713718
},
714719
)
715720
# Mask out one quadrant
716-
valid_mask[:90, :90] = False
721+
valid_mask_shape = valid_mask.shape
722+
valid_mask[: valid_mask_shape[0] // 2, : valid_mask_shape[1] // 2] = False
717723

718724
# Project with DataArray mask
719725
rectangular_map.project_pset_values_to_map(
@@ -727,6 +733,17 @@ def test_project_pset_with_dataarray_mask_push(self, mock_frame_transform_az_el)
727733
assert "counts" in rectangular_map.data_1d
728734
assert rectangular_map.data_1d["counts"].sum() > 0
729735

736+
# Check that pixel mask reduces total map counts to ~3/4 of pset counts
737+
total_pset_counts = rect_pset.data["counts"].sum()
738+
expected_map_counts = total_pset_counts * 3 / 4 # Only half should be included
739+
actual_map_counts = rectangular_map.data_1d["counts"].sum()
740+
np.testing.assert_allclose(
741+
actual_map_counts,
742+
expected_map_counts,
743+
rtol=0.1,
744+
err_msg=("Mask filtering failed"),
745+
)
746+
730747
@pytest.mark.usefixtures("_setup_rectangular_l1c_pset_products")
731748
@mock.patch("imap_processing.spice.geometry.frame_transform_az_el")
732749
def test_project_pset_with_valid_mask_pull(self, mock_frame_transform_az_el):

0 commit comments

Comments
 (0)