From 1305f8a1c4089aae5af6e7374eb681d48b3d4310 Mon Sep 17 00:00:00 2001 From: jingpengw Date: Thu, 2 Dec 2021 11:06:58 -0500 Subject: [PATCH] fix Cartesian bugs and unit tests --- chunkflow/lib/bounding_boxes.py | 11 +++++++---- chunkflow/lib/synapses.py | 17 +++++++++++++---- tests/lib/test_bounding_box.py | 14 ++++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/chunkflow/lib/bounding_boxes.py b/chunkflow/lib/bounding_boxes.py index 7b5621e7..3a07c50e 100644 --- a/chunkflow/lib/bounding_boxes.py +++ b/chunkflow/lib/bounding_boxes.py @@ -10,7 +10,6 @@ import numpy as np import h5py -from numpy.lib.arraysetops import isin from cloudvolume import CloudVolume from cloudvolume.lib import Vec, Bbox @@ -37,8 +36,12 @@ def __add__(self, offset: Union[Cartesian, int]): offset (Cartesian, int): offset """ if isinstance(offset, int): - offset = (offset, offset, offset) - return Cartesian(*[x+o for x, o in zip(self, offset)]) + return Cartesian(*[x+offset for x in self]) + else: + return Cartesian(*[x+o for x, o in zip(self, offset)]) + + def __floordiv__(self, d: int): + return Cartesian(*[x // d for x in self]) @property def vec(self): @@ -87,7 +90,7 @@ def from_center(cls, center: Cartesian, extent: int): extent (int): the range to extent, like radius """ minpt = center - extent - maxpt = center - extent + maxpt = center + extent return cls.from_corners(minpt, maxpt) diff --git a/chunkflow/lib/synapses.py b/chunkflow/lib/synapses.py index 0dd921c0..ce66fd57 100644 --- a/chunkflow/lib/synapses.py +++ b/chunkflow/lib/synapses.py @@ -164,17 +164,26 @@ def from_file(cls, fname: str, resolution: tuple = None, c_order: bool = True): return cls.from_h5(fname, resolution=resolution, c_order=c_order) else: raise ValueError(f'only support JSON and HDF5 file, but got {fname}') - + + @property + def post_coordinates(self) -> np.ndarray: + """the coordinate array + + Returns: + np.ndarray: the array of coordinates. for each row, z,y,x + """ + return self.post[:, 1:] + @property - def pre_num(self): + def pre_num(self) -> int: return self.pre.shape[0] @property - def post_num(self): + def post_num(self) -> int: return self.post.shape[0] @property - def pre_with_physical_coordinate(self): + def pre_with_physical_coordinate(self) -> np.ndarray: if self.resolution is not None: return self.pre * self.resolution else: diff --git a/tests/lib/test_bounding_box.py b/tests/lib/test_bounding_box.py index 5aed613a..e83edb2e 100644 --- a/tests/lib/test_bounding_box.py +++ b/tests/lib/test_bounding_box.py @@ -1,3 +1,5 @@ +import numpy as np + from cloudvolume.lib import Bbox, Vec from chunkflow.lib.bounding_boxes import BoundingBox, Cartesian @@ -6,12 +8,16 @@ def test_cartesian(): ct = Cartesian(1,2,3) ct += 2 - ct == Cartesian(3,4,5) + assert ct == Cartesian(3,4,5) ct -= 2 - ct == Cartesian(1,2,3) + assert ct == Cartesian(1,2,3) + + np.testing.assert_equal(ct.vec, Vec(1,2,3)) - ct.vec == Vec(1,2,3) + ct = Cartesian(3,4,5) + ct = ct // 2 + assert ct == Cartesian(1,2,2) def test_bounding_box(): bbox = Bbox.from_delta((1,3,2), (64, 32, 8)) @@ -25,6 +31,6 @@ def test_bounding_box(): bbox = BoundingBox.from_corners(minpt, maxpt) bbox = BoundingBox.from_center(Cartesian(1,2,3), 3) - bbox == BoundingBox.from_list([-2, -1, 0, 4, 5, 6]) + assert bbox == BoundingBox.from_list([-2, -1, 0, 4, 5, 6]) \ No newline at end of file