Skip to content

Implement modified N4 masking for high-res applications - #190

Merged
akhanf merged 2 commits into
mainfrom
revert-n4-mask
Jul 30, 2026
Merged

Implement modified N4 masking for high-res applications#190
akhanf merged 2 commits into
mainfrom
revert-n4-mask

Conversation

@akhanf

@akhanf akhanf commented Jul 30, 2026

Copy link
Copy Markdown
Member

This pull request updates the bias field correction workflow to improve compatibility and accuracy, especially when working with masks and the latest zarrnii library. The most important changes are grouped below:

Dependency Updates:

  • Updated the zarrnii dependency to version >=0.21.2,<0.22.0 in pyproject.toml to ensure compatibility with new features, such as mask support in bias field correction.

Workflow and Rule Enhancements:

  • Modified the n4_biasfield rule in segmentation.smk to include a mask input, which will be used for more precise bias field correction.

Script Improvements (Bias Field Correction):

  • Added import of the tempfile module in n4_biasfield.py to support temporary directory usage for intermediate file storage.
  • Changed the bias field correction script to:
    • Save the input image as OME-Zarr in a temporary directory before processing for improved I/O handling.
    • Load both the bias field and mask as ZarrNii objects from NIfTI files.
    • Pass the mask (lowres_mask) to the scaled processing step, enabling mask-aware bias field correction.

zarrnii v0.21.2 now will just set voxels outside mask to zero when using
mask with n4 apply biasfield..

however high-res apply with mask uses more memory, so using
experimental version here to break down the n4 into two steps (rechunk
to local, then apply n4)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Not ready to approve

The new unconditional temp OME-Zarr round-trip can significantly increase I/O and local scratch usage and may cause failures on environments with limited/slow temp storage unless made configurable/conditional.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR updates SPIMquant’s N4 bias field correction workflow to better support mask-aware correction and align with newer zarrnii behavior, improving accuracy/compatibility for high-resolution use cases.

Changes:

  • Add a brain mask input to the n4_biasfield Snakemake rule and pass it through to the bias field application script.
  • Update n4_biasfield.py to materialize inputs to a temporary OME-Zarr and apply the low-res bias field using a low-res mask.
  • Bump zarrnii dependency from 0.21.1 to 0.21.2 (and update lockfile) to pick up required features.
File summaries
File Description
spimquant/workflow/scripts/n4_biasfield.py Adds temp OME-Zarr materialization and passes a low-res mask into scaled bias-field application.
spimquant/workflow/rules/segmentation.smk Extends the n4_biasfield rule to include a BIDS-resolved mask input.
pyproject.toml Updates the zarrnii version constraint to >=0.21.2,<0.22.0.
pixi.lock Locks zarrnii to 0.21.2 and updates associated hashes/entries.
Review details

Comments suppressed due to low confidence (1)

spimquant/workflow/scripts/n4_biasfield.py:30

  • The comment says "try first saving to /tmp", but the code always uses the temp OME-Zarr round-trip and it may not be /tmp depending on TMPDIR settings. Suggest updating the comment to reflect what the code actually does.
        # try first saving to /tmp
  • Files reviewed: 3/4 changed files
  • Comments generated: 1
  • Review effort level: Low

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread spimquant/workflow/scripts/n4_biasfield.py Outdated
only when imaris is used

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Ready to approve

The changes are consistent with the workflow’s existing mask dependency and correctly wire mask-aware bias correction while keeping the temp materialization limited to the .ims backend.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Review details
  • Files reviewed: 3/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

@akhanf
akhanf merged commit cda52e5 into main Jul 30, 2026
3 checks passed
@akhanf
akhanf deleted the revert-n4-mask branch July 30, 2026 22:37
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