# Path: modl/input_data/image.py
# def scale_patches(X, with_mean=True, with_std=True, channel_wise=True, copy=True):
#     if copy:
#         X = X.copy()
#     if with_mean:
#         if channel_wise:
#             X -= np.mean(X, axis=(1, 2))[:, np.newaxis, np.newaxis, :]
#         else:
#             X -= np.mean(X, axis=(1, 2, 3))[:,
#                  np.newaxis, np.newaxis, np.newaxis]
#     if with_std:
#         if channel_wise:
#             n_channel = X.shape[3]
#             std = np.sqrt(np.sum(X ** 2, axis=(1, 2)))
#             std[std == 0] = 1
#             X /= std[:, np.newaxis, np.newaxis, :] * sqrt(n_channel)
#         else:
#             std = np.sqrt(np.sum(X ** 2, axis=(1, 2, 3)))
#             std[std == 0] = 1
#             X /= std[:, np.newaxis, np.newaxis, np.newaxis]
#     return X


import numpy as np
from modl.input_data.image import scale_patches
from modl.input_data.image_fast import clean_mask, fill
from numpy.testing import assert_array_almost_equal, assert_array_equal
from sklearn.feature_extraction.image import extract_patches
from sklearn.utils import check_random_state


def test_scale_patches():
    patch_size = (8, 8, 3)
    n = 100
    shape = (n, ) + patch_size
    rs = check_random_state(0)
    X = rs.randn(*shape)
    Y = scale_patches(X, with_mean=True, with_std=True, channel_wise=True)
    assert_array_almost_equal(Y.mean(axis=(1, 2)), 0)
    assert_array_almost_equal(np.sum(Y ** 2, axis=(1, 2)), 1 / 3)

    scale_patches(X, with_mean=True, with_std=True, channel_wise=True,
                  copy=False)
    assert_array_equal(X, Y)

    X = rs.randn(*shape)


# Path: modl/input_data/image.py
# def scale_patches(X, with_mean=True, with_std=True, channel_wise=True, copy=True):
#     if copy:
#         X = X.copy()
#     if with_mean:
#         if channel_wise:
#             X -= np.mean(X, axis=(1, 2))[:, np.newaxis, np.newaxis, :]
#         else:
#             X -= np.mean(X, axis=(1, 2, 3))[:,
#                  np.newaxis, np.newaxis, np.newaxis]
#     if with_std:
#         if channel_wise:
#             n_channel = X.shape[3]
#             std = np.sqrt(np.sum(X ** 2, axis=(1, 2)))
#             std[std == 0] = 1
#             X /= std[:, np.newaxis, np.newaxis, :] * sqrt(n_channel)
#         else:
#             std = np.sqrt(np.sum(X ** 2, axis=(1, 2, 3)))
#             std[std == 0] = 1
#             X /= std[:, np.newaxis, np.newaxis, np.newaxis]
#     return X


# Path: modl/input_data/tests/test_image.py
import numpy as np
from modl.input_data.image import scale_patches
from modl.input_data.image_fast import clean_mask, fill
from numpy.testing import assert_array_almost_equal, assert_array_equal
from sklearn.feature_extraction.image import extract_patches
from sklearn.utils import check_random_state



def test_scale_patches():
    patch_size = (8, 8, 3)
    n = 100
    shape = (n, ) + patch_size
    rs = check_random_state(0)
    X = rs.randn(*shape)
    Y = scale_patches(X, with_mean=True, with_std=True, channel_wise=True)
    assert_array_almost_equal(Y.mean(axis=(1, 2)), 0)
    assert_array_almost_equal(np.sum(Y ** 2, axis=(1, 2)), 1 / 3)

    scale_patches(X, with_mean=True, with_std=True, channel_wise=True,
                  copy=False)
    assert_array_equal(X, Y)

    X = rs.randn(*shape)


    Y = scale_patches(X, with_mean=False, with_std=True, channel_wise=True)