<commit_before>from __future__ import division

class Test(object):

    def __init__(self):
        pass

    def run(self):
        self.tst_identical()
        self.tst_scaled()

    def tst_identical(self):

        from dials.algorithms.integration.profile import ProfileFitting
        from scitbx.array_family import flex
        from tst_profile_helpers import gaussian

        # Create profile
        p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        s = flex.sum(p)
        p = p / s

        # Copy profile
        c = p.deep_copy()
        b = flex.double(flex.grid(9, 9, 9), 0)

        # Fit
        fit = ProfileFitting(p, c, b)
        I = fit.intensity()
        V = fit.variance()

        # Test intensity is the same
        eps = 1e-7
        assert(abs(I - flex.sum(p)) < eps)
        assert(abs(V - I) < eps)

        print 'OK'

    def tst_scaled(self):

        from dials.algorithms.integration.profile import ProfileFitting
        from scitbx.array_family import flex
        from tst_profile_helpers import gaussian

        # Create profile
        p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        s = flex.sum(p)
        p = p / s

        # Copy profile
        c = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        b = flex.double(flex.grid(9, 9, 9), 0)

        # Fit
        fit = ProfileFitting(p, c, b)
        I = fit.intensity()
        V = fit.variance()

        # Test intensity is the same
        eps = 1e-7
        assert(abs(I - flex.sum(c)) < eps)
        assert(abs(V - I) < eps)

        print 'OK'

if __name__ == '__main__':
    test = Test()
    test.run()
<commit_msg>Added tests for profile fitting with background.<commit_after>from __future__ import division

class Test(object):

    def __init__(self):
        pass

    def run(self):
        self.tst_identical()
        self.tst_scaled()
        self.tst_with_flat_background()
        self.tst_with_noisy_flat_background()

    def tst_identical(self):

        from dials.algorithms.integration.profile import ProfileFitting
        from scitbx.array_family import flex
        from tst_profile_helpers import gaussian

        # Create profile
        p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        s = flex.sum(p)
        p = p / s

        # Copy profile
        c = p.deep_copy()
        b = flex.double(flex.grid(9, 9, 9), 0)

        # Fit
        fit = ProfileFitting(p, c, b)
        I = fit.intensity()
        V = fit.variance()

        # Test intensity is the same
        eps = 1e-7
        assert(abs(I - flex.sum(p)) < eps)
        assert(abs(V - I) < eps)

        print 'OK'

    def tst_scaled(self):

        from dials.algorithms.integration.profile import ProfileFitting
        from scitbx.array_family import flex
        from tst_profile_helpers import gaussian

        # Create profile
        p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        s = flex.sum(p)
        p = p / s

        # Copy profile
        c = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        b = flex.double(flex.grid(9, 9, 9), 0)

        # Fit
        fit = ProfileFitting(p, c, b)
        I = fit.intensity()
        V = fit.variance()

        # Test intensity is the same
        eps = 1e-7
        assert(abs(I - flex.sum(c)) < eps)
        assert(abs(V - I) < eps)

        print 'OK'

    def tst_with_flat_background(self):

        from dials.algorithms.integration.profile import ProfileFitting
        from scitbx.array_family import flex
        from tst_profile_helpers import gaussian

        # Create profile
        p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        s = flex.sum(p)
        p = p / s

        # Copy profile
        c0 = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        b = flex.double(flex.grid(9, 9, 9), 5)
        c = c0 + b

        # Fit
        fit = ProfileFitting(p, c, b)
        I = fit.intensity()
        V = fit.variance()

        # Test intensity is the same
        eps = 1e-7
        assert(abs(I - flex.sum(c0)) < eps)
        assert(abs(V - (flex.sum(c0) + flex.sum(b))) < eps)

        print 'OK'

    def tst_with_noisy_flat_background(self):

        from dials.algorithms.integration.profile import ProfileFitting
        from scitbx.array_family import flex
        from tst_profile_helpers import gaussian

        # Create profile
        p = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        s = flex.sum(p)
        p = p / s

        # Copy profile
        c0 = gaussian((9, 9, 9), 1, (4, 4, 4), (2, 2, 2))
        n = flex.random_double(9 * 9 * 9)
        b = flex.double(flex.grid(9, 9, 9), 5) + n
        c = c0 + b

        # Fit
        fit = ProfileFitting(p, c, b)
        I = fit.intensity()
        V = fit.variance()

        # Test intensity is the same
        eps = 1e-7
        assert(abs(I - flex.sum(c0)) < eps)
        assert(abs(V - (flex.sum(c0) + flex.sum(b))) < eps)

        print 'OK'

if __name__ == '__main__':
    test = Test()
    test.run()

{'fraction_non_alphanumeric': 0.1272763658194917, 'fraction_numerical': 0.029417650590354213, 'mean_word_length': 1.7552370452039692, 'pattern_counts': {'":': 0, '<': 15, '<?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': '4439150', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1714, 'n_words': 485}