<commit_before>"""
Functions for geometrical image transformation and warping.
"""

from __future__ import absolute_import

import warnings

import numpy as np
import scipy.ndimage.interpolation
try:
    import skimage.transform
    scikit_image_not_found = False
except ImportError:  # pragma: no cover
    scikit_image_not_found = True  # pragma: no cover

__all__ = ['affine_transform']

def affine_transform(image, rmatrix, order=3, scale=1.0, image_center=None,
                     recenter=False, missing=0.0, use_scipy=False):
    """
    Rotates, shifts and scales an image using :func:`skimage.transform.warp`, or
    :func:`scipy.ndimage.interpolation.affine_transform` if specified. Falls back to
    the scipy function if scikit-image can't be imported.

    Parameters
    ----------
    image : ndarray
        2D Image to be rotated.
    rmatrix : 2x2
        Linear transformation rotation matrix.
    order : int 0-5
        Interpolation order to be used. When using scikit-image this parameter
        is passed into :func:`skimage.transform.warp`.
        When using scipy it is passed into
        :func:`scipy.ndimage.interpolation.affine_transform` where it controls
        the order of the spline.
    scale : float
        A scale factor for the image. Default is no scaling.
    image_center : tuple
        The point in the image to rotate around (axis of rotation).
        Default: center of the array.
    recenter : bool or array-like
        Move the axis of rotation to the center of the array or recenter coords.
        Default: True, recenter to the center of the array.
    missing : float
        The value to replace any missing data after the transformation.
    use_scipy : bool
        Force use of :func:`scipy.ndimage.interpolation.affine_transform`.
        Will set all NaNs in image to zero before doing the transform.
        Default: False unless sckit-image is not installed.

    Returns
    -------
    out : New rotated, scaled and translated image.

    Notes
    -----
    This function is used throughout the SunPy code base as an equivalent to
    the IDL's rot() function. However, this function does not use the same
    algorithm as the IDL rot() function.
    IDL's rot() calls the `POLY_2D <http://www.exelisvis.com/docs/poly_2d.html>`_
    method to calculate the inverse mapping of original to target pixel
    coordinates. This is a polynominal geometrical transformation.
    Then optionally it uses a bicubic convolution interpolation
    algorithm to map the original to target pixel values.

    This algorithm uses an affine transformation as opposed to a polynomial
    geometrical transformation. It then defaults to a bicubic convolution
    interpolation.

    The two affine transform routines this method,
    :func:`scipy.ndimage.interpolation.affine_transform` and
    :func:`skimage.transform.warp` do not use the same interpolation
    algorithm and therefore should not be expected to give identical output,
    the scikit-image routines are preferred.

    No direct comparision between this function and the IDL rot() function has
    been performed as it is a different transformation, however both are
    assumed to be mathamatically vaild.
    """

    rmatrix = rmatrix / scale
    array_center = (np.array(image.shape)[::-1]-1)/2.0

    # Make sure the image center is an array and is where it's supposed to be
    if image_center is not None:
        image_center = np.asanyarray(image_center)
    else:
        image_center = array_center

    # Determine center of rotation based on use (or not) of the recenter keyword
    if recenter:
        rot_center = array_center
    else:
        rot_center = image_center

    displacement = np.dot(rmatrix, rot_center)
    shift = image_center - displacement

    if use_scipy or scikit_image_not_found:
        if np.any(np.isnan(image)):
            warnings.warn("Setting NaNs to 0 for SciPy rotation", Warning)
        # Transform the image using the scipy affine transform
        rotated_image = scipy.ndimage.interpolation.affine_transform(
                np.nan_to_num(image).T, rmatrix, offset=shift, order=order,
                mode='constant', cval=missing).T
    else:
        # Make the rotation matrix 3x3 to include translation of the image
        skmatrix = np.zeros((3, 3))
        skmatrix[:2, :2] = rmatrix
        skmatrix[2, 2] = 1.0
        skmatrix[:2, 2] = shift
        tform = skimage.transform.AffineTransform(skmatrix)

        # Transform the image using the skimage function
        # Image data is normalised because warp() requires an array of values
        # between -1 and 1.
        adjusted_image = np.copy(image)
        im_min = np.nanmin(adjusted_image)
        adjusted_image -= im_min
        im_max = np.nanmax(adjusted_image)
        adjusted_image /= im_max
        rotated_image = skimage.transform.warp(adjusted_image, tform, order=order,
                                               mode='constant', cval=missing)


        rotated_image *= im_max
        rotated_image += im_min
    return rotated_image
<commit_msg>Add explanation of defualt order to affine_transform.<commit_after>"""
Functions for geometrical image transformation and warping.
"""

from __future__ import absolute_import

import warnings

import numpy as np
import scipy.ndimage.interpolation
try:
    import skimage.transform
    scikit_image_not_found = False
except ImportError:  # pragma: no cover
    scikit_image_not_found = True  # pragma: no cover

__all__ = ['affine_transform']

def affine_transform(image, rmatrix, order=3, scale=1.0, image_center=None,
                     recenter=False, missing=0.0, use_scipy=False):
    """
    Rotates, shifts and scales an image using :func:`skimage.transform.warp`, or
    :func:`scipy.ndimage.interpolation.affine_transform` if specified. Falls back to
    the scipy function if scikit-image can't be imported.

    Parameters
    ----------
    image : ndarray
        2D Image to be rotated.
    rmatrix : 2x2
        Linear transformation rotation matrix.
    order : int 0-5
        Interpolation order to be used. When using scikit-image this parameter
        is passed into :func:`skimage.transform.warp`.
        The default value of order 3 is a bicubic interpolation when using
        :func:`skimage.transform.warp` and a cubic spline interpolation
        when using :func:`scipy.ndimage.interpolation.affine_transform`.
        When using scipy it is passed into
        :func:`scipy.ndimage.interpolation.affine_transform` where it controls
        the order of the spline.
    scale : float
        A scale factor for the image. Default is no scaling.
    image_center : tuple
        The point in the image to rotate around (axis of rotation).
        Default: center of the array.
    recenter : bool or array-like
        Move the axis of rotation to the center of the array or recenter coords.
        Default: True, recenter to the center of the array.
    missing : float
        The value to replace any missing data after the transformation.
    use_scipy : bool
        Force use of :func:`scipy.ndimage.interpolation.affine_transform`.
        Will set all NaNs in image to zero before doing the transform.
        Default: False unless sckit-image is not installed.

    Returns
    -------
    out : New rotated, scaled and translated image.

    Notes
    -----
    This function is used throughout the SunPy code base as an equivalent to
    the IDL's rot() function. However, this function does not use the same
    algorithm as the IDL rot() function.
    IDL's rot() calls the `POLY_2D <http://www.exelisvis.com/docs/poly_2d.html>`_
    method to calculate the inverse mapping of original to target pixel
    coordinates. This is a polynominal geometrical transformation.
    Then optionally it uses a bicubic convolution interpolation
    algorithm to map the original to target pixel values.

    This algorithm uses an affine transformation as opposed to a polynomial
    geometrical transformation. It then defaults to a bicubic convolution
    interpolation.

    The two affine transform routines this method,
    :func:`scipy.ndimage.interpolation.affine_transform` and
    :func:`skimage.transform.warp` do not use the same interpolation
    algorithm and therefore should not be expected to give identical output,
    the scikit-image routines are preferred.

    No direct comparision between this function and the IDL rot() function has
    been performed as it is a different transformation, however both are
    assumed to be mathamatically vaild.
    """

    rmatrix = rmatrix / scale
    array_center = (np.array(image.shape)[::-1]-1)/2.0

    # Make sure the image center is an array and is where it's supposed to be
    if image_center is not None:
        image_center = np.asanyarray(image_center)
    else:
        image_center = array_center

    # Determine center of rotation based on use (or not) of the recenter keyword
    if recenter:
        rot_center = array_center
    else:
        rot_center = image_center

    displacement = np.dot(rmatrix, rot_center)
    shift = image_center - displacement

    if use_scipy or scikit_image_not_found:
        if np.any(np.isnan(image)):
            warnings.warn("Setting NaNs to 0 for SciPy rotation", Warning)
        # Transform the image using the scipy affine transform
        rotated_image = scipy.ndimage.interpolation.affine_transform(
                np.nan_to_num(image).T, rmatrix, offset=shift, order=order,
                mode='constant', cval=missing).T
    else:
        # Make the rotation matrix 3x3 to include translation of the image
        skmatrix = np.zeros((3, 3))
        skmatrix[:2, :2] = rmatrix
        skmatrix[2, 2] = 1.0
        skmatrix[:2, 2] = shift
        tform = skimage.transform.AffineTransform(skmatrix)

        # Transform the image using the skimage function
        # Image data is normalised because warp() requires an array of values
        # between -1 and 1.
        adjusted_image = np.copy(image)
        im_min = np.nanmin(adjusted_image)
        adjusted_image -= im_min
        im_max = np.nanmax(adjusted_image)
        adjusted_image /= im_max
        rotated_image = skimage.transform.warp(adjusted_image, tform, order=order,
                                               mode='constant', cval=missing)


        rotated_image *= im_max
        rotated_image += im_min
    return rotated_image

{'fraction_non_alphanumeric': 0.06710576555937381, 'fraction_numerical': 0.006013745704467354, 'mean_word_length': 2.7431225437656304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2850422', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2848, 'n_words': 1083}