Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plots: fix plot having multiple x fields #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions dvc/render/converter/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ def flat_datapoints(self, revision): # noqa: C901, PLR0912
)
else:
x_file, x_field = xs[0]
props_update["x"] = x_field

num_xs = len(xs)
unify_x_fields = num_xs > 1 and len({x[1] for x in xs}) > 1
props_update["x"] = "dvc_inferred_x_value" if unify_x_fields else x_field

ys = list(_get_ys(properties, file2datapoints))

num_xs = len(xs)
num_ys = len(ys)
if num_xs > 1 and num_xs != num_ys:
raise DvcException(
Expand Down Expand Up @@ -264,8 +266,9 @@ def flat_datapoints(self, revision): # noqa: C901, PLR0912
try:
_update_from_field(
datapoints,
field=x_field,
field="dvc_inferred_x_value" if unify_x_fields else x_field,
source_datapoints=x_datapoints,
source_field=x_field,
)
except IndexError:
raise DvcException( # noqa: B904
Expand Down
101 changes: 101 additions & 0 deletions tests/unit/render/test_vega_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,107 @@ def test_finding_lists(dictionary, expected_result):
},
id="multi_file_y_same_prefix",
),
pytest.param(
{
"f": {"metric": [{"x1": 1, "v": 0.1}]},
"f2": {"metric": [{"x2": 100, "v": 0.1}]},
},
{"y": {"f": ["v"], "f2": ["v"]}, "x": {"f": "x1", "f2": "x2"}},
[
{
"x1": 1,
"v": 0.1,
"dvc_inferred_x_value": 1,
REVISION: "r",
FILENAME: "f",
FIELD: "v",
},
{
"x2": 100,
"v": 0.1,
"dvc_inferred_x_value": 100,
REVISION: "r",
FILENAME: "f2",
FIELD: "v",
},
],
{
"anchors_y_definitions": [
{FILENAME: "f", FIELD: "v"},
{FILENAME: "f2", FIELD: "v"},
],
"x": "dvc_inferred_x_value",
"y": "v",
"x_label": "x",
"y_label": "v",
},
id="multiple_x_fields",
),
pytest.param(
{
"f": {
"metric": [
{"v": 1, "v2": 0.1, "x1": 100},
{"v": 2, "v2": 0.2, "x1": 1000},
]
},
"f2": {"metric": [{"x2": -2}, {"x2": -4}]},
},
{"y": ["v", "v2"], "x": {"f": "x1", "f2": "x2"}},
[
{
"dvc_inferred_x_value": 100,
"dvc_inferred_y_value": 1,
"v": 1,
"v2": 0.1,
"x1": 100,
REVISION: "r",
FILENAME: "f",
FIELD: "v",
},
{
"dvc_inferred_x_value": 1000,
"dvc_inferred_y_value": 2,
"v": 2,
"v2": 0.2,
"x1": 1000,
REVISION: "r",
FILENAME: "f",
FIELD: "v",
},
{
"dvc_inferred_x_value": -2,
"dvc_inferred_y_value": 0.1,
"v": 1,
"v2": 0.1,
"x1": 100,
REVISION: "r",
FILENAME: "f",
FIELD: "v2",
},
{
"dvc_inferred_x_value": -4,
"dvc_inferred_y_value": 0.2,
"v": 2,
"v2": 0.2,
"x1": 1000,
REVISION: "r",
FILENAME: "f",
FIELD: "v2",
},
],
{
"anchors_y_definitions": [
{FILENAME: "f", FIELD: "v"},
{FILENAME: "f", FIELD: "v2"},
],
"x": "dvc_inferred_x_value",
"y": "dvc_inferred_y_value",
"x_label": "x",
"y_label": "y",
},
id="y_list_x_dict",
),
],
)
def test_convert(
Expand Down