Exemple #1
0
def test_angle_between_vectors():
    angle1 = mm.angle_between_vectors((1, 0), (0, 1))
    assert angle1 == np.pi/2
    angle1 = mm.angle_between_vectors((1, 0), (1, 0))
    assert angle1 == 0.0
    angle1 = mm.angle_between_vectors((1, 0), (-1, 0))
    assert angle1 == np.pi
Exemple #2
0
def test_angle_between_vectors():
    angle1 = mm.angle_between_vectors((1, 0), (0, 1))
    nt.assert_equal(angle1, np.pi/2)
    angle1 = mm.angle_between_vectors((1, 0), (1, 0))
    nt.assert_equal(angle1, 0.0)
    angle1 = mm.angle_between_vectors((1, 0), (-1, 0))
    nt.assert_equal(angle1, np.pi)
Exemple #3
0
def trunk_angles(nrn, neurite_type=NeuriteType.all):
    """Calculates the angles between all the trunks of the neuron.

    The angles are defined on the x-y plane and the trees
    are sorted from the y axis and anticlock-wise.
    """
    vectors = trunk_vectors(nrn, neurite_type=neurite_type)
    # In order to avoid the failure of the process in case the neurite_type does not exist
    if not vectors.size:
        return []

    def _sort_angle(p1, p2):
        """Angle between p1-p2 to sort vectors."""
        ang1 = np.arctan2(*p1[::-1])
        ang2 = np.arctan2(*p2[::-1])
        return (ang1 - ang2)

    # Sorting angles according to x-y plane
    order = np.argsort(
        np.array([
            _sort_angle(i / np.linalg.norm(i), [0, 1]) for i in vectors[:, 0:2]
        ]))

    ordered_vectors = vectors[order][:, [COLS.X, COLS.Y]]

    return [
        morphmath.angle_between_vectors(ordered_vectors[i],
                                        ordered_vectors[i - 1])
        for i, _ in enumerate(ordered_vectors)
    ]
Exemple #4
0
def align(section, direction):
    """Rotate a section (and all its descendents) so its initial segment is along ``direction``."""
    section_dir = section.points[1] - section.points[0]
    alpha = angle_between_vectors(section_dir, direction)
    if alpha < 1e-8:
        return

    if abs(alpha - np.pi) < 1e-8:
        axis = np.cross(section_dir, [1, 0, 0])

        # Case where X axis and section_dir are colinear
        if np.linalg.norm(axis) < 1e-8:
            axis = np.cross(section_dir, [0, 1, 0])
    else:
        axis = np.cross(section_dir, direction)
    axis /= np.linalg.norm(axis)
    matrix = Rotation.from_rotvec(alpha * axis).as_matrix()

    rotate(section, matrix, origin=section.points[0])