Skip to content

Commit 1fb2e17

Browse files
authored
Merge pull request #113 from mgxd/fix/sid
BF: bidify sid and remove lingering dicom tempdirs
2 parents 0046aa6 + a1ecb20 commit 1fb2e17

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

heudiconv/cli/run.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def process_args(args):
255255
args.bids)
256256
continue
257257

258-
anon_sid = anonymize_sid(sid, args.anon_cmd) if args.anon_cmd else sid
258+
anon_sid = anonymize_sid(sid, args.anon_cmd) if args.anon_cmd else None
259259
if args.anon_cmd:
260260
lgr.info('Anonymized {} to {}'.format(sid, anon_sid))
261261

@@ -267,7 +267,8 @@ def process_args(args):
267267
# the outdir -> study_outdir datasets if not yet there
268268
if args.datalad:
269269
from ..external.dlad import prepare_datalad
270-
dl_msg = prepare_datalad(anon_study_outdir, anon_outdir, anon_sid,
270+
dlad_sid = sid if not anon_sid else anon_sid
271+
dl_msg = prepare_datalad(anon_study_outdir, anon_outdir, dlad_sid,
271272
args.session, seqinfo, dicoms, args.bids)
272273

273274
lgr.info("PROCESSING STARTS: {0}".format(

heudiconv/convert.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import logging
44
import shutil
55

6-
from .utils import (read_config, load_json, save_json, write_config,
7-
TempDirs, safe_copyfile, treat_infofile, set_readonly)
6+
from .utils import (read_config, load_json, save_json, write_config, TempDirs,
7+
safe_copyfile, treat_infofile, set_readonly,
8+
clear_temp_dicoms)
89
from .bids import (convert_sid_bids, populate_bids_templates, save_scans_key,
910
tuneup_bids_json_files, add_participant_record)
1011
from .dicoms import (group_dicoms_into_seqinfos, embed_metadata_from_dicoms,
@@ -143,6 +144,10 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
143144
outdir=tdir,
144145
min_meta=min_meta,
145146
overwrite=overwrite,)
147+
148+
for item_dicoms in filegroup.values():
149+
clear_temp_dicoms(item_dicoms)
150+
146151
if bids:
147152
if seqinfo:
148153
keys = list(seqinfo)
@@ -256,14 +261,6 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
256261
if op.exists(outname):
257262
set_readonly(outname)
258263

259-
if items:
260-
common = op.commonprefix(item_dicoms)
261-
tmp = '/'.join(common.split(op.sep)[:3])
262-
if (op.dirname(tmp) == '/tmp'
263-
and op.basename(tmp).startswith('heudiconvDCM')):
264-
# clean up directory holding dicoms
265-
shutil.rmtree(tmp)
266-
267264
if custom_callable is not None:
268265
custom_callable(*item)
269266

heudiconv/info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'nibabel',
1212
'pydicom',
1313
'nipype',
14+
'pathlib',
1415
]
1516

1617
TESTS_REQUIRES = [

heudiconv/utils.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
"""Utility objects and functions"""
22

33
import os
4-
import os.path as op
5-
from tempfile import mkdtemp
6-
from glob import glob
4+
import tempfile
75
import json
86
import re
97
import sys
108
import shutil
11-
from collections import namedtuple
129
import copy
1310
import logging
1411
import stat
12+
import os.path as op
13+
from pathlib import Path
14+
from collections import namedtuple
15+
from glob import glob
1516

1617
SeqInfo = namedtuple(
1718
'SeqInfo',
@@ -64,7 +65,7 @@ def __init__(self):
6465
self.lgr = logging.getLogger('tempdirs')
6566

6667
def __call__(self, prefix=None):
67-
tmpdir = mkdtemp(prefix=prefix)
68+
tmpdir = tempfile.mkdtemp(prefix=prefix)
6869
self.dirs.append(tmpdir)
6970
return tmpdir
7071

@@ -319,3 +320,16 @@ def is_readonly(path):
319320
perms = stat.S_IMODE(os.lstat(os.path.realpath(path)).st_mode)
320321
# should be true if anyone is allowed to write
321322
return not bool(perms & ALL_CAN_WRITE)
323+
324+
325+
def clear_temp_dicoms(item_dicoms):
326+
"""Ensures DICOM temporary directories are safely cleared"""
327+
try:
328+
tmp = Path(op.commonprefix(item_dicoms)).parents[1]
329+
except IndexError:
330+
return
331+
if (str(tmp.parent) == tempfile.gettempdir()
332+
and str(tmp.stem).startswith('heudiconvDCM')
333+
and op.exists(str(tmp))):
334+
# clean up directory holding dicoms
335+
shutil.rmtree(str(tmp))

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.[full]
1+
.[all]
22
# This is a dcmstack branch with changes for Python 3
33
# sent PR to main repo, TODO: check if merged
44
# https://github.com/ghisvail/dcmstack/pull/1

0 commit comments

Comments
 (0)