Skip to content

Commit

Permalink
Merge branch 'main' into T14418_multiprocess_safe
Browse files Browse the repository at this point in the history
  • Loading branch information
AChenQ authored Apr 15, 2021
2 parents 0c17b2d + 76fe65f commit c8d840c
Show file tree
Hide file tree
Showing 28 changed files with 146 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/source/api/client/dataset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ tensorbay.client.dataset
.. automodule:: tensorbay.client.dataset
:members:
:show-inheritance:
:exclude-members:
:exclude-members: name, dataset_id, status
1 change: 1 addition & 0 deletions docs/source/api/client/requests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tensorbay.client.requests
.. automodule:: tensorbay.client.requests
:members:
:show-inheritance:
:exclude-members: is_intern
2 changes: 1 addition & 1 deletion docs/source/api/client/segment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ tensorbay.client.segment
.. automodule:: tensorbay.client.segment
:members:
:show-inheritance:
:exclude-members:
:exclude-members: name, status
1 change: 1 addition & 0 deletions docs/source/api/dataset/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tensorbay.dataset.data
.. automodule:: tensorbay.dataset.data
:members:
:show-inheritance:
:exclude-members: target_remote_path
2 changes: 1 addition & 1 deletion docs/source/api/dataset/dataset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ tensorbay.dataset.dataset
.. automodule:: tensorbay.dataset.dataset
:members:
:show-inheritance:
:exclude-members:
:exclude-members: catalog, notes

3 changes: 3 additions & 0 deletions docs/source/api/exception.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
:orphan:
:nosearch:

############
Exceptions
############
3 changes: 3 additions & 0 deletions docs/source/api/healthcheck/healthcheck_module.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
:orphan:
:nosearch:

tensorbay.healthcheck
=====================

Expand Down
1 change: 1 addition & 0 deletions docs/source/api/label/supports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tensorbay.label.supports
.. automodule:: tensorbay.label.supports
:members:
:show-inheritance:
:exclude-members: number
1 change: 1 addition & 0 deletions docs/source/api/sensor/intrinsics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tensorbay.sensor.intrinsics
.. automodule:: tensorbay.sensor.intrinsics
:members:
:show-inheritance:
:exclude-members: camera_matrix, distortion_coefficients
1 change: 1 addition & 0 deletions docs/source/api/utility/name.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tensorbay.utility.name
.. automodule:: tensorbay.utility.name
:members:
:show-inheritance:
:exclude-members: name
1 change: 1 addition & 0 deletions docs/source/api/utility/tbrn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tensorbay.utility.tbrn
.. automodule:: tensorbay.utility.tbrn
:members:
:show-inheritance:
:exclude-members: dataset_name, segment_name, frame_index, sensor_name, remote_path, type
3 changes: 3 additions & 0 deletions docs/source/community/contribution.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
:orphan:
:nosearch:

##############
Contribution
##############
Expand Down
3 changes: 3 additions & 0 deletions docs/source/community/roadmap.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
:orphan:
:nosearch:

#########
Roadmap
#########
3 changes: 3 additions & 0 deletions docs/source/reference/release_note.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
:orphan:
:nosearch:

##############
Release Note
##############
5 changes: 5 additions & 0 deletions tensorbay/client/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class DatasetClientBase: # pylint: disable=too-many-public-methods
dataset_id: Dataset ID.
gas_client: The initial client to interact between local and TensorBay.
Attributes:
name: Dataset name.
dataset_id: Dataset ID.
status: The status of the dataset client.
"""

_client: Client
Expand Down
5 changes: 1 addition & 4 deletions tensorbay/client/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ def _dump_response(response: Response) -> str:
if "Content-Type" in response.headers:
content_type = response.headers["Content-Type"]
if content_type.startswith("application/json"):
try:
content = json.dumps(response.json(), indent=2)
except json.decoder.JSONDecodeError:
content = ""
content = json.dumps(response.json(), indent=2)
elif content_type.startswith("text"):
content = response.text
elif len(response.content) > 512:
Expand Down
40 changes: 34 additions & 6 deletions tensorbay/client/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from collections import defaultdict
from concurrent.futures import FIRST_EXCEPTION, ThreadPoolExecutor, wait
from itertools import count, repeat, zip_longest
from threading import Lock
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -48,7 +49,12 @@


class Config: # pylint: disable=too-few-public-methods
"""This is a base class defining the concept of Request Config."""
"""This is a base class defining the concept of Request Config.
Attributes:
is_intern: Whether the request is from intern.
"""

def __init__(self) -> None:

Expand Down Expand Up @@ -314,6 +320,7 @@ class PagingList(Sequence[_T], ReprMixin): # pylint: disable=too-many-ancestors
"""

_S = TypeVar("_S", bound="PagingList[_T]")
_K = TypeVar("_K")

_repr_type = ReprType.SEQUENCE

Expand All @@ -325,6 +332,7 @@ def __init__(
) -> None:
self._data: Dict[int, _T] = {}
self._attr: Dict[str, int] = {}
self._locks: DefaultDict[Union[int, str], Lock] = defaultdict(Lock)
self._func = func
self._limit = limit

Expand Down Expand Up @@ -355,12 +363,12 @@ def __getitem__(self: _S, index: Union[int, slice]) -> Union[_T, _S]:

paging_index = self._slice.start + index
if paging_index not in self._data:
self._extend(paging_index)
offset = self._get_offset(paging_index)
self._call_with_lock(offset, self._extend, offset)

return self._data[paging_index]

def _extend(self, index: int) -> int:
offset = index // self._limit * self._limit
def _extend(self, offset: int) -> int:
generator = self._func(offset, self._limit)
try:
for i in count(offset):
Expand All @@ -371,10 +379,14 @@ def _extend(self, index: int) -> int:

raise TypeError("Impossible to be here, add this to make pylint and mypy happy")

def _get_total_count(self, index: int) -> None:
offset = self._get_offset(index)
self._attr["totalCount"] = self._extend(offset)

def _get_len(self, index: int = 0) -> int:
if self._len is None:
if "totalCount" not in self._attr:
self._attr["totalCount"] = self._extend(index)
self._call_with_lock("totalCount", self._get_total_count, index)

total_count = self._attr["totalCount"]
stop = total_count if self._slice.stop is None else min(total_count, self._slice.stop)
Expand All @@ -386,6 +398,21 @@ def _get_len(self, index: int = 0) -> int:
def _make_index_positive(self, index: int) -> int:
return index if index >= 0 else self._get_len() + index

def _get_offset(self, index: int) -> int:
return index // self._limit * self._limit

def _call_with_lock(self, key: Union[int, str], func: Callable[[_K], Any], arg: _K) -> None:
lock = self._locks[key]
acquire = lock.acquire(blocking=False)
try:
if acquire:
func(arg)
del self._locks[key]
else:
lock.acquire()
finally:
lock.release()

def _get_slice(self: _S, input_slice: slice) -> _S:
start = self._slice.start
if input_slice.start is not None:
Expand All @@ -397,9 +424,10 @@ def _get_slice(self: _S, input_slice: slice) -> _S:

paging_list = self.__class__(self._func, self._limit, slice(start, stop))

# The sliced PagingList shares the "_data" and "_attr" with the root PagingList
# The sliced PagingList shares the "_data", "_attr" and "_locks" with the root PagingList
paging_list._data = self._data # pylint: disable=protected-access
paging_list._attr = self._attr # pylint: disable=protected-access
paging_list._locks = self._locks # pylint: disable=protected-access

return paging_list

Expand Down
4 changes: 4 additions & 0 deletions tensorbay/client/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class SegmentClientBase: # pylint: disable=too-many-instance-attributes
name: Segment name.
dataset_client: The dataset client.
Attributes:
name: Segment name.
status: The status of the dataset client.
"""

_EXPIRED_IN_SECOND = 240
Expand Down
1 change: 1 addition & 0 deletions tensorbay/dataset/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Data(DataBase):
path: The file local path.
timestamp: The timestamp for the file.
labels: The :class:`Labels` that contains all the label information of the file.
target_remote_path: The target remote path of the data.
"""

Expand Down
4 changes: 4 additions & 0 deletions tensorbay/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class DatasetBase(NameMixin, Sequence[_T]): # pylint: disable=too-many-ancestor
Arguments:
name: The name of the dataset.
Attributes:
catalog: The :class:`~tensorbay.label.catalog.Catalog` of the dataset.
notes: The :class:`Notes` of the dataset.
"""

_repr_type = ReprType.SEQUENCE
Expand Down
47 changes: 46 additions & 1 deletion tensorbay/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,53 @@
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""TensorBayException."""
"""TensorBay cutoms exceptions.
The class hierarchy for TensorBay custom exceptions is::
+-- :class:`TensorBayException`
+-- :class:`TensorBayOpenDatasetError`
+-- :class:`NoFileError`
+-- :class:`FileStructureError`
"""


class TensorBayException(Exception):
"""This is the base class for TensorBay custom exceptions."""


class TensorBayOpendatasetException(TensorBayException):
"""This is the base class for custom exceptions in TensorBay opendataset module."""


class NoFileError(TensorBayOpendatasetException):
"""This class defines the exception for no matching file found in the opendataset directory.
Arguments:
pattern: Glob pattern.
"""

def __init__(self, pattern: str) -> None:
super().__init__()
self._pattern = pattern

def __str__(self) -> str:
return f'No file follows the giving pattern "{self._pattern}"'


class FileStructureError(TensorBayOpendatasetException):
"""This class defines the exception for incorrect file structure in the opendataset directory.
Arguments:
message: The error message.
"""

def __init__(self, message: str) -> None:
super().__init__()
self._message = message

def __str__(self) -> str:
return self._message
1 change: 1 addition & 0 deletions tensorbay/label/supports.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class KeypointsInfo(ReprMixin, EqMixin):
description: The description of the keypoints.
Attributes:
number: The number of the set of keypoints.
names: All the names of the keypoints.
skeleton: The skeleton of the keypoints
indicating which keypoint should connect with another.
Expand Down
5 changes: 3 additions & 2 deletions tensorbay/opendataset/LISATrafficLight/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re

from ...dataset import Data, Dataset, Segment
from ...exception import FileStructureError
from ...label import Classification, LabeledBox2D
from .._utility import glob

Expand Down Expand Up @@ -64,7 +65,7 @@ def LISATrafficLight(path: str) -> Dataset:
Loaded `Dataset` object.
Raises:
TypeError: When frame number is discontinuous.
FileStructureError: When frame number is discontinuous.
"""
root_path = os.path.abspath(os.path.expanduser(path))
Expand All @@ -87,7 +88,7 @@ def LISATrafficLight(path: str) -> Dataset:

# Check the frame_number from filename: "daySequence1--00345.jpg"
if _get_frame_number(image_paths[-1]) + 1 != len(image_paths):
raise TypeError(f"Discontinuous frame number in '{filedir}'")
raise FileStructureError(f"Discontinuous frame number in '{filedir}'")

for image_path in image_paths:
data = Data(image_path)
Expand Down
22 changes: 6 additions & 16 deletions tensorbay/opendataset/_utility/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""Exceptions about open data loader."""
"""Exceptions about open data loader.
The content in this module is deprecated since v1.3.0, and will be removed in v1.5.0.
class OpenDatasetException(Exception):
"""This is the parent class to all open dataset exceptions."""
Please use :class:`~tensorbay.exception.NoFileError` instead of :class:`OpenDatasetNoFileError`.
"""

class OpenDatasetNoFileError(OpenDatasetException):
"""Exception for no file found in the opendataset directory.
from ...exception import NoFileError

Arguments:
pattern: Glob pattern.
"""

def __init__(self, pattern: str) -> None:
super().__init__()
self._pattern = pattern

def __str__(self) -> str:
return f'No file follows the giving pattern "{self._pattern}"'
OpenDatasetNoFileError = NoFileError
6 changes: 3 additions & 3 deletions tensorbay/opendataset/_utility/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from glob import glob as buildin_glob
from typing import List

from .exceptions import OpenDatasetNoFileError
from ...exception import NoFileError


def glob(pathname: str, *, recursive: bool = False) -> List[str]:
Expand All @@ -27,12 +27,12 @@ def glob(pathname: str, *, recursive: bool = False) -> List[str]:
A sorted list of paths matching a pathname pattern.
Raises:
OpenDatasetNoFileError: When there is no file matching the given pathname pattern.
NoFileError: When there is no file matching the given pathname pattern.
"""
paths = buildin_glob(pathname, recursive=recursive)
if not paths:
raise OpenDatasetNoFileError(pathname)
raise NoFileError(pathname)
paths.sort()

return paths
6 changes: 3 additions & 3 deletions tensorbay/sensor/intrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ class CameraIntrinsics(ReprMixin):
**kwargs: Float values to initialize :class:`DistortionCoefficients`.
Attributes:
_camera_matrix: A 3x3 Sequence of the camera matrix.
_distortion_coefficients: It is the deviation from rectilinear projection. It includes
radial distortion and tangential distortion.
camera_matrix: A 3x3 Sequence of the camera matrix.
distortion_coefficients: It is the deviation from rectilinear projection. It includes
radial distortion and tangential distortion.
Examples:
>>> matrix = [[1, 3, 3],
Expand Down
Loading

0 comments on commit c8d840c

Please sign in to comment.