<commit_before>from neutronpy import tools, functions
import numpy as np
from scipy.integrate import simps
import os
import unittest


class EnergyTest(unittest.TestCase):
    def test_energy(self):
        energy = tools.Neutron(e=25.)
        self.assertAlmostEqual(energy.e, 25.0, 4)
        self.assertAlmostEqual(energy.l, 1.8089, 4)
        self.assertAlmostEqual(energy.k, 3.473, 3)
        self.assertAlmostEqual(energy.v, 2187., 0)
        self.assertAlmostEqual(energy.temp, 290.113, 3)
        self.assertAlmostEqual(energy.freq, 6.045, 3)


class DataTest(unittest.TestCase):
    def build_data(self, clean=True):
        p = np.array([20., 0., 3., -0.15, 0.08, 0.2, 3., 0.15, 0.08, 0.2])
        x = np.linspace(-1, 1, 81)

        if clean:
            y = functions.voigt(p, x)
            mon = 1e5
        else:
            y = functions.voigt(p, x) + np.random.normal(loc=0., scale=5, size=len(x))
            mon = 1e3

        output = tools.Data()
        output.Q = np.vstack((item.ravel() for item in np.meshgrid(x, 0., 0., 0.))).T
        output.detector = y
        output.monitor = np.zeros(x.shape) + mon
        output.temp = np.zeros(x.shape) + 300.

        return output

    def test_load_files(self):
        data1 = tools.Data()
        data1.load_file(os.path.join(os.path.dirname(__file__), 'scan0001.dat'), os.path.join(os.path.dirname(__file__), 'scan0002.dat'), mode='SPICE')

        data2 = tools.Data()
        data2.load_file(os.path.join(os.path.dirname(__file__), 'scan0003.ng5'), mode='ICP')
        
        data3 = tools.Data()
        data3.load_file(os.path.join(os.path.dirname(__file__), 'scan0004.bt7'), mode='ICE')

    def test_combine_data(self):
        data1 = self.build_data(clean=True)
        data2 = self.build_data(clean=False)

        data = data1 + data2

        self.assertTrue((data.monitor == data1.monitor + data2.monitor).all())
        self.assertTrue((data.detector == data1.detector + data2.detector).all())
        self.assertTrue((data.temp == data1.temp).all() & (data.temp == data2.temp).all())
        self.assertTrue((data.Q == data1.Q).all() & (data.Q == data2.Q).all())

    def test_rebin(self):
        data = self.build_data(clean=True)
        Q, monitor, detector, temps = data.bin([-1, 1., 41], [-0.1, 0.1, 1], [-0.1, 0.1, 1], [3.5, 4.5, 1], [-300, 900, 1])

        self.assertEqual(Q.shape[0], 41)
        self.assertEqual(monitor.shape[0], 41)
        self.assertEqual(detector.shape[0], 41)
        self.assertEqual(temps.shape[0], 41)

        self.assertEqual(np.average(monitor), np.average(data.monitor))
        self.assertTrue(abs(simps(detector, Q[:, 0]) - simps(data.detector, data.Q[:, 0])) <= 0.1)

    def test_analysis(self):
        data = self.build_data(clean=True)

        self.assertAlmostEqual(data.integrate(), 45.8424794006, 6)
        self.assertTrue((data.position()[0] < 1e-15))
        self.assertAlmostEqual(data.width()[0], 0.3, 2)


if __name__ == '__main__':
    unittest.main()<commit_msg>Ref: Updated unittests to conform to the new Q format<commit_after>from neutronpy import tools, functions
import numpy as np
from scipy.integrate import simps
import os
import unittest


class EnergyTest(unittest.TestCase):
    def test_energy(self):
        energy = tools.Neutron(e=25.)
        self.assertAlmostEqual(energy.e, 25.0, 4)
        self.assertAlmostEqual(energy.l, 1.8089, 4)
        self.assertAlmostEqual(energy.k, 3.473, 3)
        self.assertAlmostEqual(energy.v, 2187., 0)
        self.assertAlmostEqual(energy.temp, 290.113, 3)
        self.assertAlmostEqual(energy.freq, 6.045, 3)


class DataTest(unittest.TestCase):
    def build_data(self, clean=True):
        p = np.array([20., 0., 3., -0.15, 0.08, 0.2, 3., 0.15, 0.08, 0.2])
        x = np.linspace(-1, 1, 81)

        if clean:
            y = functions.voigt(p, x)
            mon = 1e5
        else:
            y = functions.voigt(p, x) + np.random.normal(loc=0., scale=5, size=len(x))
            mon = 1e3

        output = tools.Data()
        output.Q = np.vstack((item.ravel() for item in np.meshgrid(x, 0., 0., 0., 300.))).T
        output.detector = y
        output.monitor = np.zeros(x.shape) + mon

        return output

    def test_load_files(self):
        data1 = tools.Data()
        data1.load_file(os.path.join(os.path.dirname(__file__), 'scan0001.dat'), os.path.join(os.path.dirname(__file__), 'scan0002.dat'), mode='SPICE')

        data2 = tools.Data()
        data2.load_file(os.path.join(os.path.dirname(__file__), 'scan0003.ng5'), mode='ICP')
        
        data3 = tools.Data()
        data3.load_file(os.path.join(os.path.dirname(__file__), 'scan0004.bt7'), mode='ICE')

    def test_combine_data(self):
        data1 = self.build_data(clean=True)
        data2 = self.build_data(clean=False)

        data = data1 + data2

        self.assertTrue((data.monitor == data1.monitor + data2.monitor).all())
        self.assertTrue((data.detector == data1.detector + data2.detector).all())
        self.assertTrue((data.Q == data1.Q).all() & (data.Q == data2.Q).all())

    def test_rebin(self):
        data = self.build_data(clean=True)
        Q, monitor, detector = data.bin([-1, 1., 41], [-0.1, 0.1, 1], [-0.1, 0.1, 1], [3.5, 4.5, 1], [-300, 900, 1])

        self.assertEqual(Q.shape[0], 41)
        self.assertEqual(monitor.shape[0], 41)
        self.assertEqual(detector.shape[0], 41)

        self.assertEqual(np.average(monitor), np.average(data.monitor))
        self.assertTrue(abs(simps(detector, Q[:, 0]) - simps(data.detector, data.Q[:, 0])) <= 0.1)

    def test_analysis(self):
        data = self.build_data(clean=True)

        self.assertAlmostEqual(data.integrate(), 45.8424794006, 6)
        self.assertTrue((data.position()[0] < 1e-15))
        self.assertAlmostEqual(data.width()[0], 0.3, 2)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.16541992519551174, 'fraction_numerical': 0.0569534172050323, 'mean_word_length': 2.649503722084367, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5859079', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2058, 'n_words': 391}