Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve COCO parsing error messages #684

Merged
merged 14 commits into from
Mar 23, 2022
Merged

Improve COCO parsing error messages #684

merged 14 commits into from
Mar 23, 2022

Conversation

zhiltsov-max
Copy link
Contributor

@zhiltsov-max zhiltsov-max commented Mar 18, 2022

Summary

  • Extended dataset parsing error classification
  • Added default string conversion for parsing errors
  • Added COCO json parsing errors
  • Added tests

How to test

Checklist

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.
  • I have updated the license header for each file (see an example below)
# Copyright (C) 2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

@zhiltsov-max zhiltsov-max force-pushed the zm/coco-parsing-errors branch 2 times, most recently from 21b7006 to 20703c5 Compare March 18, 2022 17:33
datumaro/components/errors.py Outdated Show resolved Hide resolved
datumaro/components/errors.py Outdated Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Outdated Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Outdated Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Outdated Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Outdated Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Outdated Show resolved Hide resolved
datumaro/plugins/coco_format/extractor.py Outdated Show resolved Hide resolved
datumaro/components/errors.py Outdated Show resolved Hide resolved
Comment on lines 863 to 874
anns = {
"images": [
{
"id": 5,
"width": 10,
"height": 5,
"file_name": "a.jpg",
}
],
"annotations": [],
"categories": [],
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these tests are very similar; I think it would be more readable to factor out the common parts into a single function that takes one parameter (a function that mangles the dataset) and returns the resulting exception. Then the individual tests could just look like this:

def mangle(anns):
    del anns["images"][0][field]

exc = self._test_mangled_annotations(mangle)
self.assertIsInstance(capture.exception.__cause__, MissingFieldError)
self.assertEqual(capture.exception.__cause__.name, field)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree about the common part extraction, I'll check if it's possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You extracted a shared annotation template (which is certainly a big improvement), but there's still a lot of duplication in the structure of the tests. How about factoring that out too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you see that?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in my code snippet above. In full it would look something like this:

    def _load_mangled_annotations(self, mangle):
        with TestDir() as test_dir:
            ann_path = osp.join(test_dir, "ann.json")
            anns = mangle(deepcopy(self.ANNOTATION_JSON_TEMPLATE))
            dump_json_file(ann_path, anns)

            with self.assertRaises(ItemImportError) as capture:
                Dataset.import_from(ann_path, "coco_instances")
                    
            return capture

    def test_can_report_missing_item_field(self):
        for field in ["id", "file_name"]:
            with self.subTest(field=field):
                def mangle(anns):
                    anns["images"][0].pop(field)

                exc = self._load_mangled_annotations(mangle)
                self.assertIsInstance(capture.exception.__cause__, MissingFieldError)
                self.assertEqual(capture.exception.__cause__.name, field)

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but it seem to save 1 line of code per test. It also requires to catch DatasetImportError and check the type and message in the test.

tests/test_coco_format.py Show resolved Hide resolved
@zhiltsov-max zhiltsov-max mentioned this pull request Mar 21, 2022
7 tasks
@@ -836,6 +848,205 @@ def test_can_pickle(self):
compare_datasets_strict(self, source, parsed)


class CocoExtractorTests(TestCase):
ANNOTATION_JSON_TEMPLATE = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it would be useful to have a test that loads the unmodified template, to ensure that any errors in the other tests are due to the modifications made in those tests and not due to the template itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the common piece was possible to extract is occasional, it is not something designed or expected. There are lots of tests that cover successful loading.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the common piece was possible to extract is occasional, it is not something designed or expected.

How is that not expected? The point of the new tests is to make sure that specific errors in the dataset cause the extraction to fail. To be certain that it's those errors that cause the failure, we need to ensure that the original template contains no errors.

Copy link
Contributor Author

@zhiltsov-max zhiltsov-max Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is checked by the error details checks. However, I can agree, that such a test could be useful on its own, because there is no such test for just a json parsing.

@IRDonch IRDonch merged commit 543ab1a into develop Mar 23, 2022
@IRDonch IRDonch deleted the zm/coco-parsing-errors branch March 23, 2022 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants