Repository: iterative/dvc
Pull request #8900 (2023-01-27 21:49:49)

plots: shorten names in the legend
Before:

![visualization(24)](https://user-images.githubusercontent.com/2308172/215207631-d994f8e3-64c9-47bb-8a80-9cef1d3b5c9a.png)

After:

![visualization(25)](https://user-images.githubusercontent.com/2308172/215207696-9359735e-23d9-412c-bbdc-b22b7d786d6a.png)



codecov[bot]: # [Codecov](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative) Report
Base: **93.62**% // Head: **93.03**% // Decreases project coverage by **`-0.60%`** :warning:
> Coverage data is based on head [(`1ed6888`)](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative) compared to base [(`a8d193b`)](https://codecov.io/gh/iterative/dvc/commit/a8d193bf2a7a6b73ba575cbd0f9c4147b759796d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #8900      +/-   ##
==========================================
- Coverage   93.62%   93.03%   -0.60%     
==========================================
  Files         453      453              
  Lines       36220    36350     +130     
  Branches     5257     4294     -963     
==========================================
- Hits        33912    33819      -93     
- Misses       1797     1986     +189     
- Partials      511      545      +34     
```


| [Impacted Files](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative) | Coverage Δ | |
|---|---|---|
| [dvc/render/converter/vega.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-ZHZjL3JlbmRlci9jb252ZXJ0ZXIvdmVnYS5weQ==) | `91.83% <100.00%> (+0.57%)` | :arrow_up: |
| [dvc/testing/remote\_tests.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-ZHZjL3Rlc3RpbmcvcmVtb3RlX3Rlc3RzLnB5) | `53.90% <0.00%> (-46.10%)` | :arrow_down: |
| [tests/func/test\_unprotect.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-dGVzdHMvZnVuYy90ZXN0X3VucHJvdGVjdC5weQ==) | `78.57% <0.00%> (-21.43%)` | :arrow_down: |
| [tests/unit/utils/test\_utils.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-dGVzdHMvdW5pdC91dGlscy90ZXN0X3V0aWxzLnB5) | `83.33% <0.00%> (-16.67%)` | :arrow_down: |
| [dvc/testing/fixtures.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-ZHZjL3Rlc3RpbmcvZml4dHVyZXMucHk=) | `81.57% <0.00%> (-15.95%)` | :arrow_down: |
| [tests/unit/test\_daemon.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-dGVzdHMvdW5pdC90ZXN0X2RhZW1vbi5weQ==) | `85.18% <0.00%> (-14.82%)` | :arrow_down: |
| [dvc/utils/fs.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-ZHZjL3V0aWxzL2ZzLnB5) | `83.63% <0.00%> (-10.91%)` | :arrow_down: |
| [tests/unit/proc/test\_manager.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-dGVzdHMvdW5pdC9wcm9jL3Rlc3RfbWFuYWdlci5weQ==) | `89.28% <0.00%> (-10.72%)` | :arrow_down: |
| [dvc/proc/manager.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-ZHZjL3Byb2MvbWFuYWdlci5weQ==) | `66.66% <0.00%> (-9.68%)` | :arrow_down: |
| [dvc/analytics.py](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative#diff-ZHZjL2FuYWx5dGljcy5weQ==) | `85.91% <0.00%> (-9.61%)` | :arrow_down: |
| ... and [57 more](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative) | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/iterative/dvc/pull/8900?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=iterative).

dberenbaum: Need to check VS Code and Studio
dberenbaum: Ping @ssachkovskaya. Could you check this doesn't break anything in Studio when you have a chance?
dberenbaum: @ssachkovskaya @yathomasi Would you like to check on how this looks in Studio before merge?
yathomasi: > this doesn't seem to break anything for VS Code but also doesn't help with its legend because the field for the legend is a separate field `filename::field` that is generated within VS Code

<img width="1549" alt="Screenshot 2023-02-24 at 15 03 09" src="https://user-images.githubusercontent.com/20840228/221140177-c640bd04-2dfe-4949-b28d-441bdc78d474.png">

For my checks, it seems to be the same case as VS Code for Studio as well. 

> If we can bring this logic into DVC, we can improve the plots across products.

Yes, now we use the same field `filename::field` in Studio as well.
dberenbaum: Great, thanks @yathomasi! Just wanted to make sure it doesn't make anything worse for now. 
dberenbaum: @iterative/dvc This one should be ready to review and merge 🙏 

Patch:
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,
                     }
                 },


Test patch:
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(