Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rettigl committed Dec 20, 2024
1 parent 8d8f9ef commit 222e307
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 100 deletions.
9 changes: 4 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This is a code that performs several tests for the settings loader."""
import os
import tempfile
from importlib.util import find_spec
from pathlib import Path

import pytest
Expand All @@ -11,7 +10,7 @@
from specsanalyzer.config import parse_config
from specsanalyzer.config import save_config

package_dir = os.path.dirname(find_spec("specsanalyzer").origin)
test_dir = os.path.dirname(__file__)
default_config_keys = [
"calib2d_file",
"nx_pixel",
Expand Down Expand Up @@ -44,18 +43,18 @@ def test_load_dict():
def test_load_config():
"""Test if the config loader can handle json and yaml files."""
config_json = load_config(
f"{package_dir}/../../tests/data/config/config.json",
f"{test_dir}/data/config/config.json",
)
config_yaml = load_config(
f"{package_dir}/../../tests/data/config/config.yaml",
f"{test_dir}/data/config/config.yaml",
)
assert config_json == config_yaml


def test_load_config_raise():
"""Test if the config loader raises an error for a wrong file type."""
with pytest.raises(TypeError):
load_config(f"{package_dir}/../../README.md")
load_config(f"{test_dir}/../README.md")


def test_complete_dictionary():
Expand Down
48 changes: 24 additions & 24 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
from specsanalyzer.convert import calculate_polynomial_coef_da
from specsanalyzer.convert import get_damatrix_from_calib2d

package_dir = os.path.dirname(specsanalyzer.__file__)
test_dir = package_dir + "/../../tests/data/"

# from specsanalyzer.convert import get_rr_da
# from specsanalyzer.convert import mcp_position_mm
# let's get all the functions to be tested
test_dir = os.path.dirname(__file__)


def test_da_matrix(): # pylint: disable=too-many-locals
Expand All @@ -24,7 +19,7 @@ def test_da_matrix(): # pylint: disable=too-many-locals

########################################
# Load the IGOR txt Di_coeff values for comparison
igor_data_path = os.fspath(f"{test_dir}/dataEPFL/R9132")
igor_data_path = os.fspath(f"{test_dir}/data/dataEPFL/R9132")

# get the Da coefficients
di_file_list = [f"{igor_data_path}/Da{i}_value.tsv" for i in np.arange(1, 8, 2)]
Expand All @@ -42,8 +37,9 @@ def test_da_matrix(): # pylint: disable=too-many-locals
igor_d_coef_list.append(np.loadtxt(f_handle, delimiter="\t"))
igor_d_coef_matrix = np.flip(np.vstack(igor_d_coef_list), axis=1)

config_path = os.fspath(f"{test_dir}/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=config_path)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
calib2d_dict = spa.calib2d
lens_mode = "WideAngleMode"
kinetic_energy = 35.000000
Expand Down Expand Up @@ -75,9 +71,10 @@ def test_conversion_matrix():
"""Check the consistency of the conversion matrix with the
Igor calculations.
"""
igor_data_path = os.fspath(f"{test_dir}/dataEPFL/R9132")
config_path = os.fspath(f"{test_dir}/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=config_path)
igor_data_path = os.fspath(f"{test_dir}/data/dataEPFL/R9132")
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
calib2d_dict = spa.calib2d
lens_mode = "WideAngleMode"
kinetic_energy = 35.000000
Expand Down Expand Up @@ -187,20 +184,21 @@ def test_conversion():

# get the raw data
raw_image_name = os.fspath(
f"{test_dir}/dataEPFL/R9132/Data9132_RAWDATA.tsv",
f"{test_dir}/data/dataEPFL/R9132/Data9132_RAWDATA.tsv",
)
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

# get the reference data
reference_image_name = os.fspath(
f"{test_dir}/dataEPFL/R9132/Data9132_IGOR_corrected.tsv",
f"{test_dir}/data/dataEPFL/R9132/Data9132_IGOR_corrected.tsv",
)
with open(reference_image_name, encoding="utf-8") as file:
reference = np.loadtxt(file, delimiter="\t")

config_path = os.fspath(f"{test_dir}/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=config_path)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
lens_mode = "WideAngleMode"
kinetic_energy = 35.000000
pass_energy = 35.000000
Expand Down Expand Up @@ -229,14 +227,14 @@ def test_conversion_from_dict():

# get the raw data
raw_image_name = os.fspath(
f"{test_dir}/dataEPFL/R9132/Data9132_RAWDATA.tsv",
f"{test_dir}/data/dataEPFL/R9132/Data9132_RAWDATA.tsv",
)
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

# get the reference data
reference_image_name = os.fspath(
f"{test_dir}/dataEPFL/R9132/Data9132_IGOR_corrected.tsv",
f"{test_dir}/data/dataEPFL/R9132/Data9132_IGOR_corrected.tsv",
)
with open(reference_image_name, encoding="utf-8") as file:
reference = np.loadtxt(file, delimiter="\t")
Expand Down Expand Up @@ -309,13 +307,14 @@ def test_recycling():
"""
# get the raw data
raw_image_name = os.fspath(
f"{test_dir}/dataEPFL/R9132/Data9132_RAWDATA.tsv",
f"{test_dir}/data/dataEPFL/R9132/Data9132_RAWDATA.tsv",
)
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

config_path = os.fspath(f"{test_dir}/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=config_path)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
lens_mode = "WideAngleMode"
kinetic_energy = 35.000000
pass_energy = 35.000000
Expand Down Expand Up @@ -348,13 +347,14 @@ def test_cropping():
"""Test function for checking that cropping parameters are correctly applied"""
# get the raw data
raw_image_name = os.fspath(
f"{test_dir}/dataEPFL/R9132/Data9132_RAWDATA.tsv",
f"{test_dir}/data/dataEPFL/R9132/Data9132_RAWDATA.tsv",
)
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

config_path = os.fspath(f"{test_dir}/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=config_path)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
lens_mode = "WideAngleMode"
kinetic_energy = 35.000000
pass_energy = 35.000000
Expand Down
6 changes: 3 additions & 3 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from specsscan.core import load_images
from specsscan.core import parse_lut_to_df

package_dir = os.path.dirname(specsscan.__file__)
test_dir = os.path.dirname(__file__)
# Path to a sample 3-D scan
scan_path_mirror = Path(f"{package_dir}/../../tests/data/4450")
scan_path_mirror = Path(f"{test_dir}/data/4450")
# Path to a sample 2-D scan
scan_path_single = Path(f"{package_dir}/../../tests/data/3610")
scan_path_single = Path(f"{test_dir}/data/3610")

df_lut = parse_lut_to_df(scan_path_mirror)

Expand Down
15 changes: 9 additions & 6 deletions tests/test_lens_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def test_lens_modes_angle(lens_mode):
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

configpath = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=configpath)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
kinetic_energy = np.random.uniform(20, 50)
pass_energy = np.random.uniform(20, 50)
work_function = 4.2
Expand Down Expand Up @@ -67,8 +68,9 @@ def test_lens_modes_space(lens_mode):
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

configpath = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=configpath)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
kinetic_energy = np.random.uniform(20, 50)
pass_energy = np.random.uniform(20, 50)
work_function = 4.2
Expand All @@ -95,8 +97,9 @@ def test_lens_raise():
with open(raw_image_name, encoding="utf-8") as file:
tsv_data = np.loadtxt(file, delimiter="\t")

configpath = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
spa = SpecsAnalyzer(config=configpath)
config_path = os.fspath(f"{test_dir}/data/dataEPFL/config/config.yaml")
calib_config = {"calib2d_file": f"{test_dir}/data/phoibos150.calib2d"}
spa = SpecsAnalyzer(config=calib_config, user_config=config_path)
kinetic_energy = 35.000000
pass_energy = 35.000000
work_function = 4.2
Expand Down
Loading

0 comments on commit 222e307

Please sign in to comment.