|
7 | 7 |
|
8 | 8 | lgr = logging.getLogger(__name__)
|
9 | 9 |
|
10 |
| -def queue_conversion(queue, studyid, queue_args=None): |
| 10 | +def queue_conversion(queue, iterarg, iterables, queue_args=None): |
11 | 11 | """
|
12 | 12 | Write out conversion arguments to file and submit to a job scheduler.
|
13 | 13 | Parses `sys.argv` for heudiconv arguments.
|
14 | 14 |
|
15 | 15 | Parameters
|
16 | 16 | ----------
|
17 | 17 | queue: string
|
18 |
| - batch scheduler to use |
19 |
| - studyid: string |
20 |
| - identifier for conversion |
| 18 | + Batch scheduler to use |
| 19 | + iterarg: str |
| 20 | + Multi-argument to index (`subjects` OR `files`) |
| 21 | + iterables: int |
| 22 | + Number of `iterarg` arguments |
21 | 23 | queue_args: string (optional)
|
22 |
| - additional queue arguments for job submission |
| 24 | + Additional queue arguments for job submission |
23 | 25 |
|
24 |
| - Returns |
25 |
| - ------- |
26 |
| - proc: int |
27 |
| - Queue submission exit code |
28 | 26 | """
|
29 | 27 |
|
30 | 28 | SUPPORTED_QUEUES = {'SLURM': 'sbatch'}
|
31 | 29 | if queue not in SUPPORTED_QUEUES:
|
32 | 30 | raise NotImplementedError("Queuing with %s is not supported", queue)
|
33 | 31 |
|
34 |
| - args = clean_args(sys.argv[1:]) |
35 |
| - # make arguments executable |
36 |
| - heudiconv_exec = which("heudiconv") or "heudiconv" |
37 |
| - args.insert(0, heudiconv_exec) |
38 |
| - convertcmd = " ".join(args) |
39 |
| - |
40 |
| - # will overwrite across subjects |
41 |
| - queue_file = os.path.abspath('heudiconv-%s.sh' % queue) |
42 |
| - with open(queue_file, 'wt') as fp: |
43 |
| - fp.write("#!/bin/bash\n") |
44 |
| - if queue_args: |
45 |
| - for qarg in queue_args.split(): |
46 |
| - fp.write("#SBATCH %s\n" % qarg) |
47 |
| - fp.write(convertcmd + "\n") |
48 |
| - |
49 |
| - cmd = [SUPPORTED_QUEUES[queue], queue_file] |
50 |
| - proc = subprocess.call(cmd) |
51 |
| - return proc |
52 |
| - |
53 |
| -def clean_args(hargs, keys=['-q', '--queue', '--queue-args']): |
| 32 | + for i in range(iterables): |
| 33 | + args = clean_args(sys.argv[1:], iterarg, i) |
| 34 | + # make arguments executable |
| 35 | + heudiconv_exec = which("heudiconv") or "heudiconv" |
| 36 | + args.insert(0, heudiconv_exec) |
| 37 | + convertcmd = " ".join(args) |
| 38 | + |
| 39 | + print(convertcmd) |
| 40 | + # will overwrite across subjects |
| 41 | + queue_file = os.path.abspath('heudiconv-%s.sh' % queue) |
| 42 | + with open(queue_file, 'wt') as fp: |
| 43 | + fp.write("#!/bin/bash\n") |
| 44 | + if queue_args: |
| 45 | + for qarg in queue_args.split(): |
| 46 | + fp.write("#SBATCH %s\n" % qarg) |
| 47 | + fp.write(convertcmd + "\n") |
| 48 | + |
| 49 | + cmd = [SUPPORTED_QUEUES[queue], queue_file] |
| 50 | + proc = subprocess.call(cmd) |
| 51 | + lgr.info("Submitted %d jobs", iterables) |
| 52 | + |
| 53 | +def clean_args(hargs, iterarg, iteridx): |
54 | 54 | """
|
55 |
| - Filters out unwanted arguments |
| 55 | + Filters arguments for batch submission. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + hargs: list |
| 60 | + Command-line arguments |
| 61 | + iterarg: str |
| 62 | + Multi-argument to index (`subjects` OR `files`) |
| 63 | + iteridx: int |
| 64 | + `iterarg` index to submit |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + cmdargs : list |
| 69 | + Filtered arguments for batch submission |
56 | 70 |
|
57 |
| - :param hargs: Arguments passed |
58 |
| - :type hargs: Iterable |
59 |
| - :param keys: Unwanted arguments |
60 |
| - :type keys: Iterable |
61 |
| - :return: Filtered arguments |
| 71 | + Example |
| 72 | + -------- |
| 73 | + >>> from heudiconv.queue import clean_args |
| 74 | + >>> cmd = ['heudiconv', '-d', '/some/{subject}/path', |
| 75 | + ... '-q', 'SLURM', |
| 76 | + ... '-s', 'sub-1', 'sub-2', 'sub-3', 'sub-4'] |
| 77 | + >>> clean_args(cmd, 'subjects', 0) |
| 78 | + ['heudiconv', '-d', '/some/{subject}/path', '-s', 'sub-1'] |
62 | 79 | """
|
| 80 | + |
| 81 | + if iterarg == "subjects": |
| 82 | + iterarg = ['-s', '--subjects'] |
| 83 | + elif iterarg == "files": |
| 84 | + iterarg = ['--files'] |
| 85 | + else: |
| 86 | + raise ValueError("Cannot index %s" % iterarg) |
| 87 | + |
| 88 | + # remove these or cause an infinite loop |
| 89 | + queue_args = ['-q', '--queue', '--queue-args'] |
| 90 | + |
| 91 | + # control variables for multi-argument parsing |
| 92 | + is_iterarg = False |
| 93 | + itercount = 0 |
| 94 | + |
63 | 95 | indicies = []
|
| 96 | + cmdargs = hargs[:] |
| 97 | + |
64 | 98 | for i, arg in enumerate(hargs):
|
65 |
| - if arg in keys: |
| 99 | + if arg.startswith('-') and is_iterarg: |
| 100 | + # moving on to another argument |
| 101 | + is_iterarg = False |
| 102 | + if is_iterarg: |
| 103 | + if iteridx != itercount: |
| 104 | + indicies.append(i) |
| 105 | + itercount += 1 |
| 106 | + if arg in iterarg: |
| 107 | + is_iterarg = True |
| 108 | + if arg in queue_args: |
66 | 109 | indicies.extend([i, i+1])
|
67 |
| - for j in sorted(indicies, reverse=True): |
68 |
| - del hargs[j] |
69 |
| - return hargs |
70 | 110 |
|
| 111 | + for j in sorted(indicies, reverse=True): |
| 112 | + del cmdargs[j] |
| 113 | + return cmdargs |
0 commit comments