From 35b648bbcfed3629bf6d6ade027680e9756081b4 Mon Sep 17 00:00:00 2001 From: Dave Berenbaum Date: Fri, 24 Feb 2023 11:26:16 -0500 Subject: [PATCH] plots: shorten names in the legend (#8900) * plots: shorten field names * plots: handle missing field names * plots: make compatible with py3.8 * plots: fix for missing data * plots: add test for field shortening * plots: strip separators regardless of os * plots: don't shorten field name * plots: drop test for shortening field name --- dvc/render/converter/vega.py | 18 +++++++++++-- tests/unit/render/test_vega_converter.py | 34 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/dvc/render/converter/vega.py b/dvc/render/converter/vega.py index b96b40e38..38979552b 100644 --- a/dvc/render/converter/vega.py +++ b/dvc/render/converter/vega.py @@ -1,3 +1,4 @@ +import os from typing import Any, Dict, Iterable, List, Optional, Tuple, Union from funcy import first, last @@ -221,7 +222,13 @@ class VegaConverter(Converter): ) all_datapoints = [] - all_y_fields = {y_field for _, y_field in ys} + if ys: + all_y_files, all_y_fields = list(zip(*ys)) + all_y_fields = set(all_y_fields) + all_y_files = set(all_y_files) + else: + all_y_files = set() + all_y_fields = set() # override to unified y field name if there are different y fields if len(all_y_fields) > 1: @@ -229,6 +236,12 @@ class VegaConverter(Converter): else: props_update["y"] = first(all_y_fields) + # get common prefix to drop from file names + if len(all_y_files) > 1: + common_prefix_len = len(os.path.commonpath(all_y_files)) + else: + common_prefix_len = 0 + for i, (y_file, y_field) in enumerate(ys): if num_xs > 1: x_file, x_field = xs[i] @@ -258,12 +271,13 @@ class VegaConverter(Converter): "They have to have same length." ) + y_file_short = y_file[common_prefix_len:].strip("/\\") _update_all( datapoints, update_dict={ VERSION_FIELD: { "revision": revision, - FILENAME_FIELD: y_file, + FILENAME_FIELD: y_file_short, "field": y_field, } }, diff --git a/tests/unit/render/test_vega_converter.py b/tests/unit/render/test_vega_converter.py index d2cc0126a..b68ca9762 100644 --- a/tests/unit/render/test_vega_converter.py +++ b/tests/unit/render/test_vega_converter.py @@ -379,6 +379,40 @@ def test_finding_lists(dictionary, expected_result): }, id="multi_source_y_single_x", ), + pytest.param( + { + "dir/f": {"metric": [{"v": 1, "v2": 0.1}]}, + "dir/f2": {"metric": [{"v": 1, "v2": 0.1}]}, + }, + {"y": {"dir/f": ["v2"], "dir/f2": ["v2"]}, "x": "v"}, + [ + { + "v": 1, + "v2": 0.1, + VERSION_FIELD: { + "revision": "r", + "filename": "f", + "field": "v2", + }, + }, + { + "v": 1, + "v2": 0.1, + VERSION_FIELD: { + "revision": "r", + "filename": "f2", + "field": "v2", + }, + }, + ], + { + "x": "v", + "y": "v2", + "x_label": "v", + "y_label": "v2", + }, + id="multi_file_y_same_prefix", + ), ], ) def test_convert(