14
14
set_readonly ,
15
15
clear_temp_dicoms ,
16
16
seqinfo_fields ,
17
+ assure_no_file_exists ,
18
+ file_md5sum
17
19
)
18
20
from .bids import (
19
21
convert_sid_bids ,
@@ -100,15 +102,35 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
100
102
if not op .exists (idir ):
101
103
os .makedirs (idir )
102
104
103
- shutil .copy (heuristic .filename , idir )
104
105
ses_suffix = "_ses-%s" % ses if ses is not None else ""
105
106
info_file = op .join (idir , '%s%s.auto.txt' % (sid , ses_suffix ))
106
107
edit_file = op .join (idir , '%s%s.edit.txt' % (sid , ses_suffix ))
107
108
filegroup_file = op .join (idir , 'filegroup%s.json' % ses_suffix )
108
109
110
+ # if conversion table(s) do not exist -- we need to prepare them
111
+ # (the *prepare* stage in https://github.com/nipy/heudiconv/issues/134)
112
+ reuse_conversion_table = op .exists (edit_file )
113
+ # We also might need to redo it if changes in the heuristic file
114
+ # detected
115
+ # ref: https://github.com/nipy/heudiconv/issues/84#issuecomment-330048609
116
+ # for more automagical wishes
117
+ target_heuristic_filename = op .join (idir , op .basename (heuristic .filename ))
118
+ # TODO:
119
+ # 1. add a test
120
+ # 2. possibly extract into a dedicated function for easier logic flow here
121
+ # and a dedicated unittest
122
+ if not reuse_conversion_table and \
123
+ op .exists (target_heuristic_filename ) and \
124
+ file_md5sum (target_heuristic_filename ) != file_md5sum (heuristic .filename ):
125
+ reuse_conversion_table = False
126
+ lgr .info (
127
+ "Will not reuse existing conversion table files because heuristic "
128
+ "has changed"
129
+ )
130
+
109
131
# MG - maybe add an option to force rerun?
110
132
# related issue : https://github.com/nipy/heudiconv/issues/84
111
- if op . exists ( edit_file ) and overwrite :
133
+ if reuse_conversion_table :
112
134
lgr .info ("Reloading existing filegroup.json "
113
135
"because %s exists" , edit_file )
114
136
info = read_config (edit_file )
@@ -122,6 +144,8 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
122
144
else :
123
145
# TODO -- might have been done outside already!
124
146
# MG -- will have to try with both dicom template, files
147
+ assure_no_file_exists (target_heuristic_filename )
148
+ safe_copyfile (heuristic .filename , idir )
125
149
if dicoms :
126
150
seqinfo = group_dicoms_into_seqinfos (
127
151
dicoms ,
@@ -131,6 +155,8 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
131
155
seqinfo_list = list (seqinfo .keys ())
132
156
filegroup = {si .series_id : x for si , x in seqinfo .items ()}
133
157
dicominfo_file = op .join (idir , 'dicominfo%s.tsv' % ses_suffix )
158
+ # allow to overwrite even if was present under git-annex already
159
+ assure_no_file_exists (dicominfo_file )
134
160
with open (dicominfo_file , 'wt' ) as fp :
135
161
fp .write ('\t ' .join ([val for val in seqinfo_fields ]) + '\n ' )
136
162
for seq in seqinfo_list :
@@ -139,7 +165,9 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
139
165
info = heuristic .infotodict (seqinfo_list )
140
166
lgr .debug ("Writing to {}, {}, {}" .format (info_file , edit_file ,
141
167
filegroup_file ))
168
+ assure_no_file_exists (info_file )
142
169
write_config (info_file , info )
170
+ assure_no_file_exists (edit_file )
143
171
write_config (edit_file , info )
144
172
save_json (filegroup_file , filegroup )
145
173
@@ -150,7 +178,7 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
150
178
else :
151
179
tdir = op .join (anon_outdir , anon_sid )
152
180
153
- if converter != 'none' :
181
+ if converter . lower () != 'none' :
154
182
lgr .info ("Doing conversion using %s" , converter )
155
183
cinfo = conversion_info (anon_sid , tdir , info , filegroup , ses )
156
184
convert (cinfo ,
@@ -218,8 +246,8 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
218
246
os .makedirs (prefix_dirname )
219
247
220
248
for outtype in outtypes :
221
- lgr .debug ("Processing %d dicoms for output type %s" ,
222
- len (item_dicoms ), outtype )
249
+ lgr .debug ("Processing %d dicoms for output type %s. Overwrite=%s " ,
250
+ len (item_dicoms ), outtype , overwrite )
223
251
lgr .debug ("Includes the following dicoms: %s" , item_dicoms )
224
252
225
253
seqtype = op .basename (op .dirname (prefix )) if bids else None
@@ -243,7 +271,8 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
243
271
244
272
bids_outfiles = save_converted_files (res , item_dicoms , bids ,
245
273
outtype , prefix ,
246
- outname_bids )
274
+ outname_bids ,
275
+ overwrite = overwrite )
247
276
248
277
# save acquisition time information if it's BIDS
249
278
# at this point we still have acquisition date
@@ -257,15 +286,23 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
257
286
safe_copyfile (op .join (convertnode .base_dir ,
258
287
convertnode .name ,
259
288
'provenance.ttl' ),
260
- prov_file )
289
+ prov_file ,
290
+ overwrite = overwrite )
261
291
prov_files .append (prov_file )
262
292
263
293
tempdirs .rmtree (tmpdir )
294
+ else :
295
+ raise RuntimeError (
296
+ "was asked to convert into %s but destination already exists"
297
+ % (outname )
298
+ )
264
299
265
300
if len (bids_outfiles ) > 1 :
266
301
lgr .warning ("For now not embedding BIDS and info generated "
267
302
".nii.gz itself since sequence produced "
268
303
"multiple files" )
304
+ elif not bids_outfiles :
305
+ lgr .debug ("No BIDS files were produced, nothing to embed to then" )
269
306
else :
270
307
embed_metadata_from_dicoms (bids , item_dicoms , outname , outname_bids ,
271
308
prov_file , scaninfo , tempdirs , with_prov ,
@@ -350,7 +387,7 @@ def nipype_convert(item_dicoms, prefix, with_prov, bids, tmpdir):
350
387
return convertnode .run ()
351
388
352
389
353
- def save_converted_files (res , item_dicoms , bids , outtype , prefix , outname_bids ):
390
+ def save_converted_files (res , item_dicoms , bids , outtype , prefix , outname_bids , overwrite ):
354
391
"""Copy converted files from tempdir to output directory.
355
392
Will rename files if necessary.
356
393
@@ -381,8 +418,8 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids):
381
418
382
419
if isdefined (res .outputs .bvecs ) and isdefined (res .outputs .bvals ):
383
420
outname_bvecs , outname_bvals = prefix + '.bvec' , prefix + '.bval'
384
- safe_copyfile (res .outputs .bvecs , outname_bvecs )
385
- safe_copyfile (res .outputs .bvals , outname_bvals )
421
+ safe_copyfile (res .outputs .bvecs , outname_bvecs , overwrite )
422
+ safe_copyfile (res .outputs .bvals , outname_bvals , overwrite )
386
423
387
424
if isinstance (res_files , list ):
388
425
# we should provide specific handling for fmap,
@@ -406,18 +443,18 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids):
406
443
407
444
for fl , suffix , bids_file in zip (res_files , suffixes , bids_files ):
408
445
outname = "%s%s.%s" % (prefix , suffix , outtype )
409
- safe_copyfile (fl , outname )
446
+ safe_copyfile (fl , outname , overwrite )
410
447
if bids_file :
411
448
outname_bids_file = "%s%s.json" % (prefix , suffix )
412
- safe_copyfile (bids_file , outname_bids_file )
449
+ safe_copyfile (bids_file , outname_bids_file , overwrite )
413
450
bids_outfiles .append (outname_bids_file )
414
451
# res_files is not a list
415
452
else :
416
453
outname = "{}.{}" .format (prefix , outtype )
417
- safe_copyfile (res_files , outname )
454
+ safe_copyfile (res_files , outname , overwrite )
418
455
if isdefined (res .outputs .bids ):
419
456
try :
420
- safe_copyfile (res .outputs .bids , outname_bids )
457
+ safe_copyfile (res .outputs .bids , outname_bids , overwrite )
421
458
bids_outfiles .append (outname_bids )
422
459
except TypeError as exc : ##catch lists
423
460
raise TypeError ("Multiple BIDS sidecars detected." )
0 commit comments