import os
from typing import Any
from .._utils import union
[docs]
class Token:
'''
The Token class is the base class which carries important information into Data objects for data
parsing functions. Subclasses of this class may have specific requirements for content.
All implementations of the Token class should not be static but also not use self, for
compatibility reasons (may be changed in the future)
'''
def __init__(self) -> None:
pass
[docs]
def verify_token(self, token: Any) -> bool:
'''
Checks whether the token is in valid format in accordance with the identifier.
- `token`: the token to check
'''
return token != ''
[docs]
class RedundantToken(Token):
'''
RedundantToken items are used for when a data item stores multiple values of itself per image
or unique item. Cases like these include multiple bounding boxes or segmentation objects.
'''
[docs]
class UniqueToken(Token):
'''
UniqueToken items are used when an identifier is a unique item pertaining to any property of an
image or entry. Unique tokens serve as valid IDs for identifying each data entry in the dataset.
'''
[docs]
class WildcardToken(Token):
'''
The WildcardToken class represents a generic wildcard which can stand for anything and will not
be used for any identifiers. The key difference is that these tokens are not affected by merge
operations.
'''
[docs]
class WildcardWordToken(WildcardToken):
'''
Disallows spaces in the wildcard.
'''
[docs]
def verify_token(self, token: str) -> bool:
return super().verify_token(token) and any(filter(str.isspace, token))
[docs]
class FilenameToken(UniqueToken):
'''
The FilenameToken class is a Token which checks for valid absolute filenames.
'''
[docs]
def verify_token(self, token: Any) -> bool:
return super().verify_token(token) and os.path.exists(token)
[docs]
class IDToken(Token):
'''
Represents an ID. Items must be integers.
'''
[docs]
def verify_token(self, token: Any) -> bool:
return isinstance(token, int)
[docs]
class WildcardIntToken(IDToken, WildcardToken):
'''
Wildcards for only integers.
'''
[docs]
class QuantityToken(Token):
'''
Represents a numeric quantity. Can be int or float.
'''
[docs]
def verify_token(self, token: Any) -> bool:
return isinstance(token, (int, float))
[docs]
class WildcardQuantityToken(QuantityToken, WildcardToken):
'''
Wildcards for only quantities.
'''
[docs]
class RedundantQuantityToken(QuantityToken, RedundantToken):
'''
Represents a redundant numeric quantity.
'''
[docs]
def verify_token(self, token: Any) -> bool:
return isinstance(token, list) and all(isinstance(x, (float, int)) for x in token)
[docs]
class RedundantIDToken(IDToken, RedundantToken):
'''
Represents a redundant ID.
'''
[docs]
def verify_token(self, token: Any) -> bool:
return isinstance(token, list) and all(isinstance(x, int) for x in token)
[docs]
class RedundantObjectToken(RedundantToken):
'''
Represents a segmentation object.
'''
[docs]
def verify_token(self, token: Any) -> bool:
return (
isinstance(token, list) and
all(
isinstance(x, list) and
all(
isinstance(y, tuple) and
isinstance(y[0], float) and
isinstance(y[1], float)
for y in x
)
for x in token
)
)
[docs]
class UniqueIDToken(IDToken, UniqueToken):
'''
Represents a unique ID.
'''