Пример #1
0
def generate_mlpipeline_metrics(metrics):
    """Generate a KFP_UI_METRICS_FILE_PATH file.

    Args:
        metrics (dict): a dictionary where the key is the metric name and the
            value is its value.
    """
    metadata = list()
    for name, value in metrics.items():
        if not isinstance(value, (int, float)):
            try:
                value = float(value)
            except ValueError:
                print("Variable {} with type {} not supported as pipeline"
                      " metric. Can only write `int` or `float` types as"
                      " pipeline metrics".format(name, type(value)))
                continue
        metadata.append({
            'name': name,
            'numberValue': value,
            'format': "RAW",
        })

    try:
        utils.ensure_or_create_dir(KFP_UI_METRICS_FILE_PATH)
    except RuntimeError:
        log.exception(
            "Writing to '%s' failed. This step will not be able to"
            " show metrics in the KFP UI.", KFP_UI_METRICS_FILE_PATH)
        return
    with open(KFP_UI_METRICS_FILE_PATH, 'w') as f:
        json.dump({'metrics': metadata}, f)
Пример #2
0
def update_uimetadata(artifact_name,
                      uimetadata_path=KFP_UI_METADATA_FILE_PATH):
    """Update ui-metadata dictionary with a new web-app entry.

    Args:
        artifact_name: Name of the artifact
        uimetadata_path: path to mlpipeline-ui-metadata.json
    """
    log.info("Adding artifact '%s' to KFP UI metadata...", artifact_name)
    try:
        outputs = get_current_uimetadata(uimetadata_path,
                                         default_if_not_exist=True)
    except json.JSONDecodeError:
        log.error("This step will not be able to visualize artifacts in the"
                  " KFP UI")
        return

    pod_name = podutils.get_pod_name()
    namespace = podutils.get_namespace()
    workflow_name = workflowutils.get_workflow_name(pod_name, namespace)
    html_artifact_entry = [{
        'type':
        'web-app',
        'storage':
        'minio',
        'source':
        'minio://mlpipeline/artifacts/{}/{}/{}'.format(workflow_name, pod_name,
                                                       artifact_name + '.tgz')
    }]
    outputs['outputs'] += html_artifact_entry

    try:
        utils.ensure_or_create_dir(uimetadata_path)
    except RuntimeError:
        log.exception(
            "Writing to '%s' failed. This step will not be able to"
            " visualize artifacts in the KFP UI.", uimetadata_path)
        return
    with open(uimetadata_path, "w") as f:
        json.dump(outputs, f)
    log.info("Artifact successfully added")