Skip to content

Fix the inverted all-masked cell guard - #83

Merged
f-PLT merged 3 commits into
mainfrom
fix-mask-bug
Sep 3, 2026
Merged

f-PLT merged 3 commits into
mainfrom
fix-mask-bug

Conversation

@f-PLT

@f-PLT f-PLT commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Both Stage-2 inference entry points refuse to predict on a cell with no observations — in theory. The guard read the
loader's masks as 1 = valid; the loader emits 1 = masked. Two quirks (t_m always carries a forced 1, st_m is
never nodata) made it a constant False: it has never fired, in either direction. A cell with zero observations
therefore received a finite, plausible FSC derived from its coordinates alone, written to the COG as a measurement and
counted as covered — no NaN, no exception, no log line.

No existing output is affected. Measured over all 7300 cubes on disk: zero would have fired. Latent, not active —
no reprocessing.

  • _loader_bridge.has_no_spacetime_observation — one shared definition replacing two copies. Fires when all three
    space-time masks are 1 everywhere. Reads named MaskedOutput fields, not indices, because a positional lookup
    under a comment asserting the opposite polarity is exactly what failed.
  • space (DEM/WorldCover), time (coarse VIIRS/ERA5) and static (location) are excluded: none can support a
    spatially-resolved FSC patch alone, and static is never nodata, so including it keeps the guard dead.
  • Per-cell "fully masked" warning → per-day count; it would have flooded the log for the exact AOI it exists to flag.
  • Three test doubles encoded the inverted convention. Left alone, five tests stay green while writing empty mosaics.
    Now real MaskedOutputs, so a double that disagrees with the loader raises instead of answering wrong.
  • New positive-path end-to-end test built from tests/fixtures/gee_reference_patches — the placeholder cube now
    (correctly) takes the nodata branch and can no longer reach the predict-and-write path.
  • AC-28 reworded; it was unsatisfiable as written.

Tests: 359 passed, 2 skipped (pre-existing, SNAP output absent). ruff, mdformat, mypy clean.


Illustration of dataflow :

        driver.py                              infer_aoi_cubes.py
        builds cubes from local sources        reads pre-built GEE cubes
                       └───────────────┬───────────────┘
                                       ▼
                308-band cube GeoTIFF — one 1 km cell × 8 timesteps
                traced example: every pixel = -9999   (cell has no data)
                                       ▼
   ┌─────────────────────────────────────────────────────────────────────┐
   │ masked_output_for_tif()                      _loader_bridge.py:78-93│
   ├─────────────────────────────────────────────────────────────────────┤
   │ a  create_valid_mask       -9999   →  valid = 0      0 = INVALID    │
   │ b  normalize                          valid = 0      unchanged      │
   │ c  invert + group-OR       valid=0  →  mask  = 1     1 = MASKED     │
   │                                        ▲▲▲ POLARITY FLIPS HERE      │
   │ d  mask_prediction_era5    t_m[-1,3:] = 1   forced on every cube    │
   └─────────────────────────────────────────────────────────────────────┘
                                       ▼
            MaskedOutput[6..11] — the six masks, 1 = MASKED
            empty cell →  s_t_h_m 1 · s_t_m_m 1 · s_t_l_m 1 · sp_m 1 · t_m 1 · st_m 0
                                       ▼
   ┌─────────────────────────────────────────────────────────────────────┐
   │ _is_fully_masked()        driver.py:55, :281-292                    │
   │                           infer_aoi_cubes.py:66, :225-230           │
   ├─────────────────────────────────────────────────────────────────────┤
   │ comment claims  1 = valid          ◄── WRONG, loader set 1 = masked │
   │ tests  not mask.any()   →  sees the 1s as data  →  returns False    │
   └─────────────────────────────────────────────────────────────────────┘
                                       ▼
        False → model runs → an FSC value is still written to the COG as a measurement
        True  → cell = None → nodata block            ◄── never reached
                                       ▼
                DailyMosaicWriter.write_day()             mosaic.py:111-178

Channel-group membership of each mask tensor, from src/snow_galileo/data/earthengine/eo.py:

idx tensor shape channel groups
6 s_t_h_m (100,100,8,7) S1, S2_RGB, S2_NIR, S2_SWIR, L_RGB, L_NIR, L_SWIR
7 s_t_m_m (5,5,8,1) S3_NIR
8 s_t_l_m (2,2,8,7) MODIS_RGB, MODIS_NIR, MODIS_SWIR, VIIRS_RGB_FINE, VIIRS_VNIR_FINE, NDSI, NDVI
9 sp_m (100,100,2) DEM, WC
10 t_m (8,4) VIIRS_RGB_COARSE, VIIRS_VNIR_COARSE, VIIRS_SWIR_COARSE, ERA5
11 st_m (1,) location

Which Tensors count as input?

tensor an observation? time-varying? spatially resolved? counts toward AC-28?
s_t_h_m (6) yes yes yes yes
s_t_m_m (7) yes yes yes yes
s_t_l_m (8) yes yes yes yes
t_m (10) — VIIRS coarse yes yes no no
t_m (10) — ERA5 no (reanalysis, always present) yes no no
sp_m (9) — DEM, WC yes no (static) yes no
st_m (11) — location no (derived geometry) no no no

Assisted by Claude Code

@f-PLT
f-PLT requested a review from marlens123 August 11, 2026 19:40
@f-PLT f-PLT self-assigned this Aug 11, 2026
@marlens123

Copy link
Copy Markdown
Collaborator

t_m always carries a forced 1
│ d mask_prediction_era5 t_m[-1,3:] = 1 forced on every cube │

just to explain why we always do t_m[-1,3:] = 1: this masks out ERA5 on every prediction day. Because we include ERA5 precipitation, but our labels (hence, all prediction days during training and model evaluation) are always acquired on cloud-free days, the ERA5 precipitation input would have been heavily biased towards 0

Base automatically changed from fortress-basin-aoi to main September 3, 2026 20:17
Both Stage-2 inference entry points guarded against predicting on a cell with no
observations, and both read the loader's masks with the wrong polarity. The loader
emits 1 = masked (landsat_eval.py:630-631), the guard's comment claimed 1 = valid, so
`all(not mask.any() ...)` asked "is this cube perfect?" instead of "is it empty?".

It was worse than inverted: it was a constant False. `mask_prediction_era5` forces a 1
into `t_m` on every cube, so the generator short-circuited before reading anything else,
and `st_m` (location, derived from the cell centre) is never nodata, so flipping the
comparison alone would have left it equally unreachable. A 100%-nodata cell therefore
received a finite, plausible FSC derived from its coordinates alone, written to the COG
as a measurement and counted in `aoi_coverage_fraction`. No NaN, no exception, no log.

Existing outputs are unaffected: measured over all 7300 cubes on disk, zero would have
triggered the guard. The bug was latent.

The replacement, `_loader_bridge.has_no_spacetime_observation`, fires when all three
space-time masks are 1 everywhere. Static ancillary (DEM, WorldCover), non-spatial time
bands (coarse VIIRS, ERA5) and derived geometry (location) are excluded: none can support
a spatially-resolved FSC patch on its own, and including location would make the
condition unsatisfiable. It reads named `MaskedOutput` fields rather than positional
indices, because a positional lookup with a comment asserting the opposite polarity is
exactly what failed here.

One definition in `_loader_bridge.py`, shared by the driver and the script, replacing two
copies. The per-cell "cell_fully_masked" warning becomes a per-day count; at grid scale
the condition it reports would fire for every cell of a sparsely-covered AOI.

Tests: all three fakes in test_inference_driver.py encoded the inverted convention, one
of them labelling all-ones masks "all-valid". Left alone, five tests would have stayed
green while silently writing empty mosaics. They now build real `MaskedOutput`s, so a
double that disagrees with the loader raises instead of answering wrong. The placeholder
end-to-end test becomes the empty-cell regression test, and a new positive-path test
derives a real cube from tests/fixtures/gee_reference_patches, since a placeholder cube
can no longer reach the predict-and-write path.

AC-28 and TASK-015-PLAN.md are amended: the original wording ("all input groups masked")
was unsatisfiable for every possible input, and the plan deferred the polarity to "the
existing mask convention" rather than stating it.

Assisted by Claude Code.
The AC-28 guard fix cited `docs/agents/bugs/MASK_CHECK_BUG.md` from seven places in
committed files. That path is not in the repository, so every one of them was a dead
link for anyone but its author. The surrounding docstrings already say what the defect
was and why the current shape avoids it, so the citations carried no information the
reader could not get in place.

Docstrings and comments only; no behaviour change.

Assisted by Claude Code.
@f-PLT
f-PLT merged commit d2a3241 into main Sep 3, 2026
3 checks passed
@f-PLT
f-PLT deleted the fix-mask-bug branch September 3, 2026 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants