Skip to content

Commit fef0483

Browse files
authored
Merge pull request #33 from michelin/BugFix---Fix-bug-when-adding-annotations
Bug fix fix bug when adding annotations
2 parents 737b548 + d447f31 commit fef0483

File tree

6 files changed

+90
-20
lines changed

6 files changed

+90
-20
lines changed

examples/object_classes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ img_pydantic = Image.pydantic()
4848

4949
# Now img_pydantic is a valid pydantic object will can be used to serialize the object
5050

51-
img_dict = img_pydantic.dict()
51+
img_dict = img_pydantic.model_dump()
5252
```
5353

5454
## Annotation class

src/yarrow/yarrow.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,18 @@ def save_to_file(
323323
324324
:param fp: File path to save the dataset to
325325
:type fp: _type_
326-
:param exclude_unset: Exclude unset keys you should not write what you don't use, defaults to True
327-
:type exclude_unset: bool, optional
326+
:param exclude_unset: Exclude unset keys you should not write what you don't use, defaults to False
327+
:type exclude_unset: bool, optional False
328+
:param exclude_none: (bool) Exclude none keys you should not write what you don't use, defaults to True
329+
:type exclude_none: bool, optional True
328330
:param indent: Number of indents in the json file, defaults to 4
329331
:type indent: int, optional
330332
:param default: default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError, defaults to str
331333
:type default: obj, optional
332334
"""
333335
with open(fp, "w") as fp:
334336
json.dump(
335-
self.dict(
337+
self.model_dump(
336338
exclude_unset=exclude_unset, exclude_none=exclude_none, **kwargs
337339
),
338340
fp,

src/yarrow/yarrow_cls.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
>>> img = Image(...)
88
img_pydantic = img.pydantic() # you have a Image_pydantic instance
9-
img_as_dict = img_pydantic.dict() # now you have a dict
9+
img_as_dict = img_pydantic.model_dump() # now you have a dict
1010
1111
"""
1212
from copy import copy
@@ -211,13 +211,15 @@ def __hash__(self) -> int:
211211

212212
def __eq__(self, other) -> bool:
213213
if isinstance(other, Annotation):
214+
214215
return all(
215216
(
216217
self.name == other.name,
217218
set(self.images) == set(other.images),
218219
self.contributor == other.contributor,
219220
set(self.categories) == set(other.categories),
220-
self.polygon == other.polygon,
221+
np.asarray(self.polygon).shape == np.asarray(other.polygon).shape
222+
and self.polygon == other.polygon,
221223
self.polyline == other.polyline,
222224
self.mask == other.mask,
223225
self.bbox == other.bbox,
@@ -704,7 +706,7 @@ def from_yarrow(cls, yarrow: YarrowDataset_pydantic) -> "YarrowDataset":
704706
# img_list = []
705707
img_id_dict = {}
706708
for img in yarrow.images:
707-
img_param = img.dict()
709+
img_param = img.model_dump()
708710

709711
# Get confidential from its id
710712
conf = next(

tests/test_cls.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def new_contributor():
3333
@pytest.fixture
3434
def new_image(new_clearance: Clearance):
3535
new_image_pydantic = rand_image(conf_id=new_clearance.id)
36-
return Image(confidential=new_clearance, **new_image_pydantic.dict())
36+
return Image(confidential=new_clearance, **new_image_pydantic.model_dump())
3737

3838

3939
@pytest.fixture
@@ -45,7 +45,7 @@ def new_annotation(
4545
images=[new_image],
4646
categories=[new_category],
4747
contributor=new_contributor,
48-
**new_annotation_pydantic.dict()
48+
**new_annotation_pydantic.model_dump()
4949
)
5050

5151

@@ -216,7 +216,7 @@ def test_parse_with_null_values(yar_dataset_pydantic: YarrowDataset_pydantic):
216216
"""
217217

218218
# First test is with keys completely removed
219-
primary_dict = yar_dataset_pydantic.dict()
219+
primary_dict = yar_dataset_pydantic.model_dump()
220220
primary_dict.pop("annotations")
221221
primary_dict.pop("contributors")
222222
primary_dict.pop("categories")
@@ -228,7 +228,7 @@ def test_parse_with_null_values(yar_dataset_pydantic: YarrowDataset_pydantic):
228228
assert isinstance(yar_dataset, YarrowDataset)
229229

230230
# Second test if values set to None
231-
second_dict = yar_dataset_pydantic.dict()
231+
second_dict = yar_dataset_pydantic.model_dump()
232232
second_dict["annotations"] = None
233233
second_dict["contributors"] = None
234234
second_dict["categories"] = None

tests/test_creation.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from datetime import datetime
2+
3+
import numpy as np
4+
import pytest
5+
6+
from yarrow import Annotation, Contributor, Image, Info, YarrowDataset
7+
8+
9+
@pytest.fixture
10+
def contributor_base():
11+
return Contributor(name="Jean Claude Vandamme", human=True)
12+
13+
14+
@pytest.fixture
15+
def yarrow_base(contributor_base):
16+
yarrow = YarrowDataset(
17+
info=Info(date_created=datetime.now(), source="Bruxelles"),
18+
images=[
19+
Image(
20+
file_name="image1.jpg",
21+
height=100,
22+
width=100,
23+
date_captured=datetime.now(),
24+
),
25+
Image(
26+
file_name="image2.jpg",
27+
height=100,
28+
width=100,
29+
date_captured=datetime.now(),
30+
),
31+
],
32+
contributors=[contributor_base],
33+
)
34+
return yarrow
35+
36+
37+
def test_create_multiple_annotations_with_same_attributes_expected_polygon_should_works(
38+
yarrow_base: YarrowDataset, contributor_base: Contributor
39+
):
40+
"""In this test we make sure adding annotations with different polygon shape is working.
41+
42+
Args:
43+
yarrow (YarrowDataset): _description_
44+
contributor (Contributor): _description_
45+
"""
46+
# Given
47+
annotation_shape_23 = Annotation(
48+
contributor_base,
49+
name="chaussure",
50+
images=yarrow_base.images,
51+
polygon=np.zeros((23, 2)),
52+
)
53+
annotation_shape_10 = Annotation(
54+
contributor_base,
55+
name="chaussure",
56+
images=yarrow_base.images,
57+
polygon=np.zeros((10, 2)),
58+
)
59+
list_annotations = [annotation_shape_23, annotation_shape_10]
60+
61+
# When
62+
yarrow_base.add_annotations(list_annotations)
63+
64+
# Then
65+
assert np.asarray(yarrow_base.annotations[0].polygon).shape[0] == 23
66+
assert np.asarray(yarrow_base.annotations[1].polygon).shape[0] == 10

tests/test_init.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,19 @@ class MetaExample(BaseModel):
9191

9292
meta_ex = MetaExample()
9393

94-
image_base = rand_image().dict(exclude_unset=True)
94+
image_base = rand_image().model_dump(exclude_unset=True)
9595
image_base["meta"] = meta_ex.json()
9696
image_meta = Image_pydantic(**image_base)
97-
assert image_meta.meta == meta_ex.dict()
97+
assert image_meta.meta == meta_ex.model_dump()
9898

99-
image_base2 = rand_image().dict(exclude_unset=True)
100-
image_base2["meta"] = meta_ex.dict()
99+
image_base2 = rand_image().model_dump(exclude_unset=True)
100+
image_base2["meta"] = meta_ex.model_dump()
101101
image_meta2 = Image_pydantic(**image_base2)
102-
assert image_meta2.meta == meta_ex.dict()
102+
assert image_meta2.meta == meta_ex.model_dump()
103103

104-
image_base3 = rand_image().dict(exclude_unset=True)
104+
image_base3 = rand_image().model_dump(exclude_unset=True)
105105
image_base3["meta"] = str(
106-
meta_ex.dict()
106+
meta_ex.model_dump()
107107
) # putting the dict output in a str should not be sufficient
108108

109109
with pytest.raises(ValidationError):
@@ -117,8 +117,8 @@ def test_annot_list_init():
117117
annot.image_id = [uuid4().hex for _ in range(5)]
118118

119119
assert isinstance(
120-
Annotation_pydantic(**annot.dict(exclude_unset=True)), Annotation_pydantic
120+
Annotation_pydantic(**annot.model_dump(exclude_unset=True)), Annotation_pydantic
121121
)
122-
new_annot = Annotation_pydantic(**annot.dict(exclude_unset=True))
122+
new_annot = Annotation_pydantic(**annot.model_dump(exclude_unset=True))
123123
assert new_annot.category_id == annot.category_id
124124
assert new_annot.image_id == annot.image_id

0 commit comments

Comments
 (0)