Exemple #1
0
def get_gauss_2d(shape, sigma, pixel_size=1, fourier=False, queue=None, block=False):
    """Get 2D Gaussian of *shape* with standard deviation *sigma* and *pixel_size*. If *fourier* is
    True the fourier transform of it is returned so it is faster for usage by convolution. Use
    command *queue* if specified. If *block* is True, wait for the kernel to finish.
    """
    shape = make_tuple(shape)
    pixel_size = get_magnitude(make_tuple(pixel_size))
    sigma = get_magnitude(make_tuple(sigma))
    LOG.debug('get_gauss_2d, shape: %s, sigma: %s, pixel size: %s, fourier: %s',
              shape, sigma, pixel_size, fourier)

    if queue is None:
        queue = cfg.OPENCL.queue
    out = cl.array.Array(queue, shape, dtype=cfg.PRECISION.np_float)

    if fourier:
        ev = cfg.OPENCL.programs['improc'].gauss_2d_f(queue,
                                                      shape[::-1],
                                                      None,
                                                      out.data,
                                                      g_util.make_vfloat2(sigma[1], sigma[0]),
                                                      g_util.make_vfloat2(pixel_size[1],
                                                      pixel_size[0]))
    else:
        ev = cfg.OPENCL.programs['improc'].gauss_2d(queue,
                                                    shape[::-1],
                                                    None,
                                                    out.data,
                                                    g_util.make_vfloat2(sigma[1], sigma[0]),
                                                    g_util.make_vfloat2(pixel_size[1],
                                                    pixel_size[0]))
    if block:
        ev.wait()

    return out
Exemple #2
0
    def test_sort(self):
        self.mesh.sort()
        x = get_magnitude(self.mesh.triangles[0, 2::3])
        # Triangles must be sorted by the last vertex
        np.testing.assert_almost_equal(x, sorted(x))

        # The greatest is the last vertex in a triangle, all other vertices must be smaller
        x = get_magnitude(self.mesh.triangles[0, :])
        for i in range(0, len(x), 3):
            self.assertTrue(x[i] <= x[i + 2])
            self.assertTrue(x[i + 1] <= x[i + 2])
Exemple #3
0
    def test_sort(self):
        self.mesh.sort()
        x = get_magnitude(self.mesh.triangles[0, 2::3])
        # Triangles must be sorted by the last vertex
        np.testing.assert_almost_equal(x, sorted(x))

        # The greatest is the last vertex in a triangle, all other vertices must be smaller
        x = get_magnitude(self.mesh.triangles[0, :])
        for i in range(0, len(x), 3):
            self.assertTrue(x[i] <= x[i + 2])
            self.assertTrue(x[i + 1] <= x[i + 2])
Exemple #4
0
def angle(vec_0, vec_1):
    """Angle between vectors *vec_0* and *vec_1*. The vectors might be 2D with 0 dimension
    specifying (x, y, z) components.
    """
    vec_0 = get_magnitude(vec_0)
    vec_1 = get_magnitude(vec_1)
    if vec_0.ndim == 2 and vec_1.ndim == 2:
        # The dot product would yield a matrix
        dot = length(vec_0 * vec_1)
    else:
        dot = np.dot(vec_0.T, vec_1)
    cross = np.cross(vec_0, vec_1, axis=0)
    lngth = length(cross)

    return np.arctan2(lngth, dot) * q.rad
def get_gauss_2d(shape, sigma, pixel_size=None, fourier=False):
    shape = make_tuple(shape)
    sigma = get_magnitude(make_tuple(sigma))
    if pixel_size is None:
        pixel_size = (1, 1)
    else:
        pixel_size = get_magnitude(make_tuple(pixel_size))

    if fourier:
        i = np.fft.fftfreq(shape[1]) / pixel_size[1]
        j = np.fft.fftfreq(shape[0]) / pixel_size[0]
        i, j = np.meshgrid(i, j)

        return np.exp(-2 * np.pi ** 2 * ((i * sigma[1]) ** 2 + (j * sigma[0]) ** 2))
    else:
        x = (np.arange(shape[1]) - shape[1] / 2) * pixel_size[1]
        y = (np.arange(shape[0]) - shape[0] / 2) * pixel_size[0]
        x, y = np.meshgrid(x, y)
        gauss = np.exp(- x ** 2 / (2. * sigma[1] ** 2) - y ** 2 / (2. * sigma[0] ** 2))

        return np.fft.ifftshift(gauss)
Exemple #6
0
def get_gauss_2d(shape, sigma, pixel_size=None, fourier=False):
    shape = make_tuple(shape)
    sigma = get_magnitude(make_tuple(sigma))
    if pixel_size is None:
        pixel_size = (1, 1)
    else:
        pixel_size = get_magnitude(make_tuple(pixel_size))

    if fourier:
        i = np.fft.fftfreq(shape[1]) / pixel_size[1]
        j = np.fft.fftfreq(shape[0]) / pixel_size[0]
        i, j = np.meshgrid(i, j)

        return np.exp(-2 * np.pi ** 2 * ((i * sigma[1]) ** 2 + (j * sigma[0]) ** 2))
    else:
        x = (np.arange(shape[1]) - shape[1] // 2) * pixel_size[1]
        y = (np.arange(shape[0]) - shape[0] // 2) * pixel_size[0]
        x, y = np.meshgrid(x, y)
        gauss = np.exp(-(x ** 2) / (2.0 * sigma[1] ** 2) - y ** 2 / (2.0 * sigma[0] ** 2))

        return np.fft.ifftshift(gauss)
Exemple #7
0
    def test_get_degenerate_triangles(self):
        triangles = get_magnitude(self.mesh.get_degenerate_triangles())

        for i in range(0, triangles.shape[1], 3):
            vertices = triangles[:, i:i + 3]
            x = vertices[0, :]
            y = vertices[1, :]
            x_any = np.any(x - x[0])
            y_any = np.any(y - y[0])
            self.assertTrue(not (x_any and y_any))

        self.mesh.rotate(45 * q.deg, X_AX)
        self.mesh.rotate(45 * q.deg, Y_AX)
        self.mesh.transform()

        self.assertEqual(0, self.mesh.get_degenerate_triangles().shape[1])
Exemple #8
0
    def get_displacement(axis, rot_axis, phi):
        """
        We first determine the local maximum rotation displacement in the plane parallel to the
        rotation plane (perpendicular to the rotation axis *rot_axis*, i.e. we work in local
        coordinates). We obtain the maximum when *phi* / 2 is aligned with an axis perpendicular to
        the *axis* we are interested in. We compute half of the local principal displacement (it is
        the displacement in one of the local principal axes direction) as the sine of the angle
        multiplied by the *length*, then we multiply by 2 to get the other half of the displacement.
        At the end we need to take into account that we have thus far worked with local principal
        axes (aligned with respect to *rot_axis*) and need to transform the displacement to the
        global coordinate system. This is obtained by taking the sine of the rotation axis and the
        principal axis angle and multiplying with the local displacement.
        """
        axis_sin = np.sin(angle(axis, rot_axis).rescale(q.rad).magnitude)

        return np.abs(axis_sin * 2 * get_magnitude(length) * np.sin(phi / 2))
Exemple #9
0
    def test_get_degenerate_triangles(self):
        triangles = get_magnitude(self.mesh.get_degenerate_triangles())

        for i in range(0, triangles.shape[1], 3):
            vertices = triangles[:, i:i + 3]
            x = vertices[0, :]
            y = vertices[1, :]
            x_any = np.any(x - x[0])
            y_any = np.any(y - y[0])
            self.assertTrue(not (x_any and y_any))

        self.mesh.rotate(45 * q.deg, X_AX)
        self.mesh.rotate(45 * q.deg, Y_AX)
        self.mesh.transform()

        self.assertEqual(0, self.mesh.get_degenerate_triangles().shape[1])
Exemple #10
0
 def test_furthest_point(self):
     self.assertAlmostEqual(get_magnitude(self.mesh.furthest_point),
                            np.sqrt(3))
Exemple #11
0
 def test_bounding_box(self):
     bbox_points = get_magnitude(self.mesh.bounding_box.points).astype(np.int).tolist()
     seed = (-1, 1)
     for point in itertools.product(seed, seed, seed):
         self.assertTrue(list(point) in bbox_points)
Exemple #12
0
 def get_px_value(value, round_func, ps):
     return int(round_func(get_magnitude(value / ps)))
Exemple #13
0
 def test_extrema(self):
     for endpoints in self.mesh.extrema:
         self.assertAlmostEqual(-1, get_magnitude(endpoints[0]))
         self.assertAlmostEqual(1, get_magnitude(endpoints[1]))
Exemple #14
0
 def test_center_of_bbox(self):
     gt = (1, 2, 3) * q.m
     mesh = self.shift_mesh(gt)
     center = get_magnitude(mesh.center_of_bbox)
     np.testing.assert_almost_equal(get_magnitude(gt), center)
Exemple #15
0
 def test_diff(self):
     gt = np.ones((3, 2)) * 2
     np.testing.assert_almost_equal(gt, get_magnitude(self.mesh.diff))
Exemple #16
0
 def test_extrema(self):
     for endpoints in self.mesh.extrema:
         self.assertAlmostEqual(-1, get_magnitude(endpoints[0]))
         self.assertAlmostEqual(1, get_magnitude(endpoints[1]))
Exemple #17
0
 def test_furthest_point(self):
     self.assertAlmostEqual(get_magnitude(self.mesh.furthest_point), np.sqrt(3))
Exemple #18
0
 def test_center_of_bbox(self):
     gt = (1, 2, 3) * q.m
     mesh = self.shift_mesh(gt)
     center = get_magnitude(mesh.center_of_bbox)
     np.testing.assert_almost_equal(get_magnitude(gt), center)
Exemple #19
0
 def test_diff(self):
     gt = np.ones((3, 2)) * 2
     np.testing.assert_almost_equal(gt, get_magnitude(self.mesh.diff))
Exemple #20
0
def test_get_magnitude():
    assert get_magnitude(1 * q.m) == 1
    assert get_magnitude(1 * q.mm) == 0.001
    assert get_magnitude(1) == 1
    assert tuple(get_magnitude((1, 2) * q.mm)) == (0.001, 0.002)
Exemple #21
0
 def test_bounding_box(self):
     bbox_points = get_magnitude(self.mesh.bounding_box.points).astype(
         np.int).tolist()
     seed = (-1, 1)
     for point in itertools.product(seed, seed, seed):
         self.assertTrue(list(point) in bbox_points)
Exemple #22
0
 def get_px_value(value, round_func, ps):
     return int(round_func(get_magnitude(value / ps)))