Repository navigation
Conversation
Adds support for sampling patches near detected cell centroids in TrainDataset, giving foreground sampling a strong prior for SmartSPIM data where cell segmentations are available but neurite skeletons are not. - `_load_cells`: reads detected_cells.csv (local or anonymous S3) and stores (z, y, x) centroids per brain - `sample_cell_voxel`: picks a random cell, jitters by ±cell_max_shift per axis, and clamps to keep the patch inside the volume - `clip_to_interior`: shared clamping helper - `sample_foreground_voxel`: checks `self.cells` first, before skeleton / segmentation / intensity fallbacks - `resolve_detected_cells_path`: derives the sibling detected_cells.csv path from a fused SmartSPIM image path (channel-matched) - `find_detected_cells_paths`: parallel existence-check across all training brains - `init_datasets`: wires in `cell_prefixes_path` and `cell_max_shift` Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
| {"anon": True} if str(csv_path).startswith("s3://") else {} | ||
| ) | ||
| df = pd.read_csv(csv_path, storage_options=storage_options) | ||
| xyz = df[["x", "y", "z"]].to_numpy(dtype=np.float32) |
There was a problem hiding this comment.
Do we know that cell coordinate are always in voxels and not physical units?
| Voxel coordinate representing a likely foreground location. | ||
| """ | ||
| if brain_id in self.skeletons and np.random.random() > 0.5: | ||
| if brain_id in self.cells: |
There was a problem hiding this comment.
Will this make it so every patch includes a detected cell? If so, would it make sense to have some patches be empty of cells?
| return brain_id, None | ||
|
|
||
| cell_paths = dict() | ||
| with ThreadPoolExecutor(max_workers=max_workers) as executor: |
There was a problem hiding this comment.
Seems like a thread pool could be overkill for checking the existence of paths for less than thousands of brains, but if it works it's probably fine.
|
|
||
| # --- Helpers --- | ||
| # --- Locate Detected Cells --- | ||
| _FUSED_IMAGE_DIR = "image_tile_fusing/OMEZarr" |
There was a problem hiding this comment.
Making these values constants means this can only be used for smartSPIM. Since we might want to apply the same approach to HCR or other modalities, it could be better to have them parameterized, with these as maybe the default values.
| brain_ids, | ||
| img_paths_json, | ||
| patch_shape, | ||
| boundary_buffer=5000, |
There was a problem hiding this comment.
Are you using the https://github.com/AllenNeuralDynamics/aind-exaspim-image-compression/blob/main/scripts/precompute.py script to generate the caches? If so, it wasn't updated to pass in these new parameters so self.cells will be empty.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The reordered parameters break an existing positional caller, and the new sampling behavior lacks regression tests.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Adds detected-cell-guided patch sampling to TrainDataset.
Changes:
- Loads and samples detected cell centroids.
- Adds boundary-safe coordinate clamping.
- Resolves and wires detected-cell CSV paths.
| File | Description |
|---|---|
data_handling.py |
Implements cell ingestion, sampling, path discovery, and dataset configuration. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| boundary_buffer=5000, | ||
| cell_prefixes_path=None, | ||
| cell_max_shift=None, |
| idx = random.randint(0, len(self.cells[brain_id]) - 1) | ||
| center = self.cells[brain_id][idx] | ||
| shift = np.array( | ||
| [np.random.randint(-r, r + 1) for r in self.cell_max_shift] | ||
| ) | ||
| return self.clip_to_interior(brain_id, center + shift) |


Adds support for sampling patches near detected cell centroids in TrainDataset, giving foreground sampling a strong prior for SmartSPIM data where cell segmentations are available but neurite skeletons are not.
_load_cells: reads detected_cells.csv (local or anonymous S3) and stores (z, y, x) centroids per brainsample_cell_voxel: picks a random cell, jitters by ±cell_max_shift per axis, and clamps to keep the patch inside the volumeclip_to_interior: shared clamping helpersample_foreground_voxel: checksself.cellsfirst, before skeleton / segmentation / intensity fallbacksresolve_detected_cells_path: derives the sibling detected_cells.csv path from a fused SmartSPIM image path (channel-matched)find_detected_cells_paths: parallel existence-check across all training brainsinit_datasets: wires incell_prefixes_pathandcell_max_shift