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

[PLA-672][external] Support for importing simple_table annotations #781

Merged
merged 2 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions darwin/datatypes.py
Expand Up @@ -998,6 +998,51 @@ def make_table(
)


def make_simple_table(
class_name: str,
bounding_box: BoundingBox,
col_offsets: List[float],
row_offsets: List[float],
subs: Optional[List[SubAnnotation]] = None,
slot_names: Optional[List[str]] = None,
) -> Annotation:
"""
Creates and returns a simple table annotation.

Parameters
----------
class_name : str
The name of the class for this ``Annotation``.

bounding_box : BoundingBox
Bounding box that wraps around the table.

col_offsets : List[float]
List of floats representing the column offsets.

row_offsets : List[float]
List of floats representing the row offsets.

subs : Optional[List[SubAnnotation]], default: None
List of ``SubAnnotation``\\s for this ``Annotation``.

Returns
-------
Annotation
A simple table ``Annotation``.
"""
return Annotation(
AnnotationClass(class_name, "simple_table"),
{
"bounding_box": bounding_box,
"col_offsets": col_offsets,
"row_offsets": row_offsets,
},
subs or [],
slot_names=slot_names or [],
)


def make_string(
class_name: str,
sources: List[Dict[str, UnknownType]],
Expand Down
12 changes: 7 additions & 5 deletions darwin/importer/importer.py
Expand Up @@ -92,6 +92,7 @@ def build_main_annotations_lookup_table(
"tag",
"string",
"table",
"simple_table",
"graph",
"mask",
"raster_layer",
Expand Down Expand Up @@ -280,7 +281,9 @@ def _get_team_properties_annotation_lookup(client):
team_properties = client.get_team_properties()

# (property-name, annotation_class_id): FullProperty object
team_properties_annotation_lookup: Dict[Tuple[str, Optional[int]], FullProperty] = {}
team_properties_annotation_lookup: Dict[
Tuple[str, Optional[int]], FullProperty
] = {}
for prop in team_properties:
team_properties_annotation_lookup[(prop.name, prop.annotation_class_id)] = prop

Expand Down Expand Up @@ -425,7 +428,6 @@ def _import_properties(
a_prop.name,
annotation_class_id,
) not in team_properties_annotation_lookup:

# check if fullproperty exists in create_properties
for full_property in create_properties:
if (
Expand Down Expand Up @@ -893,9 +895,9 @@ def import_annotations( # noqa: C901

# Need to re parse the files since we didn't save the annotations in memory
for local_path in set(local_file.path for local_file in local_files): # noqa: C401
imported_files: Union[List[dt.AnnotationFile], dt.AnnotationFile, None] = (
importer(local_path)
)
imported_files: Union[
List[dt.AnnotationFile], dt.AnnotationFile, None
] = importer(local_path)
if imported_files is None:
parsed_files = []
elif not isinstance(imported_files, List):
Expand Down
8 changes: 8 additions & 0 deletions darwin/utils/utils.py
Expand Up @@ -823,6 +823,14 @@ def _parse_darwin_annotation(annotation: Dict[str, Any]) -> Optional[dt.Annotati
annotation["table"]["cells"],
slot_names=slot_names,
)
elif "simple_table" in annotation:
main_annotation = dt.make_simple_table(
name,
annotation["simple_table"]["bounding_box"],
annotation["simple_table"]["col_offsets"],
annotation["simple_table"]["row_offsets"],
slot_names=slot_names,
)
elif "string" in annotation:
main_annotation = dt.make_string(
name, annotation["string"]["sources"], slot_names=slot_names
Expand Down