From 8b5ad93f40a8eb90e4489896de0025c504a801ec Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 18:54:56 +0100 Subject: [PATCH 1/7] make JSON indentation configurable --- pyodi/apps/coco/coco_merge.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pyodi/apps/coco/coco_merge.py b/pyodi/apps/coco/coco_merge.py index 8c2d46e..d799cac 100644 --- a/pyodi/apps/coco/coco_merge.py +++ b/pyodi/apps/coco/coco_merge.py @@ -16,19 +16,25 @@ # API REFERENCE """ # noqa: E501 import json -from typing import Any, Dict +from typing import Any, Dict, Union from loguru import logger @logger.catch(reraise=True) -def coco_merge(input_extend: str, input_add: str, output_file: str,) -> str: +def coco_merge( + input_extend: str, + input_add: str, + output_file: str, + json_indentation: Union[int, None] = 2, +) -> str: """Merge COCO annotation files. Args: input_extend: Path to input file to be extended. input_add: Path to input file to be added. output_file : Path to output file with merged annotations. + json_indentation: If set to a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of `0` will only insert newlines. `None` is the most compact representation (no indentation). """ with open(input_extend, "r") as f: data_extend = json.load(f) @@ -88,6 +94,6 @@ def coco_merge(input_extend: str, input_add: str, output_file: str,) -> str: ) with open(output_file, "w") as f: - json.dump(output, f, indent=2) + json.dump(output, f, indent=json_indentation) return output_file From b4485ba4a32485829f0b8b7a993266c12878b4cb Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 18:58:21 +0100 Subject: [PATCH 2/7] bump version to 0.0.7 --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 38619af..023f999 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pyodi -version = 0.0.6 +version = 0.0.7 author = Pyodi description = Object Detection Insights long_description = file: README.md From 6d96c2149df49c9fc4df6ffcee63ec56887445dc Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 20:56:34 +0100 Subject: [PATCH 3/7] apply suggestions from code review Co-authored-by: David de la Iglesia Castro --- pyodi/apps/coco/coco_merge.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyodi/apps/coco/coco_merge.py b/pyodi/apps/coco/coco_merge.py index d799cac..90ccc32 100644 --- a/pyodi/apps/coco/coco_merge.py +++ b/pyodi/apps/coco/coco_merge.py @@ -26,7 +26,7 @@ def coco_merge( input_extend: str, input_add: str, output_file: str, - json_indentation: Union[int, None] = 2, + indent: Optional[int] = None, ) -> str: """Merge COCO annotation files. @@ -34,7 +34,7 @@ def coco_merge( input_extend: Path to input file to be extended. input_add: Path to input file to be added. output_file : Path to output file with merged annotations. - json_indentation: If set to a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of `0` will only insert newlines. `None` is the most compact representation (no indentation). + indent: Argument passed to `json.dump`. See https://docs.python.org/3/library/json.html#json.dump. """ with open(input_extend, "r") as f: data_extend = json.load(f) From bf2af2e7b67ef62ec64c1f7201fbf020f4518f5c Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 21:29:06 +0100 Subject: [PATCH 4/7] add unit test for JSON indentation --- pyodi/apps/coco/coco_merge.py | 4 ++-- tests/apps/test_coco_merge.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pyodi/apps/coco/coco_merge.py b/pyodi/apps/coco/coco_merge.py index 90ccc32..ebfb29d 100644 --- a/pyodi/apps/coco/coco_merge.py +++ b/pyodi/apps/coco/coco_merge.py @@ -16,7 +16,7 @@ # API REFERENCE """ # noqa: E501 import json -from typing import Any, Dict, Union +from typing import Any, Dict, Optional from loguru import logger @@ -94,6 +94,6 @@ def coco_merge( ) with open(output_file, "w") as f: - json.dump(output, f, indent=json_indentation) + json.dump(output, f, indent=indent) return output_file diff --git a/tests/apps/test_coco_merge.py b/tests/apps/test_coco_merge.py index 1fb4b15..ca45e10 100644 --- a/tests/apps/test_coco_merge.py +++ b/tests/apps/test_coco_merge.py @@ -1,5 +1,6 @@ import json +from mock import patch, ANY from pyodi.apps.coco.coco_merge import coco_merge @@ -56,3 +57,37 @@ def test_coco_merge(tmp_path): range(len(data["annotations"])) ) assert [i["category_id"] for i in data["annotations"]] == [1, 2, 1, 3, 3, 1] + + +def test_coco_merge_with_json_indent(tmp_path): + images = [{"id": 0, "file_name": "0.jpg"}] + anns1 = [{"image_id": 0, "category_id": 0, "id": 0}] + anns2 = [{"image_id": 0, "category_id": 1, "id": 0}] + categories = [{"id": 0, "name": "excavator"}, {"id": 1, "name": "bus"}] + + coco1 = dict(images=images, annotations=anns1, categories=categories) + coco2 = dict(images=images, annotations=anns2, categories=categories) + + tmp_files = [] + for i, coco_data in enumerate([coco1, coco2]): + tmp_files.append(tmp_path / f"{i}.json") + with open(tmp_files[-1], "w") as f: + json.dump(coco_data, f) + + with patch("json.dump") as mock: + coco_merge( + input_extend=tmp_path / "0.json", + input_add=tmp_path / "1.json", + output_file=tmp_path / "result.json", + indent=None, + ) + mock.assert_called_once_with(ANY, ANY, indent=None) + + with patch("json.dump") as mock: + coco_merge( + input_extend=tmp_path / "0.json", + input_add=tmp_path / "1.json", + output_file=tmp_path / "result.json", + indent=2, + ) + mock.assert_called_once_with(ANY, ANY, indent=2) From 1b325941166561ca158e26a3859bf46f64417f91 Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 21:43:19 +0100 Subject: [PATCH 5/7] fix linter error --- pyodi/apps/coco/coco_merge.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pyodi/apps/coco/coco_merge.py b/pyodi/apps/coco/coco_merge.py index ebfb29d..d62c07c 100644 --- a/pyodi/apps/coco/coco_merge.py +++ b/pyodi/apps/coco/coco_merge.py @@ -23,10 +23,7 @@ @logger.catch(reraise=True) def coco_merge( - input_extend: str, - input_add: str, - output_file: str, - indent: Optional[int] = None, + input_extend: str, input_add: str, output_file: str, indent: Optional[int] = None, ) -> str: """Merge COCO annotation files. From 5b0bcfb25d67b86e0f4d325e65d0bdf243b50ffb Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 21:43:41 +0100 Subject: [PATCH 6/7] parametrize test to remove duplicate code --- tests/apps/test_coco_merge.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/tests/apps/test_coco_merge.py b/tests/apps/test_coco_merge.py index ca45e10..2e6b352 100644 --- a/tests/apps/test_coco_merge.py +++ b/tests/apps/test_coco_merge.py @@ -1,6 +1,8 @@ import json -from mock import patch, ANY +import pytest +from mock import ANY, patch + from pyodi.apps.coco.coco_merge import coco_merge @@ -59,7 +61,8 @@ def test_coco_merge(tmp_path): assert [i["category_id"] for i in data["annotations"]] == [1, 2, 1, 3, 3, 1] -def test_coco_merge_with_json_indent(tmp_path): +@pytest.mark.parametrize("indent", [None, 2]) +def test_coco_merge_with_json_indent(tmp_path, indent): images = [{"id": 0, "file_name": "0.jpg"}] anns1 = [{"image_id": 0, "category_id": 0, "id": 0}] anns2 = [{"image_id": 0, "category_id": 1, "id": 0}] @@ -79,15 +82,6 @@ def test_coco_merge_with_json_indent(tmp_path): input_extend=tmp_path / "0.json", input_add=tmp_path / "1.json", output_file=tmp_path / "result.json", - indent=None, - ) - mock.assert_called_once_with(ANY, ANY, indent=None) - - with patch("json.dump") as mock: - coco_merge( - input_extend=tmp_path / "0.json", - input_add=tmp_path / "1.json", - output_file=tmp_path / "result.json", - indent=2, + indent=indent, ) - mock.assert_called_once_with(ANY, ANY, indent=2) + mock.assert_called_once_with(ANY, ANY, indent=indent) From cce6d450c81bf2e4c6699e49934a054d66ba1ecf Mon Sep 17 00:00:00 2001 From: philenius Date: Mon, 13 Dec 2021 21:48:24 +0100 Subject: [PATCH 7/7] add mock v4.0.3 for testing --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 023f999..c6fe274 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,7 @@ dev = mkdocs==1.1.2 mkdocstrings==0.14.0 mkdocs-material==6.2.8 + mock==4.0.3 mypy==0.800 pre-commit==2.10.1 pydocstyle==5.1.1