<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import pytest
from ...datasets.make import make_noise_image
from ..core import (MeanBackground, MedianBackground, ModeEstimatorBackground,
                    MMMBackground, SExtractorBackground,
                    BiweightLocationBackground, StdBackgroundRMS,
                    MADStdBackgroundRMS, BiweightMidvarianceBackgroundRMS)


BKG = 0.0
STD = 0.5
DATA = make_noise_image((100, 100), type='gaussian', mean=BKG, stddev=STD,
                        random_state=12345)

BKG_CLASS = [MeanBackground, MedianBackground, ModeEstimatorBackground,
             MMMBackground, SExtractorBackground, BiweightLocationBackground]

RMS_CLASS = [StdBackgroundRMS, MADStdBackgroundRMS,
             BiweightMidvarianceBackgroundRMS]


@pytest.mark.parametrize('bkg_class', BKG_CLASS)
def test_background(bkg_class):
    bkg = bkg_class(sigma=3.0)
    assert_allclose(bkg.calc_background(DATA), BKG, atol=1.e-2)
    assert_allclose(bkg(DATA), bkg.calc_background(DATA))


@pytest.mark.parametrize('bkg_class', BKG_CLASS)
def test_background_axis(bkg_class):
    bkg = bkg_class(sigma=3.0)

    bkg_arr = bkg.calc_background(DATA, axis=0)
    bkgi = []
    for i in range(100):
        bkgi.append(bkg.calc_background(DATA[:, i]))
    bkgi = np.array(bkgi)
    assert_allclose(bkg_arr, bkgi)

    bkg_arr = bkg.calc_background(DATA, axis=1)
    bkgi = []
    for i in range(100):
        bkgi.append(bkg.calc_background(DATA[i, :]))
    bkgi = np.array(bkgi)
    assert_allclose(bkg_arr, bkgi)


def test_sextrator_background_zero_std():
    data = np.ones((100, 100))
    bkg = SExtractorBackground(sigclip=False)
    assert_allclose(bkg.calc_background(data), 1.0)


def test_sextrator_background_skew():
    data = np.arange(100)
    data[70:] = 1.e7
    bkg = SExtractorBackground(sigclip=False)
    assert_allclose(bkg.calc_background(data), np.median(data))


@pytest.mark.parametrize('rms_class', RMS_CLASS)
def test_background_rms(rms_class):
    bkgrms = rms_class(sigma=3.0)
    assert_allclose(bkgrms.calc_background_rms(DATA), STD, atol=1.e-2)
    assert_allclose(bkgrms(DATA), bkgrms.calc_background_rms(DATA))
<commit_msg>Test that background is scalar if axis is None<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import pytest
from ...datasets.make import make_noise_image
from ..core import (MeanBackground, MedianBackground, ModeEstimatorBackground,
                    MMMBackground, SExtractorBackground,
                    BiweightLocationBackground, StdBackgroundRMS,
                    MADStdBackgroundRMS, BiweightMidvarianceBackgroundRMS)


BKG = 0.0
STD = 0.5
DATA = make_noise_image((100, 100), type='gaussian', mean=BKG, stddev=STD,
                        random_state=12345)

BKG_CLASS = [MeanBackground, MedianBackground, ModeEstimatorBackground,
             MMMBackground, SExtractorBackground, BiweightLocationBackground]

RMS_CLASS = [StdBackgroundRMS, MADStdBackgroundRMS,
             BiweightMidvarianceBackgroundRMS]


@pytest.mark.parametrize('bkg_class', BKG_CLASS)
def test_background(bkg_class):
    bkg = bkg_class(sigma=3.0)
    bkgval = bkg.calc_background(DATA)
    assert not np.ma.isMaskedArray(bkgval)
    assert_allclose(bkgval, BKG, atol=1.e-2)
    assert_allclose(bkg(DATA), bkg.calc_background(DATA))


@pytest.mark.parametrize('bkg_class', BKG_CLASS)
def test_background_axis(bkg_class):
    bkg = bkg_class(sigma=3.0)

    bkg_arr = bkg.calc_background(DATA, axis=0)
    bkgi = []
    for i in range(100):
        bkgi.append(bkg.calc_background(DATA[:, i]))
    bkgi = np.array(bkgi)
    assert_allclose(bkg_arr, bkgi)

    bkg_arr = bkg.calc_background(DATA, axis=1)
    bkgi = []
    for i in range(100):
        bkgi.append(bkg.calc_background(DATA[i, :]))
    bkgi = np.array(bkgi)
    assert_allclose(bkg_arr, bkgi)


def test_sextrator_background_zero_std():
    data = np.ones((100, 100))
    bkg = SExtractorBackground(sigclip=False)
    assert_allclose(bkg.calc_background(data), 1.0)


def test_sextrator_background_skew():
    data = np.arange(100)
    data[70:] = 1.e7
    bkg = SExtractorBackground(sigclip=False)
    assert_allclose(bkg.calc_background(data), np.median(data))


@pytest.mark.parametrize('rms_class', RMS_CLASS)
def test_background_rms(rms_class):
    bkgrms = rms_class(sigma=3.0)
    assert_allclose(bkgrms.calc_background_rms(DATA), STD, atol=1.e-2)
    assert_allclose(bkgrms(DATA), bkgrms.calc_background_rms(DATA))

{'fraction_non_alphanumeric': 0.103750509580106, 'fraction_numerical': 0.019975540154912354, 'mean_word_length': 4.063983488132095, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3298593', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1711, 'n_words': 295}