fslsub.py 14.5 KB
Newer Older
Paul McCarthy's avatar
Paul McCarthy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
#
# fslsub.py - Functions for using fsl_sub.
#
# Author: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
#
"""This module submits jobs to a computing cluster using FSL's fsl_sub command
line tool. It is assumed that the computing cluster is managed by SGE.

Example usage, building a short pipeline::

    from fsl.utils.fslsub import submit, wait

    # submits bet to veryshort queue unless <mask_filename> already exists
    bet_job = submit('bet <input_filename> -m',
                     queue='veryshort.q',
                     output='<mask_filename>')

    # submits another job
    other_job = submit('some other pre-processing step', queue='short.q')

    # submits cuda job, that should only start after both preparatory jobs are
23
24
    # finished. This will work if bet_job and other_job are single job-ids
    # (i.e., strings) or a sequence of multiple job-ids
Paul McCarthy's avatar
Paul McCarthy committed
25
    cuda_job = submit('expensive job',
26
                      wait_for=(bet_job, other_job),
Paul McCarthy's avatar
Paul McCarthy committed
27
28
29
30
31
32
33
34
35
36
                      queue='cuda.q')

    # waits for the cuda job to finish
    wait(cuda_job)

.. autosummary::
   :nosignatures:

   submit
   info
37
   output
Paul McCarthy's avatar
Paul McCarthy committed
38
   wait
39
   func_to_cmd
Paul McCarthy's avatar
Paul McCarthy committed
40
41
42
"""


Paul McCarthy's avatar
Paul McCarthy committed
43
from six import BytesIO
44
import subprocess as sp
45
import os.path as op
46
import os
47
import glob
Paul McCarthy's avatar
Paul McCarthy committed
48
49
50
51
import time
import pickle
import sys
import tempfile
52
import logging
53
import importlib
54
55
56
57
58
from dataclasses import dataclass, asdict
from typing import Optional, Collection, Union
import argparse
import warnings
from subprocess import run, PIPE
Paul McCarthy's avatar
Paul McCarthy committed
59
60
61
62
63


log = logging.getLogger(__name__)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
@dataclass
class SubmitParams(object):
    """
    Represents the fsl_sub parameters
    """
    minutes: Optional[float] = None
    queue: Optional[str] = None
    architecture: Optional[str] = None
    priority: Optional[int] = None
    email: Optional[str] = None
    wait_for: Union[str, None, Collection[str]] = None
    job_name: Optional[str] = None
    ram: Optional[int] = None
    logdir: Optional[str] = None
    mail_options: Optional[str] = None
    flags: Optional[str] = None
    verbose: bool = False
    environment: dict = None

    cmd_line_flags = {
        '-T': 'minutes',
        '-q': 'queue',
        '-a': 'architecture',
        '-p': 'priority',
        '-M': 'email',
        '-N': 'job_name',
        '-R': 'ram',
        '-l': 'logdir',
        '-m': 'mail_options',
        '-F': 'flags',
    }

    def __post_init__(self):
        if self.environment is None:
            self.environment = {}

    def as_flags(self, ):
        """
        Creates flags for submission using fsl_sub

        All parameters changed from their default value (typically None) will be included in the flags.

        :return: tuple with the flags
        """
        res = []
        for key, value in self.cmd_line_flags.items():
            if getattr(self, value) is not None:
                res.extend((key, str(getattr(self, value))))
        if self.verbose:
            res.append('-v')
        if self.wait_for is not None and len(_flatten_job_ids(self.wait_for)) > 0:
            res.extend(('-j', _flatten_job_ids(self.wait_for)))
        return tuple(res)

    def __str__(self):
        return f'SubmitParams({" ".join(self.as_flags())})'

    def __call__(self, *cmd, **kwargs):
        """
        Submits the command to the cluster.

        :param cmd: string or tuple of strings with the command to submit
        :param kwargs: Keyword arguments can override any parameters set in self
        :return: job ID
        """
        from fsl.utils.run import prepareArgs
        runner = self.update(**kwargs)
        cmd = prepareArgs(cmd)
        log.debug(' '.join(('fsl_sub', ) + tuple(runner.as_flags()) + tuple(cmd)))
        env = dict(os.environ)
        env.update(runner.environment)
        jobid = run(
            ('fsl_sub', ) + tuple(runner.as_flags()) + tuple(cmd),
            stdout=PIPE, check=True, env=env,
            ).stdout.decode().strip()
        log.debug(f'Job submitted as {jobid}')
        return jobid

    def update(self, **kwargs):
        """
        Creates a new SubmitParams withe updated parameters
        """
        values = asdict(self)
        values.update(kwargs)
        return SubmitParams(**values)

    @classmethod
    def add_to_parser(cls, parser: argparse.ArgumentParser, as_group='fsl_sub commands', skip=()):
        """
        Adds submission parameters to the parser

        :param parser: parser that should understand submission commands
        :param as_group: add as a new group
        :param skip: sequence of argument flags/names that should not be added to the parser
        :return: the group the arguments got added to
        """
        from fsl.utils.run import runfsl, prepareArgs
        try:
            fsl_sub_run = runfsl(['fsl_sub'])
        except FileNotFoundError:
            warnings.warn('fsl_sub was not found')
            return
        doc_lines = fsl_sub_run.stdout.decode().splitlines()
        nspaces = 1
        for line in doc_lines:
            if len(line.strip()) > 0:
                while line.startswith(' ' * nspaces):
                    nspaces += 1
        nspaces -= 1
        if as_group:
            group = parser.add_argument_group(as_group)
        else:
            group = parser
        for flag, value in cls.cmd_line_flags.items():
            if value in skip or flag in skip:
                continue
            explanation = None
            for line in doc_lines:
                if explanation is not None and len(line.strip()) > 0 and line.strip()[0] != '-':
                    explanation.append(line[nspaces:].strip())
                elif explanation is not None:
                    break
                elif line.strip().startswith(flag):
                    explanation = [line[nspaces:].strip()]
            explanation = ' '.join(explanation)
            as_type = {'minutes': float, 'priority': int, 'ram': int, 'verbose': None}
            action = 'store_true' if value == 'verbose' else 'store'
            group.add_argument(flag, dest='sub_' + value, help=explanation, action=action,
                               type=as_type[value] if value in as_type else str)
        return group

    @classmethod
    def from_args(cls, args):
        as_dict = {value: getattr(args, 'sub_' + value, None) for value in cls.cmd_line_flags.values()}
        return cls(**as_dict)


201
def submit(*command,
Paul McCarthy's avatar
Paul McCarthy committed
202
203
204
205
206
207
208
209
210
211
212
213
214
           minutes=None,
           queue=None,
           architecture=None,
           priority=None,
           email=None,
           wait_for=None,
           job_name=None,
           ram=None,
           logdir=None,
           mail_options=None,
           output=None,
           flags=False,
           multi_threaded=None,
215
216
           verbose=False,
           env=None):
217
218
219
220
    """
    Submits a given command to the cluster

    You can pass the command and arguments as a single string, or as a regular or unpacked sequence.
Paul McCarthy's avatar
Paul McCarthy committed
221

222
    :arg command:        string or regular/unpacked sequence of strings with the job command
Paul McCarthy's avatar
Paul McCarthy committed
223
224
225
226
227
228
229
230
    :arg minutes:        Estimated job length in minutes, used to auto-set
                         queue name
    :arg queue:          Explicitly sets the queue name
    :arg architecture:   e.g., darwin or lx24-amd64
    :arg priority:       Lower priority [0:-1024] default = 0
    :arg email:          Who to email after job completion
    :arg wait_for:       Place a hold on this task until the job-ids in this
                         string or tuple are complete
231
    :arg job_name:       Specify job name as it will appear on the queue
Paul McCarthy's avatar
Paul McCarthy committed
232
233
234
235
236
237
238
239
240
    :arg ram:            Max total RAM to use for job (integer in MB)
    :arg logdir:         where to output logfiles
    :arg mail_options:   Change the SGE mail options, see qsub for details
    :arg output:         If <output> image or file already exists, do nothing
                         and exit
    :arg flags:          If True, use flags embedded in scripts to set SGE
                         queuing options
    :arg multi_threaded: Submit a multi-threaded task - Set to a tuple
                         containing two elements:
Paul McCarthy's avatar
Paul McCarthy committed
241

Paul McCarthy's avatar
Paul McCarthy committed
242
                          - <pename>: a PE configures for the requested queues
Paul McCarthy's avatar
Paul McCarthy committed
243

Paul McCarthy's avatar
Paul McCarthy committed
244
                          - <threads>: number of threads to run
Paul McCarthy's avatar
Paul McCarthy committed
245

Paul McCarthy's avatar
Paul McCarthy committed
246
    :arg verbose:        If True, use verbose mode
247
    :arg env:            Dict containing environment variables
Paul McCarthy's avatar
Paul McCarthy committed
248

249
    :return:             string of submitted job id
Paul McCarthy's avatar
Paul McCarthy committed
250
251
    """

252
    from fsl.utils.run import runfsl, prepareArgs
Paul McCarthy's avatar
Paul McCarthy committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

    base_cmd = ['fsl_sub']

    for flag, variable_name in [
            ('-T', 'minutes'),
            ('-q', 'queue'),
            ('-a', 'architecture'),
            ('-p', 'priority'),
            ('-M', 'email'),
            ('-N', 'job_name'),
            ('-R', 'ram'),
            ('-l', 'logdir'),
            ('-m', 'mail_options'),
            ('-z', 'output')]:
        variable = locals()[variable_name]
        if variable:
            base_cmd.extend([flag, str(variable)])

    if flags:
        base_cmd.append('-F')
    if verbose:
        base_cmd.append('-v')

    if wait_for:
277
        base_cmd.extend(['-j', _flatten_job_ids(wait_for)])
Paul McCarthy's avatar
Paul McCarthy committed
278
279
280
281
282

    if multi_threaded:
        base_cmd.append('-s')
        base_cmd.extend(multi_threaded)

283
    base_cmd.extend(prepareArgs(command))
Paul McCarthy's avatar
Paul McCarthy committed
284

285
    return runfsl(*base_cmd, env=env).strip()
Paul McCarthy's avatar
Paul McCarthy committed
286
287
288
289
290
291
292
293
294
295
296


def info(job_id):
    """Gets information on a given job id

    Uses `qstat -j <job_id>`

    :arg job_id: string with job id
    :return:     dictionary with information on the submitted job (empty
                 if job does not exist)
    """
Paul McCarthy's avatar
Paul McCarthy committed
297
    from fsl.utils.run import run
Paul McCarthy's avatar
Paul McCarthy committed
298
    try:
299
        result = run(['qstat', '-j', job_id], exitcode=True)[0]
Paul McCarthy's avatar
Paul McCarthy committed
300
    except FileNotFoundError:
301
        log.debug("qstat not found; assuming not on cluster")
Paul McCarthy's avatar
Paul McCarthy committed
302
303
304
305
306
        return {}
    if 'Following jobs do not exist:' in result:
        return {}
    res = {}
    for line in result.splitlines()[1:]:
307
308
309
        kv = line.split(':', 1)
        if len(kv) == 2:
            res[kv[0].strip()] = kv[1].strip()
Paul McCarthy's avatar
Paul McCarthy committed
310
311
312
    return res


313
def output(job_id, logdir='.', command=None, name=None):
314
315
    """Returns the output of the given job.

316
    :arg job_id:  String containing job ID.
317
    :arg logdir:  Directory containing the log - defaults to
318
                  the current directory.
319
320
    :arg command: Command that was run. Not currently used.
    :arg name:    Job name if it was specified. Not currently used.
321
322
323
    :returns:     A tuple containing the standard output and standard error.
    """

324
325
    stdout = list(glob.glob(op.join(logdir, '*.o{}'.format(job_id))))
    stderr = list(glob.glob(op.join(logdir, '*.e{}'.format(job_id))))
326

327
328
329
330
331
332
    if len(stdout) != 1 or len(stderr) != 1:
        raise ValueError('No/too many error/output files for job {}: stdout: '
                         '{}, stderr: {}'.format(job_id, stdout, stderr))

    stdout = stdout[0]
    stderr = stderr[0]
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

    if op.exists(stdout):
        with open(stdout, 'rt') as f:
            stdout = f.read()
    else:
        stdout = None

    if op.exists(stderr):
        with open(stderr, 'rt') as f:
            stderr = f.read()
    else:
        stderr = None

    return stdout, stderr


Paul McCarthy's avatar
Paul McCarthy committed
349
350
351
352
353
354
355
def wait(job_ids):
    """Wait for one or more jobs to finish

    :arg job_ids: string or tuple of strings with jobs that should finish
                  before continuing
    """
    start_time = time.time()
356
    for job_id in _flatten_job_ids(job_ids).split(','):
Paul McCarthy's avatar
Paul McCarthy committed
357
358
359
360
361
362
363
364
        log.debug('Waiting for job {}'.format(job_id))
        while len(info(job_id)) > 0:
            wait_time = min(max(1, (time.time() - start_time) / 3.), 20)
            time.sleep(wait_time)
        log.debug('Job {} finished, continuing to next'.format(job_id))
    log.debug('All jobs have finished')


365
366
367
368
369
370
371
372
373
374
375
def _flatten_job_ids(job_ids):
    """
    Returns a potentially nested sequence of job ids as a single comma-separated string

    :param job_ids: possibly nested sequence of job ids. The job ids themselves should be strings.
    :return: comma-separated string of job ids
    """
    def unpack(job_ids):
        """Unpack the (nested) job-ids in a single set"""
        if isinstance(job_ids, str):
            return {job_ids}
376
377
        elif isinstance(job_ids, int):
            return {str(job_ids)}
378
379
380
381
382
383
384
385
386
        else:
            res = set()
            for job_id in job_ids:
                res.update(unpack(job_id))
            return res

    return ','.join(sorted(unpack(job_ids)))


Paul McCarthy's avatar
Paul McCarthy committed
387
388
389
390
391
392
393
394
395
_external_job = """#!{}
# This is a temporary file designed to run the python function {},
# so that it can be submitted to the cluster

import pickle
from six import BytesIO
from importlib import import_module

pickle_bytes = BytesIO({})
396
397
398
399
400
401
402
name_type, name, func_name, args, kwargs = pickle.load(pickle_bytes)

if name_type == 'module':
    # retrieves a function defined in an external module
    func = getattr(import_module(name), func_name)
elif name_type == 'script':
    # retrieves a function defined in the __main__ script
403
    local_execute = {{'__name__': '__not_main__', '__file__': name}}
404
405
406
407
    exec(open(name, 'r').read(), local_execute)
    func = local_execute[func_name]
else:
    raise ValueError('Unknown name_type: %r' % name_type)
Paul McCarthy's avatar
Paul McCarthy committed
408
409
410
411
412
413
414
415
416
417
418

res = func(*args, **kwargs)
if res is not None:
    with open(__file__ + '_out.pickle') as f:
        pickle.dump(f, res)
"""


def func_to_cmd(func, args, kwargs, tmp_dir=None, clean=False):
    """Defines the command needed to run the function from the command line

419
420
421
422
    WARNING: if submitting a function defined in the __main__ script,
    the script will be run again to retrieve this function. Make sure there is a
    "if __name__ == '__main__'" guard to prevent the full script from being rerun.

Paul McCarthy's avatar
Paul McCarthy committed
423
424
425
426
    :arg func:    function to be run
    :arg args:    positional arguments
    :arg kwargs:  keyword arguments
    :arg tmp_dir: directory where to store the temporary file
427
    :arg clean:   if True removes the submitted script after running it
Paul McCarthy's avatar
Paul McCarthy committed
428
429
430
    :return:      string which will run the function
    """
    pickle_bytes = BytesIO()
431
432
433
434
435
436
    if func.__module__ == '__main__':
        pickle.dump(('script', importlib.import_module('__main__').__file__, func.__name__,
                     args, kwargs), pickle_bytes)
    else:
        pickle.dump(('module', func.__module__, func.__name__,
                     args, kwargs), pickle_bytes)
Paul McCarthy's avatar
Paul McCarthy committed
437
438
439
440
441
442
443
444
445
446
447
    python_cmd = _external_job.format(sys.executable,
                                      func.__name__,
                                      pickle_bytes.getvalue())

    _, filename = tempfile.mkstemp(prefix=func.__name__ + '_',
                                   suffix='.py',
                                   dir=tmp_dir)

    with open(filename, 'w') as python_file:
        python_file.write(python_cmd)

448
    return sys.executable + " " + filename + ('; rm ' + filename if clean else '')