Skip to content

fix: preserve original datatype on output instead of hardcoding signed short - #7

Merged
akhanf merged 1 commit into
masterfrom
copilot/fix-signed-short-casting-issue
Jul 9, 2026
Merged

fix: preserve original datatype on output instead of hardcoding signed short#7
akhanf merged 1 commit into
masterfrom
copilot/fix-signed-short-casting-issue

Conversation

Copilot AI commented Jun 24, 2026

Copy link
Copy Markdown

fslmaths -odt short (INT16) was hardcoded as the output datatype, silently corrupting UINT16 images by mapping values > 32767 to negative numbers.

Changes

  • run.sh: Use fslinfo to read the original image's NIfTI datatype, then map it to the correct fslmaths -odt argument before writing the final output. Since fslmaths does not support ushort, UINT16 inputs are mapped to int (INT32), which holds all UINT16 values (0–65535) without sign corruption.
orig_dtype=$(fslinfo $nii | grep "^data_type" | awk '{print $2}')
case "$orig_dtype" in
    UINT8)   odt="char"   ;;
    INT16)   odt="short"  ;;
    UINT16)  odt="int"    ;;  # fslmaths has no ushort; INT32 holds all UINT16 values without sign corruption
    INT32)   odt="int"    ;;
    FLOAT32) odt="float"  ;;
    FLOAT64) odt="double" ;;
    *)       odt="short"  ;;
esac
fslmaths $out_unwarped $out_unwarped -odt $odt

No new dependencies — fslinfo is part of the existing FSL 5.0 install.

@akhanf

akhanf commented Jun 24, 2026

Copy link
Copy Markdown
Member

@copilot: the problem is fslmaths does not support odt of ushort:
Error: Unknown datatype "ushort" - Possible datatypes are: char short int float double input

Copilot AI commented Jun 24, 2026

Copy link
Copy Markdown
Author

@copilot: the problem is fslmaths does not support odt of ushort:
Error: Unknown datatype "ushort" - Possible datatypes are: char short int float double input

...

Good catch. Since fslmaths only supports char short int float double input, I've remapped UINT16 → int (INT32). INT32 can hold all UINT16 values (0–65535) without any sign corruption. Fixed in commit fix: map UINT16 to int (INT32) since fslmaths does not support ushort.

Copilot AI changed the title fix: preserve unsigned short datatype on output instead of hardcoding signed short fix: preserve original datatype on output instead of hardcoding signed short Jun 24, 2026
Copilot AI requested a review from akhanf June 24, 2026 19:52
@akhanf
akhanf marked this pull request as ready for review June 24, 2026 20:02
@akhanf
akhanf force-pushed the copilot/fix-signed-short-casting-issue branch from 309cbf7 to f093422 Compare July 9, 2026 16:39
@akhanf

akhanf commented Jul 9, 2026

Copy link
Copy Markdown
Member

cleaned up history that involved deps etc, since those dealt with in the pixi PR..
this change has been tested to work there

@akhanf
akhanf merged commit 2ef318a into master Jul 9, 2026
@akhanf
akhanf deleted the copilot/fix-signed-short-casting-issue branch July 9, 2026 16:41
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