Esempio n. 1
0
def command_plot(args):

    import matplotlib
    matplotlib.use('Agg')

    from grond.environment import Environment

    def setup(parser):
        pass

    details = ''

    parser, options, args = cl_parse('plot', args, setup, details)

    if len(args) not in (2, 3):
        help_and_die(parser, 'two or three arguments required')

    env = Environment(args[1:])
    from grond import plot
    if args[0] == 'list':
        plot_names, plot_doc = zip(*[(pc.name, pc.__doc__)
                                     for pc in env.get_plot_classes()])
        plot_descs = [doc.split('\n')[0].strip() for doc in plot_doc]
        left_spaces = max([len(pn) for pn in plot_names])

        for name, desc in zip(plot_names, plot_descs):
            print('{name:<{ls}} - {desc}'.format(ls=left_spaces,
                                                 name=name,
                                                 desc=desc))

    elif args[0] == 'config':
        plot_config_collection = plot.get_plot_config_collection(env)
        print(plot_config_collection)

    elif args[0] == 'all':
        plot_names = plot.get_plot_names(env)
        plot.make_plots(env, plot_names=plot_names)

    elif op.exists(args[0]):
        plots = plot.PlotConfigCollection.load(args[0])
        plot.make_plots(env, plots)

    else:
        plot_names = [name.strip() for name in args[0].split(',')]
        plot.make_plots(env, plot_names=plot_names)
Esempio n. 2
0
def command_plot(args):

    def setup(parser):
        parser.add_option(
            '--show', dest='show', action='store_true',
            help='show plot for interactive inspection')

    details = ''

    parser, options, args = cl_parse('plot', args, setup, details)

    if not options.show:
        import matplotlib
        matplotlib.use('Agg')

    from grond.environment import Environment

    if len(args) not in (1, 2, 3):
        help_and_die(parser, '1, 2 or 3 arguments required')

    if len(args) > 1:
        env = Environment(args[1:])
    else:
        env = None

    from grond import plot
    if args[0] == 'list':

        def get_doc_title(doc):
            for ln in doc.split('\n'):
                ln = ln.strip()
                if ln != '':
                    ln = ln.strip('.')
                    return ln
            return 'Undocumented.'

        if env:
            plot_classes = env.get_plot_classes()
        else:
            plot_classes = plot.get_all_plot_classes()

        plot_names, plot_doc = zip(*[(pc.name, pc.__doc__)
                                     for pc in plot_classes])

        plot_descs = [get_doc_title(doc) for doc in plot_doc]
        left_spaces = max([len(pn) for pn in plot_names])

        for name, desc in zip(plot_names, plot_descs):
            print('{name:<{ls}} - {desc}'.format(
                ls=left_spaces, name=name, desc=desc))

    elif args[0] == 'config':
        plot_config_collection = plot.get_plot_config_collection(env)
        print(plot_config_collection)

    elif args[0] == 'all':
        if env is None:
            help_and_die(parser, 'two or three arguments required')
        plot_names = plot.get_plot_names(env)
        plot.make_plots(env, plot_names=plot_names, show=options.show)

    elif op.exists(args[0]):
        if env is None:
            help_and_die(parser, 'two or three arguments required')
        plots = plot.PlotConfigCollection.load(args[0])
        plot.make_plots(env, plots, show=options.show)

    else:
        if env is None:
            help_and_die(parser, 'two or three arguments required')
        plot_names = [name.strip() for name in args[0].split(',')]
        plot.make_plots(env, plot_names=plot_names, show=options.show)
Esempio n. 3
0
def report(env, report_config=None, update_without_plotting=False):
    if report_config is None:
        report_config = ReportConfig()
        report_config.set_basepath('.')

    event_name = env.get_current_event_name()
    problem = env.get_problem()
    logger.info('Creating report for event %s...' % event_name)

    fp = report_config.expand_path
    report_path = expand_template(
        op.join(fp(report_config.reports_base_path),
                report_config.report_sub_path),
        dict(event_name=event_name, problem_name=problem.name))

    if op.exists(report_path) and not update_without_plotting:
        shutil.rmtree(report_path)

    try:
        problem.dump_problem_info(report_path)

        guts.dump(env.get_config(),
                  filename=op.join(report_path, 'config.yaml'),
                  header=True)

        util.ensuredir(report_path)
        plots_dir_out = op.join(report_path, 'plots')
        util.ensuredir(plots_dir_out)

        event = env.get_dataset().get_event()
        guts.dump(event, filename=op.join(report_path, 'event.reference.yaml'))

        try:
            rundir_path = env.get_rundir_path()

            core.export('stats', [rundir_path],
                        filename=op.join(report_path, 'stats.yaml'))

            core.export('best', [rundir_path],
                        filename=op.join(report_path,
                                         'event.solution.best.yaml'),
                        type='event-yaml')

            core.export('mean', [rundir_path],
                        filename=op.join(report_path,
                                         'event.solution.mean.yaml'),
                        type='event-yaml')

            core.export('ensemble', [rundir_path],
                        filename=op.join(report_path,
                                         'event.solution.ensemble.yaml'),
                        type='event-yaml')

        except (environment.NoRundirAvailable, ProblemInfoNotAvailable,
                ProblemDataNotAvailable):

            pass

        if not update_without_plotting:
            from grond import plot
            plot.make_plots(env, plots_path=op.join(report_path, 'plots'))

        rie = ReportIndexEntry(path='.',
                               problem_name=problem.name,
                               grond_version=problem.grond_version)

        fn = op.join(report_path, 'event.solution.best.yaml')
        if op.exists(fn):
            rie.event_best = guts.load(filename=fn)

        fn = op.join(report_path, 'event.reference.yaml')
        if op.exists(fn):
            rie.event_reference = guts.load(filename=fn)

        fn = op.join(report_path, 'index.yaml')
        guts.dump(rie, filename=fn)

    except Exception as e:
        logger.warn(
            'report generation failed, removing incomplete report dir: %s' %
            report_path)
        raise e

        if op.exists(report_path):
            shutil.rmtree(report_path)

    report_index(report_config)
    report_archive(report_config)
Esempio n. 4
0
def report(env,
           report_config=None,
           update_without_plotting=False,
           make_index=True,
           make_archive=True,
           nthreads=0):

    if report_config is None:
        report_config = ReportConfig()
        report_config.set_basepath('.')

    event_name = env.get_current_event_name()
    problem = env.get_problem()
    logger.info('Creating report entry for run "%s"...' % problem.name)

    optimiser = env.get_optimiser()
    optimiser.set_nthreads(nthreads)

    fp = report_config.expand_path
    entry_path = expand_template(
        op.join(fp(report_config.report_base_path),
                report_config.entries_sub_path),
        dict(event_name=event_name, problem_name=problem.name))

    if op.exists(entry_path) and not update_without_plotting:
        shutil.rmtree(entry_path)

    try:
        problem.dump_problem_info(entry_path)

        guts.dump(env.get_config(),
                  filename=op.join(entry_path, 'config.yaml'),
                  header=True)

        util.ensuredir(entry_path)
        plots_dir_out = op.join(entry_path, 'plots')
        util.ensuredir(plots_dir_out)

        event = env.get_dataset().get_event()
        guts.dump(event, filename=op.join(entry_path, 'event.reference.yaml'))

        try:
            rundir_path = env.get_rundir_path()

            core.export('stats', [rundir_path],
                        filename=op.join(entry_path, 'stats.yaml'))

            core.export('best', [rundir_path],
                        filename=op.join(entry_path,
                                         'event.solution.best.yaml'),
                        type='event-yaml')

            core.export('mean', [rundir_path],
                        filename=op.join(entry_path,
                                         'event.solution.mean.yaml'),
                        type='event-yaml')

            core.export('ensemble', [rundir_path],
                        filename=op.join(entry_path,
                                         'event.solution.ensemble.yaml'),
                        type='event-yaml')

        except (environment.NoRundirAvailable, ProblemInfoNotAvailable,
                ProblemDataNotAvailable):

            pass

        if not update_without_plotting:
            from grond import plot
            pcc = report_config.plot_config_collection.get_weeded(env)
            plot.make_plots(env,
                            plots_path=op.join(entry_path, 'plots'),
                            plot_config_collection=pcc)

        try:
            run_info = env.get_run_info()
        except environment.NoRundirAvailable:
            run_info = None

        rie = ReportIndexEntry(path='.',
                               problem_name=problem.name,
                               grond_version=problem.grond_version,
                               run_info=run_info)

        fn = op.join(entry_path, 'event.solution.best.yaml')
        if op.exists(fn):
            rie.event_best = guts.load(filename=fn)

        fn = op.join(entry_path, 'event.reference.yaml')
        if op.exists(fn):
            rie.event_reference = guts.load(filename=fn)

        fn = op.join(entry_path, 'index.yaml')
        guts.dump(rie, filename=fn)

    except Exception as e:
        logger.warn(
            'Failed to create report entry, removing incomplete subdirectory: '
            '%s' % entry_path)
        raise e

        if op.exists(entry_path):
            shutil.rmtree(entry_path)

    logger.info('Done creating report entry for run "%s".' % problem.name)

    if make_index:
        report_index(report_config)

    if make_archive:
        report_archive(report_config)