Skip to content

Commit f57e2a1

Browse files
committed
ENH: set defaults for common suffixes
1 parent d266e67 commit f57e2a1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

niworkflows/interfaces/bids.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
"""Interfaces for handling BIDS-like neuroimaging structures."""
44

5+
from collections import defaultdict
56
from json import dumps
67
from pathlib import Path
78
from shutil import copytree, rmtree
@@ -27,6 +28,18 @@
2728
LOGGER = logging.getLogger('nipype.interface')
2829

2930

31+
def _none():
32+
return None
33+
34+
35+
# Automatically coerce certain suffixes (DerivativesDataSink)
36+
DEFAULT_DTYPES = defaultdict(_none, (
37+
("mask", "uint8"),
38+
("dseg", "int16"),
39+
("probseg", "float32"))
40+
)
41+
42+
3043
class _BIDSBaseInputSpec(BaseInterfaceInputSpec):
3144
bids_dir = traits.Either(
3245
(None, Directory(exists=True)), usedefault=True,
@@ -467,7 +480,8 @@ def _run_interface(self, runtime):
467480
self._results['compression'].append(_copy_any(fname, out_file))
468481

469482
is_nii = out_file.endswith(('.nii', '.nii.gz'))
470-
if is_nii and any((self.inputs.check_hdr, self.inputs.data_dtype)):
483+
data_dtype = self.inputs.data_dtype or DEFAULT_DTYPES[self.inputs.suffix]
484+
if is_nii and any((self.inputs.check_hdr, data_dtype)):
471485
# Do not use mmap; if we need to access the data at all, it will be to
472486
# rewrite, risking a BusError
473487
nii = nb.load(out_file, mmap=False)
@@ -497,11 +511,11 @@ def _run_interface(self, runtime):
497511
# Rewrite file with new header
498512
overwrite_header(nii, out_file)
499513

500-
if self.inputs.data_dtype:
514+
if data_dtype:
501515
if self.inputs.check_hdr:
502516
# load updated NIfTI
503517
nii = nb.load(out_file, mmap=False)
504-
data_dtype = np.dtype(self.inputs.data_dtype)
518+
data_dtype = np.dtype(data_dtype)
505519
if nii.get_data_dtype() != data_dtype:
506520
nii.set_data_dtype(data_dtype)
507521
nii.to_filename(out_file)

0 commit comments

Comments
 (0)