Ejemplo n.º 1
0
    def test_spread_excluder_multi_axes_per_gen(self):
        gx1_pos = np.array([1, 2, 3, 4, 5])
        gx2_pos = np.array([11, 10, 9, 8, 7])
        gy_pos = np.array([-1, 0, 1])
        gz_pos = np.array([1, 0, -1, -2, -3])
        mask_x1z_func = lambda px, pz: (px-4)**2 + (pz+1)**2 <= 1
        exz = Mock(axes=["gx1", "gz"], create_mask=Mock(side_effect=mask_x1z_func))
        gx = Mock(axes=["gx1", "gx2"], positions={"gx1":gx1_pos, "gx2":gx2_pos}, size=5, alternate=False)
        gy = Mock(axes=["gy"], positions={"gy":gy_pos}, size=3, alternate=False)
        gz = Mock(axes=["gz"], positions={"gz":gz_pos}, size=5, alternate=False)
        d = Dimension.merge_dimensions([Dimension(gz), Dimension(gy), Dimension(gx)])

        d.apply_excluder(exz)
        d.prepare()

        x1_positions = np.tile(gx1_pos, 15)
        x2_positions = np.tile(gx2_pos, 15)
        y_positions = np.repeat(np.tile(gy_pos, 5), 5)
        z_positions = np.repeat(gz_pos, 15)

        mask = mask_x1z_func(x1_positions, z_positions)
        expected_x1 = x1_positions[mask].tolist()
        expected_x2 = x2_positions[mask].tolist()
        expected_y = y_positions[mask].tolist()
        expected_z = z_positions[mask].tolist()

        self.assertEqual(expected_x1, d.get_positions("gx1").tolist())
        self.assertEqual(expected_x2, d.get_positions("gx2").tolist())
        self.assertEqual(expected_y, d.get_positions("gy").tolist())
        self.assertEqual(expected_z, d.get_positions("gz").tolist())
Ejemplo n.º 2
0
    def test_merge_dimensions(self):
        g, h = Mock(), Mock()
        g.axes, h.axes = ["gx", "gy"], ["hx", "hy"]
        g.size, h.size = 16, 64
        g.positions = {"gx":np.array([0, 1, 2]), "gy":np.array([10, 11, 12])}
        h.positions = {"hx":np.array([0, -1, -2]), "hy":np.array([-10, -11, -12])}
        outer, inner = Dimension(g), Dimension(h)
        om1, om2 = Mock(), Mock()
        im1, im2 = Mock(), Mock()
        outer._masks = [{"repeat":2, "tile":3, "mask":om1},
            {"repeat":5, "tile":7, "mask":om2}]
        inner._masks = [{"repeat":11, "tile":13, "mask":im1},
            {"repeat":17, "tile":19, "mask":im2}]
        combined = Dimension.merge_dimensions([outer, inner])

        self.assertEqual(g.size * h.size, combined._max_length)
        self.assertEqual(outer.alternate or inner.alternate, combined.alternate)
        self.assertEqual(["gx", "gy", "hx", "hy"], combined.axes)
        self.assertEqual([2, 12, 0, -10], combined.upper)
        self.assertEqual([0, 10, -2, -12], combined.lower)
        expected_masks = [
            {"repeat":128, "tile":3, "mask":om1},
            {"repeat":320, "tile":7, "mask":om2},
            {"repeat":11, "tile":13*16, "mask":im1},
            {"repeat":17, "tile":19*16, "mask":im2}]
        self.assertEqual(expected_masks, combined._masks)
Ejemplo n.º 3
0
    def test_merge_three(self):
        g1, g2, g3 = Mock(), Mock(), Mock()
        g1.axes, g2.axes, g3.axes = ["g1"], ["g2"], ["g3"]
        g1.size, g2.size, g3.size = 3, 4, 5
        g1.positions = {"g1":np.array([0, 1, 2])}
        g2.positions = {"g2":np.array([-1, 0, 1, 2])}
        g3.positions = {"g3":np.array([-2, 0, 2, 4, 6])}
        d1, d2, d3 = Dimension(g1), Dimension(g2), Dimension(g3)
        d1_m, d2_m, d3_m = Mock(), Mock(), Mock()
        d1._masks = [{"repeat":1, "tile":1, "mask":d1_m}]
        d2._masks = [{"repeat":2, "tile":3, "mask":d2_m}]
        d3._masks = [{"repeat":5, "tile":7, "mask":d3_m}]

        combined = Dimension.merge_dimensions([d1, d2, d3])

        self.assertEqual(60, combined._max_length)
        self.assertEqual(g1.alternate or g2.alternate or g3.alternate, combined.alternate)
        self.assertEqual(["g1", "g2", "g3"], combined.axes)
        self.assertEqual([0, -1, -2], combined.lower)
        self.assertEqual([2, 2, 6], combined.upper)

        expected_masks = [
                {"repeat":20, "tile":1, "mask":d1_m},
                {"repeat":10, "tile":9, "mask":d2_m},
                {"repeat":5, "tile":84, "mask":d3_m}]
        self.assertEqual(expected_masks, combined._masks)
Ejemplo n.º 4
0
    def test_high_dimensional_excluder(self):
        w_pos = np.array([0, 1, 2, 3, 4, 5])
        x_pos = np.array([0, 1, 2, 3, 4, 5])
        y_pos = np.array([0, 1, 2, 3, 4, 5])
        z_pos = np.array([0, 1, 2, 3, 4, 5])
        mask_function = lambda pw, px, py, pz: (pw-2)**2 + (px-2)**2 + (py-1)**2 + (pz-3)**2 <= 1.1
        excluder = Mock(axes=["w", "x", "y", "z"], create_mask=Mock(side_effect=mask_function))
        gw = Mock(axes=["w"], positions={"w":w_pos}, size=len(w_pos), alternate=False)
        gx = Mock(axes=["x"], positions={"x":x_pos}, size=len(x_pos), alternate=False)
        gy = Mock(axes=["y"], positions={"y":y_pos}, size=len(y_pos), alternate=False)
        gz = Mock(axes=["z"], positions={"z":z_pos}, size=len(z_pos), alternate=False)
        d = Dimension.merge_dimensions([Dimension(gz), Dimension(gy), Dimension(gx), Dimension(gw)])

        d.apply_excluder(excluder)
        d.prepare()

        w_positions = np.tile(w_pos, len(x_pos) * len(y_pos) * len(z_pos))
        x_positions = np.repeat(np.tile(x_pos, len(y_pos) * len(z_pos)), len(w_pos))
        y_positions = np.repeat(np.tile(y_pos, len(z_pos)), len(w_pos) * len(x_pos))
        z_positions = np.repeat(z_pos, len(w_pos) * len(x_pos) * len(y_pos))
        mask = mask_function(w_positions, x_positions, y_positions, z_positions)
        w_expected = w_positions[mask].tolist()
        x_expected = x_positions[mask].tolist()
        y_expected = y_positions[mask].tolist()
        z_expected = z_positions[mask].tolist()

        self.assertEqual(w_expected, d.get_positions("w").tolist())
        self.assertEqual(x_expected, d.get_positions("x").tolist())
        self.assertEqual(y_expected, d.get_positions("y").tolist())
        self.assertEqual(z_expected, d.get_positions("z").tolist())
    def test_excluder_spread_axes(self):
        sp = SpiralGenerator(["s1", "s2"], ["mm", "mm"], centre=[0, 0], radius=1, scale=0.5, alternate=True)
        y = LineGenerator("y", "mm", 0, 1, 3, True)
        z = LineGenerator("z", "mm", -2, 3, 6, True)
        e = ROIExcluder([CircularROI([0., 0.], 1.0)], ["s1", "z"])
        g = CompoundGenerator([z, y, sp], [e], [])

        g.prepare()

        s1_pos, s2_pos = sp.positions["s1"], sp.positions["s2"]
        s1_pos = np.tile(np.append(s1_pos, s1_pos[::-1]), 9)
        s2_pos = np.tile(np.append(s2_pos, s2_pos[::-1]), 9)
        y_pos = np.tile(np.repeat(np.array([0, 0.5, 1.0, 1.0, 0.5, 0]), sp.size), 3)
        z_pos = np.repeat(np.array([-2, -1, 0, 1, 2, 3]), sp.size * 3)

        mask_func = lambda ps1, pz: ps1**2 + pz**2 <= 1
        mask = mask_func(s1_pos, z_pos)

        expected_s1 = s1_pos[mask]
        expected_s2 = s2_pos[mask]
        expected_y = y_pos[mask]
        expected_z = z_pos[mask]
        expected_positions = [{'s1':ps1, 's2':ps2, 'y':py, 'z':pz}
                for (ps1, ps2, py, pz) in zip(expected_s1, expected_s2, expected_y, expected_z)]
        positions = [point.positions for point in list(g.iterator())]

        self.assertEqual(positions, expected_positions)
Ejemplo n.º 6
0
    def test_single_alternating_generator_with_excluder(self):
        x_pos = np.array([0, 1, 2, 3, 4, 5])
        x_bounds = np.array([-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5])
        y_pos = np.array([10, 11, 12, 13, 14, 15])
        y_bounds = np.array([9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5])
        g = Mock(axes=["x", "y"],
                 positions={
                     "x": x_pos,
                     "y": y_pos
                 },
                 bounds={
                     "x": x_bounds,
                     "y": y_bounds
                 },
                 size=6)
        g.alternate = True
        mask = np.array([1, 1, 0, 1, 0, 0], dtype=np.int8)
        e = Mock(axes=["x", "y"], create_mask=Mock(return_value=mask))

        d = Dimension([g], [e])

        self.assertTrue(d.alternate)

        d.prepare()

        expected_x = [0, 1, 3]
        expected_y = [10, 11, 13]
        self.assertEqual(expected_x, d.positions["x"].tolist())
        self.assertEqual(expected_y, d.positions["y"].tolist())
Ejemplo n.º 7
0
 def produce_points(self):
     self.points = {}
     self.points_lower = {}
     self.points_upper = {}
     for axis in range_(self.num_axes):
         axis_name = self.name[axis]
         start = self.start[axis]
         stop = self.stop[axis]
         d = stop - start
         if self.num == 1:
             self.points[axis_name] = np.array([start])
             self.points_upper[axis_name] = np.array([start + 0.5 * d])
             self.points_lower[axis_name] = np.array([start - 0.5 * d])
         else:
             n = self.num - 1.
             s = d / n
             upper_start = start + 0.5 * d / n
             upper_stop = stop + 0.5 * d / n
             lower_start = start - 0.5 * d / n
             lower_stop = stop - 0.5 * d / n
             self.points[axis_name] = np.linspace(
                 float(start), float(stop), self.num)
             self.points_upper[axis_name] = np.linspace(
                 float(upper_start), float(upper_stop), self.num)
             self.points_lower[axis_name] = np.linspace(
                 float(lower_start), float(lower_stop), self.num)
Ejemplo n.º 8
0
 def test_apply_excluders_over_multiple_gens(self):
     gx_pos = np.array([1, 2, 3, 4, 5])
     gy_pos = np.zeros(5)
     hx_pos = np.zeros(5)
     hy_pos = np.array([-1, -2, -3])
     mask = np.full(15, 1, dtype=np.int8)
     e = Mock(axes=["gx", "hy"], create_mask=Mock(return_value=mask))
     g = Mock(axes=["gx", "gy"],
              positions={
                  "gx": gx_pos,
                  "gy": gy_pos
              },
              size=len(gx_pos),
              alternate=False)
     h = Mock(axes=["hx", "hy"],
              positions={
                  "hx": hx_pos,
                  "hy": hy_pos
              },
              size=len(hy_pos),
              alternate=False)
     d = Dimension(g)
     d.generators = [g, h]
     d.size = g.size * h.size
     d.apply_excluder(e)
     d._masks[0]["mask"] = d._masks[0]["mask"].tolist()
     self.assertEqual([{
         "repeat": 1,
         "tile": 1,
         "mask": mask.tolist()
     }], d._masks)
     self.assertTrue((np.repeat(np.array([1, 2, 3, 4, 5]),
                                3) == e.create_mask.call_args[0][0]).all())
     self.assertTrue((np.tile(np.array([-1, -2, -3]),
                              5) == e.create_mask.call_args[0][1]).all())
Ejemplo n.º 9
0
    def test_get_mesh_map(self):
        # Set up a generator, with 3x4 grid with alternating x and a circular
        # excluder such that the four 'corners' of the grid are excluded
        gx_pos = np.array([0.1, 0.2, 0.3])
        gx_bounds = np.array([0.05, 0.15, 0.25, 0.35])
        gy_pos = np.array([1.1, 1.2, 1.3, 1.4])
        mask_func = lambda px, py: (px - 0.2)**2 + (py - 1.25)**2 <= 0.0225
        gx = Mock(axes=["gx"],
                  positions={"gx": gx_pos},
                  bounds={"gx": gx_bounds},
                  size=3,
                  alternate=True)
        gy = Mock(axes=["gy"],
                  positions={"gy": gy_pos},
                  size=4,
                  alternate=False)
        e = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_func))

        d = Dimension([gy, gx], [e])
        d.prepare()

        self.assertEqual([1, 2, 1, 0, 0, 1, 2, 1],
                         d.get_mesh_map("gx").tolist())
        self.assertEqual([0, 1, 1, 1, 2, 2, 2, 3],
                         d.get_mesh_map("gy").tolist())
Ejemplo n.º 10
0
 def test_mask_points(self):
     points = [np.array([7., 9.]), np.array([11., 11.])]
     points_cp = [axis.copy().tolist() for axis in points]
     expected = [True, False]
     mask = self.roi.mask_points(points)
     self.assertEqual(expected, mask.tolist())
     self.assertEqual(points_cp, [axis.tolist() for axis in points])
Ejemplo n.º 11
0
 def test_get_positions_with_alternating(self):
     gx_pos = np.array([0, 1, 2, 3])
     gy_pos = np.array([0, 1, 2, 3])
     gz_pos = np.array([0, 1, 2, 3])
     mask_xy_func = lambda px, py: (px-1)**2 + (py-2)**2 <= 2
     mask_yz_func = lambda py, pz: (py-2)**2 + (pz-1)**2 <= 1
     exy = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_xy_func))
     eyz = Mock(axes=["gy", "gz"], create_mask=Mock(side_effect=mask_yz_func))
     gx = Mock(axes=["gx"], positions={"gx":gx_pos}, size=4, alternate=True)
     gy = Mock(axes=["gy"], positions={"gy":gy_pos}, size=4, alternate=True)
     gz = Mock(axes=["gz"], positions={"gz":gz_pos}, size=4, alternate=True)
     dx = Dimension(gx)
     dy = Dimension(gy)
     dz = Dimension(gz)
     d = Dimension.merge_dimensions([dz, dy, dx])
     d.apply_excluder(exy)
     d.apply_excluder(eyz)
     d.prepare()
     self.assertEqual(15, d.size)
     self.assertEqual(
         [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2],
         d.get_positions("gz").tolist())
     self.assertEqual(
         [2, 2, 2, 3, 3, 3, 2, 2, 2, 1, 1, 1, 2, 2, 2],
         d.get_positions("gy").tolist())
     self.assertEqual(
         [0, 1, 2, 0, 1, 2, 2, 1, 0, 0, 1, 2, 0, 1, 2],
         d.get_positions("gx").tolist())
Ejemplo n.º 12
0
    def test_create_mask_returns_all_points(self):
        x_points = np.array([1, 2, 3, 4])
        y_points = np.array([10, 10, 20, 20])
        expected_mask = np.array([True, True, True, True])

        mask = self.e.create_mask(x_points, y_points)

        self.assertEqual(expected_mask.tolist(), mask.tolist())
 def test_simple_point_contains(self):
     vertices_x = [0, 1, 2, 2, -1, -1]
     vertices_y = [0, 0, -1, 1, 1, -1]
     roi = PolygonalROI(vertices_x, vertices_y)
     px = [-0.9, 1.9, 0.5, 0.5, 2.2, -1.1]
     py = [-0.85, 0.85, -0.5, 0.5, 0.5, 0.5]
     p = [np.array(px), np.array(py)]
     expected = [True, True, False, True, False, False]
     mask = roi.mask_points(p)
     self.assertEquals(expected, mask.tolist())
 def test_mask_points(self):
     roi = EllipticalROI([1, 2], [2, 1], pi / 6)
     points = [
         np.array([3, 1, 2.73, 3.00, -0.73]),
         np.array([1, 0, 3.00, 4.27, 1.000])
     ]
     points_cp = [axis.copy().tolist() for axis in points]
     expected = [False, False, True, False, True]
     self.assertEquals(expected, roi.mask_points(points).tolist())
     self.assertEqual(points_cp, [axis.tolist() for axis in points])
 def test_mask(self):
     roi = RectangularROI([1, 2], 1, 1, pi / 4)
     points = [
         np.array([2, 2, 1, 1, 1.7, 0.3, 1.01, 0.99]),
         np.array([3, 2, 2, 3.4, 2.7, 2.705, 1.99, 1.99])
     ]
     points_cp = [axis.copy().tolist() for axis in points]
     expected = [False, False, True, True, True, True, False, False]
     mask = roi.mask_points(points)
     self.assertEqual(expected, mask.tolist())
     self.assertEqual(points_cp, [axis.tolist() for axis in points])
Ejemplo n.º 16
0
 def test_get_positions_single_gen(self):
     gx_pos = np.tile(np.array([0, 1, 2, 3]), 4)
     gy_pos = np.repeat(np.array([0, 1, 2, 3]), 4)
     mask_func = lambda px, py: (px-1)**2 + (py-2)**2 <= 1
     g = Mock(axes=["gx", "gy"], positions={"gx":gx_pos, "gy":gy_pos}, size=16, alternate=False)
     e = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_func))
     d = Dimension(g)
     d.apply_excluder(e)
     d.prepare()
     self.assertEqual([1, 0, 1, 2, 1], d.get_positions("gx").tolist())
     self.assertEqual([1, 2, 2, 2, 3], d.get_positions("gy").tolist())
Ejemplo n.º 17
0
 def __init__(self, points_x, points_y):
     # type: (UPointsX, UPointsY) -> None
     super(PolygonalROI, self).__init__()
     if len(points_x) != len(points_y):
         raise ValueError("Point arrays must be the same size")
     if len(points_x) < 3:
         raise ValueError("Polygon requires at least 3 vertices")
     # TODO: check points are not all collinear
     #       (i.e. describe at least a triangle)
     self.points_x = APointsX(np.array(points_x, dtype=np.float64))
     self.points_y = APointsY(np.array(points_y, dtype=np.float64))
 def test_mask_points(self):
     l = LinearROI([1, 1], 2 * sqrt(2), -pi / 4 - 2 * pi)
     px = [
         1, 2, 3, 1 + 1e-10, 3 - 1e-10, 2 + 1e-10, 2 - 1e-14, 3 + 1e-14, 1
     ]
     py = [1, 0, -1, 1, -1, 0 + 1e-10, 0 + 1e-14, -1 - 1e-14, 1 + 1e-14]
     p = [np.array(px), np.array(py)]
     points_cp = [axis.copy().tolist() for axis in p]
     expected = [True, True, True, False, False, False, True, True, True]
     mask = l.mask_points(p, 1e-12)
     self.assertEquals(expected, mask.tolist())
     self.assertEqual(points_cp, [axis.tolist() for axis in p])
Ejemplo n.º 19
0
    def test_single_axis_excluder(self):
        x_pos = np.array([0, 1, 2, 3, 4, 5])
        y_pos = np.array([10, 11, 12, 13, 14, 15])
        g = Mock(axes=["x", "y"], positions={"x":x_pos, "y":y_pos}, size=len(x_pos))
        g.alternate = False
        e = Mock(axes=["x"], create_mask=lambda x: (2 <= x) & (x < 4) | (x == 5))
        d = Dimension(g)
        d.apply_excluder(e)
        d.prepare()

        self.assertEqual([2, 3, 5], d.get_positions('x').tolist())
        self.assertEqual([12, 13, 15], d.get_positions('y').tolist())
 def test_complex_mask_points(self):
     vertices_x = [0, 0, 2, 2, 1, 1, 3, 3]
     vertices_y = [0, 2, 2, 1, 1, 3, 3, 0]
     roi = PolygonalROI(vertices_x, vertices_y)
     px = [0.5, 0.5, 1.5, 1.5, 2.5,  2.5, 3.5, -0.5, 3.5, 2, 3, 0]
     py = [0.5, 2.5, 1.5, 2.5, 2.5, -0.5, 1.5,  0.5, 0.5, 0, 2, 1.5]
     p = [np.array(px), np.array(py)]
     points_cp = [axis.copy().tolist() for axis in p]
     expected = [True, False, False, True, True, False, False, False, False,
                 True, False, True]
     mask = roi.mask_points(p)
     self.assertEquals(expected, mask.tolist())
     self.assertEqual(points_cp, [axis.tolist() for axis in p])
Ejemplo n.º 21
0
 def test_apply_excluders_over_single_alternating(self):
     x_pos = np.array([1, 2, 3, 4, 5])
     y_pos = np.array([10, 11, 12, 13, 14, 15])
     g = Mock(axes=["x", "y"], positions={"x":x_pos, "y":y_pos})
     g.alternate = True
     mask = np.array([1, 1, 0, 1, 0, 0], dtype=np.int8)
     e = Mock(axes=["x", "y"], create_mask=Mock(return_value=mask))
     d = Dimension(g)
     d.apply_excluder(e)
     d._masks[0]["mask"] = d._masks[0]["mask"].tolist()
     self.assertEqual([{"repeat":1, "tile":0.5, "mask":mask.tolist()}], d._masks)
     self.assertTrue((np.append(x_pos, x_pos[::-1]) == e.create_mask.call_args[0][0]).all())
     self.assertTrue((np.append(y_pos, y_pos[::-1]) == e.create_mask.call_args[0][1]).all())
Ejemplo n.º 22
0
    def test_positions_non_alternate(self):
        g1, g2, g3 = Mock(), Mock(), Mock()
        g1.axes, g2.axes, g3.axes = ["g1"], ["g2"], ["g3"]
        g1.size, g2.size, g3.size = 3, 4, 5
        g1.positions = {"g1": np.array([0, 1, 2])}
        g1.bounds = {"g1": np.array([-0.5, 0.5, 1.5, 2.5])}
        g2.positions = {"g2": np.array([-1, 0, 1, 2])}
        g2.bounds = {"g2": np.array([-1.5, -0.5, 0.5, 1.5, 2.5])}
        g3.positions = {"g3": np.array([-2, 0, 2, 4, 6])}
        g3.bounds = {"g3": np.array([-3, -1, 1, 3, 5, 7])}
        g1.alternate = False
        g2.alternate = False
        g3.alternate = False
        e1, e2 = Mock(), Mock()
        e1.axes = ["g1", "g2"]
        e2.axes = ["g2", "g3"]
        m1 = np.repeat(np.array([0, 1, 0, 1, 1, 0]), 10)
        m2 = np.tile(np.array([1, 0, 0, 1, 1, 1]), 10)
        e1.create_mask.return_value = m1
        e2.create_mask.return_value = m2

        d = Dimension([g1, g2, g3], [e1, e2])
        d.prepare()

        expected_mask = m1 & m2
        expected_indices = expected_mask.nonzero()[0]
        expected_g1 = np.repeat(g1.positions["g1"], 5 * 4)[expected_indices]
        expected_g2 = np.tile(np.repeat(g2.positions["g2"], 5),
                              3)[expected_indices]
        expected_g3 = np.tile(g3.positions["g3"], 3 * 4)[expected_indices]
        self.assertEqual(expected_mask.tolist(), d.mask.tolist())
        self.assertEqual(expected_indices.tolist(), d.indices.tolist())
        self.assertEqual(expected_g1.tolist(), d.positions["g1"].tolist())
        self.assertEqual(expected_g2.tolist(), d.positions["g2"].tolist())
        self.assertEqual(expected_g3.tolist(), d.positions["g3"].tolist())
Ejemplo n.º 23
0
    def test_dim_invalid_alternating(self):
        g1 = Mock(axes=["x"],
                  positions={"x": np.array([1, 2, 3])},
                  bounds={"x": np.array([0.5, 1.5, 2.5, 3.5])},
                  size=3,
                  alternate=True)
        g2 = Mock(axes=["y"],
                  positions={"y": np.array([1, 2, 3])},
                  bounds={"y": np.array([0.5, 1.5, 2.5, 3.5])},
                  size=3,
                  alternate=False)

        with self.assertRaises(ValueError):
            d = Dimension([g1, g2])
Ejemplo n.º 24
0
    def test_two_generators_init(self):
        g, h = Mock(), Mock()
        g.axes, h.axes = ["gx", "gy"], ["hx", "hy"]
        g.size, h.size = 16, 64
        g.alternate = False
        h.alternate = False
        g.positions = {"gx": np.array([0, 1, 2]), "gy": np.array([10, 11, 12])}
        g.bounds = {
            "gx": np.array([-0.5, 0.5, 1.5, 2.5]),
            "gy": np.array([9.5, 10.5, 11.5, 12.5])
        }
        h.positions = {
            "hx": np.array([0, -1, -2]),
            "hy": np.array([-10, -11, -12])
        }
        h.bounds = {
            "hx": np.array([0.5, -0.5, -1.5, -2.5]),
            "hy": np.array([-9.5, -10.5, -11.5, -12.5])
        }

        d = Dimension([g, h])

        self.assertEqual(False, d.alternate)
        self.assertEqual(["gx", "gy", "hx", "hy"], d.axes)
        self.assertEqual([2, 12, 0, -10], d.upper)
        self.assertEqual([0, 10, -2, -12], d.lower)
    def test_mask_points(self):
        roi = PointROI([1, 2])
        px = [1, 0, 1 + 1e-15, 1, 1]
        py = [2, 0, 2, 2 + 1e-15, 2 + 1e-14]
        expected = [True, False, True, True, False]
        p = [np.array(px), np.array(py)]
        points_cp = [axis.copy().tolist() for axis in p]
        mask = roi.mask_points(p, 2e-15)
        self.assertEqual(expected, mask.tolist())

        mask = roi.mask_points([np.array(px), np.array(py)], 0)
        expected = [True, False, False, False, False]
        self.assertEqual(expected, mask.tolist())
        self.assertEqual(points_cp, [axis.tolist() for axis in p])
Ejemplo n.º 26
0
 def test_init(self):
     g = Mock()
     g.axes = ["x", "y"]
     g.positions = {"x":np.array([0, 1, 2]), "y":np.array([10, 11, 12])}
     g.size = 3
     d = Dimension(g)
     d.prepare()
     self.assertEqual([g], d.generators)
     self.assertEqual(["x", "y"], d.axes)
     self.assertEqual([], d._masks)
     self.assertEqual(g.alternate, d.alternate)
     self.assertEqual(g.size, d.size)
     self.assertEqual([2, 12], d.upper)
     self.assertEqual([0, 10], d.lower)
Ejemplo n.º 27
0
 def test_get_positions_after_merge(self):
     gx_pos = np.array([0, 1, 2, 3])
     gy_pos = np.array([0, 1, 2, 3])
     mask_func = lambda px, py: (px-1)**2 + (py-2)**2 <= 1
     gx = Mock(axes=["gx"], positions={"gx":gx_pos}, size=4, alternate=False)
     gy = Mock(axes=["gy"], positions={"gy":gy_pos}, size=4, alternate=False)
     e = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_func))
     dx = Dimension(gx)
     dy = Dimension(gy)
     d = Dimension.merge_dimensions([dy, dx])
     d.apply_excluder(e)
     d.prepare()
     self.assertEqual([1, 0, 1, 2, 1], d.get_positions("gx").tolist())
     self.assertEqual([1, 2, 2, 2, 3], d.get_positions("gy").tolist())
    def test_mask_points(self):
        s = SectorROI((0, 1), (1, 2), (-pi / 2, pi / 2))
        p = [
            np.array([0, 1, 0, 2.05, 1, 0.7, -1.5, 0.00]),
            np.array([0, 0, 1, 0.00, 1, 0.7, 0.00, 2.05])
        ]
        points_cp = [axis.copy().tolist() for axis in p]
        expected = [True, True, False, False, True, False, False, True]
        mask = s.mask_points(p)
        self.assertEquals(expected, mask.tolist())

        s2 = SectorROI((0, 0), (1, 2),
                       (-5 * pi / 2, -3 * pi / 2))  #the same sector
        mask = s.mask_points(p)
        self.assertEquals(expected, mask.tolist())
        self.assertEqual(points_cp, [axis.tolist() for axis in p])
Ejemplo n.º 29
0
 def test_apply_excluders_with_scaling(self):
     g1_pos = np.array([1, 2, 3])
     g2_pos = np.array([-1, -2])
     mask_func = lambda px, py: np.full(len(px), 1, dtype=np.int8)
     g1 = Mock(axes=["g1"], positions={"g1":g1_pos}, size=len(g1_pos))
     g2 = Mock(axes=["g2"], positions={"g2":g2_pos}, size=len(g2_pos))
     e = Mock(axes=["g1", "g2"], create_mask=Mock(side_effect=mask_func))
     d = Dimension(g1)
     d.alternate = True
     d.generators = [Mock(size=5, axes=[]), g1, g2, Mock(size=7, axes=[])]
     d.size = 5 * len(g1_pos) * len(g2_pos) * 7
     d.apply_excluder(e)
     d._masks[0]["mask"] = d._masks[0]["mask"].tolist()
     expected_mask = [1] * 12
     self.assertEqual([{"repeat":7, "tile":2.5, "mask":expected_mask}], d._masks)
     self.assertTrue((np.repeat(np.append(g1_pos, g1_pos[::-1]), 2) == e.create_mask.call_args[0][0]).all())
     self.assertTrue((np.tile(np.append(g2_pos, g2_pos[::-1]), 3) == e.create_mask.call_args[0][1]).all())
Ejemplo n.º 30
0
    def test_excluder_over_spread_axes(self):
        gw_pos = np.array([0.1, 0.2])
        gw_bounds = np.array([0.05, 0.15, 0.25])
        gx_pos = np.array([0, 1, 2, 3])
        gy_pos = np.array([10, 11, 12, 13])
        gz_pos = np.array([100, 101, 102, 103])
        go_pos = np.array([1000, 1001, 1002])
        mask_xz_func = lambda px, pz: (px - 1)**2 + (pz - 102)**2 <= 1
        exz = Mock(axes=["gx", "gz"],
                   create_mask=Mock(side_effect=mask_xz_func))
        gw = Mock(axes=["gw"],
                  positions={"gw": gw_pos},
                  bounds={"gw": gw_bounds},
                  size=2,
                  alternate=False)
        gx = Mock(axes=["gx"],
                  positions={"gx": gx_pos},
                  size=4,
                  alternate=False)
        gy = Mock(axes=["gy"],
                  positions={"gy": gy_pos},
                  size=4,
                  alternate=False)
        gz = Mock(axes=["gz"],
                  positions={"gz": gz_pos},
                  size=4,
                  alternate=False)
        go = Mock(axes=["go"],
                  positions={"go": go_pos},
                  size=3,
                  alternate=False)

        d = Dimension([go, gz, gy, gx, gw], [exz])
        d.prepare()

        x_positions = np.tile(np.array([0, 1, 2, 3]), 16)
        y_positions = np.repeat(np.tile(np.array([10, 11, 12, 13]), 4), 4)
        z_positions = np.repeat(np.array([100, 101, 102, 103]), 16)
        x_positions = np.tile(np.repeat(x_positions, gw.size), go.size)
        y_positions = np.tile(np.repeat(y_positions, gw.size), go.size)
        z_positions = np.tile(np.repeat(z_positions, gw.size), go.size)

        mask = mask_xz_func(x_positions, z_positions)
        expected_x = x_positions[mask].tolist()
        expected_y = y_positions[mask].tolist()
        expected_z = z_positions[mask].tolist()

        self.assertEqual(expected_x, d.get_positions("gx").tolist())
        self.assertEqual(expected_y, d.get_positions("gy").tolist())
        self.assertEqual(expected_z, d.get_positions("gz").tolist())