Skip to content

Commit

Permalink
add formats interfaces, fixes #105 #120
Browse files Browse the repository at this point in the history
- add `interface` sub-pacakge with base.py
  + defines `BaseFormat`
- add seq sub-package in `interface` with base.py
  + defines `SeqLike` interface
- add bbox sub-package in `interface` with base.py
  + definfes `BBoxLike` interface
- import BaseFormat and sub-packages in interface/__init__.py
- import inside __init__ in Transcriber to avoid circular imports
  • Loading branch information
NickleDave committed May 1, 2022
1 parent 6a839f8 commit 7c495c2
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/crowsetta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
__version__,
)

# --- need to import everything used by formats before importing formats
from . import interface

from .transcriber import Transcriber
from .segment import Segment
from .sequence import Sequence
from .annotation import Annotation
from .meta import Meta

# ---- ok, now import formats
from . import (
birdsongrec,
formats,
Expand Down
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/crowsetta/interface/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .base import BaseFormat
from .bbox import BBoxLike
from .seq import SeqLike
27 changes: 27 additions & 0 deletions src/crowsetta/interface/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import abc


class BaseFormat(abc.ABC):
"""abstract base class
that declares the interface
for any sub-class that
represents annotations
loaded from a file
in a specific format
for annotating vocalizations.
"""
@classmethod
@abc.abstractmethod
def from_file(cls) -> 'Self':
"""loads an annotation from a file
in a given format.
"""
...

def to_annot(self) -> 'Union[crowsetta.Annotation,List[crowsetta.Annotation]]':
"""converts the instance representing annotations
loaded from a file into a `crowsetta.Annotation`
or a list of `crowsetta.Annotation`s,
that can be used to convert to other formats
"""
...
1 change: 1 addition & 0 deletions src/crowsetta/interface/bbox/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .base import BBoxLike
20 changes: 20 additions & 0 deletions src/crowsetta/interface/bbox/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import abc

from ..base import BaseFormat


class BBoxLike(BaseFormat, abc.ABC):
"""abstract base class defining the interface
for any annotation format
that can be represented as a set of bounding boxes,
where each bounding box has corners
((x_min, y_max), (x_min, y_min), (x_max, y_max), (x_max, y_min))
and a label.
"""
def to_bbox(self) -> 'Union[crowsetta.BBox, Sequence[crowsetta.BBox]]':
"""converts the annotation to
a ``crowsetta.BBox`` instance
or a python set of
``crowsetta.BBox`` instances.
"""
...
1 change: 1 addition & 0 deletions src/crowsetta/interface/seq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .base import SeqLike
20 changes: 20 additions & 0 deletions src/crowsetta/interface/seq/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import abc

from ..base import BaseFormat


class SeqLike(BaseFormat, abc.ABC):
"""abstract base class defining the interface
for any annotation format
that can be represented as a sequence of segments,
with each segment having an onset time,
offset time, and a label.
"""
def to_seq(self) -> 'Union[crowsetta.Sequence, Sequence[crowsetta.Sequence]]':
"""converts the annotation to
a ``crowsetta.Sequence`` instance
or a python sequence of
``crowsetta.Sequence``s
(e.g., a list or tuple).
"""
...
13 changes: 7 additions & 6 deletions src/crowsetta/transcriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
import importlib.util
import warnings

from . import (
formats,
generic
)
from .meta import Meta

HERE = os.path.dirname(__file__)

VALID_CONFIG_DICT_KEYS = {'module', 'from_file', 'to_csv', 'to_format'}
Expand Down Expand Up @@ -58,6 +52,13 @@ def __init__(self, format, config=None):
>>> scribe = crowsetta.Transcriber(format='myformat_name', config=my_config)
>>> seq = scribe.from_file(file='my_annotation.mat')
"""
# avoid circular imports
from . import (
formats,
generic
)
from .meta import Meta

# make sure format specified is either installed or that user also specified a config
if (format in formats._INSTALLED and config is None) or (format and config is not None):
pass
Expand Down

0 comments on commit 7c495c2

Please sign in to comment.