Skip to content

Commit 66bc53f

Browse files
committed
fix: revert to explicit relative imports, use heudiconv executable for queue submissions
1 parent 5c5b5d1 commit 66bc53f

File tree

10 files changed

+39
-50
lines changed

10 files changed

+39
-50
lines changed

heudiconv/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# set logger handler
22
import logging
33
import os
4-
from heudiconv.info import (__version__, __packagename__)
4+
from .info import (__version__, __packagename__)
55

66
# Rudimentary logging support.
77
lgr = logging.getLogger(__name__)

heudiconv/bids.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from random import sample
1212
from glob import glob
1313

14-
from heudiconv.external.pydicom import dcm
14+
from .external.pydicom import dcm
1515

16-
from heudiconv.parser import find_files
17-
from heudiconv.utils import (
16+
from .parser import find_files
17+
from .utils import (
1818
load_json,
1919
save_json,
2020
create_file_if_missing,

heudiconv/cli/run.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from argparse import ArgumentParser
66
import sys
77

8-
from heudiconv import __version__, __packagename__
9-
from heudiconv.parser import get_study_sessions
10-
from heudiconv.utils import load_heuristic, anonymize_sid, treat_infofile, SeqInfo
11-
from heudiconv.convert import prep_conversion
12-
from heudiconv.bids import populate_bids_templates, tuneup_bids_json_files
13-
from heudiconv.queue import queue_conversion
8+
from .. import __version__, __packagename__
9+
from ..parser import get_study_sessions
10+
from ..utils import load_heuristic, anonymize_sid, treat_infofile, SeqInfo
11+
from ..convert import prep_conversion
12+
from ..bids import populate_bids_templates, tuneup_bids_json_files
13+
from ..queue import queue_conversion
1414

1515
import inspect
1616
import logging
@@ -84,11 +84,11 @@ def process_extra_commands(outdir, args):
8484
elif args.command == 'sanitize-jsons':
8585
tuneup_bids_json_files(args.files)
8686
elif args.command == 'heuristics':
87-
from heudiconv.utils import get_known_heuristics_with_descriptions
87+
from .utils import get_known_heuristics_with_descriptions
8888
for name_desc in get_known_heuristics_with_descriptions().items():
8989
print("- %s: %s" % name_desc)
9090
elif args.command == 'heuristic-info':
91-
from heudiconv.utils import get_heuristic_description, get_known_heuristic_names
91+
from .utils import get_heuristic_description, get_known_heuristic_names
9292
if not args.heuristic:
9393
raise ValueError("Specify heuristic using -f. Known are: %s"
9494
% ', '.join(get_known_heuristic_names()))
@@ -284,15 +284,6 @@ def process_args(args):
284284
continue
285285

286286
if args.queue:
287-
# if seqinfo and not dicoms:
288-
# # flatten them all and provide into batching, which again
289-
# # would group them... heh
290-
# dicoms = sum(seqinfo.values(), [])
291-
# raise NotImplementedError(
292-
# "we already grouped them so need to add a switch to avoid "
293-
# "any grouping, so no outdir prefix doubled etc")
294-
295-
pyscript = op.abspath(inspect.getfile(inspect.currentframe()))
296287

297288
studyid = sid
298289
if session:
@@ -302,8 +293,7 @@ def process_args(args):
302293
# remove any separators
303294
studyid = studyid.replace(op.sep, '_')
304295

305-
queue_conversion(pyscript,
306-
args.queue,
296+
queue_conversion(args.queue,
307297
studyid,
308298
args.queue_args)
309299
continue

heudiconv/convert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shutil
55
import sys
66

7-
from heudiconv.utils import (
7+
from .utils import (
88
read_config,
99
load_json,
1010
save_json,
@@ -18,15 +18,15 @@
1818
assure_no_file_exists,
1919
file_md5sum
2020
)
21-
from heudiconv.bids import (
21+
from .bids import (
2222
convert_sid_bids,
2323
populate_bids_templates,
2424
save_scans_key,
2525
tuneup_bids_json_files,
2626
add_participant_record,
2727
BIDSError
2828
)
29-
from heudiconv.dicoms import (
29+
from .dicoms import (
3030
group_dicoms_into_seqinfos,
3131
embed_metadata_from_dicoms,
3232
compress_dicoms

heudiconv/dicoms.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from collections import OrderedDict
66
import tarfile
77

8-
from heudiconv.external.pydicom import dcm
9-
from heudiconv.utils import SeqInfo, load_json, set_readonly
8+
from .external.pydicom import dcm
9+
from .utils import SeqInfo, load_json, set_readonly
1010

1111
lgr = logging.getLogger(__name__)
1212

@@ -55,10 +55,10 @@ def group_dicoms_into_seqinfos(files, file_filter, dcmfilter, grouping):
5555
lgr.info('Filtering out {0} dicoms based on their filename'.format(
5656
nfl_before-nfl_after))
5757
for fidx, filename in enumerate(files):
58-
from heudiconv.external.dcmstack import ds
58+
import nibabel.nicom.dicomwrappers as dw
5959
# TODO after getting a regression test check if the same behavior
6060
# with stop_before_pixels=True
61-
mw = ds.wrapper_from_data(dcm.read_file(filename, force=True))
61+
mw = dw.wrapper_from_data(dcm.read_file(filename, force=True))
6262

6363
for sig in ('iop', 'ICE_Dims', 'SequenceName'):
6464
try:
@@ -385,7 +385,7 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
385385
import re
386386

387387
if not min_meta:
388-
import dcmstack as ds
388+
from .external.dcmstack import ds
389389
stack = ds.parse_and_stack(dcmfiles, force=True).values()
390390
if len(stack) > 1:
391391
raise ValueError('Found multiple series')

heudiconv/external/dcmstack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from __future__ import absolute_import
44

5-
from heudiconv.external.pydicom import dcm # to assure that we have it one way or another
5+
from .pydicom import dcm # to assure that we have it one way or another
66

77
try:
88
import dcmstack as ds
99
except ImportError as e:
10-
from heudiconv import lgr
10+
from .. import lgr
1111
# looks different between py2 and 3 so we go for very rudimentary matching
1212
e_str = str(e)
1313
# there were changes from how

heudiconv/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import tarfile
1010
from tempfile import mkdtemp
1111

12-
from heudiconv.dicoms import group_dicoms_into_seqinfos
13-
from heudiconv.utils import (
12+
from .dicoms import group_dicoms_into_seqinfos
13+
from .utils import (
1414
docstring_parameter,
1515
StudySessionInfo,
1616
)

heudiconv/queue.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import subprocess
22
import sys
33
import os
4-
54
import logging
65

6+
from .utils import which
7+
78
lgr = logging.getLogger(__name__)
89

9-
def queue_conversion(pyscript, queue, studyid, queue_args=None):
10+
def queue_conversion(queue, studyid, queue_args=None):
1011
"""
1112
Write out conversion arguments to file and submit to a job scheduler.
1213
Parses `sys.argv` for heudiconv arguments.
1314
1415
Parameters
1516
----------
16-
pyscript: file
17-
path to `heudiconv` script
1817
queue: string
1918
batch scheduler to use
2019
studyid: string
@@ -34,9 +33,8 @@ def queue_conversion(pyscript, queue, studyid, queue_args=None):
3433

3534
args = clean_args(sys.argv[1:])
3635
# make arguments executable
37-
args.insert(0, pyscript)
38-
pypath = sys.executable or "python"
39-
args.insert(0, pypath)
36+
heudiconv_exec = which("heudiconv") or "heudiconv"
37+
args.insert(0, heudiconv_exec)
4038
convertcmd = " ".join(args)
4139

4240
# will overwrite across subjects

heudiconv/tests/test_queue.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import subprocess
44

55
from heudiconv.cli.run import main as runner
6-
from heudiconv.queue import clean_args
6+
from heudiconv.queue import clean_args, which
77
from .utils import TESTS_DATA_PATH
88
import pytest
9-
from nipype.utils.filemanip import which
109

1110
@pytest.mark.skipif(which("sbatch"), reason="skip a real slurm call")
1211
@pytest.mark.parametrize(

heudiconv/utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from pathlib import Path
1313
from collections import namedtuple
1414
from glob import glob
15+
from subprocess import check_output
16+
17+
from nipype.utils.filemanip import which
1518

1619
import logging
1720
lgr = logging.getLogger(__name__)
@@ -103,18 +106,17 @@ def dec(obj):
103106

104107

105108
def anonymize_sid(sid, anon_sid_cmd):
106-
import sys
107-
from subprocess import check_output
108-
109+
109110
cmd = [anon_sid_cmd, sid]
110111
shell_return = check_output(cmd)
111112

112-
### Handle subprocess returning a bytes literal string to a python3 interpreter
113-
if all([sys.version_info[0] > 2, isinstance(shell_return, bytes), isinstance(sid, str)]):
113+
if all([sys.version_info[0] > 2,
114+
isinstance(shell_return, bytes),
115+
isinstance(sid, str)]):
114116
anon_sid = shell_return.decode()
115117
else:
116118
anon_sid = shell_return
117-
119+
118120
return anon_sid.strip()
119121

120122

0 commit comments

Comments
 (0)