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

cleanup JSON conversion for all data types #164

Open
wants to merge 2 commits into
base: kylel/2022-10/relation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions mmda/types/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(
self.id = id
self.doc = doc
self.field = field
self.metadata = metadata if metadata else Metadata()
self.metadata = metadata

@abstractmethod
def to_json(self) -> Dict:
Expand Down Expand Up @@ -128,10 +128,11 @@ def to_json(self) -> Dict:
box_group_dict = dict(
boxes=[box.to_json() for box in self.boxes],
id=self.id,
metadata=self.metadata.to_json()
metadata=self.metadata.to_json() if self.metadata else None
)
return {
key: value for key, value in box_group_dict.items() if value
key: value for key, value in box_group_dict.items()
if value is not None
} # only serialize non-null values

@classmethod
Expand All @@ -146,6 +147,8 @@ def from_json(cls, box_group_dict: Dict) -> "BoxGroup":
metadata_dict = {
"type": box_group_dict.get("type", None)
}
metadata_dict = {key: value for key, value in metadata_dict.items() if value is not None}
metadata = Metadata.from_json(metadata_dict) if metadata_dict else None

return cls(
boxes=[
Expand All @@ -155,9 +158,10 @@ def from_json(cls, box_group_dict: Dict) -> "BoxGroup":
for box_dict in box_group_dict.get("boxes", [])
],
id=box_group_dict.get("id", None),
metadata=Metadata.from_json(metadata_dict),
metadata=metadata,
)


def __getitem__(self, key: int):
return self.boxes[key]

Expand All @@ -179,12 +183,15 @@ def __deepcopy__(self, memo):
@property
def type(self) -> str:
logging.warning(msg='`.type` to be deprecated in future versions. Use `.metadata.type`')
return self.metadata.get("type", None)
return self.metadata.get("type", None) if self.metadata else None

@type.setter
def type(self, type: Union[str, None]) -> None:
logging.warning(msg='`.type` to be deprecated in future versions. Use `.metadata.type`')
self.metadata.type = type
if self.metadata:
self.metadata.type = type
else:
self.metadata = Metadata(type=type)


class SpanGroup(Annotation):
Expand Down Expand Up @@ -213,7 +220,7 @@ def to_json(self) -> Dict:
span_group_dict = dict(
spans=[span.to_json() for span in self.spans],
id=self.id,
metadata=self.metadata.to_json(),
metadata=self.metadata.to_json() if self.metadata else None,
box_group=self.box_group.to_json() if self.box_group else None
)
return {
Expand All @@ -240,14 +247,16 @@ def from_json(cls, span_group_dict: Dict) -> "SpanGroup":
"type": span_group_dict.get("type", None),
"text": span_group_dict.get("text", None)
}
metadata_dict = {key: value for key, value in metadata_dict.items() if value is not None}
metadata = Metadata.from_json(metadata_dict) if metadata_dict else None

return cls(
spans=[
Span.from_json(span_dict=span_dict)
for span_dict in span_group_dict["spans"]
],
id=span_group_dict.get("id", None),
metadata=Metadata.from_json(metadata_dict),
metadata=metadata,
box_group=box_group,
)

Expand Down Expand Up @@ -295,23 +304,31 @@ def __deepcopy__(self, memo):
@property
def type(self) -> str:
logging.warning(msg='`.type` to be deprecated in future versions. Use `.metadata.type`')
return self.metadata.get("type", None)
return self.metadata.get("type", None) if self.metadata else None

@type.setter
def type(self, type: Union[str, None]) -> None:
logging.warning(msg='`.type` to be deprecated in future versions. Use `.metadata.type`')
self.metadata.type = type
if self.metadata:
self.metadata.type = type
else:
self.metadata = Metadata(type=type)

@property
def text(self) -> str:
maybe_text = self.metadata.get("text", None)
if maybe_text is None:
return " ".join(self.symbols)
return maybe_text
if self.metadata:
maybe_text = self.metadata.get("text", None)
if maybe_text:
return maybe_text
# default behavior is convenient
return " ".join(self.symbols)

@text.setter
def text(self, text: Union[str, None]) -> None:
self.metadata.text = text
if self.metadata:
self.metadata.text = text
else:
self.metadata = Metadata(text=text)


class Relation(Annotation):
Expand All @@ -338,7 +355,7 @@ def to_json(self) -> Dict:
key=str(self.key.name),
value=str(self.value.name),
id=self.id,
metadata=self.metadata.to_json()
metadata=self.metadata.to_json() if self.metadata else None
)
return {
key: value
Expand Down
36 changes: 0 additions & 36 deletions tests/test_types/test_annotation.py

This file was deleted.

18 changes: 0 additions & 18 deletions tests/test_types/test_box.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/test_types/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ def test__empty_annotations_work(self):
annotations = []
doc.annotate(my_cool_field=annotations)
self.assertEqual(doc.my_cool_field, [])

Loading