コード例 #1
0
    def __enter__(self):
        Path(c.DEFAULT_PARENT_FOLDER).mkdir(exist_ok=True)

        if not is_name_available(self.name, c.DEFAULT_PARENT_FOLDER):
            raise ValueError("Name '{}' is already occupied.".format(
                self.name))

        self.path.mkdir(exist_ok=False)

        if self.verbose:
            string = 'Running experiment ' + utils.get_short_uuid(self.uuid)
            if self.name:
                string += " (alias '{}')".format(self.name)
            if self.title:
                string += ': ' + self.title
            print(string)

        setup_procedure(self.path, self.name, self.title, self.tags)

        self._create_streams()

        return self
コード例 #2
0
def create_parameters(uuids):
    experiment_json_by_uuid = {
        uuid: utils.load_experiment_json(uuid)
        for uuid in uuids
    }

    params_by_uuid = {
        uuid: experiment_json['parameters']
        for uuid, experiment_json in experiment_json_by_uuid.items()
    }
    params_by_uuid = {
        utils.get_short_uuid(uuid): params
        for uuid, params in params_by_uuid.items()
    }

    all_params = set()
    for params in params_by_uuid.values():
        all_params.update(params)
    all_params = sorted(list(all_params))

    rows = []
    for param in all_params:
        row = {'Parameter': param}
        for uuid, params in params_by_uuid.items():
            value = params[param] if param in params else None
            if type(value) in (int, float):
                value = str(
                    utils.round_to_significant_digits(value,
                                                      N_SIGNIFICANT_DIGITS))
            row[uuid] = value
        rows.append(row)

    # The last column has width 100%:
    attrs = [[]] * len(uuids) + [[('style', 'width: 100%;')]]

    return create_table(['Parameter', *params_by_uuid.keys()],
                        rows,
                        id='parameter-table',
                        attrs=attrs)
コード例 #3
0
def create_experiment_div(uuid, restore_button):
    path = Path(c.DEFAULT_PARENT_FOLDER) / uuid

    experiment_json = utils.load_json(str(path / c.METADATA_JSON_FILENAME))

    template = Template('''
    <div>
        <button class="btn btn-primary button-go-back" style="height: 38px; width: 61px;">{{ fa_icon('arrow-left') }}</button>
        {% if restore_button %}
            <button class="btn btn-primary button-restore-source-code" style="height: 38px; width: 61px;" data-toggle="tooltip" title="Restore code"><i class="material-icons">restore</i></button>
        {% endif %}
        <hr>
        <div>
            <h5>
                {% if title %}
                    {{ uuid_color }} {{ short_uuid }} - {{ title }}
                {% else %}
                    {{ uuid_color }} {{ short_uuid }}
                {% endif %}
            </h5>
            {{ status_icon }} {{ filename }}<span style="display:inline-block; width: 16px;"></span>{{ tags }}
        </div>
        <hr>
    </div>
    ''')

    tags = sorted(experiment_json['tags'])
    header = template.render(
        fa_icon=html_utils.fa_icon,
        title=experiment_json['title'],
        uuid_color=html_utils.color_circle(uuid),
        short_uuid=utils.get_short_uuid(uuid),
        status_icon=html_utils.get_status_icon_tag(experiment_json['status']),
        filename=experiment_json['filename'],
        tags=' '.join([html_utils.badge(tag) for tag in tags]),
        restore_button=restore_button)

    content_by_tab_name = collections.OrderedDict()
    content_by_tab_name[html_utils.icon_title('eye',
                                              'Summary')] = create_summary(
                                                  uuid, path, experiment_json)
    content_by_tab_name[html_utils.icon_title(
        'chart-bar', 'Parameters')] = html_utils.create_parameters([uuid])
    content_by_tab_name[html_utils.icon_title('code', 'Code')] = create_code(
        uuid, path, experiment_json)
    content_by_tab_name[html_utils.icon_title(
        'cube', 'Packages')] = create_packages(path)
    content_by_tab_name[html_utils.icon_title('terminal',
                                              'Output')] = create_short_output(
                                                  path, uuid)
    content_by_tab_name[html_utils.icon_title(
        'chart-area', 'Charts')] = html_utils.create_charts([uuid])
    content_by_tab_name[html_utils.icon_title('image',
                                              'Images')] = create_images(path)

    content_by_tab_name = collections.OrderedDict([
        (key, html_utils.margin(value))
        for key, value in content_by_tab_name.items()
    ])

    tabs_html = html_utils.create_tabs(content_by_tab_name,
                                       tabs_id='experiment-tabs')

    return header + tabs_html
コード例 #4
0
def create_procedure_item_by_column(uuid, path, metadata, all_scalars,
                                    all_params):
    name = metadata['name']
    start = datetime.datetime.strptime(metadata['startedDatetime'],
                                       "%Y-%m-%dT%H:%M:%S.%f")
    end = None if metadata[
        'endedDatetime'] is None else datetime.datetime.strptime(
            metadata['endedDatetime'], "%Y-%m-%dT%H:%M:%S.%f")

    if end is None:
        duration = datetime.datetime.now() - start
    else:
        duration = end - start
    duration = utils.floor_timedelta(duration)

    status = metadata['status']
    tags = sorted(metadata['tags'])

    file_space = utils.get_file_space_representation(str(path /
                                                         c.FILES_FOLDER))

    experiment_pid = int(metadata['pid'])
    pids = psutil.pids()
    pid_icon_name = 'fas fa-play text-success' if experiment_pid in pids else 'fas fa-stop text-danger'

    # Set lightbulb class:
    if metadata['status'] == 'running':
        lightbulb_class = 'text-{}'.format(
            'primary' if metadata['conclusion'] else 'secondary')
    else:
        lightbulb_class = 'text-{}'.format(
            'primary' if metadata['conclusion'] else 'danger')

    infoicon_class = 'text-primary' if metadata[
        'description'] else 'text-secondary'

    arguments = utils.arguments_to_string(metadata['arguments'])

    procedure_item_by_column = {
        'select-row': '',
        'DetailsControl': '',
        'UUID': uuid,
        'Show': "<button class='btn btn-primary btn-xs experiment-button' id='button-{}'>Show</button>".format(uuid),
        'Icons': same_line('{}<span style="display:inline-block; width: 12px;"></span>{}<span style="display:inline-block; width: 12px;"></span>{}'.format(
            html_utils.get_status_icon_tag(status),
            html_utils.icon('fas fa-info-circle {}'.format(infoicon_class)),
            html_utils.icon('fas fa-lightbulb {}'.format(lightbulb_class)),
        )),
        'PID': same_line(html_utils.fa_icon(pid_icon_name) + ' ' + str(experiment_pid)),
        'Name': name if len(name) > 0 else None,
        'Title': cgi.escape(metadata['title']) if metadata['title'] else None,
        'Filename': same_line(html_utils.color_circle_and_string(metadata['filename'])),
        'Duration': str(duration),
        'Start': start.strftime('%Y-%m-%d %H:%M:%S'),
        'End': end.strftime('%Y-%m-%d %H:%M:%S') if end is not None else None,
        'Tags': ' '.join([html_utils.badge(tag) for tag in tags]),
        'File space': file_space,
        'ID': same_line("""<button class='btn btn-light btn-xs' onclick="copyToClipboard('{}')">{}</button>""".format(utils.get_short_uuid(uuid), html_utils.fa_icon('copy')) \
            + ' ' + html_utils.color_circle(uuid) + ' ' + utils.get_short_uuid(uuid)),
        'Git commit': same_line(html_utils.color_circle_and_string(metadata['git']['short'])) if metadata['git'] is not None else None,
        'Description': markdown.markdown(cgi.escape(metadata['description'])),
        'Conclusion': markdown.markdown(cgi.escape(metadata['conclusion'])),
        'Arguments': html_utils.monospace(arguments + """ <button class='btn btn-light btn-xs' onclick="copyToClipboard('{}')">{}</button>""".format(arguments, html_utils.fa_icon('copy')))
            if arguments else '',
        'Exception': html_utils.monospace('{}: {}'.format(metadata['exceptionType'], metadata['exceptionValue']) if metadata['exceptionType'] is not None else ''),
    }

    for scalar_name in all_scalars:
        value = get_scalar_value(path, scalar_name)
        if value is not None:
            value = str(
                utils.round_to_significant_digits(value, N_SIGNIFICANT_DIGITS))
        procedure_item_by_column[scalar_name] = value

    params = metadata['parameters']
    for name in all_params:
        if name in params:
            value = params[name]
            if type(value) in (int, float):
                value = utils.round_to_significant_digits(
                    value, N_SIGNIFICANT_DIGITS)
            value = str(value)
        else:
            value = None

        procedure_item_by_column[name] = value

    return procedure_item_by_column
コード例 #5
0
def circle_with_short_uuid(uuid):
    return '{} {}'.format(color_circle(uuid), utils.get_short_uuid(uuid))
コード例 #6
0
def create_charts(uuids):
    paths = [Path(c.DEFAULT_PARENT_FOLDER) / uuid for uuid in uuids]

    html = ''
    for uuid in uuids:
        experiment_json = utils.load_experiment_json(uuid)
        title = experiment_json['title']

        if title:
            html += '{} {} - {}\n<br>'.format(color_circle(uuid),
                                              utils.get_short_uuid(uuid),
                                              title)
        else:
            html += '{} {}\n<br>'.format(color_circle(uuid),
                                         utils.get_short_uuid(uuid))

    scalar_names = get_all_scalar_names(paths)

    hover = HoverTool(tooltips=[
        ('step', '@x'),
        ('value', '@y'),
    ],
                      mode='vline')

    plots = []

    for scalar_name in scalar_names:
        plot = figure(
            tools=[hover, 'reset', 'pan', 'wheel_zoom', 'box_zoom'],
            title=scalar_name,
            x_axis_label='Step',
            width=FIGURE_WIDTH,
            height=FIGURE_HEIGHT,
        )

        for uuid, path in zip(uuids, paths):
            scalar_file = path / c.SCALARS_FOLDER / '{}.csv'.format(
                scalar_name)

            if scalar_file.is_file():
                df = pd.read_csv(scalar_file)

                source = ColumnDataSource(data={
                    'x': df['step'],
                    'y': df['value'],
                })

                color = colorhash.ColorHash(uuid).rgb

                plot.line(
                    'x',
                    'y',
                    source=source,
                    line_color=bokeh.colors.RGB(*color),
                    legend=utils.get_short_uuid(uuid),
                    line_width=2,
                )

        plot.legend.location = "top_left"
        plot.legend.click_policy = "hide"

        script, div = components(plot)
        plots.append('{}\n{}'.format(script, div))

    return html + '\n\n'.join(plots)