5
5
from math import nan
6
6
import shutil
7
7
import sys
8
+ import random
8
9
import re
9
10
10
11
from .due import due , Doi
16
17
write_config ,
17
18
TempDirs ,
18
19
safe_copyfile ,
20
+ safe_movefile ,
19
21
treat_infofile ,
20
22
set_readonly ,
21
23
clear_temp_dicoms ,
@@ -614,7 +616,11 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconf
614
616
convertnode = Node (Dcm2niix (from_file = fromfile ), name = 'convert' )
615
617
convertnode .base_dir = tmpdir
616
618
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
618
624
619
625
if nipype .__version__ .split ('.' )[0 ] == '0' :
620
626
# deprecated since 1.0, might be needed(?) before
@@ -627,7 +633,7 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids_options, tmpdir, dcmconf
627
633
# prov information
628
634
prov_file = prefix + '_prov.ttl' if with_prov else None
629
635
if prov_file :
630
- safe_copyfile (op .join (convertnode .base_dir ,
636
+ safe_movefile (op .join (convertnode .base_dir ,
631
637
convertnode .name ,
632
638
'provenance.ttl' ),
633
639
prov_file )
@@ -669,8 +675,8 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
669
675
670
676
if isdefined (res .outputs .bvecs ) and isdefined (res .outputs .bvals ):
671
677
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 )
674
680
675
681
if isinstance (res_files , list ):
676
682
res_files = sorted (res_files )
@@ -754,19 +760,19 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
754
760
outfile = outname + '.' + outtype
755
761
756
762
# Write the files needed:
757
- safe_copyfile (fl , outfile , overwrite )
763
+ safe_movefile (fl , outfile , overwrite )
758
764
if bids_file :
759
765
outname_bids_file = "%s.json" % (outname )
760
- safe_copyfile (bids_file , outname_bids_file , overwrite )
766
+ safe_movefile (bids_file , outname_bids_file , overwrite )
761
767
bids_outfiles .append (outname_bids_file )
762
768
763
769
# res_files is not a list
764
770
else :
765
771
outname = "{}.{}" .format (prefix , outtype )
766
- safe_copyfile (res_files , outname , overwrite )
772
+ safe_movefile (res_files , outname , overwrite )
767
773
if isdefined (res .outputs .bids ):
768
774
try :
769
- safe_copyfile (res .outputs .bids , outname_bids , overwrite )
775
+ safe_movefile (res .outputs .bids , outname_bids , overwrite )
770
776
bids_outfiles .append (outname_bids )
771
777
except TypeError as exc : ##catch lists
772
778
raise TypeError ("Multiple BIDS sidecars detected." )
0 commit comments