Skip to content

feat: add cell-location sampling to TrainDataset - #40

Open
anna-grim wants to merge 1 commit into
mainfrom
feat-cell-sampling
Open

anna-grim wants to merge 1 commit into
mainfrom
feat-cell-sampling

Conversation

@anna-grim

Copy link
Copy Markdown
Collaborator

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

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>
@anna-grim
anna-grim requested a review from carshadi September 29, 2026 18:45
{"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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity · 1 Low severity

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.

Comment on lines +1586 to +1588
boundary_buffer=5000,
cell_prefixes_path=None,
cell_max_shift=None,
Comment on lines +649 to +654
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)
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.

3 participants