Skip to content

Commit

Permalink
add annotationarray.from_array_infer_attrs method
Browse files Browse the repository at this point in the history
  • Loading branch information
d-v-b committed Aug 14, 2024
1 parent a133a64 commit a5f2241
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/cellmap_schemas/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TypeVar,
)
import numpy as np
import numpy.typing as npt
from typing_extensions import Self
from pydantic_zarr.v2 import GroupSpec, ArraySpec
from pydantic import BaseModel, model_validator, field_serializer
Expand Down Expand Up @@ -266,15 +267,15 @@ def check_encoding(self: "AnnotationArrayAttrs"):
@classmethod
def from_array(
cls,
data: np.ndarray,
array: np.ndarray,
class_name: TName,
annotation_type: AnnotationType,
complement_counts: None | dict[Possibility, int] | Literal["auto"],
) -> Self:
if complement_counts == "auto":
num_unknown = sum(data == annotation_type.encoding["unknown"])
num_absent = sum(data == annotation_type.encoding["absent"])
num_present = data.size - (num_unknown + num_absent)
num_unknown = sum(array == annotation_type.encoding["unknown"])
num_absent = sum(array == annotation_type.encoding["absent"])
num_present = array.size - (num_unknown + num_absent)

if isinstance(annotation_type, SemanticSegmentation):
complement_counts_parsed = {"unknown": num_unknown, "absent": num_absent}
Expand Down Expand Up @@ -392,6 +393,21 @@ class AnnotationArray(ArraySpec):

attributes: CellmapWrapper[AnnotationWrapper[AnnotationArrayAttrs]]

@classmethod
def from_array_infer_attrs(
cls,
array: npt.NDArray[Any],
class_name: TName,
annotation_type: AnnotationType,
complement_counts: None | dict[Possibility, int] | Literal["auto"],
**kwargs,
) -> Self:
annotation_attrs = AnnotationArrayAttrs.from_array(
array, class_name, annotation_type, complement_counts
)
annotation_attrs_wrapped = wrap_attributes(annotation_attrs)
return super().from_array(array, attributes=annotation_attrs_wrapped, **kwargs)


class AnnotationGroup(GroupSpec):
"""
Expand Down

0 comments on commit a5f2241

Please sign in to comment.