Skip to content

[WIP] Handle dcm2niix not compressing nifti files #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions heudiconv/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
for bids_file in bids_files:
# get filenames
f_name = "/".join(bids_file.split("/")[-2:])
f_name = f_name.replace("json", "nii.gz")
f_name = f_name.replace("json", outtype_from_bidsfile(bids_file))
rows[f_name] = get_formatted_scans_key_row(item[-1][0])
subj_, ses_ = find_subj_ses(f_name)
if not subj_:
Expand Down Expand Up @@ -938,9 +938,9 @@
# acq_times for the compatible fmaps:
acq_times_fmaps = {
k: acq_times[
# remove session folder and '.json', add '.nii.gz':
# remove session folder and '.json', add outtype:
remove_suffix(remove_prefix(v[0], sess_folder + op.sep), ".json")
+ ".nii.gz"
+ '.' + outtype_from_bidsfile(v[0])
]
for k, v in compatible_fmap_groups.items()
}
Expand All @@ -956,7 +956,7 @@
acq_times[
# remove session folder and '.json', add '.nii.gz':
remove_suffix(remove_prefix(json_file, sess_folder + op.sep), ".json")
+ ".nii.gz"
+ '.' + outtype_from_bidsfile(json_file)
]
)
# differences in acquisition time (abs value):
Expand Down Expand Up @@ -1209,3 +1209,22 @@
clean_label,
)
return clean_label


def outtype_from_bidsfile(json_file: str) -> str:
"""Returns outtype ('nii' or 'nii.gz') for an existing json bids file

Parameters
----------
filename: string

Returns
-------
fileext: string
file extension / outtype
"""
json_path = Path(json_file)
for suffix in ['nii.gz', 'nii']:
if op.exists(json_path.with_suffix('.' + suffix)):
return suffix
raise RuntimeError('No accompanying file found for %s' % json_file)

Check warning on line 1230 in heudiconv/bids.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/bids.py#L1230

Added line #L1230 was not covered by tests
33 changes: 29 additions & 4 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import shutil
import sys
import json
from types import ModuleType
from typing import TYPE_CHECKING, Any, List, Optional, cast

Expand Down Expand Up @@ -620,16 +621,40 @@
description="DICOM to NIfTI + .json sidecar conversion utility",
tags=["implementation"],
)
outname, scaninfo = (prefix + "." + outtype, prefix + scaninfo_suffix)
outnames = [prefix + "." + t for t in ["nii", "nii.gz"]]
scaninfo = prefix + scaninfo_suffix

if not op.exists(outname) or overwrite:
if not any([op.exists(outname) for outname in outnames]) or overwrite:
tmpdir = tempdirs("dcm2niix")

# demote outtype if dcmconfig explicitly disables compression
# and issue a warning (user supplied mixed signals here)
if dcmconfig is not None:
with open(dcmconfig) as f:
dcmconfig_dict = json.loads(f.read())
if dcmconfig_dict.get('compress', 'y') == 'n':
outtype = 'nii'
lgr.warning("Demoting outtype to uncompressed nifti, because "

Check warning on line 637 in heudiconv/convert.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/convert.py#L633-L637

Added lines #L633 - L637 were not covered by tests
"compress is set to 'n' in supplied dcmconfig")

# run conversion through nipype
res, prov_file = nipype_convert(
item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconfig
)

# try to handle compression failures from dcm2niix
if outtype == 'nii.gz':
converted_files = res.outputs.converted_files
if isinstance(converted_files, list):
# we do not handle mixed compression results from dcm2niix, yet
# fail, if any of the outputs weren't compressed properly
assert all([x.endswith('nii.gz') for x in converted_files])
else:
if converted_files.endswith('nii'):
lgr.warning("Conversion returned uncompressed nifti (>4GB?) - "

Check warning on line 654 in heudiconv/convert.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/convert.py#L654

Added line #L654 was not covered by tests
"demoting outtype to 'nii'")
outtype = 'nii'

Check warning on line 656 in heudiconv/convert.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/convert.py#L656

Added line #L656 was not covered by tests

bids_outfiles = save_converted_files(
res,
item_dicoms,
Expand All @@ -653,8 +678,8 @@
tempdirs.rmtree(tmpdir)
else:
raise RuntimeError(
"was asked to convert into %s but destination already exists"
% (outname)
"was asked to convert into %s.nii[.gz] but destination already exists"
% (prefix)
)

# add the taskname field to the json file(s):
Expand Down