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

Make BoundingBox objects hashable. #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions connectomics/common/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ def copy(self, *args, **kwargs) -> 'MutableArray':
def __str__(self):
return np.ndarray.__repr__(self)

def __hash__(self):
return hash(self.tobytes())


class MutableArray(array_mixins.MutableArrayMixin, ImmutableArray):
"""Strongly typed mutable version of np.ndarray."""
Expand Down
19 changes: 11 additions & 8 deletions connectomics/common/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
BoolSequence = Union[bool, Sequence[bool]]


@dataclasses.dataclass
@dataclasses.dataclass(frozen=True, init=False)
class BoundingBoxBase(Generic[T]):
"""BoundingBox encapsulates start/end coordinate pairs of the same length."""
_start: Tuple[T, ...]
_end: Tuple[T, ...]
_size: Tuple[T, ...]
is_border_start: array.ImmutableArray
is_border_end: array.ImmutableArray

def __init__(
self,
Expand Down Expand Up @@ -87,8 +88,8 @@ def __init__(
else:
size = end - start

self._start = self._tupleize(start)
self._size = self._tupleize(size)
object.__setattr__(self, '_start', self._tupleize(start))
object.__setattr__(self, '_size', self._tupleize(size))

if len(self.start) != len(self.end) or len(self.end) != len(self.start):
raise ValueError(
Expand All @@ -99,17 +100,19 @@ def __init__(
if len(is_border_start) != self.rank:
raise ValueError(
f'is_border_start needs to have exactly {self.rank} items')
self.is_border_start = np.asarray(is_border_start)
is_border_start = np.asarray(is_border_start)
else:
self.is_border_start = np.zeros(self.rank, dtype=bool)
is_border_start = np.zeros(self.rank, dtype=bool)
object.__setattr__(self, 'is_border_start', array.ImmutableArray(is_border_start))

if is_border_end is not None:
if len(is_border_end) != self.rank:
raise ValueError(
f'is_border_end needs to have exactly {self.rank} items')
self.is_border_end = np.asarray(is_border_end)
is_border_end = np.asarray(is_border_end)
else:
self.is_border_end = np.zeros(self.rank, dtype=bool)
is_border_end = np.zeros(self.rank, dtype=bool)
object.__setattr__(self, 'is_border_end', array.ImmutableArray(is_border_end))

def __eq__(self: S, other: S) -> bool:
for k, v in self.__dict__.items():
Expand Down
5 changes: 5 additions & 0 deletions connectomics/common/bounding_box_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ def test_to_slice_float(self):
expected_4d = none_slice * (4 - dim_size) + expected_slice
self.assertEqual(expected_4d, box.to_slice4d())

def test_hashability(self):
b = Box(start=[1, 2, 3], size=[4, 5, 6])
d = {b: 1}
self.assertEqual(d[b], 1)


class GlobalTest(absltest.TestCase):

Expand Down