|
| 1 | +import subprocess |
| 2 | +import sys |
1 | 3 | import os
|
2 |
| -import os.path as op |
3 | 4 |
|
4 | 5 | import logging
|
5 | 6 |
|
6 | 7 | lgr = logging.getLogger(__name__)
|
7 | 8 |
|
8 |
| -# start with SLURM but extend past that #TODO |
9 |
| -def queue_conversion(progname, queue, outdir, heuristic, dicoms, sid, |
10 |
| - anon_cmd, converter, session,with_prov, bids): |
11 |
| - |
12 |
| - # Rework this... |
13 |
| - convertcmd = ' '.join(['python', progname, |
14 |
| - '-o', outdir, |
15 |
| - '-f', heuristic, |
16 |
| - '-s', sid, |
17 |
| - '--anon-cmd', anon_cmd, |
18 |
| - '-c', converter]) |
19 |
| - if session: |
20 |
| - convertcmd += " --ses '%s'" % session |
21 |
| - if with_prov: |
22 |
| - convertcmd += " --with-prov" |
23 |
| - if bids: |
24 |
| - convertcmd += " --bids" |
25 |
| - if dicoms: |
26 |
| - convertcmd += " --files" |
27 |
| - convertcmd += [" '%s'" % f for f in dicoms] |
28 |
| - |
29 |
| - script_file = 'dicom-%s.sh' % sid |
30 |
| - with open(script_file, 'wt') as fp: |
31 |
| - fp.writelines(['#!/bin/bash\n', convertcmd]) |
32 |
| - outcmd = 'sbatch -J dicom-%s -p %s -N1 -c2 --mem=20G %s' \ |
33 |
| - % (sid, queue, script_file) |
34 |
| - |
35 |
| - os.system(outcmd) |
| 9 | +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] |
| 59 | + if queue_args: |
| 60 | + cmd.insert(1, queue_args) |
| 61 | + proc = subprocess.call(cmd) |
| 62 | + return proc |
0 commit comments