Skip to content

Stop enforcing dtype on save - #137

Open
C-Achard wants to merge 3 commits into
cy/sample-datafrom
cy/preserve-dtype
Open

C-Achard wants to merge 3 commits into
cy/sample-datafrom
cy/preserve-dtype

Conversation

@C-Achard

Copy link
Copy Markdown
Collaborator

Stops save_layer from casting every saved layer to float32. Images now keep their dtype, and labels are saved in the smallest uint that keeps the max label.

Changes

utils.save_layer

  • Before: every array was cast to float32 and written as float32.
  • Now: the array is written with its own dtype. With the new labels=True argument, non-negative integer labels are first narrowed with the new optimize_labels_dtype (via np.min_scalar_type). Floats, negative values and empty arrays unchanged.

Utilities plugins (plugin_convert.py)

  • Saving: remove large objects, remove small objects, to semantic and to instance now save with labels=True. Fragment and anisotropy pass labels=True only when the input layer is a Labels layer.
  • Remove large objects: no longer forces uint16.
  • Anisotropy: the resized volume is cast back to the input layer's dtype before saving and display. Previously it was saved as float32 and shown as the image dtype or uint16.

instance_segmentation.to_semantic

No longer modifies: imageimage >= 1] = 1 replaced by (image >= 1).astype(np.uint16).


Stack created with GitHub Stacks CLI • Give Feedback 💬

@C-Achard
C-Achard added this pull request to stack #138 November 20, 2026 09:04
@C-Achard C-Achard self-assigned this Nov 20, 2026
@C-Achard
C-Achard requested a balanced review from Copilot November 20, 2026 09:06

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.

🟢 Approval recommended

No active review issues are recorded for the dtype-preservation and label-storage changes.

No open issues 🎉

What changed in this PR

This PR preserves image dtypes when saving layers and reduces label storage size without losing label values.

Changes:

  • Preserve array dtypes in save_layer, with optional narrowing of non-negative integer labels to the smallest unsigned dtype.
  • Update conversion utilities to use label-aware saving and preserve input dtype after anisotropy resizing.
  • Make semantic conversion non-mutating and add regression tests for dtype preservation, label narrowing, and input immutability.
File Description
napari_cellseg3d/​utils.py Preserve saved dtypes and add optional label dtype optimization.
napari_cellseg3d/​code_plugins/​plugin_convert.py Apply label-aware saving and remove forced dtype conversions.
napari_cellseg3d/​code_models/​instance_segmentation.py Produce semantic masks without modifying the input.
napari_cellseg3d/​_tests/​test_utils.py Test dtype-preserving saves and label narrowing.
napari_cellseg3d/​_tests/​test_instance_segmentation.py Verify semantic conversion output and input immutability.

Review effort: Balanced

@C-Achard
C-Achard marked this pull request as ready for review November 20, 2026 09:12

This branch has not been deployed

No deployments
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