# Path: photutils/utils/_moments.py
# def _moments(data, order=1):
#     """
#     Calculate the raw image moments up to the specified order.
# 
#     Parameters
#     ----------
#     data : 2D array-like
#         The input 2D array.
# 
#     order : int, optional
#         The maximum order of the moments to calculate.
# 
#     Returns
#     -------
#     moments : 2D `~numpy.ndarray`
#         The raw image moments.
#     """
#     return _moments_central(data, center=(0, 0), order=order)
# 
# def _moments_central(data, center=None, order=1):
#     """
#     Calculate the central image moments up to the specified order.
# 
#     Parameters
#     ----------
#     data : 2D array-like
#         The input 2D array.
# 
#     center : tuple of two floats or `None`, optional
#         The ``(x, y)`` center position.  If `None` it will calculated as
#         the "center of mass" of the input ``data``.
# 
#     order : int, optional
#         The maximum order of the moments to calculate.
# 
#     Returns
#     -------
#     moments : 2D `~numpy.ndarray`
#         The central image moments.
#     """
#     data = np.asarray(data).astype(float)
# 
#     if data.ndim != 2:
#         raise ValueError('data must be a 2D array.')
# 
#     if center is None:
#         from ..centroids import centroid_com
#         center = centroid_com(data)
# 
#     indices = np.ogrid[[slice(0, i) for i in data.shape]]
#     ypowers = (indices[0] - center[1]) ** np.arange(order + 1)
#     xpowers = np.transpose(indices[1] - center[0]) ** np.arange(order + 1)
# 
#     return np.dot(np.dot(np.transpose(ypowers), data), xpowers)


import numpy as np
import pytest
from numpy.testing import assert_equal, assert_allclose
from .._moments import _moments, _moments_central



def test_moments():
    data = np.array([[0, 1], [0, 1]])
    moments = _moments(data, order=2)
    result = np.array([[2, 2, 2], [1, 1, 1], [1, 1, 1]])

    assert_equal(moments, result)
    assert_allclose(moments[0, 1] / moments[0, 0], 1.0)
    assert_allclose(moments[1, 0] / moments[0, 0], 0.5)


def test_moments_central():
    data = np.array([[0, 1], [0, 1]])
    moments = _moments_central(data, order=2)
    result = np.array([[2., 0., 0.], [0., 0., 0.], [0.5, 0., 0.]])
    assert_allclose(moments, result)


def test_moments_central_nonsquare():
    data = np.array([[0, 1], [0, 1], [0, 1]])
    moments = _moments_central(data, order=2)
    result = np.array([[3., 0., 0.], [0., 0., 0.], [2., 0., 0.]])
    assert_allclose(moments, result)


def test_moments_central_invalid_dim():
    data = np.arange(27).reshape(3, 3, 3)
    with pytest.raises(ValueError):

# Path: photutils/utils/_moments.py
# def _moments(data, order=1):
#     """
#     Calculate the raw image moments up to the specified order.
# 
#     Parameters
#     ----------
#     data : 2D array-like
#         The input 2D array.
# 
#     order : int, optional
#         The maximum order of the moments to calculate.
# 
#     Returns
#     -------
#     moments : 2D `~numpy.ndarray`
#         The raw image moments.
#     """
#     return _moments_central(data, center=(0, 0), order=order)
# 
# def _moments_central(data, center=None, order=1):
#     """
#     Calculate the central image moments up to the specified order.
# 
#     Parameters
#     ----------
#     data : 2D array-like
#         The input 2D array.
# 
#     center : tuple of two floats or `None`, optional
#         The ``(x, y)`` center position.  If `None` it will calculated as
#         the "center of mass" of the input ``data``.
# 
#     order : int, optional
#         The maximum order of the moments to calculate.
# 
#     Returns
#     -------
#     moments : 2D `~numpy.ndarray`
#         The central image moments.
#     """
#     data = np.asarray(data).astype(float)
# 
#     if data.ndim != 2:
#         raise ValueError('data must be a 2D array.')
# 
#     if center is None:
#         from ..centroids import centroid_com
#         center = centroid_com(data)
# 
#     indices = np.ogrid[[slice(0, i) for i in data.shape]]
#     ypowers = (indices[0] - center[1]) ** np.arange(order + 1)
#     xpowers = np.transpose(indices[1] - center[0]) ** np.arange(order + 1)
# 
#     return np.dot(np.dot(np.transpose(ypowers), data), xpowers)


# Path: photutils/utils/tests/test_moments.py
import numpy as np
import pytest
from numpy.testing import assert_equal, assert_allclose
from .._moments import _moments, _moments_central




def test_moments():
    data = np.array([[0, 1], [0, 1]])
    moments = _moments(data, order=2)
    result = np.array([[2, 2, 2], [1, 1, 1], [1, 1, 1]])

    assert_equal(moments, result)
    assert_allclose(moments[0, 1] / moments[0, 0], 1.0)
    assert_allclose(moments[1, 0] / moments[0, 0], 0.5)


def test_moments_central():
    data = np.array([[0, 1], [0, 1]])
    moments = _moments_central(data, order=2)
    result = np.array([[2., 0., 0.], [0., 0., 0.], [0.5, 0., 0.]])
    assert_allclose(moments, result)


def test_moments_central_nonsquare():
    data = np.array([[0, 1], [0, 1], [0, 1]])
    moments = _moments_central(data, order=2)
    result = np.array([[3., 0., 0.], [0., 0., 0.], [2., 0., 0.]])
    assert_allclose(moments, result)


def test_moments_central_invalid_dim():
    data = np.arange(27).reshape(3, 3, 3)
    with pytest.raises(ValueError):

        _moments_central(data, order=3)