<commit_before>from __future__ import division

import numpy as np
from ..util.dtype import dtype_range

__all__ = ['mse', 'nrmse', 'psnr']


def mse(X, Y):
    """Compute the mean-squared error between two images.

    Parameters
    ----------
    X, Y : ndarray
        Image.  Any dimensionality.

    Returns
    -------
    mse : float
        The MSE metric.

    """
    if not X.shape == Y.shape:
        raise ValueError('Input images must have the same dimensions.')
    if not X.dtype == Y.dtype:
        raise ValueError('Input images must have the same dtype.')
    return np.square(X - Y).mean()


def nrmse(im_true, im_test, norm_type='Euclidean'):
    """Compute the normalized root mean-squared error between two images.

    Parameters
    ----------
    im_true : ndarray
        Ground-truth image.
    im_test : ndarray
        Test image.
    norm_type : {'Euclidean', 'min-max', 'mean'}
        Controls the normalization method to use in the denominator of the
        NRMSE.  There is no standard method of normalization across the
        literature [1]_.  The methods available here are as follows:

        - 'Euclidean' : normalize by the Euclidean norm of ``im_true``.
        - 'min-max'   : normalize by the intensity range of ``im_true``.
        - 'mean'      : normalize by the mean of ``im_true``.

    Returns
    -------
    nrmse : float
        The NRMSE metric.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Root-mean-square_deviation

    """
    if not im_true.dtype == im_test.dtype:
        raise ValueError('Input images must have the same dtype.')

    if not im_true.shape == im_test.shape:
        raise ValueError('Input images must have the same dimensions.')

    norm_type = norm_type.lower()
    if norm_type == 'euclidean':
        denom = np.sqrt((im_true*im_true).mean())
    elif norm_type == 'min-max':
        denom = im_true.max() - im_true.min()
    elif norm_type == 'mean':
        denom = im_true.mean()
    else:
        raise ValueError("Unsupported norm_type")
    return np.sqrt(mse(im_true, im_test)) / denom


def psnr(im_true, im_test, dynamic_range=None):
    """ Compute the peak signal to noise ratio (PSNR) for an image.

    Parameters
    ----------
    im_true : ndarray
        Ground-truth image.
    im_test : ndarray
        Test image.
    dynamic_range : int
        The dynamic range of the input image (distance between minimum and
        maximum possible values).  By default, this is estimated from the image
        data-type.

    Returns
    -------
    psnr : float
        The PSNR metric.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio

    """
    if not im_true.dtype == im_test.dtype:
        raise ValueError('Input images must have the same dtype.')

    if not im_true.shape == im_test.shape:
        raise ValueError('Input images must have the same dimensions.')

    if dynamic_range is None:
        dmin, dmax = dtype_range[im_true.dtype.type]
        dynamic_range = dmax - dmin

    im_true = im_true.astype(np.float64)
    im_test = im_test.astype(np.float64)<commit_msg>NRMSE, MSE, PSNR: make sure all inputs are float prior to the calculations.<commit_after>from __future__ import division

import numpy as np
from ..util.dtype import dtype_range

__all__ = ['mse', 'nrmse', 'psnr']


def _as_floats(X, Y):
    """Promote X, Y to nearest appropriate floating point precision."""
    float_type = np.result_type(X.dtype, Y.dtype, np.float32)
    if X.dtype != float_type:
        X = X.astype(float_type)
    if Y.dtype != float_type:
        Y = Y.astype(float_type)
    return X, Y


def mse(X, Y):
    """Compute the mean-squared error between two images.

    Parameters
    ----------
    X, Y : ndarray
        Image.  Any dimensionality.

    Returns
    -------
    mse : float
        The MSE metric.

    """
    X, Y = _as_floats(X, Y)

    if not X.shape == Y.shape:
        raise ValueError('Input images must have the same dimensions.')
    if not X.dtype == Y.dtype:
        raise ValueError('Input images must have the same dtype.')
    return np.square(X - Y).mean()


def nrmse(im_true, im_test, norm_type='Euclidean'):
    """Compute the normalized root mean-squared error between two images.

    Parameters
    ----------
    im_true : ndarray
        Ground-truth image.
    im_test : ndarray
        Test image.
    norm_type : {'Euclidean', 'min-max', 'mean'}
        Controls the normalization method to use in the denominator of the
        NRMSE.  There is no standard method of normalization across the
        literature [1]_.  The methods available here are as follows:

        - 'Euclidean' : normalize by the Euclidean norm of ``im_true``.
        - 'min-max'   : normalize by the intensity range of ``im_true``.
        - 'mean'      : normalize by the mean of ``im_true``.

    Returns
    -------
    nrmse : float
        The NRMSE metric.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Root-mean-square_deviation

    """
    if not im_true.dtype == im_test.dtype:
        raise ValueError('Input images must have the same dtype.')

    if not im_true.shape == im_test.shape:
        raise ValueError('Input images must have the same dimensions.')

    im_true, im_test = _as_floats(im_true, im_test)

    norm_type = norm_type.lower()
    if norm_type == 'euclidean':
        denom = np.sqrt((im_true*im_true).mean())
    elif norm_type == 'min-max':
        denom = im_true.max() - im_true.min()
    elif norm_type == 'mean':
        denom = im_true.mean()
    else:
        raise ValueError("Unsupported norm_type")
    return np.sqrt(mse(im_true, im_test)) / denom


def psnr(im_true, im_test, dynamic_range=None):
    """ Compute the peak signal to noise ratio (PSNR) for an image.

    Parameters
    ----------
    im_true : ndarray
        Ground-truth image.
    im_test : ndarray
        Test image.
    dynamic_range : int
        The dynamic range of the input image (distance between minimum and
        maximum possible values).  By default, this is estimated from the image
        data-type.

    Returns
    -------
    psnr : float
        The PSNR metric.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio

    """
    if not im_true.dtype == im_test.dtype:
        raise ValueError('Input images must have the same dtype.')

    if not im_true.shape == im_test.shape:
        raise ValueError('Input images must have the same dimensions.')

    if dynamic_range is None:
        dmin, dmax = dtype_range[im_true.dtype.type]
        dynamic_range = dmax - dmin

    im_true, im_test = _as_floats(im_true, im_test)
{'fraction_non_alphanumeric': 0.11657261657261657, 'fraction_numerical': 0.001782001782001782, 'mean_word_length': 2.2599225556631173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4533517', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2049, 'n_words': 648}