def test_intersection(self): for (x0a, x1a, y0a, y1a, z0a, z1a), \ (x0b, x1b, y0b, y1b, z0b, z1b), \ (x0e, x1e, y0e, y1e, z0e, z1e), \ in ((( 0, 10, 10, 20, 30, 40), ( 5, 8, 10, 20, 30, 40), ( 5, 8, 10, 20, 30, 40)), ((0, 10, 10, 20, 30, 40), (0, 10, 15, 18, 30, 40), (0, 10, 15, 18, 30, 40)), ((0, 10, 10, 20, 30, 40), (0, 10, 10, 20, 35, 38), (0, 10, 10, 20, 35, 38)), ((0, 10, 10, 20, 30, 40), (9, 19, 10, 20, 30, 40), (9, 10, 10, 20, 30, 40))): v0 = volume.VExtent(x0a, x1a, y0a, y1a, z0a, z1a) v1 = volume.VExtent(x0b, x1b, y0b, y1b, z0b, z1b) ve = volume.VExtent(x0e, x1e, y0e, y1e, z0e, z1e) for va, vb in ((v0, v1), (v1, v0)): vi = va.intersection(vb) self.assertEqual(vi.x0, ve.x0) self.assertEqual(vi.x1, ve.x1) self.assertEqual(vi.y0, ve.y0) self.assertEqual(vi.y1, ve.y1) self.assertEqual(vi.z0, ve.z0) self.assertEqual(vi.z1, ve.z1)
def test_zmax(self): # # Test where the edge is the x minimum # stack = volume.VExtent(10, 30, 10, 35, 20, 45) ostack = volume.VExtent(10, 30, 10, 35, 20, 50) v = stack.intersection(ostack) d = volume.get_distance_from_edge(v, stack, ostack) expected = np.array([[[i] * 20] * 25 for i in range(25, 0, -1)]) np.testing.assert_equal(d, expected)
def test_xmax(self): # # Test where the edge is the x minimum # stack = volume.VExtent(0, 20, 10, 35, 20, 50) ostack = volume.VExtent(10, 30, 10, 35, 20, 50) v = stack.intersection(ostack) d = volume.get_distance_from_edge(v, stack, ostack) expected = np.array([[np.arange(10, 0, -1)] * 25] * 30) np.testing.assert_equal(d, expected)
def test_imread_overlap(self): with make_case() as ps: xml_path, stacks = ps v = volume.TSVVolume.load(xml_path) e = volume.VExtent(976, 1048, 976, 1048, 0, 2) expected = np.zeros(e.shape, np.float32) v00 = volume.VExtent(976, 1024, 976, 1024, 0, 2) s00 = v.stacks[0][0] i00 = s00.imread(v00).astype(np.float32) v01 = volume.VExtent(1000, 1048, 976, 1024, 0, 2) s01 = v.stacks[0][1] i01 = s01.imread(v01).astype(np.float32) v10 = volume.VExtent(976, 1024, 1000, 1048, 0, 2) s10 = v.stacks[1][0] i10 = s10.imread(v10).astype(np.float32) v11 = volume.VExtent(1000, 1048, 1000, 1048, 0, 2) s11 = v.stacks[1][1] i11 = s11.imread(v11).astype(np.float32) m00, m01, m10, m11 = np.ones((4, 2, 48, 48), np.float32) m = np.zeros((2, 72, 72), np.float32) volume.compute_cosine(v00, s00, s01, i00) volume.compute_cosine(v00, s00, s10, i00) volume.compute_cosine(v00, s00, s11, i00) volume.compute_cosine(v00, s00, s01, m00) volume.compute_cosine(v00, s00, s10, m00) volume.compute_cosine(v00, s00, s11, m00) expected[:, :48, :48] += i00.astype(expected.dtype) m[:, :48, :48] += m00 volume.compute_cosine(v01, s01, s00, i01) volume.compute_cosine(v01, s01, s10, i01) volume.compute_cosine(v01, s01, s11, i01) volume.compute_cosine(v01, s01, s00, m01) volume.compute_cosine(v01, s01, s10, m01) volume.compute_cosine(v01, s01, s11, m01) expected[:, :48, -48:] += i01.astype(expected.dtype) m[:, :48, -48:] += m01 volume.compute_cosine(v10, s10, s00, i10) volume.compute_cosine(v10, s10, s01, i10) volume.compute_cosine(v10, s10, s11, i10) volume.compute_cosine(v10, s10, s00, m10) volume.compute_cosine(v10, s10, s01, m10) volume.compute_cosine(v10, s10, s11, m10) expected[:, -48:, :48] += i10.astype(expected.dtype) m[:, -48:, :48] += m10 volume.compute_cosine(v11, s11, s00, i11) volume.compute_cosine(v11, s11, s01, i11) volume.compute_cosine(v11, s11, s10, i11) volume.compute_cosine(v11, s11, s00, m11) volume.compute_cosine(v11, s11, s01, m11) volume.compute_cosine(v11, s11, s10, m11) expected[:, -48:, -48:] += i11.astype(expected.dtype) m[:, -48:, -48:] += m11 expected = np.around(expected / m) img = v.imread(e, np.uint16) np.testing.assert_almost_equal(img, expected, 0)
def test_two(self): # # Test two at once # stack = volume.VExtent(10, 30, 20, 35, 20, 45) ostack = volume.VExtent(10, 30, 10, 35, 20, 50) v = stack.intersection(ostack) d = volume.get_distance_from_edge(v, stack, ostack) expected_y = np.array([[[i] * 20 for i in range(1, 16)]] * 25) expected_z = np.array([[[i] * 20] * 15 for i in range(25, 0, -1)]) expected = np.minimum(expected_y, expected_z) np.testing.assert_equal(d, expected)
def test_x(self): # # We test at the border, 1/2 is not overlapping and 1/2 is # with make_case() as ps: xml_path, stacks = ps v = volume.TSVVolume.load(xml_path) e = volume.VExtent(976, 1024, 0, 10, 0, 2) img = np.random.RandomState(1234).randint(0, 65535, (2, 10, 48)) img = img.astype(np.float32) cosine = np.cos( np.arctan2( np.arange(1, 25).astype(float), np.arange(24, 0, -1).astype(float))) mult = np.hstack([np.ones(24), cosine**2]) expected = img.astype(float) * mult[np.newaxis, np.newaxis, :] s0 = v.stacks[0][0] s1 = v.stacks[0][1] # Quick check to make sure we got the right stack self.assertEqual(s0.x0, 0) self.assertEqual(s0.y0, 0) self.assertEqual(s0.z0, 0) self.assertEqual(s1.x0, 1000) self.assertEqual(s1.y0, 0) self.assertEqual(s1.z0, 0) volume.compute_cosine(e, s0, s1, img) np.testing.assert_almost_equal(img, expected, 0)
def test_imread_no_overlap(self): with make_case() as ps: xml_path, stacks = ps v = volume.TSVVolume.load(xml_path) e = volume.VExtent(0, 10, 20, 30, 0, 2) img = v.imread(e, np.uint16) for z in range(2): np.testing.assert_equal(img[z], stacks[0][0][z][20:30, :10])
def test_read_z_offset(self): with make_case() as ps: xml_path, stacks = ps v = volume.TSVVolume.load(xml_path) e = volume.VExtent(0, 10, 11, 21, 1, 2) img = np.zeros(e.shape) v.stacks[0][0].imread(e, img) np.testing.assert_equal(img[0], stacks[0][0][1][11:21, :10])
def test_imread_raw(self): with make_case(use_raw=True) as ps: my_xml_path, stacks = ps v = volume.TSVVolume.load(my_xml_path) e = volume.VExtent(0, 10, 11, 21, 0, 2) img = v.stacks[0][0].imread(e) np.testing.assert_equal(img[0], stacks[0][0][0][11:21, :10]) np.testing.assert_equal(img[1], stacks[0][0][1][11:21, :10])
def test_imread_raw(self): with make_case(use_raw=True) as ps: xml_path, stacks = ps root = pathlib.Path(xml_path).parent v = volume.TSVSimpleVolume(root, 1.8, 1.8, 1.8) e = volume.VExtent(0, 10, 11, 21, 0, 2) img = v.stacks[0][0].imread(e) np.testing.assert_equal(img[0], stacks[0][0][0][11:21, :10]) np.testing.assert_equal(img[1], stacks[0][0][1][11:21, :10])
def test_intersects(self): for (x0a, x1a, y0a, y1a, z0a, z1a), \ (x0b, x1b, y0b, y1b, z0b, z1b), \ intersects in ((( 0, 10, 10, 20, 30, 40), (10, 20, 10, 20, 30, 40), False), ((0, 10, 10, 20, 30, 40), (0, 10, 20, 30, 30, 40), False), ((0, 10, 10, 20, 30, 40), (0, 10, 10, 20, 40, 50), False), (( 0, 10, 10, 20, 30, 40), ( 9, 19, 10, 20, 30, 40), True), ((0, 10, 10, 20, 30, 40), (0, 20, 19, 29, 30, 40), True), ((0, 10, 10, 20, 30, 40), (0, 10, 10, 20, 39, 49), True)): v0 = volume.VExtent(x0a, x1a, y0a, y1a, z0a, z1a) v1 = volume.VExtent(x0b, x1b, y0b, y1b, z0b, z1b) self.assertEqual(v0.intersects(v1), intersects) self.assertEqual(v1.intersects(v0), intersects)
def test_imread_inplace(self): with make_case() as ps: xml_path, stacks = ps root = pathlib.Path(xml_path).parent v = volume.TSVSimpleVolume(root, 1.8, 1.8, 1.8) e = volume.VExtent(0, 10, 11, 21, 0, 2) img = np.zeros(e.shape) v.stacks[0][0].imread(e, img) np.testing.assert_equal(img[0], stacks[0][0][0][11:21, :10]) np.testing.assert_equal(img[1], stacks[0][0][1][11:21, :10])
def test_contains(self): for (x0a, x1a, y0a, y1a, z0a, z1a), \ (x0b, x1b, y0b, y1b, z0b, z1b), \ contains in ((( 0, 10, 10, 20, 30, 40), (10, 20, 10, 20, 30, 40), False), ((0, 10, 10, 20, 30, 40), (0, 10, 20, 30, 30, 40), False), ((0, 10, 10, 20, 30, 40), (0, 10, 10, 20, 40, 50), False), (( 0, 10, 10, 20, 30, 40), ( 9, 19, 10, 20, 30, 40), False), ((0, 10, 10, 20, 30, 40), (0, 20, 19, 29, 30, 40), False), ((0, 10, 10, 20, 30, 40), (0, 10, 10, 20, 39, 49), False), ((0, 10, 10, 20, 30, 40), (1, 9, 11, 19, 31, 39), True)): v0 = volume.VExtent(x0a, x1a, y0a, y1a, z0a, z1a) v1 = volume.VExtent(x0b, x1b, y0b, y1b, z0b, z1b) self.assertEqual(v0.contains(v1), contains)
def test_diagnostic_img(self): with make_case() as ps: xml_path, stacks = ps v = volume.TSVVolume.load(xml_path) e = volume.VExtent(1000, 1024, 1000, 1024, 0, 2) img = v.make_diagnostic_img(e) self.assertTupleEqual(tuple(img.shape[:-1]), tuple(e.shape)) self.assertEqual(img.shape[-1], 4) for z in range(2): np.testing.assert_equal(img[z, :, :, 0], stacks[0][0][z][-24:, -24:]) np.testing.assert_equal(img[z, :, :, 1], stacks[0][1][z][-24:, :24]) np.testing.assert_equal(img[z, :, :, 2], stacks[1][0][z][:24, -24:]) np.testing.assert_equal(img[z, :, :, 3], stacks[1][1][z][:24, :24])
def test_inv_y(self): with make_case() as ps: xml_path, stacks = ps v = volume.TSVVolume.load(xml_path) e = volume.VExtent(0, 10, 1000, 1048, 0, 2) img = np.random.RandomState(1234).randint(0, 65535, (2, 48, 10))\ .astype(np.float32) cosine = np.cos( np.arctan2( np.arange(24, 0, -1).astype(float), np.arange(1, 25).astype(float))) mult = np.hstack((cosine**2, np.ones(24))) expected = img.astype(float) * mult[np.newaxis, :, np.newaxis] s0 = v.stacks[0][0] s1 = v.stacks[1][0] # Quick check to make sure we got the right stack self.assertEqual(s0.x0, 0) self.assertEqual(s0.y0, 0) self.assertEqual(s0.z0, 0) self.assertEqual(s1.x0, 0) self.assertEqual(s1.y0, 1000) self.assertEqual(s1.z0, 0) volume.compute_cosine(e, s1, s0, img) np.testing.assert_almost_equal(img, expected.astype(np.uint16), 0)