From cb7977472a841d1521b5b9ede7d8a1d83cdc1895 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 5 Dec 2023 21:41:54 +0000 Subject: [PATCH 1/4] coco test --- e2e_tests/cli/convert/test_convert.py | 3 +- e2e_tests/data/coco/from/base_annotation.json | 67 +++++++++++++ e2e_tests/data/coco/to/output.json | 99 +++++++++++++++++++ 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 e2e_tests/data/coco/from/base_annotation.json create mode 100644 e2e_tests/data/coco/to/output.json diff --git a/e2e_tests/cli/convert/test_convert.py b/e2e_tests/cli/convert/test_convert.py index 4cd8f3273..a845cfd49 100644 --- a/e2e_tests/cli/convert/test_convert.py +++ b/e2e_tests/cli/convert/test_convert.py @@ -47,7 +47,8 @@ def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, st @pytest.mark.parametrize( "format, input_path, expectation_path", - [("yolo_segmented", data_path / "yolov8/from", data_path / "yolov8/to")], + [("yolo_segmented", data_path / "yolov8/from", data_path / "yolov8/to"), + ("coco", data_path / "coco/from", data_path / "coco/to")], ) def test_darwin_convert( self, format: str, input_path: Path, expectation_path: Path, tmp_path: Path diff --git a/e2e_tests/data/coco/from/base_annotation.json b/e2e_tests/data/coco/from/base_annotation.json new file mode 100644 index 000000000..b6b6e2d3c --- /dev/null +++ b/e2e_tests/data/coco/from/base_annotation.json @@ -0,0 +1,67 @@ +{ + "version": "2.0", + "schema_ref": "https://darwin-public.s3.eu-west-1.amazonaws.com/darwin_json/2.0/schema.json", + "item": { + "name": "", + "path": "/" + }, + "annotations": [ + { + "bounding_box": { + "h": 1.0, + "w": 1.0, + "x": 0.0, + "y": 0.0 + }, + "id": "007882ff-99c4-4c6f-b71b-79cfc147fef6", + "name": "test_bb" + }, + { + "ellipse": { + "angle": 0.0, + "center": { + "x": 1.0, + "y": 1.0 + }, + "radius": { + "x": 1.0, + "y": 1.0 + } + }, + "id": "320a60f2-643b-4d74-a117-0ea2fdfe7a61", + "name": "test_ellipse" + }, + { + "bounding_box": { + "h": 1.0, + "w": 1.0, + "x": 0.0, + "y": 0.0 + }, + "id": "012dcc6c-5b77-406b-8cd7-d9567c8b00b7", + "name": "test_poly", + "polygon": { + "paths": [ + [ + { + "x": 0.0, + "y": 0.0 + }, + { + "x": 1.0, + "y": 0.0 + }, + { + "x": 1.0, + "y": 1.0 + }, + { + "x": 0.0, + "y": 1.0 + } + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/e2e_tests/data/coco/to/output.json b/e2e_tests/data/coco/to/output.json new file mode 100644 index 000000000..74ca00a02 --- /dev/null +++ b/e2e_tests/data/coco/to/output.json @@ -0,0 +1,99 @@ +{ + "info": { + "description": "Exported from Darwin", + "url": "n/a", + "version": "n/a", + "year": 2023, + "contributor": "n/a", + "date_created": "2023/12/05" + }, + "licenses": [ + { + "url": "n/a", + "id": 0, + "name": "placeholder license" + } + ], + "images": [ + { + "license": 0, + "file_name": "", + "coco_url": "n/a", + "height": null, + "width": null, + "date_captured": "", + "flickr_url": "n/a", + "darwin_url": null, + "darwin_workview_url": null, + "id": 2043925204, + "tag_ids": [] + } + ], + "annotations": [ + { + "id": 1, + "image_id": 2043925204, + "category_id": 348813479, + "segmentation": [ + [ + 0.0, + 0.0, + 1.0, + 0.0, + 1.0, + 1.0, + 0.0, + 1.0 + ] + ], + "area": 1.0, + "bbox": [ + 0.0, + 0.0, + 1.0, + 1.0 + ], + "iscrowd": 0, + "extra": {} + }, + { + "id": 3, + "image_id": 2043925204, + "category_id": 3961009249, + "segmentation": [ + [ + 0.0, + 0.0, + 1.0, + 0.0, + 1.0, + 1.0, + 0.0, + 1.0 + ] + ], + "area": 1.0, + "bbox": [ + 0.0, + 0.0, + 1.0, + 1.0 + ], + "iscrowd": 0, + "extra": {} + } + ], + "categories": [ + { + "id": 348813479, + "name": "test_bb", + "supercategory": "root" + }, + { + "id": 3961009249, + "name": "test_poly", + "supercategory": "root" + } + ], + "tag_categories": [] +} \ No newline at end of file From 742a4a2401ff197791c91ca74ac8219d08be445b Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 5 Dec 2023 22:34:55 +0000 Subject: [PATCH 2/4] changes to test asserts --- e2e_tests/cli/convert/test_convert.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/e2e_tests/cli/convert/test_convert.py b/e2e_tests/cli/convert/test_convert.py index a845cfd49..9f10117af 100644 --- a/e2e_tests/cli/convert/test_convert.py +++ b/e2e_tests/cli/convert/test_convert.py @@ -15,7 +15,7 @@ class TestExportCli: def config(self) -> None: assert self.data_path.exists(), "Data path does not exist, tests cannot run" - def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, str]: + def compare_directories(self, path: Path, expected_path: Path) -> None: """ Compare two directories recursively """ @@ -25,9 +25,7 @@ def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, st for file in path.iterdir(): if file.is_dir(): # Recursively compare directories - result = self.compare_directories(file, expected_path / file.name) - if not result[0]: - return result + self.compare_directories(file, expected_path / file.name) else: if file.name.startswith("."): # Ignore hidden files @@ -41,9 +39,13 @@ def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, st expected_content = f.read() if content != expected_content: - return (False, f"File {file} does not match expected file") + print(f"Expected file: {expected_path / file.name}") + print(f"Expected Content: \n{expected_content}") + print("---------------------") + print(f"Actual file: {file}") + print(f"Actual Content: \n{content}") + assert False, f"File {file} does not match expected file" - return (True, "") @pytest.mark.parametrize( "format, input_path, expectation_path", @@ -71,7 +73,7 @@ def test_darwin_convert( ) assert_cli(result, 0) - assert self.compare_directories(expectation_path, tmp_path)[0] + self.compare_directories(expectation_path, tmp_path) if __name__ == "__main__": From d150f35583b1518fa58ed023d5461a952ea185a5 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Wed, 6 Dec 2023 12:31:09 +0000 Subject: [PATCH 3/4] changes for coco specific --- e2e_tests/cli/convert/test_convert.py | 28 +++++++++++++++++++++++---- e2e_tests/data/coco/to/output.json | 8 ++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/e2e_tests/cli/convert/test_convert.py b/e2e_tests/cli/convert/test_convert.py index 9f10117af..95fa3ff53 100644 --- a/e2e_tests/cli/convert/test_convert.py +++ b/e2e_tests/cli/convert/test_convert.py @@ -1,7 +1,10 @@ +import datetime +import sys from os.path import dirname from pathlib import Path -from typing import Tuple +from tempfile import mkdtemp +import orjson as json import pytest from e2e_tests.helpers import assert_cli, run_cli_command @@ -46,11 +49,10 @@ def compare_directories(self, path: Path, expected_path: Path) -> None: print(f"Actual Content: \n{content}") assert False, f"File {file} does not match expected file" - @pytest.mark.parametrize( "format, input_path, expectation_path", [("yolo_segmented", data_path / "yolov8/from", data_path / "yolov8/to"), - ("coco", data_path / "coco/from", data_path / "coco/to")], + pytest.param("coco", data_path / "coco/from", data_path / "coco/to", marks=pytest.mark.skipif(sys.platform == "win32", reason="File paths are different on Windows, leading to test failure"))], ) def test_darwin_convert( self, format: str, input_path: Path, expectation_path: Path, tmp_path: Path @@ -71,10 +73,28 @@ def test_darwin_convert( result = run_cli_command( f"darwin convert {format} {str(input_path)} {str(tmp_path)}" ) - + if format == "coco": + self.patch_coco(tmp_path / "output.json") assert_cli(result, 0) self.compare_directories(expectation_path, tmp_path) + def patch_coco(self, path: Path) -> None: + """ + Patch coco file to match the expected output, includes changes to year and date_created, + wrapped in try except so that format errors are still caught later with correct error messages + """ + try: + with open(path, "r") as f: + contents = f.read() + temp = json.loads(contents) + temp["info"]["year"] = 2023 + temp["info"]["date_created"] = "2023/12/05" + with open(path, "w") as f: + op = json.dumps(temp, option=json.OPT_INDENT_2 | json.OPT_SERIALIZE_NUMPY).decode("utf-8") + f.write(op) + except Exception: + print(f"Error patching {path}") + if __name__ == "__main__": pytest.main(["-vv", "-s", __file__]) diff --git a/e2e_tests/data/coco/to/output.json b/e2e_tests/data/coco/to/output.json index 74ca00a02..69e009d94 100644 --- a/e2e_tests/data/coco/to/output.json +++ b/e2e_tests/data/coco/to/output.json @@ -85,13 +85,13 @@ ], "categories": [ { - "id": 348813479, - "name": "test_bb", + "id": 3961009249, + "name": "test_poly", "supercategory": "root" }, { - "id": 3961009249, - "name": "test_poly", + "id": 348813479, + "name": "test_bb", "supercategory": "root" } ], From 1d3616e6ca639de04e196a618b02f745daa84ee1 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Wed, 6 Dec 2023 13:36:20 +0000 Subject: [PATCH 4/4] removing useless imports --- e2e_tests/cli/convert/test_convert.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/e2e_tests/cli/convert/test_convert.py b/e2e_tests/cli/convert/test_convert.py index 95fa3ff53..c37a44157 100644 --- a/e2e_tests/cli/convert/test_convert.py +++ b/e2e_tests/cli/convert/test_convert.py @@ -1,8 +1,6 @@ -import datetime import sys from os.path import dirname from pathlib import Path -from tempfile import mkdtemp import orjson as json import pytest