Skip to content

Commit 5c5b5d1

Browse files
committed
fix: better support for queue args
1 parent b2b1d2b commit 5c5b5d1

File tree

3 files changed

+82
-53
lines changed

3 files changed

+82
-53
lines changed

heudiconv/cli/run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ def get_parser():
221221
default=None,
222222
help='batch system to submit jobs in parallel')
223223
submission.add_argument('--queue-args', dest='queue_args', default=None,
224-
help='Additional queue arguments')
224+
help='Additional queue arguments passed as '
225+
'single string of Argument=Value pairs space '
226+
'separated.')
225227
return parser
226228

227229

heudiconv/queue.py

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,66 @@
77
lgr = logging.getLogger(__name__)
88

99
def queue_conversion(pyscript, queue, studyid, queue_args=None):
10-
"""
11-
Write out conversion arguments to file and submit to a job scheduler.
12-
Parses `sys.argv` for heudiconv arguments.
13-
14-
Parameters
15-
----------
16-
pyscript: file
17-
path to `heudiconv` script
18-
queue: string
19-
batch scheduler to use
20-
studyid: string
21-
identifier for conversion
22-
queue_args: string (optional)
23-
additional queue arguments for job submission
24-
25-
Returns
26-
-------
27-
proc: int
28-
Queue submission exit code
29-
"""
30-
31-
SUPPORTED_QUEUES = {'SLURM': 'sbatch'}
32-
if queue not in SUPPORTED_QUEUES:
33-
raise NotImplementedError("Queuing with %s is not supported", queue)
34-
35-
args = sys.argv[1:]
36-
# search args for queue flag
37-
for i, arg in enumerate(args):
38-
if arg in ["-q", "--queue"]:
39-
break
40-
if i == len(args) - 1:
41-
raise RuntimeError(
42-
"Queue flag not found (must be provided as a command-line arg)"
43-
)
44-
# remove queue flag and value
45-
del args[i:i+2]
46-
47-
# make arguments executable again
48-
args.insert(0, pyscript)
49-
pypath = sys.executable or "python"
50-
args.insert(0, pypath)
51-
convertcmd = " ".join(args)
52-
53-
# will overwrite across subjects
54-
queue_file = os.path.abspath('heudiconv-%s.sh' % queue)
55-
with open(queue_file, 'wt') as fp:
56-
fp.writelines(['#!/bin/bash\n', convertcmd, '\n'])
57-
58-
cmd = [SUPPORTED_QUEUES[queue], queue_file]
10+
"""
11+
Write out conversion arguments to file and submit to a job scheduler.
12+
Parses `sys.argv` for heudiconv arguments.
13+
14+
Parameters
15+
----------
16+
pyscript: file
17+
path to `heudiconv` script
18+
queue: string
19+
batch scheduler to use
20+
studyid: string
21+
identifier for conversion
22+
queue_args: string (optional)
23+
additional queue arguments for job submission
24+
25+
Returns
26+
-------
27+
proc: int
28+
Queue submission exit code
29+
"""
30+
31+
SUPPORTED_QUEUES = {'SLURM': 'sbatch'}
32+
if queue not in SUPPORTED_QUEUES:
33+
raise NotImplementedError("Queuing with %s is not supported", queue)
34+
35+
args = clean_args(sys.argv[1:])
36+
# make arguments executable
37+
args.insert(0, pyscript)
38+
pypath = sys.executable or "python"
39+
args.insert(0, pypath)
40+
convertcmd = " ".join(args)
41+
42+
# will overwrite across subjects
43+
queue_file = os.path.abspath('heudiconv-%s.sh' % queue)
44+
with open(queue_file, 'wt') as fp:
45+
fp.write("#!/bin/bash\n")
5946
if queue_args:
60-
cmd.insert(1, queue_args)
61-
proc = subprocess.call(cmd)
62-
return proc
47+
for qarg in queue_args.split():
48+
fp.write("#SBATCH %s\n" % qarg)
49+
fp.write(convertcmd + "\n")
50+
51+
cmd = [SUPPORTED_QUEUES[queue], queue_file]
52+
proc = subprocess.call(cmd)
53+
return proc
54+
55+
def clean_args(hargs, keys=['-q', '--queue', '--queue-args']):
56+
"""
57+
Filters out unwanted arguments
58+
59+
:param hargs: Arguments passed
60+
:type hargs: Iterable
61+
:param keys: Unwanted arguments
62+
:type keys: Iterable
63+
:return: Filtered arguments
64+
"""
65+
indicies = []
66+
for i, arg in enumerate(hargs):
67+
if arg in keys:
68+
indicies.extend([i, i+1])
69+
for j in sorted(indicies, reverse=True):
70+
del hargs[j]
71+
return hargs
72+

heudiconv/tests/test_queue.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44

55
from heudiconv.cli.run import main as runner
6+
from heudiconv.queue import clean_args
67
from .utils import TESTS_DATA_PATH
78
import pytest
89
from nipype.utils.filemanip import which
@@ -44,3 +45,19 @@ def test_queue_no_slurm(tmpdir, invocation):
4445
finally:
4546
# revert before breaking something
4647
sys.argv = _sys_args
48+
49+
def test_argument_filtering(tmpdir):
50+
cmdargs = [
51+
'heudiconv',
52+
'--files',
53+
'/fake/path/to/files',
54+
'-f',
55+
'convertall',
56+
'-q',
57+
'SLURM',
58+
'--queue-args',
59+
'--cpus-per-task=4 --contiguous --time=10'
60+
]
61+
filtered = cmdargs[:-4]
62+
63+
assert(clean_args(cmdargs) == filtered)

0 commit comments

Comments
 (0)