<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst

def test_array_shapes():
    from pytest import raises
    import numpy as np
    from ..aperture import annulus_elliptical

    data = np.ones((10, 10), dtype=np.float)

    # Input single object and single aperture
    flux = annulus_elliptical(data, 5., 5., 2., 5., 5., np.pi / 4.)
    assert np.isscalar(flux) == True

    # One object, multiple apertures
    flux = annulus_elliptical(data, 2., 5.,
                       [[2.],[5.]], [[5.],[8.]], [[5.],[8.]], np.pi/2.)
    assert flux.shape == (2, 1)

    # Multiple objects, single aperture
    flux = annulus_elliptical(data, [2., 3., 4.], [5., 6., 7.],
                       2., 5., 5., np.pi/2.)
    assert flux.shape == (3,)

    # Multiple objects, multiple apertures
    flux = annulus_elliptical(data, [2., 3., 4.], [5., 6., 7.],
                       [2.,3.,4.], [5.,6.,7.], [5.,6.,7.], np.pi/2.)
    assert flux.shape == (3,)

    # Multiple objects, multiple apertures per object
    flux = annulus_elliptical(data, [2., 3., 4.], [5., 6., 7.],
                       [[2.], [5.]], [[5.], [8.]], [[5.], [8.]], np.pi/2.)
    assert flux.shape == (2, 3)

    # Mismatched number of objects and apertures
    with raises(ValueError):
        flux = annulus_elliptical(data, [2., 3., 4.], [5., 6., 7.],
                           [2.,3.], [5.,6.], [5.,6.], np.pi/2.)


def test_in_and_out_of_array():
    from ..aperture import annulus_elliptical
    import numpy as np

    # An annulus fully in the array
    data = np.ones((40, 40), dtype=np.float)
    a_in = 5.
    a_out = 10.
    b_out = 5.
    theta = np.pi / 4.
    flux = annulus_elliptical(data, 20., 20., a_in, a_out, b_out, theta)
    true_flux = np.pi * (a_out * b_out - a_in ** 2 * b_out / a_out)
    assert abs((flux - true_flux) / true_flux) < 0.01

    # An annulus fully out of the array should return 0
    a_in = 5.
    a_out = 10.
    b_out = 5.
    theta = np.pi / 4.
    flux = annulus_elliptical(data, 60., 60., a_in, a_out, b_out, theta)
    true_flux = np.pi * (a_out * b_out - a_in ** 2 * b_out / a_out)
    assert abs(flux) < 0.001


def test_error_and_gain():

    from ..aperture import aperture_elliptical
    import numpy as np

    data = np.ones((40, 40), dtype=np.float)
    xc = 20.
    yc = 20.
    a = 10.
    b = 5.
    theta = -np.pi / 4.
    area = np.pi * a * b
    true_flux = area

    # Scalar error, no gain.
    error = 1.
    flux, fluxerr = aperture_elliptical(data, xc, yc, a, b, theta,
                                        error=error)
    assert abs((flux - true_flux) / true_flux) < 0.01
    true_error = error * np.sqrt(area)
    # Error should be exact to machine precision for apertures 
    # with defined area.
    assert abs((fluxerr - true_error) / true_error) < 0.01

    # Scalar error, scalar gain.
    error = 1.
    gain = 1.
    flux, fluxerr = aperture_elliptical(data, xc, yc, a, b, theta,
                                        error=error, gain=gain)
    assert abs((flux - true_flux) / true_flux) < 0.01
    true_error = np.sqrt(error ** 2 * area + flux)
    assert abs((fluxerr - true_error) / true_error) < 0.01

    # Scalar error, Array gain.
    error = 1.
    gain = np.ones(data.shape, dtype=np.float)
    flux, fluxerr = aperture_elliptical(data, xc, yc, a, b, theta,
                                        error=error, gain=gain)
    assert abs((flux - true_flux) / true_flux) < 0.01
    true_error = np.sqrt(error ** 2 * area + flux)
    assert abs((fluxerr - true_error) / true_error) < 0.01

    # Array error, no gain.
    error = np.ones(data.shape, dtype=np.float)
    flux, fluxerr = aperture_elliptical(data, xc, yc, a, b, theta,
                                        error=error)
    assert abs((flux - true_flux) / true_flux) < 0.01
    true_error = np.sqrt(area)
    assert abs((fluxerr - true_error) / true_error) < 0.01

    # Array error, scalar gain.
    error = np.ones(data.shape, dtype=np.float)
    gain = 1.
    flux, fluxerr = aperture_elliptical(data, xc, yc, a, b, theta,
                                        error=error, gain=gain)
    assert abs((flux - true_flux) / true_flux) < 0.01
    true_error = np.sqrt(area + flux)
    assert abs((fluxerr - true_error) / true_error) < 0.01

    # Array error, Array gain.
    error = np.ones(data.shape, dtype=np.float)
    gain = np.ones(data.shape, dtype=np.float)
    flux, fluxerr = aperture_elliptical(data, xc, yc, a, b, theta,
                                        error=error, gain=gain)
    assert abs((flux - true_flux) / true_flux) < 0.01
    true_error = np.sqrt(area + flux)
    assert abs((fluxerr - true_error) / true_error) < 0.01<commit_msg>Changed tests to a class-based system and make tests more granular<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst

import pytest
import numpy as np
from numpy.testing import assert_array_almost_equal_nulp

from ..aperture import annulus_elliptical
from ..aperture import aperture_elliptical


class TestArrayShapes(object):

    def setup_class(self):
        self.data = np.ones((10, 10), dtype=np.float)

    def test_single_object_single_aperture(self):
        # Input single object and single aperture
        flux = annulus_elliptical(self.data, 5., 5., 2., 5., 5., np.pi / 4.)
        assert np.isscalar(flux) == True

    def test_single_object_multiple_apertures(self):
        # One object, multiple apertures
        flux = annulus_elliptical(self.data, 2., 5.,
                           [[2.], [5.]], [[5.], [8.]], [[5.], [8.]], np.pi / 2.)
        assert flux.shape == (2, 1)

    def test_multiple_objects_single_aperture(self):
        # Multiple objects, single aperture
        flux = annulus_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
                           2., 5., 5., np.pi / 2.)
        assert flux.shape == (3, )

    def test_multiple_objects_multiple_apertures(self):
        # Multiple objects, multiple apertures
        flux = annulus_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
                           [2., 3., 4.], [5., 6., 7.], [5., 6., 7.], np.pi / 2.)
        assert flux.shape == (3, )

    def test_multiple_objects_multiple_aperture_per_object(self):
        # Multiple objects, multiple apertures per object
        flux = annulus_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
                           [[2.], [5.]], [[5.], [8.]], [[5.], [8.]], np.pi / 2.)
        assert flux.shape == (2, 3)

    def test_mismatch_object_apertures(self):
        # Mismatched number of objects and apertures
        with pytest.raises(ValueError) as exc:
            annulus_elliptical(self.data, [2., 3., 4.], [5., 6., 7.],
                               [2., 3.], [5., 6.], [5., 6.], np.pi / 2.)
        assert exc.value.args[0] == "trailing dimension of 'apertures' must be 1 or match length of xc, yc"


def test_in_and_out_of_array():
    from ..aperture import annulus_elliptical
    import numpy as np

    # An annulus fully in the array
    data = np.ones((40, 40), dtype=np.float)
    a_in = 5.
    a_out = 10.
    b_out = 5.
    theta = np.pi / 4.
    flux = annulus_elliptical(data, 20., 20., a_in, a_out, b_out, theta)
    true_flux = np.pi * (a_out * b_out - a_in ** 2 * b_out / a_out)
    assert abs((flux - true_flux) / true_flux) < 0.01

    # An annulus fully out of the array should return 0
    a_in = 5.
    a_out = 10.
    b_out = 5.
    theta = np.pi / 4.
    flux = annulus_elliptical(data, 60., 60., a_in, a_out, b_out, theta)
    true_flux = np.pi * (a_out * b_out - a_in ** 2 * b_out / a_out)
    assert abs(flux) < 0.001


class TestErrorGain(object):

    def setup_class(self):
        self.data = np.ones((40, 40), dtype=np.float)
        self.xc = 20.
        self.yc = 20.
        self.a = 10.
        self.b = 5.
        self.theta = -np.pi / 4.
        self.area = np.pi * self.a * self.b
        self.true_flux = self.area

    def test_scalar_error_no_gain(self):

        # Scalar error, no gain.
        error = 1.
        flux, fluxerr = aperture_elliptical(self.data, self.xc, self.yc,
                                            self.a, self.b, self.theta,
                                            error=error)
        assert abs((flux - self.true_flux) / self.true_flux) < 0.01

        # Error should be exact to machine precision for apertures
        # with defined area.
        true_error = error * np.sqrt(self.area)
        assert_array_almost_equal_nulp(fluxerr, true_error, 1)

    def test_scalar_error_scalar_gain(self):

        # Scalar error, scalar gain.
        error = 1.
        gain = 1.
        flux, fluxerr = aperture_elliptical(self.data, self.xc, self.yc,
                                            self.a, self.b, self.theta,
                                            error=error, gain=gain)

        assert abs((flux - self.true_flux) / self.true_flux) < 0.01
        true_error = np.sqrt(error ** 2 * self.area + flux)
        assert_array_almost_equal_nulp(fluxerr, true_error, 1)

    def test_scalar_error_array_gain(self):

        # Scalar error, Array gain.
        error = 1.
        gain = np.ones(self.data.shape, dtype=np.float)
        flux, fluxerr = aperture_elliptical(self.data, self.xc, self.yc,
                                            self.a, self.b, self.theta,
                                            error=error, gain=gain)
        assert abs((flux - self.true_flux) / self.true_flux) < 0.01
        true_error = np.sqrt(error ** 2 * self.area + flux)
        assert abs((fluxerr - true_error) / true_error) < 0.01

    def test_array_error_no_gain(self):

        # Array error, no gain.
        error = np.ones(self.data.shape, dtype=np.float)
        flux, fluxerr = aperture_elliptical(self.data, self.xc, self.yc,
                                            self.a, self.b, self.theta,
                                            error=error)
        assert abs((flux - self.true_flux) / self.true_flux) < 0.01
        true_error = np.sqrt(self.area)
        assert abs((fluxerr - true_error) / true_error) < 0.01

    def test_array_error_scalar_gain(self):

        # Array error, scalar gain.
        error = np.ones(self.data.shape, dtype=np.float)
        gain = 1.
        flux, fluxerr = aperture_elliptical(self.data, self.xc, self.yc,
                                            self.a, self.b, self.theta,
                                            error=error, gain=gain)
        assert abs((flux - self.true_flux) / self.true_flux) < 0.01
        true_error = np.sqrt(self.area + flux)
        assert abs((fluxerr - true_error) / true_error) < 0.01

    def test_array_error_array_gain(self):

        # Array error, Array gain.
        error = np.ones(self.data.shape, dtype=np.float)
        gain = np.ones(self.data.shape, dtype=np.float)
        flux, fluxerr = aperture_elliptical(self.data, self.xc, self.yc,
                                            self.a, self.b, self.theta,
                                            error=error, gain=gain)
        assert abs((flux - self.true_flux) / self.true_flux) < 0.01
        true_error = np.sqrt(self.area + flux)
        assert abs((fluxerr - true_error) / true_error) < 0.01
{'fraction_non_alphanumeric': 0.13448367873343414, 'fraction_numerical': 0.029173708085030687, 'mean_word_length': 1.8786482334869432, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7464932', 'n_tokens_mistral': 4133, 'n_tokens_neox': 3698, 'n_words': 992}