Skip to content

Commit

Permalink
Merge pull request #485 from dbic/bf-split-prefix
Browse files Browse the repository at this point in the history
BF: split prefix into path (if any) and provide only basename as prefix for dcm2niix invocation
  • Loading branch information
yarikoptic authored Dec 23, 2020
2 parents ee5cebb + bfa2ae3 commit 48fd898
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
22 changes: 14 additions & 8 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from math import nan
import shutil
import sys
import random
import re

from .due import due, Doi
Expand All @@ -16,6 +17,7 @@
write_config,
TempDirs,
safe_copyfile,
safe_movefile,
treat_infofile,
set_readonly,
clear_temp_dicoms,
Expand Down Expand Up @@ -614,7 +616,11 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconf
convertnode = Node(Dcm2niix(from_file=fromfile), name='convert')
convertnode.base_dir = tmpdir
convertnode.inputs.source_names = item_dicoms
convertnode.inputs.out_filename = prefix
convertnode.inputs.out_filename = op.basename(prefix) + "_heudiconv%03d" % random.randint(0, 999)
prefix_dir = op.dirname(prefix)
# if provided prefix had a path in it -- pass is as output_dir instead of default curdir
if prefix_dir:
convertnode.inputs.output_dir = prefix_dir

if nipype.__version__.split('.')[0] == '0':
# deprecated since 1.0, might be needed(?) before
Expand All @@ -627,7 +633,7 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconf
# prov information
prov_file = prefix + '_prov.ttl' if with_prov else None
if prov_file:
safe_copyfile(op.join(convertnode.base_dir,
safe_movefile(op.join(convertnode.base_dir,
convertnode.name,
'provenance.ttl'),
prov_file)
Expand Down Expand Up @@ -669,8 +675,8 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam

if isdefined(res.outputs.bvecs) and isdefined(res.outputs.bvals):
outname_bvecs, outname_bvals = prefix + '.bvec', prefix + '.bval'
safe_copyfile(res.outputs.bvecs, outname_bvecs, overwrite)
safe_copyfile(res.outputs.bvals, outname_bvals, overwrite)
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)

if isinstance(res_files, list):
res_files = sorted(res_files)
Expand Down Expand Up @@ -754,19 +760,19 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
outfile = outname + '.' + outtype

# Write the files needed:
safe_copyfile(fl, outfile, overwrite)
safe_movefile(fl, outfile, overwrite)
if bids_file:
outname_bids_file = "%s.json" % (outname)
safe_copyfile(bids_file, outname_bids_file, overwrite)
safe_movefile(bids_file, outname_bids_file, overwrite)
bids_outfiles.append(outname_bids_file)

# res_files is not a list
else:
outname = "{}.{}".format(prefix, outtype)
safe_copyfile(res_files, outname, overwrite)
safe_movefile(res_files, outname, overwrite)
if isdefined(res.outputs.bids):
try:
safe_copyfile(res.outputs.bids, outname_bids, overwrite)
safe_movefile(res.outputs.bids, outname_bids, overwrite)
bids_outfiles.append(outname_bids)
except TypeError as exc: ##catch lists
raise TypeError("Multiple BIDS sidecars detected.")
Expand Down
7 changes: 6 additions & 1 deletion heudiconv/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import logging
import os
import os.path as op
Expand All @@ -13,9 +14,13 @@
from .utils import (
docstring_parameter,
StudySessionInfo,
TempDirs,
)

lgr = logging.getLogger(__name__)
tempdirs = TempDirs()
# Ensure they are cleaned up upon exit
atexit.register(tempdirs.cleanup)

_VCS_REGEX = '%s\.(?:git|gitattributes|svn|bzr|hg)(?:%s|$)' % (op.sep, op.sep)

Expand Down Expand Up @@ -66,7 +71,7 @@ def get_extracted_dicoms(fl):
# of all files in all tarballs

# cannot use TempDirs since will trigger cleanup with __del__
tmpdir = mkdtemp(prefix='heudiconvDCM')
tmpdir = tempdirs('heudiconvDCM')

sessions = defaultdict(list)
session = 0
Expand Down
25 changes: 21 additions & 4 deletions heudiconv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,35 @@ def get_known_heuristics_with_descriptions():


def safe_copyfile(src, dest, overwrite=False):
"""Copy file but blow if destination name already exists
"""Copy file but blow if destination name already exists"""
return _safe_op_file(src, dest, "copyfile", overwrite=overwrite)


def safe_movefile(src, dest, overwrite=False):
"""Move file but blow if destination name already exists"""
return _safe_op_file(src, dest, "move", overwrite=overwrite)


def _safe_op_file(src, dest, operation, overwrite=False):
"""Copy or move file but blow if destination name already exists
Parameters
----------
operation: str, {copyfile, move}
"""
if op.isdir(dest):
dest = op.join(dest, op.basename(src))
if op.realpath(src) == op.realpath(dest):
lgr.debug("Source %s = destination %s", src, dest)
return
if op.lexists(dest):
if not overwrite:
raise RuntimeError(
"was asked to copy %s but destination already exists: %s"
% (src, dest)
"was asked to %s %s but destination already exists: %s"
% (operation, src, dest)
)
os.unlink(dest)
shutil.copyfile(src, dest)
getattr(shutil, operation)(src, dest)


# Globals to check filewriting permissions
Expand Down

0 comments on commit 48fd898

Please sign in to comment.