Skip to content

Commit a9998d6

Browse files
committed
Getter and Setter methods for attribute data parameters (ImageAnnotation)
1 parent 1d980d2 commit a9998d6

File tree

4 files changed

+54
-26
lines changed

4 files changed

+54
-26
lines changed

forte/common/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
# The index storing entry type in the internal entry data of DataStore.
1212
ENTRY_TYPE_INDEX = 3
1313

14+
# The index storing the payload ID in internal entry data of DataStore
15+
PAYLOAD_INDEX = 0
16+
1417
# The index storing entry type (specific to Link and Group type). It is saved
1518
# in the `tid_idx_dict` in DataStore.
1619
ENTRY_DICT_TYPE_INDEX = 0

forte/data/ontology/top.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
PARENT_TID_INDEX,
3333
CHILD_TID_INDEX,
3434
MEMBER_TID_INDEX,
35+
PAYLOAD_INDEX,
3536
)
3637

3738
__all__ = [
@@ -60,8 +61,15 @@
6061
make sure it available across the ontology system:
6162
1. Create a new top level class that inherits from `Entry` or `MultiEntry`
6263
2. Add the new class to `SinglePackEntries` or `MultiPackEntries`
63-
3. Register a new method in `DataStore`: `add_<new_entry>_raw()`
64-
4. Insert a new conditional branch in `EntryConverter.save_entry_object()`
64+
3. Insert a new conditional branch in `EntryConverter.save_entry_object()`
65+
4. Decide two main attributes which will qualify as your `attribute_data`
66+
parameters. These parameters will be passes in your branch of
67+
`EntryConverter.save_entry_object()`. If there are no such parameters,
68+
you can pass None
69+
5. add `getter` and `setter` functions to update `attribute_data` parameters
70+
if you have any
71+
6. If additional attributes are required, make the class a `dataclass` and set
72+
`dataclass` attributes.
6573
"""
6674

6775

@@ -1032,6 +1040,28 @@ def __init__(self, pack: PackType, image_payload_idx: int = 0):
10321040
else:
10331041
self._image_payload_idx = image_payload_idx
10341042

1043+
@property
1044+
def image_payload_idx(self):
1045+
r"""Getter function of ``image_payload_idx``. The function will first try to
1046+
retrieve the image_payload_idx index from ``DataStore`` in ``self.pack``. If
1047+
this attempt fails, it will directly return the value in ``_image_payload_idx``.
1048+
"""
1049+
try:
1050+
self._image_payload_idx = self.pack.get_entry_raw(self.tid)[
1051+
PAYLOAD_INDEX
1052+
]
1053+
except KeyError:
1054+
pass
1055+
return self._image_payload_idx
1056+
1057+
@image_payload_idx.setter
1058+
def image_payload_idx(self, val: int):
1059+
r"""Setter function of ``image_payload_idx``. The update will also be populated
1060+
into ``DataStore`` in ``self.pack``.
1061+
"""
1062+
self._image_payload_idx = val
1063+
self.pack.get_entry_raw(self.tid)[PAYLOAD_INDEX] = val
1064+
10351065
def compute_iou(self, other) -> int:
10361066
intersection = np.sum(np.logical_and(self.image, other.image))
10371067
union = np.sum(np.logical_or(self.image, other.image))

tests/forte/data/data_store_test.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,6 @@ def setUp(self) -> None:
155155
},
156156
}
157157

158-
self.base_type_attributes = {
159-
"forte.data.ontology.top.Generics": {"parent_class": {"Entry"}},
160-
"forte.data.ontology.top.Annotation": {"parent_class": {"Entry"}},
161-
"forte.data.ontology.top.Link": {"parent_class": {"BaseLink"}},
162-
"forte.data.ontology.top.Group": {
163-
"parent_class": {"Entry", "BaseGroup"}
164-
},
165-
"forte.data.ontology.top.MultiPackGeneric": {
166-
"parent_class": {"Entry", "MultiEntry"}
167-
},
168-
"forte.data.ontology.top.MultiPackLink": {
169-
"parent_class": {"MultiEntry", "BaseLink"}
170-
},
171-
"forte.data.ontology.top.MultiPackGroup": {
172-
"parent_class": {"Entry", "MultiEntry", "BaseGroup"}
173-
},
174-
"forte.data.ontology.top.Query": {"parent_class": {"Generics"}},
175-
"forte.data.ontology.top.AudioAnnotation": {
176-
"parent_class": {"Entry"}
177-
},
178-
"forte.data.ontology.top.BoundingBox": {"parent_class": {"Box"}},
179-
}
180-
181158
DataStore._type_attributes["ft.onto.base_ontology.Document"] = {
182159
"attributes": {
183160
"document_class": 4,
@@ -1124,7 +1101,7 @@ def test_get(self):
11241101
"forte.data.ontology.top.Annotation", range_annotation=(1, 20)
11251102
)
11261103
)
1127-
self.assertEqual(len(instances), 2)
1104+
self.assertEqual(len(instances), 4)
11281105

11291106
# get groups with subclasses
11301107
instances = list(self.data_store.get("forte.data.ontology.top.Group"))

tests/forte/image_annotation_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from sortedcontainers import SortedList
2424
from forte.data.ontology.top import ImageAnnotation, BoundingBox, Box, Region
2525
from forte.data.data_pack import DataPack
26+
from forte.common import constants
2627

2728

2829
class ImageAnnotationTest(unittest.TestCase):
@@ -108,8 +109,10 @@ def setUp(self):
108109
width=2,
109110
image_payload_idx=0,
110111
)
112+
self.datapack_2.add_entry(self.box1)
111113

112114
self.region1 = Region(pack=self.datapack_2, image_payload_idx=1)
115+
self.datapack_2.add_entry(self.region1)
113116

114117
def test_entry_methods(self):
115118
bb_type = "forte.data.ontology.top.BoundingBox"
@@ -191,6 +194,21 @@ def test_update_image_annotation(self):
191194
# Check new value
192195
self.assertEqual(bb1_height, 5)
193196

197+
# Updating Non-Dataclass fields
198+
199+
# Check current value
200+
self.assertEqual(self.bb4.image_payload_idx, 1)
201+
202+
# Change a parameter of the entry object
203+
self.bb4.image_payload_idx = 2
204+
205+
# Fetch attribute value from data store
206+
bb4_payload = self.datapack_1._data_store.get_entry(
207+
self.bb4.tid
208+
)[0][constants.PAYLOAD_INDEX]
209+
# Check new value
210+
self.assertEqual(bb4_payload, 2)
211+
194212
def test_compute_iou(self):
195213
box1 = self.bb1
196214
box2 = self.bb2

0 commit comments

Comments
 (0)