plots: cleanup data extraction#6355
Merged
Merged
Conversation
pared
commented
Jul 22, 2021
Comment on lines
+36
to
+39
| if extension in (".json", ".yaml"): | ||
| return DictData(filename, revision, content) | ||
| if extension in (".csv", ".tsv"): | ||
| return ListData(filename, revision, content) |
Contributor
Author
There was a problem hiding this comment.
After moving data parsing to collect in #5984 we can abstract obtained data.
Comment on lines
-71
to
-98
| def _apply_path(data, path=None, **kwargs): | ||
| if not path or not isinstance(data, dict): | ||
| return data | ||
|
|
||
| import jsonpath_ng | ||
|
|
||
| found = jsonpath_ng.parse(path).find(data) | ||
| first_datum = first(found) | ||
| if ( | ||
| len(found) == 1 | ||
| and isinstance(first_datum.value, list) | ||
| and isinstance(first(first_datum.value), dict) | ||
| ): | ||
| data_points = first_datum.value | ||
| elif len(first_datum.path.fields) == 1: | ||
| field_name = first(first_datum.path.fields) | ||
| data_points = [{field_name: datum.value} for datum in found] | ||
| else: | ||
| raise PlotDataStructureError() | ||
|
|
||
| if not isinstance(data_points, list) or not ( | ||
| isinstance(first(data_points), dict) | ||
| ): | ||
| raise PlotDataStructureError() | ||
|
|
||
| return data_points | ||
|
|
||
|
|
Contributor
Author
There was a problem hiding this comment.
Not supported from #3840 I recall that we have been talking about keeping it in another form, but anyway, it can always be brought back from git history.
Comment on lines
-563
to
-576
| @pytest.mark.parametrize("data_class", [JSONPlotData, YAMLPlotData]) | ||
| def test_find_data_in_dict(tmp_dir, data_class): | ||
| metric = [{"accuracy": 1, "loss": 2}, {"accuracy": 3, "loss": 4}] | ||
| dmetric = {"train": metric} | ||
|
|
||
| plot_data = data_class("-", "revision", dmetric) | ||
|
|
||
| expected = metric | ||
| for d in expected: | ||
| d["rev"] = "revision" | ||
|
|
||
| assert list(map(dict, plot_data.to_datapoints())) == expected | ||
|
|
||
|
|
Comment on lines
-5
to
-22
| from dvc.repo.plots.data import _apply_path, _find_data, _lists | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "path,expected_result", | ||
| [ | ||
| ("$.some.path[*].a", [{"a": 1}, {"a": 4}]), | ||
| ("$.some.path", [{"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}]), | ||
| ], | ||
| ) | ||
| def test_parse_json(path, expected_result): | ||
| value = { | ||
| "some": {"path": [{"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}]} | ||
| } | ||
|
|
||
| result = _apply_path(value, path=path) | ||
|
|
||
| assert result == expected_result |
Contributor
Author
There was a problem hiding this comment.
Same as before, no need for this test for now.
Comment on lines
-43
to
-44
| def test_finding_data(fields): | ||
| data = {"a": {"b": [{"x": 2, "y": 3}, {"x": 1, "y": 5}]}} |
Contributor
Author
There was a problem hiding this comment.
Expanded scope of the test to test to_datapoints and remove redundancy in tests (test_finding_data was checking basically the same as test_find_data_in_dict).
pared
commented
Jul 22, 2021
| "nanotime>=0.5.2", | ||
| "pyasn1>=0.4.1", | ||
| "voluptuous>=0.11.7", | ||
| "jsonpath-ng>=1.5.1", |
Contributor
Author
pmrowla
approved these changes
Jul 23, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
❗ I have followed the Contributing to DVC checklist.
📖 If this PR requires documentation updates, I have created a separate PR (or issue, at least) in dvc.org and linked it here.
Thank you for the contribution - we'll try to review it as soon as possible. 🙏