示例#1
0
def find_vega(repo, plots_data, target):
    found = dpath.util.search(plots_data, ["*", "*", target])
    from dvc.render.vega import VegaRenderer

    if found and VegaRenderer.matches(found):
        return VegaRenderer(found, repo.plots.templates).get_vega()
    return ""
示例#2
0
def match_renderers(plots_data, templates):
    from dvc.render.image import ImageRenderer
    from dvc.render.vega import VegaRenderer

    renderers = []
    for g in group_by_filename(plots_data):
        if VegaRenderer.matches(g):
            renderers.append(VegaRenderer(g, templates))
        if ImageRenderer.matches(g):
            renderers.append(ImageRenderer(g))
    return renderers
示例#3
0
def test_as_json(tmp_dir, scm, dvc):
    metric = [{"a": 1, "b": 2, "c": 3}, {"a": 2, "b": 3, "c": 4}]
    data = {"workspace": {"data": {"file.json": {"data": metric}}}}
    props = {"fields": {"b"}}

    renderer = VegaRenderer(data,
                            template=dvc.plots.templates.load(),
                            properties=props)
    plot_content = renderer.asdict()
    plot_as_json = first(json.loads(renderer.as_json()))

    assert plot_as_json["type"] == "vega"
    assert plot_as_json["revisions"] == ["workspace"]
    assert plot_as_json["content"] == plot_content
示例#4
0
def test_plot_default_choose_column(tmp_dir, scm, dvc):
    metric = [{"a": 1, "b": 2, "c": 3}, {"a": 2, "b": 3, "c": 4}]
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": {
                        "fields": {"b"}
                    }
                }
            }
        }
    }

    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()
    plot_content = json.loads(plot_string)

    assert plot_content["data"]["values"] == [
        {
            INDEX_FIELD: 0,
            "b": 2,
            REVISION_FIELD: "workspace"
        },
        {
            INDEX_FIELD: 1,
            "b": 3,
            REVISION_FIELD: "workspace"
        },
    ]
    assert plot_content["encoding"]["x"]["field"] == INDEX_FIELD
    assert plot_content["encoding"]["y"]["field"] == "b"
示例#5
0
def test_custom_template(tmp_dir, scm, dvc, custom_template):
    metric = [{"a": 1, "b": 2}, {"a": 2, "b": 3}]
    props = {"template": os.fspath(custom_template), "x": "a", "y": "b"}
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": props
                }
            }
        }
    }

    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()

    plot_content = json.loads(plot_string)
    assert plot_content["data"]["values"] == [
        {
            "a": 1,
            "b": 2,
            REVISION_FIELD: "workspace"
        },
        {
            "a": 2,
            "b": 3,
            REVISION_FIELD: "workspace"
        },
    ]
    assert plot_content["encoding"]["x"]["field"] == "a"
    assert plot_content["encoding"]["y"]["field"] == "b"
示例#6
0
文件: utils.py 项目: isidentical/dvc
def find_vega(repo, plots_data, target):
    # TODO same as group_by_filename
    grouped = group_by_filename(plots_data)
    found = None
    for plot_group in grouped:
        files = get_files(plot_group)
        assert len(files) == 1
        file = files.pop()
        if file == target:
            found = plot_group
            break

    from dvc.render.vega import VegaRenderer

    if found and VegaRenderer.matches(found):
        return VegaRenderer(found, repo.plots.templates).get_vega()
    return ""
示例#7
0
def test_bad_template(tmp_dir, dvc):
    metric = [{"val": 2}, {"val": 3}]
    data = {"workspace": {"data": {"file.json": {"data": metric}}}}

    from dvc.repo.plots.template import Template

    with pytest.raises(BadTemplateError):
        VegaRenderer(data, Template("name", "content")).asdict()
示例#8
0
def test_raise_on_wrong_field(tmp_dir, scm, dvc):
    metric = [{"val": 2}, {"val": 3}]
    props = {"x": "no_val"}
    data = {"workspace": {"data": {"file.json": {"data": metric}}}}

    with pytest.raises(NoFieldInDataError):
        VegaRenderer(data,
                     template=dvc.plots.templates.load(),
                     properties=props).asdict()
示例#9
0
def test_matches(extension, matches):
    filename = "file" + extension
    data = {
        "HEAD": {
            "data": {
                filename: {}
            }
        },
        "v1": {
            "data": {
                filename: {}
            }
        },
    }
    assert VegaRenderer.matches(data) == matches
示例#10
0
def test_raise_on_no_template(tmp_dir, dvc):
    metric = [{"val": 2}, {"val": 3}]
    props = {"template": "non_existing_template.json"}
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": props
                }
            }
        }
    }

    with pytest.raises(TemplateNotFoundError):
        VegaRenderer(data, dvc.plots.templates).get_vega()
示例#11
0
def test_raise_on_wrong_field(tmp_dir, scm, dvc):
    metric = [{"val": 2}, {"val": 3}]
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": {
                        "x": "no_val"
                    }
                }
            }
        }
    }

    with pytest.raises(NoFieldInDataError):
        VegaRenderer(data, dvc.plots.templates).get_vega()
示例#12
0
def test_bad_template(tmp_dir, dvc):
    metric = [{"val": 2}, {"val": 3}]
    (tmp_dir / "template.json").dump({"a": "b", "c": "d"})
    props = {"template": "template.json"}
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": props
                }
            }
        }
    }

    with pytest.raises(BadTemplateError):
        VegaRenderer(data, dvc.plots.templates).get_vega()
示例#13
0
def test_confusion(tmp_dir, dvc):
    confusion_matrix = [
        {
            "predicted": "B",
            "actual": "A"
        },
        {
            "predicted": "A",
            "actual": "A"
        },
    ]
    props = {"template": "confusion", "x": "predicted", "y": "actual"}

    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": confusion_matrix,
                    "props": props
                }
            }
        }
    }

    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()

    plot_content = json.loads(plot_string)
    assert plot_content["data"]["values"] == [
        {
            "predicted": "B",
            "actual": "A",
            REVISION_FIELD: "workspace"
        },
        {
            "predicted": "A",
            "actual": "A",
            REVISION_FIELD: "workspace"
        },
    ]
    assert plot_content["spec"]["transform"][0]["groupby"] == [
        "actual",
        "predicted",
    ]
    assert plot_content["spec"]["encoding"]["x"]["field"] == "predicted"
    assert plot_content["spec"]["encoding"]["y"]["field"] == "actual"
示例#14
0
def test_one_column(tmp_dir, dvc):
    props = {
        "x_label": "x_title",
        "y_label": "y_title",
        "title": "mytitle",
    }
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": [{
                        "val": 2
                    }, {
                        "val": 3
                    }]
                }
            }
        }
    }

    plot_content = VegaRenderer(data,
                                template=dvc.plots.templates.load(),
                                properties=props).asdict()

    assert plot_content["title"] == "mytitle"
    assert plot_content["data"]["values"] == [
        {
            "val": 2,
            INDEX_FIELD: 0,
            REVISION_FIELD: "workspace"
        },
        {
            "val": 3,
            INDEX_FIELD: 1,
            REVISION_FIELD: "workspace"
        },
    ]
    assert (first(
        plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD)
    assert first(plot_content["layer"])["encoding"]["y"]["field"] == "val"
    assert first(plot_content["layer"])["encoding"]["x"]["title"] == "x_title"
    assert first(plot_content["layer"])["encoding"]["y"]["title"] == "y_title"
示例#15
0
def test_one_column(tmp_dir, scm, dvc):
    props = {
        "x_label": "x_title",
        "y_label": "y_title",
        "title": "mytitle",
    }
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": [{
                        "val": 2
                    }, {
                        "val": 3
                    }],
                    "props": props
                }
            }
        }
    }

    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()

    plot_content = json.loads(plot_string)
    assert plot_content["title"] == "mytitle"
    assert plot_content["data"]["values"] == [
        {
            "val": 2,
            INDEX_FIELD: 0,
            REVISION_FIELD: "workspace"
        },
        {
            "val": 3,
            INDEX_FIELD: 1,
            REVISION_FIELD: "workspace"
        },
    ]
    assert (first(
        plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD)
    assert first(plot_content["layer"])["encoding"]["y"]["field"] == "val"
    assert first(plot_content["layer"])["encoding"]["x"]["title"] == "x_title"
    assert first(plot_content["layer"])["encoding"]["y"]["title"] == "y_title"
示例#16
0
def test_choose_axes(tmp_dir, scm, dvc):
    metric = [
        OrderedDict([("first_val", 100), ("second_val", 100), ("val", 2)]),
        OrderedDict([("first_val", 200), ("second_val", 300), ("val", 3)]),
    ]

    props = {"x": "first_val", "y": "second_val"}

    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": props
                }
            }
        }
    }
    plot_content = VegaRenderer(data,
                                template=dvc.plots.templates.load(),
                                properties=props).asdict()

    assert plot_content["data"]["values"] == [
        {
            "val": 2,
            REVISION_FIELD: "workspace",
            "first_val": 100,
            "second_val": 100,
        },
        {
            "val": 3,
            REVISION_FIELD: "workspace",
            "first_val": 200,
            "second_val": 300,
        },
    ]
    assert (first(
        plot_content["layer"])["encoding"]["x"]["field"] == "first_val")
    assert (first(
        plot_content["layer"])["encoding"]["y"]["field"] == "second_val")
示例#17
0
def test_multiple_columns(tmp_dir, scm, dvc):
    metric = [
        OrderedDict([("first_val", 100), ("second_val", 100), ("val", 2)]),
        OrderedDict([("first_val", 200), ("second_val", 300), ("val", 3)]),
    ]

    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": {}
                }
            }
        }
    }

    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()

    plot_content = json.loads(plot_string)
    assert plot_content["data"]["values"] == [
        {
            "val": 2,
            INDEX_FIELD: 0,
            REVISION_FIELD: "workspace",
            "first_val": 100,
            "second_val": 100,
        },
        {
            "val": 3,
            INDEX_FIELD: 1,
            REVISION_FIELD: "workspace",
            "first_val": 200,
            "second_val": 300,
        },
    ]
    assert (first(
        plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD)
    assert first(plot_content["layer"])["encoding"]["y"]["field"] == "val"
示例#18
0
def test_metric_missing(tmp_dir, scm, dvc, caplog):

    metric = [{"y": 2}, {"y": 3}]
    data = {
        "v2": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": {}
                }
            }
        },
        "workspace": {
            "data": {
                "file.json": {
                    "error": FileNotFoundError(),
                    "props": {}
                }
            }
        },
    }
    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()

    plot_content = json.loads(plot_string)
    assert plot_content["data"]["values"] == [
        {
            "y": 2,
            INDEX_FIELD: 0,
            REVISION_FIELD: "v2"
        },
        {
            "y": 3,
            INDEX_FIELD: 1,
            REVISION_FIELD: "v2"
        },
    ]
    assert (first(
        plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD)
    assert first(plot_content["layer"])["encoding"]["y"]["field"] == "y"
示例#19
0
def test_plot_choose_columns(tmp_dir, scm, dvc, custom_template):
    metric = [{"a": 1, "b": 2, "c": 3}, {"a": 2, "b": 3, "c": 4}]
    props = {
        "fields": {"b", "c"},
        "x": "b",
        "y": "c",
    }
    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": props
                }
            }
        }
    }

    plot_content = VegaRenderer(
        data,
        template=dvc.plots.templates.load(os.fspath(custom_template)),
        properties=props,
    ).asdict()

    assert plot_content["data"]["values"] == [
        {
            "b": 2,
            "c": 3,
            REVISION_FIELD: "workspace"
        },
        {
            "b": 3,
            "c": 4,
            REVISION_FIELD: "workspace"
        },
    ]
    assert plot_content["encoding"]["x"]["field"] == "b"
    assert plot_content["encoding"]["y"]["field"] == "c"
示例#20
0
def test_choose_axes(tmp_dir, scm, dvc):
    metric = [
        OrderedDict([("first_val", 100), ("second_val", 100), ("val", 2)]),
        OrderedDict([("first_val", 200), ("second_val", 300), ("val", 3)]),
    ]

    props = {"x": "first_val", "y": "second_val"}

    data = {
        "workspace": {
            "data": {
                "file.json": {
                    "data": metric,
                    "props": props
                }
            }
        }
    }
    plot_string = VegaRenderer(data, dvc.plots.templates).get_vega()

    plot_content = json.loads(plot_string)
    assert plot_content["data"]["values"] == [
        {
            "val": 2,
            REVISION_FIELD: "workspace",
            "first_val": 100,
            "second_val": 100,
        },
        {
            "val": 3,
            REVISION_FIELD: "workspace",
            "first_val": 200,
            "second_val": 300,
        },
    ]
    assert plot_content["encoding"]["x"]["field"] == "first_val"
    assert plot_content["encoding"]["y"]["field"] == "second_val"
示例#21
0
def test_confusion(tmp_dir, dvc):
    confusion_matrix = [
        {
            "predicted": "B",
            "actual": "A"
        },
        {
            "predicted": "A",
            "actual": "A"
        },
    ]
    props = {"template": "confusion", "x": "predicted", "y": "actual"}

    data = {"workspace": {"data": {"file.json": {"data": confusion_matrix}}}}

    plot_content = VegaRenderer(data,
                                template=dvc.plots.templates.load("confusion"),
                                properties=props).asdict()

    assert plot_content["data"]["values"] == [
        {
            "predicted": "B",
            "actual": "A",
            REVISION_FIELD: "workspace"
        },
        {
            "predicted": "A",
            "actual": "A",
            REVISION_FIELD: "workspace"
        },
    ]
    assert plot_content["spec"]["transform"][0]["groupby"] == [
        "actual",
        "predicted",
    ]
    assert plot_content["spec"]["encoding"]["x"]["field"] == "predicted"
    assert plot_content["spec"]["encoding"]["y"]["field"] == "actual"
示例#22
0
 def get_vega_string(data, filename):
     file_data = dpath.util.search(data, ["*", "*", filename])
     return VegaRenderer(file_data,
                         dvc.plots.templates.load()).partial_html()
示例#23
0
def test_multiple_revs_default(tmp_dir, scm, dvc):
    metric_1 = [{"y": 2}, {"y": 3}]
    metric_2 = [{"y": 3}, {"y": 5}]
    metric_3 = [{"y": 5}, {"y": 6}]

    data = {
        "HEAD": {
            "data": {
                "file.json": {
                    "data": metric_3,
                    "props": {
                        "fields": {"y"}
                    }
                }
            }
        },
        "v2": {
            "data": {
                "file.json": {
                    "data": metric_2,
                    "props": {
                        "fields": {"y"}
                    }
                }
            }
        },
        "v1": {
            "data": {
                "file.json": {
                    "data": metric_1,
                    "props": {
                        "fields": {"y"}
                    }
                }
            }
        },
    }

    plot_content = VegaRenderer(data, dvc.plots.templates.load()).asdict()

    assert plot_content["data"]["values"] == [
        {
            "y": 5,
            INDEX_FIELD: 0,
            REVISION_FIELD: "HEAD"
        },
        {
            "y": 6,
            INDEX_FIELD: 1,
            REVISION_FIELD: "HEAD"
        },
        {
            "y": 3,
            INDEX_FIELD: 0,
            REVISION_FIELD: "v2"
        },
        {
            "y": 5,
            INDEX_FIELD: 1,
            REVISION_FIELD: "v2"
        },
        {
            "y": 2,
            INDEX_FIELD: 0,
            REVISION_FIELD: "v1"
        },
        {
            "y": 3,
            INDEX_FIELD: 1,
            REVISION_FIELD: "v1"
        },
    ]
    assert (first(
        plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD)
    assert first(plot_content["layer"])["encoding"]["y"]["field"] == "y"