Skip to content

Commit

Permalink
fix Cartesian bugs and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiuliren committed Jan 5, 2022
1 parent 6b4fc22 commit 1305f8a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
11 changes: 7 additions & 4 deletions chunkflow/lib/bounding_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)


Expand Down
17 changes: 13 additions & 4 deletions chunkflow/lib/synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions tests/lib/test_bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

from cloudvolume.lib import Bbox, Vec

from chunkflow.lib.bounding_boxes import BoundingBox, Cartesian
Expand All @@ -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))
Expand All @@ -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])


0 comments on commit 1305f8a

Please sign in to comment.