diff --git a/.github/workflows/go-rxr-tests.yml b/.github/workflows/go-rxr-tests.yml new file mode 100644 index 00000000..1db7f895 --- /dev/null +++ b/.github/workflows/go-rxr-tests.yml @@ -0,0 +1,50 @@ +# This workflow is designed to set up the necessary Python environment, install dependencies, +# and run the full suite of non-GUI unit tests for the GO-RXR project. It ensures that the code is +# consistently tested on Ubuntu 22.04 with Python 3.10.12. For further details, see: +# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: GO-RXR Tests + +on: + push: + branches: ["main", "version-joss-rev"] + pull_request: + branches: ["main", "version-joss-rev"] + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-22.04 + + steps: + - name: Check out the code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10.12' + + - name: Install dependencies + run: | + pip install --upgrade pip setuptools + pip install -r requirements.txt + + - name: Install PyQt5 Dependencies + run: | + sudo apt-get install -y pyqt5-dev libqt5multimedia5-plugins + + - name: Install Reflectivity Package + run: | + python setup_reflectivity.py install + + - name: Run tests + run: | + python -m unittest discover -s TESTS -p "test_data_fitting.py" + python -m unittest discover -s TESTS -p "test_data_structure.py" + python -m unittest discover -s TESTS -p "test_density.py" + python -m unittest discover -s TESTS -p "test_material_model.py" + python -m unittest discover -s TESTS -p "test_material_structure.py" + python -m unittest discover -s TESTS -p "test_reflectivity.py" diff --git a/GUI_GO.py b/GUI_GO.py index 05e2d51d..d1cf2f58 100644 --- a/GUI_GO.py +++ b/GUI_GO.py @@ -10960,10 +10960,12 @@ def checkbracket(myStr): return True """ -def checkscript(sample, fname=''): +def checkscript(sample, fname='', testing=False): if fname == '': script = os.getcwd() + '/DATA/default_script.txt' + elif testing: + script = fname else: script = os.getcwd() + fname #script = fname diff --git a/README.md b/README.md index 5e806908..e10f9777 100644 --- a/README.md +++ b/README.md @@ -313,3 +313,8 @@ The GO-RXR software package has been utilized for analyzing the RXR data in the If issues feel free to contact me at lsk601@usask.ca. + + +# Test workflow trigger + + diff --git a/TESTS/run_all_tests.py b/TESTS/run_all_tests.py index 4a21ef4f..86eab96e 100644 --- a/TESTS/run_all_tests.py +++ b/TESTS/run_all_tests.py @@ -7,6 +7,8 @@ # Add the parent directory to the system path sys.path.append(parent_dir) +from UTILS import ROOT_DIR + def load_tests_from_file(test_file): # Load the test suite from the given file loader = unittest.TestLoader() @@ -30,5 +32,6 @@ def discover_and_run_tests(test_dir): print(f"Total tests run: {tot_test}") if __name__ == "__main__": - test_dir = "." + + test_dir = ROOT_DIR + '/TESTS' discover_and_run_tests(test_dir) \ No newline at end of file diff --git a/TESTS/test_data_fitting.py b/TESTS/test_data_fitting.py index c405512c..97ecc411 100644 --- a/TESTS/test_data_fitting.py +++ b/TESTS/test_data_fitting.py @@ -12,6 +12,7 @@ material_structure as ms, data_structure as ds, global_optimization as go, + ROOT_DIR, ) from UTILS.global_optimization import changeSampleParams from GUI_GO import checkscript @@ -21,20 +22,21 @@ # into the terminal class TestDataFitting(unittest.TestCase): - + + def __init__(self, methodName: str = "runTest") -> None: + super().__init__(methodName) + + self.root_dir = ROOT_DIR + self.filename = 'Pim4uc_test.h5' + self.my_path = self.root_dir + '/TESTS/test_data/' + self.filename + self.script_path = self.root_dir + '/TESTS/test_data/test_script.txt' + def test_ChangeSampleParams_element(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [[1, 'STRUCTURAL', 'ELEMENT', 'A', 'THICKNESS'], [3, 'STRUCTURAL', 'ELEMENT', 'A', 'THICKNESS'], [2, 'STRUCTURAL', 'ELEMENT', 'Ti', 'DENSITY'], [4, 'STRUCTURAL', 'ELEMENT', 'O', 'DENSITY'], @@ -50,7 +52,8 @@ def test_ChangeSampleParams_element(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + # my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, my_script, orbitals) @@ -93,18 +96,11 @@ def test_ChangeSampleParams_element(self): self.assertListEqual(orbitals['Mn2'], orbitals_new['Mn2']) def test_ChangeSampleParams_compound(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [[0, 'STRUCTURAL', 'COMPOUND','ROUGHNESS',0],[1, 'STRUCTURAL', 'COMPOUND','THICKNESS',1], [2, 'STRUCTURAL', 'COMPOUND','DENSITY',2], [3, 'STRUCTURAL', 'COMPOUND','ROUGHNESS',0], @@ -122,7 +118,7 @@ def test_ChangeSampleParams_compound(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -183,18 +179,11 @@ def test_ChangeSampleParams_compound(self): self.assertListEqual(orbitals['Mn2'], orbitals_new['Mn2']) def test_ChangeSampleParams_variation(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [[0, 'POLYMORPHOUS', 'A', 'Sr'], [1, 'POLYMORPHOUS', 'A', 'La'], [2, 'POLYMORPHOUS', 'A', 'Sr'], [3, 'POLYMORPHOUS', 'Mn', 'Mn2+'], @@ -210,7 +199,7 @@ def test_ChangeSampleParams_variation(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, my_script, orbitals) @@ -282,18 +271,11 @@ def test_ChangeSampleParams_variation(self): self.assertListEqual(orbitals['Mn2'], orbitals_new['Mn2']) def test_ChangeSampleParams_magnetic(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [[3, 'MAGNETIC', 'Mn', 'Mn2+'], [4, 'MAGNETIC', 'Mn', 'Mn3+'], [5, 'MAGNETIC', 'Mn', 'Mn2+'], [6, 'MAGNETIC', 'Mn', 'Mn3+']] @@ -307,7 +289,7 @@ def test_ChangeSampleParams_magnetic(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -349,18 +331,11 @@ def test_ChangeSampleParams_magnetic(self): self.assertListEqual(orbitals['Mn2'], orbitals_new['Mn2']) def test_ChangeSampleParams_eShift(self): + # test scattering factors - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' + sample = ds.ReadSampleHDF5(self.my_path) - sample = ds.ReadSampleHDF5(my_path) - - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [['SCATTERING FACTOR', 'STRUCTURAL', 'Sr'], ['SCATTERING FACTOR', 'STRUCTURAL', 'Ti'], ['SCATTERING FACTOR', 'STRUCTURAL', 'O'], ['SCATTERING FACTOR', 'STRUCTURAL', 'La'], @@ -375,7 +350,7 @@ def test_ChangeSampleParams_eShift(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -399,18 +374,11 @@ def test_ChangeSampleParams_eShift(self): self.assertEqual(sample_new.eShift[key], solutions[key]) def test_ChangeSampleParams_eShift_mag(self): - # test magnetic scattering factors - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # test magnetic scattering factors + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [['SCATTERING FACTOR', 'MAGNETIC', 'Co'], ['SCATTERING FACTOR', 'MAGNETIC', 'Ni']] @@ -423,7 +391,7 @@ def test_ChangeSampleParams_eShift_mag(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -446,18 +414,11 @@ def test_ChangeSampleParams_eShift_mag(self): self.assertEqual(sample_new.mag_eShift[key], solutions[key]) def test_ChangeSampleParams_orbitals(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [['ORBITAL', 'DEXY', 'Mn2'], ['ORBITAL', 'DEXZYZ', 'Mn2'], ['ORBITAL', 'DEX2Y2', 'Mn2'], ['ORBITAL', 'DEZZ', 'Mn2']] @@ -470,7 +431,7 @@ def test_ChangeSampleParams_orbitals(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -507,18 +468,11 @@ def test_ChangeSampleParams_orbitals(self): self.assertListEqual(orbitals_solution['Mn2'], orbitals_new['Mn2']) def test_ChangeSampleParams_scaling(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [['SCALING FACTOR', '107_799.82_S'], ['SCALING FACTOR', '51-52_640.2_AL_Asymm'], ['SCALING FACTOR', '110_E600.18_Th25.0_LC'], ['SCALING FACTOR', '114-115_E600.18_Th35.0_AC_Asymm'], @@ -533,7 +487,7 @@ def test_ChangeSampleParams_scaling(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -576,19 +530,11 @@ def test_ChangeSampleParams_scaling(self): self.assertEqual(scaleF_solutions[skey], scaleF_new[skey]) def test_ChangeSampleParams_all(self): - # Tests to element fit - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [['SCALING FACTOR', 'ALL SCANS'], ['BACKGROUND SHIFT', 'ALL SCANS']] @@ -601,7 +547,7 @@ def test_ChangeSampleParams_all(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -644,19 +590,11 @@ def test_ChangeSampleParams_all(self): self.assertEqual(scaleF_solutions[skey], scaleF_new[skey]) def test_ChangeSampleParams_shift(self): - # Tests to element fit - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [['BACKGROUND SHIFT', '107_799.82_S'], ['BACKGROUND SHIFT', '51-52_640.2_AL_Asymm'], ['BACKGROUND SHIFT', '110_E600.18_Th25.0_LC'], @@ -672,7 +610,7 @@ def test_ChangeSampleParams_shift(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -715,18 +653,11 @@ def test_ChangeSampleParams_shift(self): self.assertEqual(backS_solutions[skey], backS[skey]) def test_ChangeSampleParams_script(self): - # Tests to element fit - filename = 'Pim4uc_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - else: - my_path = os.getcwd() + '/test_data/' + filename - script_name = '/test_data/test_script.txt' - sample = ds.ReadSampleHDF5(my_path) + # Tests to element fit + sample = ds.ReadSampleHDF5(self.my_path) - data, data_dict, sim_dict = ds.ReadDataHDF5(my_path) + data, data_dict, sim_dict = ds.ReadDataHDF5(self.my_path) parameters = [] x = [] @@ -737,7 +668,7 @@ def test_ChangeSampleParams_script(self): backS[name] = 0 scaleF[name] = 1 - my_script, problem, my_error = checkscript(sample, fname=script_name) # load in the script + my_script, problem, my_error = checkscript(sample, fname=self.script_path, testing=True) # load in the script orbitals = {'Mn2': [0, 0, 0, 0]} # orbitals sample_new, backS_new, scaleF_new, orbitals_new = changeSampleParams(x, parameters, sample, backS, scaleF, @@ -762,5 +693,6 @@ def test_ChangeSampleParams_script(self): # test orbital self.assertListEqual(orbitals['Mn2'], orbitals_new['Mn2']) + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/TESTS/test_data_structure.py b/TESTS/test_data_structure.py index a1290198..4a28ea83 100644 --- a/TESTS/test_data_structure.py +++ b/TESTS/test_data_structure.py @@ -9,6 +9,8 @@ import numpy as np import unittest from UTILS.data_structure import * +from UTILS import ROOT_DIR + # Define epsilon using np.finfo(float).eps EPS = np.sqrt(np.finfo(float).eps) @@ -18,6 +20,20 @@ # into the terminal class TestMaterialStructure(unittest.TestCase): + + def __init__(self, methodName: str = "runTest") -> None: + super().__init__(methodName) + + self.root_dir = ROOT_DIR + + self.filename1 = 'Pim7uc.all' + self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1 + + self.filename2 = 'Pim7uc.pkl' + self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2 + + + def test_getTitleInfo(self): # testing function that retrieves information from ReMagX data names @@ -41,16 +57,8 @@ def test_getTitleInfo(self): self.assertEqual(A, angle[i]) def test_ReadReMagX(self): - filename = 'Pim7uc.all' - filename2 = 'Pim7uc.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - my_path_2 = os.getcwd() + '/test_data/' + filename2 - else: - my_path = os.getcwd() + '/test_data/' + filename - my_path_2 = os.getcwd() + '/test_data/' + filename2 - data_info, data_dict = Read_ReMagX(my_path) + data_info, data_dict = Read_ReMagX(self.my_path1) data_info_solution = [[2, 'Reflectivity', '8_399.39_S'], [3, 'Reflectivity', '13_499.93_S'], [4, 'Reflectivity', '18_600.18_S'], [5, 'Reflectivity', '23_700.14_S'], @@ -134,7 +142,7 @@ def test_ReadReMagX(self): for x, y in zip(data_info, data_info_solution): self.assertAlmostEqual(x, y, delta=EPS) - with open(my_path_2, 'rb') as file: + with open(self.my_path2, 'rb') as file: data_dict_solution = pickle.load(file) for key in data_dict.keys(): for param in data_dict[key].keys(): @@ -163,6 +171,7 @@ def test_ReadReMagX(self): elif param == 'Angle': self.assertEqual(data_dict[key][param], data_dict_solution[key][param]) + def test_evaluateList(self): # tests to make sure that a simple list can be evaluated tests = ['[0,1]', '[0.01, 0.52, 20, 800]', '[0.01 , 0.21 , 0.55, 2]', '[0 2 6 1313 22]', '[0 1 2 0.02]'] @@ -173,6 +182,7 @@ def test_evaluateList(self): value = evaluate_list(test) self.assertListEqual(value, solutions[i]) + def test_find_parameter_bound(self): # seperate the parameter boundaries tests = ['[1,[0,1]]', '[0.5, [ 0.25, 1]], [10, [5 ,12]]', '[0.028, [ 0.01, 0.5]], [4, [2 ,11.5]], [6, [5 ,7]]', @@ -266,8 +276,7 @@ def test_evaluate_parameters(self): for i, test in enumerate(tests): value = evaluate_parameters(test) - self.assertListEqual(value, solution[i]) - + self.assertListEqual(value, solution[i]) if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/TESTS/test_density.py b/TESTS/test_density.py index a128e1b7..9fdf5ce7 100644 --- a/TESTS/test_density.py +++ b/TESTS/test_density.py @@ -14,6 +14,8 @@ importlib.reload(ms) import numpy as np +from UTILS import ROOT_DIR + # Define epsilon using np.finfo(float).eps EPS = np.sqrt(np.finfo(float).eps) @@ -22,6 +24,36 @@ # into the terminal class TestDensityProfile(unittest.TestCase): + + def __init__(self, methodName: str = "runTest") -> None: + super().__init__(methodName) + + self.root_dir = ROOT_DIR + + self.filename1 = 'simple_density.pkl' + self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1 + + self.filename2 = 'var_density.pkl' + self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2 + + self.filename3 = 'surfaceImpurity_density.pkl' + self.my_path3 = self.root_dir + '/TESTS/test_data/' + self.filename3 + + self.filename4 = 'unitCell_density.pkl' + self.my_path4 = self.root_dir + '/TESTS/test_data/' + self.filename4 + + self.filename5 = 'linked_density.pkl' + self.my_path5 = self.root_dir + '/TESTS/test_data/' + self.filename5 + + self.filename6 = 'mag_density.pkl' + self.my_path6 = self.root_dir + '/TESTS/test_data/' + self.filename6 + + self.filename7 = 'dummy_density.pkl' + self.my_path7 = self.root_dir + '/TESTS/test_data/' + self.filename7 + + self.filename8 = 'negative_density.pkl' + self.my_path8 = self.root_dir + '/TESTS/test_data/' + self.filename8 + def test_simple_profile(self): # Test a simple density profile @@ -39,22 +71,14 @@ def test_simple_profile(self): test_case = density test_case['Thickness'] = thickness - filename = 'simple_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path1, 'rb') as file: solution = pickle.load(file) - # len(loaded_data) #Counter(loaded_data) self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same total = sum(sum(abs(test_case[key]-solution[key])) for key in test_case.keys()) - print('total', total) # self.assertEqual(total, 0) # checks to make sure that the values are correct # Assert that the total is close to 0 with an epsilon difference @@ -78,13 +102,7 @@ def test_elementVar(self): test_case = density test_case['Thickness'] = thickness - filename = 'var_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path2, 'rb') as file: solution = pickle.load(file) # len(loaded_data) @@ -108,13 +126,7 @@ def test_surfaceImpurity(self): test_case = density test_case['Thickness'] = thickness - filename = 'surfaceImpurity_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path3, 'rb') as file: solution = pickle.load(file) # len(loaded_data) @@ -155,13 +167,7 @@ def test_unitCell(self): test_case = density test_case['Thickness'] = thickness - filename = 'unitCell_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path4, 'rb') as file: solution = pickle.load(file) # len(loaded_data) @@ -188,16 +194,9 @@ def test_linkedRoughness(self): test_case = density test_case['Thickness'] = thickness - filename = 'linked_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path5, 'rb') as file: solution = pickle.load(file) - # len(loaded_data) # Counter(loaded_data) self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same @@ -225,13 +224,7 @@ def test_magnetic(self): for key in mag_density: test_case['Mag:' + key] = mag_density[key] - filename = 'mag_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path6, 'rb') as file: solution = pickle.load(file) # len(loaded_data) @@ -256,16 +249,9 @@ def test_dummy(self): test_case = density test_case['Thickness'] = thickness - filename = 'dummy_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path7, 'rb') as file: solution = pickle.load(file) - # len(loaded_data) # Counter(loaded_data) self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same @@ -289,16 +275,9 @@ def test_negative_test(self): test_case = density test_case['Thickness'] = thickness - filename = 'negative_density.pkl' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - with open(my_path, 'rb') as file: + with open(self.my_path8, 'rb') as file: solution = pickle.load(file) - # len(loaded_data) # Counter(loaded_data) self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same @@ -307,5 +286,6 @@ def test_negative_test(self): self.assertEqual(total,0) # checks to make sure that the values are correct + if __name__ == '__main__': unittest.main() diff --git a/TESTS/test_material_model.py b/TESTS/test_material_model.py index 373b4457..fef543c8 100644 --- a/TESTS/test_material_model.py +++ b/TESTS/test_material_model.py @@ -9,6 +9,7 @@ import UTILS.material_model as mm import numpy as np import UTILS.material_structure as ms +from UTILS import ROOT_DIR import unittest # This test script can be executed by inputting @@ -16,6 +17,18 @@ # into the terminal class TestMaterialModel(unittest.TestCase): + + def __init__(self, methodName: str = "runTest") -> None: + super().__init__(methodName) + + self.root_dir = ROOT_DIR + + self.filename1 = 'optical_energy.txt' + self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1 + + self.filename2 = 'optical_theta.txt' + self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2 + def test_form_factors_E(self): form_factors = ['La','Mn','O'] @@ -87,13 +100,8 @@ def test_form_factors_Elist_mag(self): self.assertTrue(my_sum <1e-7) def test_MOC(self): - filename = 'optical_energy.txt' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - solution = np.loadtxt(my_path) + solution = np.loadtxt(self.my_path1) sample = ms.slab(3) sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5]) @@ -136,13 +144,7 @@ def test_MOC(self): def test_magnetic_optical_constant(self): - filename = 'optical_theta.txt' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - solution = np.loadtxt(my_path) + solution = np.loadtxt(self.my_path2) sample = ms.slab(3) sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5]) @@ -178,13 +180,8 @@ def test_magnetic_optical_constant(self): self.assertTrue(total_beta<1e-7) def test_IoR(self): - filename = 'optical_energy.txt' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - solution = np.loadtxt(my_path) + solution = np.loadtxt(self.my_path1) sample = ms.slab(3) sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5]) @@ -220,13 +217,8 @@ def test_IoR(self): self.assertTrue(total < 1e-7) def test_index_of_refraction(self): - filename = 'optical_theta.txt' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - solution = np.loadtxt(my_path) + solution = np.loadtxt(self.my_path2) sample = ms.slab(3) sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5]) @@ -263,5 +255,6 @@ def test_index_of_refraction(self): self.assertTrue(total_delta < 1e-7) self.assertTrue(total_beta < 1e-7) + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/TESTS/test_material_structure.py b/TESTS/test_material_structure.py index 625da241..0b961d91 100644 --- a/TESTS/test_material_structure.py +++ b/TESTS/test_material_structure.py @@ -11,25 +11,38 @@ import UTILS.material_structure as ms import pickle import UTILS.data_structure as ds +from UTILS import ROOT_DIR # This test script can be executed by inputting # -> python -m unittest -v test_material_structure.py # into the terminal class TestMaterialStructure(unittest.TestCase): + + def __init__(self, methodName: str = "runTest") -> None: + super().__init__(methodName) + + self.root_dir = ROOT_DIR + + self.filename1 = 'optical_energy.txt' + self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1 + + self.filename2 = 'ALS_test.pkl' + self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2 + + self.filename3 = '7uc_sample_test.h5' + self.my_path3 = self.root_dir + '/TESTS/test_data/' + self.filename3 + + self.filename4 = 'LSMO_test.h5' + self.my_path4 = self.root_dir + '/TESTS/test_data/' + self.filename4 + + def test_adaptive_layer_segmentation(self): + # Test the adaptive layer segmentation implementation - filename = 'optical_energy.txt' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - path = os.getcwd() + '/test_data/' - else: - my_path = os.getcwd() + '/test_data/' + filename - path = os.getcwd() + '/test_data/' + optical = np.loadtxt(self.my_path1) - optical = np.loadtxt(my_path) - - with open(path+'ALS_test.pkl', 'rb') as file: + with open(self.my_path2, 'rb') as file: solution = pickle.load(file) precision = [1e-1, 1e-2, 1e-5, 1e-10, 1e-20] @@ -53,7 +66,6 @@ def test_adaptive_layer_segmentation(self): compare_list = [idx for idx in solution[prec][i]] # transforms numpy array to list self.assertListEqual(my_slabs, compare_list) - def test_checkstring(self): # Testing the function that retrieves the next element an its stoichiometric relation # - this function can only deal with integer stoichiometries @@ -87,18 +99,10 @@ def test_find_stoichiometry(self): for i,ele in enumerate(formula_keys[idx]): self.assertEqual(formula[ele].stoichiometry,stoichiometry[idx][i]) - - def test_get_roughness(self): - # Testing the getRoughness function used in the scrip - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the getRoughness function used in the scrip + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[10, 'A'], [10, 'O'], [11, 'C2'], [11, 'O'], [4, 'A'], [4, 'B'], [4, 'O'], [11, 'all']] roughness = [0, 0.5, 3.726885630758733, 2.0653473900870867, 0.0, 0.0, 0.0, 2] @@ -107,17 +111,10 @@ def test_get_roughness(self): sigma = sample.getRoughness(params[0],params[1]) self.assertEqual(sigma, roughness[idx]) - def test_set_roughness(self): - # Testing the setRoughness function used in the script - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - sample = ds.ReadSampleHDF5(my_path) + # Testing the setRoughness function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) # retrieves initial configuration of the sample definition info = [] @@ -147,15 +144,9 @@ def test_set_roughness(self): self.assertEqual(layer[ele], sample.structure[i][ele].roughness) def test_get_density(self): + # Testing the setDensity function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - sample = ds.ReadSampleHDF5(my_path) + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[10, 'A'], [10, 'O'], [11, 'C2'], [11, 'O'], [4, 'A'], [4, 'B'], [4, 'O'], [8, 'all']] density = [0.0020259688666223116, 0.084, 0.08597982010497454, 0.046124560151674036, 0.028, 0.028, 0.084, 0.028] @@ -165,15 +156,9 @@ def test_get_density(self): self.assertEqual(rho, density[idx]) def test_set_density(self): - # Testing the setDensity function used in the script - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - sample = ds.ReadSampleHDF5(my_path) + # Testing the setDensity function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) # retrieves initial configuration of the sample definition info = [] @@ -203,15 +188,9 @@ def test_set_density(self): self.assertEqual(layer[ele], sample.structure[i][ele].density) def test_get_thickness(self): + # Testing the getThickness function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - sample = ds.ReadSampleHDF5(my_path) + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[10, 'A'], [10, 'O'], [11, 'C2'], [11, 'O'], [4, 'A'], [4, 'B'], [4, 'O'], [7, 'all'], [3,'all']] thickness = [3.97, 3.97, 10.862294545882191, 3.9972874500963402, 3.97, 3.97, 3.97, 3.97,3.94] @@ -221,15 +200,9 @@ def test_get_thickness(self): self.assertEqual(d, thickness[idx]) def test_set_thickness(self): - # Testing the setDensity function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the setDensity function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) # retrieves initial configuration of the sample definition info = [] @@ -259,15 +232,9 @@ def test_set_thickness(self): self.assertEqual(layer[ele], sample.structure[i][ele].thickness) def test_get_totalThickness(self): - # Testing the getCombinedThickness function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the getCombinedThickness function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[0,1,'A'],[1,2, 'B'],[1,3, 'all'],[1,4,'A'],[1,5,'B'],[1,6, 'O'],[2,3, 'B'],[1,10, 'all'], [5,7, 'A'],[7,10, 'B'],[0,11, 'O']] @@ -279,13 +246,9 @@ def test_get_totalThickness(self): self.assertEqual(dtot,solutions[i]) def test_set_combinedThickness(self): - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the setCombinedThickness function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[1, 2, 'B'], [1, 3, 'all'], [1, 4, 'A'], [1, 5, 'B'], [1, 6, 'O'], [2, 3, 'B'], [1, 10, 'all'], @@ -303,14 +266,9 @@ def test_set_combinedThickness(self): self.assertListEqual(test_list, total_thickness_solutions) def test_setVariationConstant(self): - # Tests setVariationConstant function for script feature - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Tests setVariationConstant function for script feature + sample = ds.ReadSampleHDF5(self.my_path3) # layers 1 to 10 # Ti, Mn2, Mn3 @@ -359,14 +317,9 @@ def test_setVariationConstant(self): self.assertEqual(sample.structure[params[0]]['B'].poly_ratio[j], solutions[i][key]) def test_setMultiVarConstant(self): - # Tests setMultiVarConstant function for script feature - filename = 'LSMO_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Tests setMultiVarConstant function for script feature + sample = ds.ReadSampleHDF5(self.my_path4) parameters = [[2, 'B', ['Ti', 'Mn2+'], [0.95, 0]], [3, 'B', ['Ti', 'Mn2+'], [0.44, 0.25]], [4, 'A', ['Sr'], [0.777]], [5, 'B', ['Ti', 'Mn3+'], [0.22, 0.22]], @@ -388,16 +341,10 @@ def test_setMultiVarConstant(self): for j, key in enumerate(sample.structure[params[0]][params[1]].polymorph): self.assertEqual(sample.structure[params[0]][params[1]].poly_ratio[j], solutions[i][key]) - def test_setRatio(self): - # Tests setRatio function for script feature - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Tests setRatio function for script feature + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[0, 'B', 'Ti', 'Mn2', 0.5], [1, 'B', 'Ti', 'Mn3', 0.1], [2, 'B', 'Mn3', 'Mn2', 0.9], [3, 'B', 'Mn2', 'Mn3', 0.75], [4, 'B', 'Ti', 'Mn2', 0.55], [5, 'B', 'Mn2', 'Ti', 0.925], @@ -422,15 +369,9 @@ def test_setRatio(self): self.assertEqual(sample.structure[params[0]][params[1]].poly_ratio[j], solutions[i][key]) def test_get_magDensity(self): - # Testing the getMagDensity function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the getMagDensity function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) parameters = [[2, 'B','Mn3'], [3, 'B','Mn3'], [4, 'B','Mn3'], [5, 'B','Mn3'], [6, 'B','Mn3'], [7, 'B', 'Mn3'], [8, 'B', 'Mn3'], [9, 'B', 'Mn3'], [10, 'B', 'Mn3']] @@ -443,15 +384,9 @@ def test_get_magDensity(self): self.assertEqual(rho, mag_density[idx]) def test_set_magDensity(self): - # Testing the setDensity function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the setDensity function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) # retrieves initial configuration of the sample definition @@ -481,17 +416,10 @@ def test_set_magDensity(self): self.assertListEqual(test_list, mag_eval) - def test_get_eShift(self): - # Testing the getEshift function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the getEshift function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) eShift = {'O': 0, 'C': 0, 'Sr': 0, 'La': -0.1, 'A': 0, 'B': 0, 'Ti': 0, 'Mn2': -0.95, 'Mn3': -0.95} @@ -500,15 +428,9 @@ def test_get_eShift(self): self.assertEqual(eShift_func, eShift[key]) def test_set_eShift(self): - # Testing the getEshift function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the getEshift function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) eShift_new = {'O': 1.5, 'C': -1, 'Sr': -0.0001, 'La': -1.1, 'A': 0.652, 'B': 2.11, 'Ti': 0.05, 'Mn2': 0, 'Mn3': 0.55} eShift = {'O': 0, 'C': 0, 'Sr': 0, 'La': -0.1, 'A': 0, 'B': 0, 'Ti': 0, 'Mn2': -0.95, 'Mn3': -0.95} @@ -521,15 +443,9 @@ def test_set_eShift(self): self.assertEqual(value, eShift[test_key]) def test_get_eShift_mag(self): - # Testing the getMagEshift function used in the script - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - - sample = ds.ReadSampleHDF5(my_path) + # Testing the getMagEshift function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) mag_eShift = {'Ni': -0.95, 'Co': -0.95} @@ -538,15 +454,9 @@ def test_get_eShift_mag(self): self.assertEqual(eShift_func, mag_eShift[key]) def test_set_eShift_mag(self): - # Testing the getMagEshift function used in the script - - filename = '7uc_sample_test.h5' - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + filename - else: - my_path = os.getcwd() + '/test_data/' + filename - sample = ds.ReadSampleHDF5(my_path) + # Testing the getMagEshift function used in the script + sample = ds.ReadSampleHDF5(self.my_path3) mag_eShift = {'Ni': -0.95, 'Co': -0.95} mag_eShift_new = {'Ni': -0.55, 'Co': 1.2} diff --git a/TESTS/test_reflectivity.py b/TESTS/test_reflectivity.py index 91fc64a0..e6b81ff9 100644 --- a/TESTS/test_reflectivity.py +++ b/TESTS/test_reflectivity.py @@ -8,18 +8,14 @@ import numpy as np import UTILS.material_structure as ms +from UTILS import ROOT_DIR import unittest # This test script can be executed by inputting # -> python -m unittest -v test_reflectivity.py # into the terminal -def load_reflections(fname): - - if os.getcwd().split('\\')[-1] == 'Testing': - my_path = os.getcwd() + '/test_data/' + fname - else: - my_path = os.getcwd() + '/test_data/' + fname +def load_reflections(my_path): data = np.loadtxt(my_path) theta = data[:,0] @@ -27,12 +23,31 @@ def load_reflections(fname): return theta,R class TestReflectivity(unittest.TestCase): + + def __init__(self, methodName: str = "runTest") -> None: + super().__init__(methodName) + + self.root_dir = ROOT_DIR + + self.filename1 = 'Si-Al-10A-550.txt' + self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1 + + self.filename2 = 'Si-Al-50A-550.txt' + self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2 + + self.filename3 = 'AgBr-AlAs-50A-550.txt' + self.my_path3 = self.root_dir + '/TESTS/test_data/' + self.filename3 + + self.filename4 = 'AgBr-AlAs-50A-550_pi.txt' + self.my_path4 = self.root_dir + '/TESTS/test_data/' + self.filename4 + + def test_single_element_sigma(self): + # Reflectivity retrieved from the Center of X-ray Optics # - calculated for sigma-polarized light - theta, Rsol = load_reflections('Si-Al-10A-550.txt') # calculated reflectivity - #theta, Rsol = load_reflections('AgBr-AlAs-50A-550.txt') # calculated reflectivity + theta, Rsol = load_reflections(self.my_path1) # calculated reflectivity sample = ms.slab(2) sample.addlayer(0, 'Si', 50, density=0.028) @@ -55,14 +70,12 @@ def test_single_element_larger_film_sigma(self): # Reflectivity retrieved from the Center of X-ray Optics # - calculated for sigma-polarized light - theta, Rsol = load_reflections('Si-Al-50A-550.txt') # calculated reflectivity - #theta, Rsol = load_reflections('AgBr-AlAs-50A-550.txt') # calculated reflectivity + theta, Rsol = load_reflections(self.my_path2) # calculated reflectivity sample = ms.slab(2) sample.addlayer(0, 'Si', 50, density=0.028) sample.addlayer(1,'Al',50, density=0.028) - E = 550 qz = np.sin(theta * np.pi / 180) * (E * 0.001013546143) @@ -76,10 +89,11 @@ def test_single_element_larger_film_sigma(self): self.assertTrue(total < 0.002) def test_compound_film_sigma(self): + # Reflectivity retrieved from the Center of X-ray Optics # - calculated for sigma-polarized light - theta, Rsol = load_reflections('AgBr-AlAs-50A-550.txt') # calculated reflectivity + theta, Rsol = load_reflections(self.my_path3) # calculated reflectivity sample = ms.slab(2) sample.addlayer(0, 'AlAs', 50, density=3.81) @@ -102,7 +116,7 @@ def test_compound_film_pi(self): # Reflectivity retrieved from the Center of X-ray Optics # - calculated for sigma-polarized light - theta, Rsol = load_reflections('AgBr-AlAs-50A-550_pi.txt') # calculated reflectivity + theta, Rsol = load_reflections(self.my_path4) # calculated reflectivity sample = ms.slab(2) sample.addlayer(0, 'AlAs', 50, density=3.81)