Fix prediction mask names and background value#16
Conversation
Use the `label` and `fold` columns produced by the upstream k-fold split instead of deriving labels from coverage columns and randomly splitting val. Memory-mapped via HuggingFace datasets so the full embedding parquet no longer has to fit in numpy. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…ddings Datamodule downloads embeddings + kfold artifacts from MLflow, joins on (slide_id, x, y) via pyarrow, applies class mapping, tissue/class coverage filters, and exposes per-fold splits via set_val_fold(). Training script loops folds in a single run and logs per-fold + aggregate metrics. Probe adds per-class F1, confusion matrix figures, optional input L2-norm and class weights. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The experiment file was declaring /class_mapping as a fresh default while configs/ml/linear_probe.yaml already had one, which Hydra rejects as a duplicate. Mark it as an override so the experiment replaces the base default. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ml/train.py uses @with_cli_args(["+ml=linear_probe"]), so the decorator already injects that arg. Passing it again on the command line caused Hydra to load configs/ml/linear_probe.yaml twice and reject duplicate defaults. Rely on the decorator and pass only +experiment=... Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ng refs
Two interpolation problems prevented Hydra from resolving the linear-probe
config:
1. configs/ml.yaml uses ${random_seed:} and configs/ml/linear_probe.yaml
uses ${len:...}, but neither resolver is registered anywhere. Register
both at module import time in ml/train.py.
2. The class_mapping yamls use # @Package _global_, so class_mapping,
class_indices, and class_names land at the config root. The references
in linear_probe.yaml were doubly nested (e.g. class_mapping.class_mapping).
Drop the prefix.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The filtered tiles parquet collapses ROI columns at tiling time, so
kfold writes canonical names ("Epithelium", etc.) directly into `label`.
The raw→canonical lookup built from the BB-suffixed YAML lists matched
none of these and dropped the entire 1.1M-tile dataset under
drop_unmapped=True.
Extend _raw_to_canonical with identity entries for every canonical class
so modern parquets pass through while legacy un-collapsed labels still
collapse correctly. "background" stays unmapped → dropped, as intended.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- Add EmbeddingsDataModule.compute_class_weights("balanced"|"inverse")
using sklearn-style weights from the current train fold.
- train.py resolves class_weights="balanced"/"inverse" via the
datamodule and passes the resulting list to LinearProbe at instantiate
time (per-fold, since splits change).
- Bump class_coverage_min from 0.0 to 0.5 to drop mosaic tiles.
- Drop the redundant /class_mapping default from configs/ml/linear_probe.yaml;
experiment files now own the choice.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Extract derive_labels logic to shared preprocessing/_labels.py, then use it in both split/kfold_split.py and the new embedding_dataset pipeline. The new pipeline joins k-fold (train) / filter_tiles (test) tile metadata with precomputed embeddings after applying tissue + per-dominant-class ROI thresholds, and emits a SlidesTilesLoader-compatible Parquet dataset as an MLflow artifact. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Joining 1M+ rows of list<double> embeddings was either OOMing on to_pandas() or hitting int32 list-offset overflow inside take(). The fix: - read embeddings into Arrow only and cast each chunk to large_list so take() concatenation uses int64 offsets; - run the join on keys plus a synthetic row index because Acero refuses list columns in non-key fields, then pull embeddings via take(); - combine_chunks() before take() for an O(N) single-pass copy; - write the parquet straight from Arrow, never materialising the embedding column in pandas. Also bumps the kube job memory to 64Gi to give the combined-chunks + take() peak some headroom, and trims the verbose [timing] prints down to one progress line per split. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Without this guard a malformed train artifact would crash deep inside apply_thresholds with a confusing KeyError. Surface a clear error that points at the expected upstream artifact instead. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Clear the batch buffer only on rank!=0 or after a successful write so the on_test_end fallback no longer hits an always-empty buffer. Add diagnostic prints to the silent early-return guards and an idempotency flag so the two write hooks cooperate. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
# Conflicts: # configs/experiment/ml/linear_classifier_test_adamw.yaml # configs/experiment/ml/linear_classifier_test_lbfgs.yaml # configs/experiment/preprocessing/embeddings_virchow2_tissue_tiles_05mpp.yaml # configs/ml/task/final_linear_classifier.yaml # configs/preprocessing/embeddings.yaml # ml/callbacks/tiff_prediction_map_writer.py # preprocessing/embeddings.py
📝 WalkthroughWalkthroughTiffPredictionMapWriter changes default background value from 255 to 0, removes blake2b-based filename generation for simpler path-based naming, and configuration files explicitly set the background value to match the new implementation default. ChangesTIFF Prediction Map Writer Update
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@ml/callbacks/tiff_prediction_map_writer.py`:
- Around line 519-520: _current implementation of _slide_prediction_filename
returns only the basename which causes silent overwrites for identically-named
files from different directories; update _slide_prediction_filename to preserve
the readable basename but append a stable, short disambiguator derived from the
full input path (e.g., first 8 chars of a hash of str(Path(path).resolve()) or
include parent folder name) before the .tiff suffix so names remain
deterministic and human-readable, and/or add a duplicate-detection check in the
writer that raises an error if two inputs would map to the same output; ensure
you reference and change the _slide_prediction_filename function and the caller
that writes prediction files so the new name format is used consistently.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: f2437f6d-25d0-430a-ab4e-c58d20390ed7
📒 Files selected for processing (3)
configs/experiment/ml/test_linear_virchow2_lbfgs.yamlconfigs/ml/trainer/final_with_prediction_maps.yamlml/callbacks/tiff_prediction_map_writer.py
There was a problem hiding this comment.
Code Review
This pull request updates the TiffPredictionMapWriter by changing the default background_value from 255 to 0 in both the configuration files and the class constructor. Additionally, it simplifies the _slide_prediction_filename function by removing the hash suffix and previous sanitization logic. Feedback suggests that this simplification could lead to filename collisions and filesystem compatibility issues, recommending a more robust sanitization approach that handles illegal characters while maintaining Unicode support.
Summary
0Motivation
Prediction masks were difficult to map back to their source WSIs because filenames had generated hash suffixes and non-ASCII characters were
sanitized. This broke names such as
61 Follikuläres Schilddrüsenkarzinom.Prediction masks also used
255for uncovered/background pixels, while the report-facing annotation masks use0after remapping. This causedreport overlays to render the whole non-predicted slide area as white.
Summary by CodeRabbit