1
1
"""Handle BIDS specific operations"""
2
2
3
+ import hashlib
3
4
import os
4
5
import os .path as op
5
6
import logging
@@ -196,7 +197,7 @@ def save_scans_key(item, bids_files):
196
197
"""
197
198
Parameters
198
199
----------
199
- items :
200
+ item :
200
201
bids_files: str or list
201
202
202
203
Returns
@@ -214,7 +215,7 @@ def save_scans_key(item, bids_files):
214
215
# get filenames
215
216
f_name = '/' .join (bids_file .split ('/' )[- 2 :])
216
217
f_name = f_name .replace ('json' , 'nii.gz' )
217
- rows [f_name ] = get_formatted_scans_key_row (item )
218
+ rows [f_name ] = get_formatted_scans_key_row (item [ - 1 ][ 0 ] )
218
219
subj_ , ses_ = find_subj_ses (f_name )
219
220
if not subj_ :
220
221
lgr .warning (
@@ -279,7 +280,7 @@ def add_rows_to_scans_keys_file(fn, newrows):
279
280
writer .writerows ([header ] + data_rows_sorted )
280
281
281
282
282
- def get_formatted_scans_key_row (item ):
283
+ def get_formatted_scans_key_row (dcm_fn ):
283
284
"""
284
285
Parameters
285
286
----------
@@ -291,25 +292,27 @@ def get_formatted_scans_key_row(item):
291
292
[ISO acquisition time, performing physician name, random string]
292
293
293
294
"""
294
- dcm_fn = item [- 1 ][0 ]
295
- from heudiconv .external .dcmstack import ds
296
- mw = ds .wrapper_from_data (dcm .read_file (dcm_fn ,
297
- stop_before_pixels = True ,
298
- force = True ))
295
+ dcm_data = dcm .read_file (dcm_fn , stop_before_pixels = True , force = True )
299
296
# we need to store filenames and acquisition times
300
297
# parse date and time and get it into isoformat
301
298
try :
302
- date = mw . dcm_data .ContentDate
303
- time = mw . dcm_data .ContentTime .split ('.' )[0 ]
299
+ date = dcm_data .ContentDate
300
+ time = dcm_data .ContentTime .split ('.' )[0 ]
304
301
td = time + date
305
302
acq_time = datetime .strptime (td , '%H%M%S%Y%m%d' ).isoformat ()
306
303
except AttributeError as exc :
307
304
lgr .warning ("Failed to get date/time for the content: %s" , str (exc ))
308
305
acq_time = None
309
306
# add random string
310
- randstr = '' .join (map (chr , sample (k = 8 , population = range (33 , 127 ))))
307
+ # But let's make it reproducible by using all UIDs
308
+ # (might change across versions?)
309
+ randcontent = u'' .join (
310
+ [getattr (dcm_data , f ) or '' for f in sorted (dir (dcm_data ))
311
+ if f .endswith ('UID' )]
312
+ )
313
+ randstr = hashlib .md5 (randcontent .encode ()).hexdigest ()[:8 ]
311
314
try :
312
- perfphys = mw . dcm_data .PerformingPhysicianName
315
+ perfphys = dcm_data .PerformingPhysicianName
313
316
except AttributeError :
314
317
perfphys = ''
315
318
row = [acq_time , perfphys , randstr ]
0 commit comments