|
7 | 7 | lgr = logging.getLogger(__name__)
|
8 | 8 |
|
9 | 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] |
| 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") |
59 | 46 | 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 | + |
0 commit comments