예제 #1
0
def command_cluster(args):
    from grond import Clustering
    from grond.clustering import metrics, methods, read_config, write_config

    def setup(parser):
        parser.add_option(
            '--metric',
            dest='metric',
            metavar='METRIC',
            default='kagan_angle',
            choices=metrics.metrics,
            help='metric to measure model distances. Choices: [%s]. Default: '
            'kagan_angle' % ', '.join(metrics.metrics))

        parser.add_option(
            '--write-config',
            dest='write_config',
            metavar='FILE',
            help='write configuration (or default configuration) to FILE')

    method = args[0] if args else ''
    try:
        parser, options, args = cl_parse(
            'cluster',
            args[1:],
            setup=Clustering.cli_setup(method, setup),
            details='Available clustering methods: [%s]. Use '
            '"grond cluster <method> --help" to get list of method '
            'dependent options.' % ', '.join(methods))

        if method not in Clustering.name_to_class and not op.exists(method):
            help_and_die(
                parser, 'no such clustering method: %s' %
                method if method else 'no clustering method specified')

        if op.exists(method):
            clustering = read_config(method)
        else:
            clustering = Clustering.cli_instantiate(method, options)

        if options.write_config:
            write_config(clustering, options.write_config)
        else:
            if len(args) != 1:
                help_and_die(parser, 'no rundir')
            run_path, = args

            grond.cluster(run_path, clustering, metric=options.metric)

    except grond.GrondError as e:
        die(str(e))
예제 #2
0
def command_report(args):

    import matplotlib
    matplotlib.use('Agg')

    from pyrocko import parimap

    from grond.environment import Environment
    from grond.report import \
        report_index, report_archive, serve_ip, serve_report, read_config, \
        write_config, ReportConfig

    def setup(parser):
        parser.add_option(
            '--index-only',
            dest='index_only',
            action='store_true',
            help='create index only')
        parser.add_option(
            '--serve', '-s',
            dest='serve',
            action='store_true',
            help='start http service')
        parser.add_option(
            '--serve-external', '-S',
            dest='serve_external',
            action='store_true',
            help='shortcut for --serve --host=default --fixed-port')
        parser.add_option(
            '--host',
            dest='host',
            default='localhost',
            help='<ip> to start the http server on. Special values for '
                 '<ip>: "*" binds to all available interfaces, "default" '
                 'to default external interface, "localhost" to "127.0.0.1".')
        parser.add_option(
            '--port',
            dest='port',
            type=int,
            default=8383,
            help='set default http server port. Will count up if port is '
                 'already in use unless --fixed-port is given.')
        parser.add_option(
            '--fixed-port',
            dest='fixed_port',
            action='store_true',
            help='fail if port is already in use')
        parser.add_option(
            '--open', '-o',
            dest='open',
            action='store_true',
            help='open report in browser')
        parser.add_option(
            '--config',
            dest='config',
            metavar='FILE',
            help='report configuration file to use')
        parser.add_option(
            '--write-config',
            dest='write_config',
            metavar='FILE',
            help='write configuration (or default configuration) to FILE')
        parser.add_option(
            '--update-without-plotting',
            dest='update_without_plotting',
            action='store_true',
            help='quick-and-dirty update parameter files without plotting')
        parser.add_option(
            '--parallel', dest='nparallel', type=int, default=1,
            help='set number of runs to process in parallel, '
                 'If set to more than one, --status=quiet is implied.')
        parser.add_option(
            '--threads', dest='nthreads', type=int, default=1,
            help='set number of threads per process (default: 1).'
                 'Set to 0 to use all available cores.')
        parser.add_option(
            '--no-archive',
            dest='no_archive',
            action='store_true',
            help='don\'t create archive file.')

    parser, options, args = cl_parse('report', args, setup)

    s_conf = ''
    if options.config:
        try:
            conf = read_config(options.config)
        except grond.GrondError as e:
            die(str(e))

        s_conf = ' --config="%s"' % options.config
    else:
        from grond import plot
        conf = ReportConfig(
            plot_config_collection=plot.get_plot_config_collection())
        conf.set_basepath('.')

    if options.write_config:
        try:
            write_config(conf, options.write_config)
            sys.exit(0)

        except grond.GrondError as e:
            die(str(e))

    # commandline options that can override config values
    if options.no_archive:
        conf.make_archive = False

    if len(args) == 1 and op.exists(op.join(args[0], 'index.html')):
        conf.report_base_path = conf.rel_path(args[0])
        s_conf = ' %s' % args[0]
        args = []

    report_base_path = conf.expand_path(conf.report_base_path)

    if options.index_only:
        report_index(conf)
        report_archive(conf)
        args = []

    entries_generated = False

    payload = []
    if args and all(op.isdir(rundir) for rundir in args):
        rundirs = args
        all_failed = True
        for rundir in rundirs:
            payload.append((
                [rundir], None, conf, options.update_without_plotting,
                options.nthreads))

    elif args:
        try:
            env = Environment(args)
            for event_name in env.get_selected_event_names():
                payload.append((
                    args, event_name, conf, options.update_without_plotting,
                    options.nthreads))

        except grond.GrondError as e:
            die(str(e))

    if payload:
        entries_generated = []
        for result in parimap.parimap(
                make_report, *zip(*payload), nprocs=options.nparallel):

            entries_generated.append(result)

        all_failed = not any(entries_generated)
        entries_generated = any(entries_generated)

        if all_failed:
            die('no report entries generated')

        report_index(conf)
        report_archive(conf)

    if options.serve or options.serve_external:
        if options.serve_external:
            host = 'default'
        else:
            host = options.host

        addr = serve_ip(host), options.port

        serve_report(
            addr,
            report_config=conf,
            fixed_port=options.fixed_port or options.serve_external,
            open=options.open)

    elif options.open:
        import webbrowser
        url = 'file://%s/index.html' % op.abspath(report_base_path)
        webbrowser.open(url)

    else:
        if not entries_generated and not options.index_only:
            logger.info('Nothing to do, see: grond report --help')

    if entries_generated and not (options.serve or options.serve_external):
        logger.info(CLIHints('report', config=s_conf))