Skip to content

Commit

Permalink
Merge pull request #208 from mgxd/enh/dcm2niix-opts
Browse files Browse the repository at this point in the history
enh: allow additional dcm2niix configuration
  • Loading branch information
mgxd authored Jan 23, 2019
2 parents 083f460 + c78d25b commit 3700462
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
13 changes: 7 additions & 6 deletions heudiconv/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def get_parser():
'jsons')
parser.add_argument('--random-seed', type=int, default=None,
help='Random seed to initialize RNG')
parser.add_argument('--dcmconfig', default=None,
help='JSON file for additional dcm2niix configuration')
submission = parser.add_argument_group('Conversion submission options')
submission.add_argument('-q', '--queue', default=None,
help='select batch system to submit jobs to instead'
Expand All @@ -231,14 +233,12 @@ def process_args(args):

outdir = op.abspath(args.outdir)

if args.command:
process_extra_commands(outdir, args)
return

lgr.info(INIT_MSG(packname=__packagename__,
version=__version__))


if args.command:
process_extra_commands(outdir, args)
return
#
# Load heuristic -- better do it asap to make sure it loads correctly
#
Expand Down Expand Up @@ -335,7 +335,8 @@ def process_args(args):
bids=args.bids,
seqinfo=seqinfo,
min_meta=args.minmeta,
overwrite=args.overwrite,)
overwrite=args.overwrite,
dcmconfig=args.dcmconfig,)

lgr.info("PROCESSING DONE: {0}".format(
str(dict(subject=sid, outdir=study_outdir, session=session))))
Expand Down
37 changes: 29 additions & 8 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def conversion_info(subject, outdir, info, filegroup, ses):

def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
anon_outdir, with_prov, ses, bids, seqinfo, min_meta,
overwrite):
overwrite, dcmconfig):
if dicoms:
lgr.info("Processing %d dicoms", len(dicoms))
elif seqinfo:
Expand Down Expand Up @@ -194,7 +194,8 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
bids=bids,
outdir=tdir,
min_meta=min_meta,
overwrite=overwrite,)
overwrite=overwrite,
dcmconfig=dcmconfig,)

for item_dicoms in filegroup.values():
clear_temp_dicoms(item_dicoms)
Expand All @@ -211,7 +212,8 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,


def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
bids, outdir, min_meta, overwrite, symlink=True, prov_file=None):
bids, outdir, min_meta, overwrite, symlink=True, prov_file=None,
dcmconfig=None):
"""Perform actual conversion (calls to converter etc) given info from
heuristic's `infotodict`
Expand Down Expand Up @@ -278,7 +280,7 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,

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

bids_outfiles = save_converted_files(res, item_dicoms, bids,
outtype, prefix,
Expand Down Expand Up @@ -382,8 +384,25 @@ def convert_dicom(item_dicoms, bids, prefix,
shutil.copyfile(filename, outfile)


def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir):
""" """
def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir, dcmconfig=None):
"""
Converts DICOMs grouped from heuristic using Nipype's Dcm2niix interface.
Parameters
----------
item_dicoms : List
DICOM files to convert
prefix : String
Heuristic output path
with_prov : Bool
Store provenance information
bids : Bool
Output BIDS sidecar JSONs
tmpdir : Directory
Conversion working directory
dcmconfig : File (optional)
JSON file used for additional Dcm2niix configuration
"""
import nipype
if with_prov:
from nipype import config
Expand All @@ -393,9 +412,11 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir):

item_dicoms = list(map(op.abspath, item_dicoms)) # absolute paths

dicom_dir = op.dirname(item_dicoms[0]) if item_dicoms else None
fromfile = dcmconfig if dcmconfig else None
if fromfile:
lgr.info("Using custom config file %s", fromfile)

convertnode = Node(Dcm2niix(), name='convert')
convertnode = Node(Dcm2niix(from_file=fromfile), name='convert')
convertnode.base_dir = tmpdir
convertnode.inputs.source_names = item_dicoms
convertnode.inputs.out_filename = op.basename(op.dirname(prefix))
Expand Down

0 comments on commit 3700462

Please sign in to comment.