Skip to content

Commit 48fd898

Browse files
authored
Merge pull request #485 from dbic/bf-split-prefix
BF: split prefix into path (if any) and provide only basename as prefix for dcm2niix invocation
2 parents ee5cebb + bfa2ae3 commit 48fd898

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

heudiconv/convert.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from math import nan
66
import shutil
77
import sys
8+
import random
89
import re
910

1011
from .due import due, Doi
@@ -16,6 +17,7 @@
1617
write_config,
1718
TempDirs,
1819
safe_copyfile,
20+
safe_movefile,
1921
treat_infofile,
2022
set_readonly,
2123
clear_temp_dicoms,
@@ -614,7 +616,11 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconf
614616
convertnode = Node(Dcm2niix(from_file=fromfile), name='convert')
615617
convertnode.base_dir = tmpdir
616618
convertnode.inputs.source_names = item_dicoms
617-
convertnode.inputs.out_filename = prefix
619+
convertnode.inputs.out_filename = op.basename(prefix) + "_heudiconv%03d" % random.randint(0, 999)
620+
prefix_dir = op.dirname(prefix)
621+
# if provided prefix had a path in it -- pass is as output_dir instead of default curdir
622+
if prefix_dir:
623+
convertnode.inputs.output_dir = prefix_dir
618624

619625
if nipype.__version__.split('.')[0] == '0':
620626
# deprecated since 1.0, might be needed(?) before
@@ -627,7 +633,7 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconf
627633
# prov information
628634
prov_file = prefix + '_prov.ttl' if with_prov else None
629635
if prov_file:
630-
safe_copyfile(op.join(convertnode.base_dir,
636+
safe_movefile(op.join(convertnode.base_dir,
631637
convertnode.name,
632638
'provenance.ttl'),
633639
prov_file)
@@ -669,8 +675,8 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
669675

670676
if isdefined(res.outputs.bvecs) and isdefined(res.outputs.bvals):
671677
outname_bvecs, outname_bvals = prefix + '.bvec', prefix + '.bval'
672-
safe_copyfile(res.outputs.bvecs, outname_bvecs, overwrite)
673-
safe_copyfile(res.outputs.bvals, outname_bvals, overwrite)
678+
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
679+
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
674680

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

756762
# Write the files needed:
757-
safe_copyfile(fl, outfile, overwrite)
763+
safe_movefile(fl, outfile, overwrite)
758764
if bids_file:
759765
outname_bids_file = "%s.json" % (outname)
760-
safe_copyfile(bids_file, outname_bids_file, overwrite)
766+
safe_movefile(bids_file, outname_bids_file, overwrite)
761767
bids_outfiles.append(outname_bids_file)
762768

763769
# res_files is not a list
764770
else:
765771
outname = "{}.{}".format(prefix, outtype)
766-
safe_copyfile(res_files, outname, overwrite)
772+
safe_movefile(res_files, outname, overwrite)
767773
if isdefined(res.outputs.bids):
768774
try:
769-
safe_copyfile(res.outputs.bids, outname_bids, overwrite)
775+
safe_movefile(res.outputs.bids, outname_bids, overwrite)
770776
bids_outfiles.append(outname_bids)
771777
except TypeError as exc: ##catch lists
772778
raise TypeError("Multiple BIDS sidecars detected.")

heudiconv/parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import atexit
12
import logging
23
import os
34
import os.path as op
@@ -13,9 +14,13 @@
1314
from .utils import (
1415
docstring_parameter,
1516
StudySessionInfo,
17+
TempDirs,
1618
)
1719

1820
lgr = logging.getLogger(__name__)
21+
tempdirs = TempDirs()
22+
# Ensure they are cleaned up upon exit
23+
atexit.register(tempdirs.cleanup)
1924

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

@@ -66,7 +71,7 @@ def get_extracted_dicoms(fl):
6671
# of all files in all tarballs
6772

6873
# cannot use TempDirs since will trigger cleanup with __del__
69-
tmpdir = mkdtemp(prefix='heudiconvDCM')
74+
tmpdir = tempdirs('heudiconvDCM')
7075

7176
sessions = defaultdict(list)
7277
session = 0

heudiconv/utils.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,35 @@ def get_known_heuristics_with_descriptions():
354354

355355

356356
def safe_copyfile(src, dest, overwrite=False):
357-
"""Copy file but blow if destination name already exists
357+
"""Copy file but blow if destination name already exists"""
358+
return _safe_op_file(src, dest, "copyfile", overwrite=overwrite)
359+
360+
361+
def safe_movefile(src, dest, overwrite=False):
362+
"""Move file but blow if destination name already exists"""
363+
return _safe_op_file(src, dest, "move", overwrite=overwrite)
364+
365+
366+
def _safe_op_file(src, dest, operation, overwrite=False):
367+
"""Copy or move file but blow if destination name already exists
368+
369+
Parameters
370+
----------
371+
operation: str, {copyfile, move}
358372
"""
359373
if op.isdir(dest):
360374
dest = op.join(dest, op.basename(src))
375+
if op.realpath(src) == op.realpath(dest):
376+
lgr.debug("Source %s = destination %s", src, dest)
377+
return
361378
if op.lexists(dest):
362379
if not overwrite:
363380
raise RuntimeError(
364-
"was asked to copy %s but destination already exists: %s"
365-
% (src, dest)
381+
"was asked to %s %s but destination already exists: %s"
382+
% (operation, src, dest)
366383
)
367384
os.unlink(dest)
368-
shutil.copyfile(src, dest)
385+
getattr(shutil, operation)(src, dest)
369386

370387

371388
# Globals to check filewriting permissions

0 commit comments

Comments
 (0)