dipy/tracking/streamline.py
def orient_by_rois(streamlines, roi1, roi2, affine=None, copy=True):
    """Orient a set of streamlines according to a pair of ROIs

    Parameters
    ----------
    streamlines : list
        List of 3d arrays. Each array contains the xyz coordinates of a single
        streamline.
    roi1, roi2 : ndarray
        Binary masks designating the location of the regions of interest, or
        coordinate arrays (n-by-3 array with ROI coordinate in each row).
    affine : ndarray
        Affine transformation from voxels to streamlines. Default: identity.
    copy : bool
        Whether to make a copy of the input, or mutate the input inplace.

    Returns
    -------
    streamlines : list
        The same 3D arrays, but reoriented with respect to the ROIs

    Examples
    --------
    >>> streamlines = [np.array([[0, 0., 0],
    ...                          [1, 0., 0.],
    ...                          [2, 0., 0.]]),
    ...                np.array([[2, 0., 0.],
    ...                          [1, 0., 0],
    ...                          [0, 0,  0.]])]
    >>> roi1 = np.zeros((4, 4, 4), dtype=bool)
    >>> roi2 = np.zeros_like(roi1)
    >>> roi1[0, 0, 0] = True
    >>> roi2[1, 0, 0] = True
    >>> orient_by_rois(streamlines, roi1, roi2)
    [array([[ 0.,  0.,  0.],
           [ 1.,  0.,  0.],
           [ 2.,  0.,  0.]]), array([[ 0.,  0.,  0.],
           [ 1.,  0.,  0.],
           [ 2.,  0.,  0.]])]

    """

    # If we don't already have coordinates on our hands:
    if len(roi1.shape) == 3:
        roi1 = np.asarray(np.where(roi1.astype(bool))).T
    if len(roi2.shape) == 3:
        roi2 = np.asarray(np.where(roi2.astype(bool))).T

    if affine is not None:
        roi1 = apply_affine(affine, roi1)
        roi2 = apply_affine(affine, roi2)

    # Make a copy, so you don't change the output in place:
    if copy:
        new_sl = deepcopy(streamlines)
    else:
        new_sl = streamlines

    for idx, sl in enumerate(streamlines):
        dist1 = cdist(sl, roi1, 'euclidean')
        dist2 = cdist(sl, roi2, 'euclidean')
        min1 = np.argmin(dist1, 0)
        min2 = np.argmin(dist2, 0)
        if min1[0] > min2[0]:
            new_sl[idx] = sl[::-1]

    return new_sl
Orient a set of streamlines according to a pair of ROIs

    Parameters
    ----------
    streamlines : list
        List of 3d arrays. Each array contains the xyz coordinates of a single
        streamline.
    roi1, roi2 : ndarray
        Binary masks designating the location of the regions of interest, or
        coordinate arrays (n-by-3 array with ROI coordinate in each row).
    affine : ndarray
        Affine transformation from voxels to streamlines. Default: identity.
    copy : bool
        Whether to make a copy of the input, or mutate the input inplace.

    Returns
    -------
    streamlines : list
        The same 3D arrays, but reoriented with respect to the ROIs

    Examples
    --------
    >>> streamlines = [np.array([[0, 0., 0],
    ...                          [1, 0., 0.],
    ...                          [2, 0., 0.]]),
    ...                np.array([[2, 0., 0.],
    ...                          [1, 0., 0],
    ...                          [0, 0,  0.]])]
    >>> roi1 = np.zeros((4, 4, 4), dtype=bool)
    >>> roi2 = np.zeros_like(roi1)
    >>> roi1[0, 0, 0] = True
    >>> roi2[1, 0, 0] = True
    >>> orient_by_rois(streamlines, roi1, roi2)
    [array([[ 0.,  0.,  0.],
           [ 1.,  0.,  0.],
           [ 2.,  0.,  0.]]), array([[ 0.,  0.,  0.],
           [ 1.,  0.,  0.],
           [ 2.,  0.,  0.]])]

    
Orient a set of streamlines according to a pair of ROIs
Parameters

streamlines : list
List of 3d arrays. Each array contains the xyz coordinates of a single
streamline.
roi1, roi2 : ndarray
Binary masks designating the location of the regions of interest, or
coordinate arrays (n-by-3 array with ROI coordinate in each row).
affine : ndarray
Affine transformation from voxels to streamlines. Default: identity.
copy : bool
Whether to make a copy of the input, or mutate the input inplace.

Returns

streamlines : list
The same 3D arrays, but reoriented with respect to the ROIs

Examples


Orient a set of streamlines according to a pair of ROIs Parameters streamlines : list List of 3d arrays . Each array contains the xyz coordinates of a single streamline . roi1 roi2 : ndarray Binary masks designating the location of the regions of interest or coordinate arrays ( n - by - 3 array with ROI coordinate in each row ) . affine : ndarray Affine transformation from voxels to streamlines . Default : identity . copy : bool Whether to make a copy of the input or mutate the input inplace . Returns streamlines : list The same 3D arrays but reoriented with respect to the ROIs Examples
def orient_by_rois(streamlines, roi1, roi2, affine=None, copy=True):
    if len(roi1.shape) == 3:
        roi1 = np.asarray(np.where(roi1.astype(bool))).T
    if len(roi2.shape) == 3:
        roi2 = np.asarray(np.where(roi2.astype(bool))).T
    if affine is not None:
        roi1 = apply_affine(affine, roi1)
        roi2 = apply_affine(affine, roi2)
    if copy:
        new_sl = deepcopy(streamlines)
    else:
        new_sl = streamlines
    for idx, sl in enumerate(streamlines):
        dist1 = cdist(sl, roi1, 'euclidean')
        dist2 = cdist(sl, roi2, 'euclidean')
        min1 = np.argmin(dist1, 0)
        min2 = np.argmin(dist2, 0)
        if min1[0] > min2[0]:
            new_sl[idx] = sl[::-1]
    return new_sl
def orient_by_rois ( streamlines , roi1 , roi2 , affine = None , copy = True ) : if len ( roi1 . shape ) == 3 : roi1 = np . asarray ( np . where ( roi1 . astype ( bool ) ) ) . T if len ( roi2 . shape ) == 3 : roi2 = np . asarray ( np . where ( roi2 . astype ( bool ) ) ) . T if affine is not None : roi1 = apply_affine ( affine , roi1 ) roi2 = apply_affine ( affine , roi2 ) if copy : new_sl = deepcopy ( streamlines ) else : new_sl = streamlines for idx , sl in enumerate ( streamlines ) : dist1 = cdist ( sl , roi1 , 'euclidean' ) dist2 = cdist ( sl , roi2 , 'euclidean' ) min1 = np . argmin ( dist1 , 0 ) min2 = np . argmin ( dist2 , 0 ) if min1 [ 0 ] > min2 [ 0 ] : new_sl [ idx ] = sl [ : : - 1 ] return new_sl
Orient a set of streamlines according to a pair of ROIs
Parameters
Orient a set of streamlines according to a pair of ROIs Parameters
"""Orient a set of streamlines according to a pair of ROIs

    Parameters
    ----------
    streamlines : list
        List of 3d arrays. Each array contains the xyz coordinates of a single
        streamline.
    roi1, roi2 : ndarray
        Binary masks designating the location of the regions of interest, or
        coordinate arrays (n-by-3 array with ROI coordinate in each row).
    affine : ndarray
        Affine transformation from voxels to streamlines. Default: identity.
    copy : bool
        Whether to make a copy of the input, or mutate the input inplace.

    Returns
    -------
    streamlines : list
        The same 3D arrays, but reoriented with respect to the ROIs

    Examples
    --------
    >>> streamlines = [np.array([[0, 0., 0],
    ...                          [1, 0., 0.],
    ...                          [2, 0., 0.]]),
    ...                np.array([[2, 0., 0.],
    ...                          [1, 0., 0],
    ...                          [0, 0,  0.]])]
    >>> roi1 = np.zeros((4, 4, 4), dtype=bool)
    >>> roi2 = np.zeros_like(roi1)
    >>> roi1[0, 0, 0] = True
    >>> roi2[1, 0, 0] = True
    >>> orient_by_rois(streamlines, roi1, roi2)
    [array([[ 0.,  0.,  0.],
           [ 1.,  0.,  0.],
           [ 2.,  0.,  0.]]), array([[ 0.,  0.,  0.],
           [ 1.,  0.,  0.],
           [ 2.,  0.,  0.]])]

    """ # If we don't already have coordinates on our hands: # Make a copy, so you don't change the output in place:
streamlines
roi1
roi2
affine
copy