From 0d886beeb821cf3284fa1f9a16bf9bc2e2e1e813 Mon Sep 17 00:00:00 2001 From: rettigl Date: Sun, 24 Dec 2023 23:25:40 +0100 Subject: [PATCH 1/2] add explicit return types for untyped functions to allow type checking fix or disable resulting type errors --- sed/calibrator/momentum.py | 6 +-- tests/calibrator/test_delay.py | 10 ++-- tests/calibrator/test_energy.py | 43 +++++++-------- tests/calibrator/test_momentum.py | 16 +++--- tests/loader/flash/test_flash_loader.py | 21 ++++---- tests/loader/sxp/test_sxp_loader.py | 7 +-- tests/loader/test_loaders.py | 12 ++--- tests/loader/test_mirrorutil.py | 6 +-- tests/loader/test_utils.py | 4 +- tests/test_binning.py | 43 +++++++-------- tests/test_config.py | 12 ++--- tests/test_diagnostics.py | 2 +- tests/test_io.py | 8 +-- tests/test_processor.py | 72 +++++++++++++------------ tests/test_sed.py | 2 +- 15 files changed, 128 insertions(+), 136 deletions(-) diff --git a/sed/calibrator/momentum.py b/sed/calibrator/momentum.py index 17349ef1..c3f7e344 100644 --- a/sed/calibrator/momentum.py +++ b/sed/calibrator/momentum.py @@ -95,8 +95,8 @@ def __init__( self.mdist: float = np.nan self.mcvdist: float = np.nan self.mvvdist: float = np.nan - self.cvdist: float = np.nan - self.vvdist: float = np.nan + self.cvdist: np.ndarray = np.array(np.nan) + self.vvdist: np.ndarray = np.array(np.nan) self.rdeform_field: np.ndarray = None self.cdeform_field: np.ndarray = None self.rdeform_field_bkp: np.ndarray = None @@ -573,7 +573,7 @@ def apply_func(apply: bool): # pylint: disable=unused-argument plt.show() - def calc_geometric_distances(self): + def calc_geometric_distances(self) -> None: """Calculate geometric distances involving the center and the vertices. Distances calculated include center-vertex and nearest-neighbor vertex-vertex distances. diff --git a/tests/calibrator/test_delay.py b/tests/calibrator/test_delay.py index f1baccef..3d386967 100644 --- a/tests/calibrator/test_delay.py +++ b/tests/calibrator/test_delay.py @@ -2,6 +2,8 @@ """ import os from importlib.util import find_spec +from typing import Any +from typing import Dict import dask.dataframe import numpy as np @@ -16,7 +18,7 @@ file = package_dir + "/../tests/data/loader/mpes/Scan0030_2.h5" -def test_delay_parameters_from_file(): +def test_delay_parameters_from_file() -> None: """Test the option to extract the delay parameters from a file""" config = parse_config( config={ @@ -45,7 +47,7 @@ def test_delay_parameters_from_file(): assert "delay_range_mm" in metadata["calibration"] -def test_delay_parameters_from_delay_range(): +def test_delay_parameters_from_delay_range() -> None: """Test the option to extract the delay parameters from a delay range""" # from keywords config = parse_config( @@ -78,7 +80,7 @@ def test_delay_parameters_from_delay_range(): assert metadata["calibration"]["adc_range"] == (100, 1000) -def test_delay_parameters_from_delay_range_mm(): +def test_delay_parameters_from_delay_range_mm() -> None: """Test the option to extract the delay parameters from a mm range + t0""" # from keywords config = parse_config( @@ -107,7 +109,7 @@ def test_delay_parameters_from_delay_range_mm(): collect_metadata=False, ) dc = DelayCalibrator(config=config) - calibration = {"delay_range_mm": (1, 15)} + calibration: Dict[str, Any] = {"delay_range_mm": (1, 15)} with pytest.raises(NotImplementedError): dc.append_delay_axis(df, calibration=calibration) calibration["time0"] = 1 diff --git a/tests/calibrator/test_energy.py b/tests/calibrator/test_energy.py index 3d8f2572..ed5a40bf 100644 --- a/tests/calibrator/test_energy.py +++ b/tests/calibrator/test_energy.py @@ -38,7 +38,7 @@ biases = np.asarray(next(reader)) -def test_bin_data_and_read_biases_from_files(): +def test_bin_data_and_read_biases_from_files() -> None: """Test binning the data and extracting the bias values from the files""" config = parse_config( config={"dataframe": {"tof_binning": 2}, "energy": {"bias_key": "@KTOF:Lens:Sample:V"}}, @@ -77,7 +77,7 @@ def test_bin_data_and_read_biases_from_files(): ec.bin_data(data_files=files) -def test_energy_calibrator_from_arrays_norm(): +def test_energy_calibrator_from_arrays_norm() -> None: """Test loading the energy, bias and tof traces into the class""" config = parse_config( config={"dataframe": {"tof_binning": 2}}, @@ -97,7 +97,7 @@ def test_energy_calibrator_from_arrays_norm(): assert np.max(ec.traces_normed[i, :]) == 1 -def test_feature_extract(): +def test_feature_extract() -> None: """Test generating the ranges, and extracting the features""" config = parse_config( config={"dataframe": {"tof_binning": 2}}, @@ -130,7 +130,7 @@ def test_feature_extract(): ) -def test_adjust_ranges(): +def test_adjust_ranges() -> None: """Test the interactive function for adjusting the feature ranges""" config = parse_config( config={"dataframe": {"tof_binning": 2}}, @@ -170,7 +170,7 @@ def test_adjust_ranges(): "energy_scale, calibration_method", itertools.product(energy_scales, calibration_methods), ) -def test_calibrate_append(energy_scale: str, calibration_method: str): +def test_calibrate_append(energy_scale: str, calibration_method: str) -> None: """Test if the calibration routines generate the correct slope of energy vs. tof, and the application to the data frame. @@ -225,7 +225,7 @@ def test_calibrate_append(energy_scale: str, calibration_method: str): "calib_type, calib_dict", zip(calib_types, calib_dicts), ) -def test_append_energy_axis_from_dict_kwds(calib_type: str, calib_dict: dict): +def test_append_energy_axis_from_dict_kwds(calib_type: str, calib_dict: dict) -> None: """Function to test if the energy calibration is correctly applied using a dict or kwd parameters. @@ -256,7 +256,7 @@ def test_append_energy_axis_from_dict_kwds(calib_type: str, calib_dict: dict): assert metadata["calibration"]["calib_type"] == calib_type -def test_append_energy_axis_raises(): +def test_append_energy_axis_raises() -> None: """Test if apply_correction raises the correct errors""" config = parse_config(config={}, folder_config={}, user_config={}, system_config={}) loader = get_loader(loader_name="mpes", config=config) @@ -271,7 +271,7 @@ def test_append_energy_axis_raises(): ) -def test_append_tof_ns_axis(): +def test_append_tof_ns_axis() -> None: """Function to test if the tof_ns calibration is correctly applied. TODO: add further tests once the discussion about units is done. """ @@ -342,7 +342,7 @@ def test_append_tof_ns_axis(): "correction_type, correction_kwd", zip(correction_types, correction_kwds), ) -def test_energy_correction(correction_type: str, correction_kwd: dict): +def test_energy_correction(correction_type: str, correction_kwd: dict) -> None: """Function to test if all energy correction functions generate symmetric curves with the maximum at the cetner x/y. @@ -425,9 +425,7 @@ def test_energy_correction(correction_type: str, correction_kwd: dict): "correction_type", correction_types, ) -def test_adjust_energy_correction_raises( - correction_type: str, -): +def test_adjust_energy_correction_raises(correction_type: str) -> None: """Function to test if the adjust_energy_correction function raises the correct errors. Args: @@ -469,10 +467,7 @@ def test_adjust_energy_correction_raises( "correction_type, correction_kwd", zip(correction_types, correction_kwds), ) -def test_energy_correction_from_dict_kwds( - correction_type: str, - correction_kwd: dict, -): +def test_energy_correction_from_dict_kwds(correction_type: str, correction_kwd: dict) -> None: """Function to test if the energy correction is correctly applied using a dict or kwd parameters. @@ -534,9 +529,7 @@ def test_energy_correction_from_dict_kwds( "correction_type", correction_types, ) -def test_apply_energy_correction_raises( - correction_type: str, -): +def test_apply_energy_correction_raises(correction_type: str) -> None: """Function to test if the apply_energy_correction raises the correct errors. Args: @@ -572,7 +565,7 @@ def test_apply_energy_correction_raises( assert config["dataframe"]["corrected_tof_column"] in df.columns -def test_add_offsets_functionality(): +def test_add_offsets_functionality() -> None: """test the add_offsets function""" config = parse_config( config={ @@ -631,7 +624,7 @@ def test_add_offsets_functionality(): loader=get_loader("flash", config=config), ) t_df = dask.dataframe.from_pandas(df.copy(), npartitions=2) - res, meta = ec.add_offsets(t_df, **params) # pylint disable=unexpected-keyword-arg + res, meta = ec.add_offsets(t_df, **params) # type: ignore[arg-type] # pylint disable=unexpected-keyword-arg np.testing.assert_allclose(res["energy"].values, exp_vals.values) params["applied"] = True assert meta == params @@ -639,9 +632,9 @@ def test_add_offsets_functionality(): # test with different energy scale -def test_add_offset_raises(): +def test_add_offset_raises() -> None: """test if add_offset raises the correct errors""" - cfg_dict = { + cfg_dict: Dict[str, Any] = { "energy": { "calibration": { "energy_scale": "kinetic", @@ -705,9 +698,9 @@ def test_add_offset_raises(): _ = ec.add_offsets(t_df) -def test_align_dld_sectors(): +def test_align_dld_sectors() -> None: """test functionality and error handling of align_dld_sectors""" - cfg_dict = { + cfg_dict: Dict[str, Any] = { "dataframe": { "tof_column": "dldTimeSteps", "sector_id_column": "dldSectorId", diff --git a/tests/calibrator/test_momentum.py b/tests/calibrator/test_momentum.py index 7aad2b47..c4bd7a99 100644 --- a/tests/calibrator/test_momentum.py +++ b/tests/calibrator/test_momentum.py @@ -33,7 +33,7 @@ momentum_map = np.asarray(momentum_map_list).T -def test_bin_data_and_slice_image(): +def test_bin_data_and_slice_image() -> None: """Test binning the data and slicing of the image""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -51,7 +51,7 @@ def test_bin_data_and_slice_image(): assert sed_processor.mc.slice.shape == (512, 512) -def test_feature_extract(): +def test_feature_extract() -> None: """Testextracting the feature from a 2D slice""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -73,7 +73,7 @@ def test_feature_extract(): "include_center", [True, False], ) -def test_splinewarp(include_center: bool): +def test_splinewarp(include_center: bool) -> None: """Test the generation of the splinewarp etimate. Args: @@ -109,7 +109,7 @@ def test_splinewarp(include_center: bool): assert len(mc.ptargs) == len(mc.prefs) -def test_pose_correction(): +def test_pose_correction() -> None: """Test the adjustment of the pose correction.""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -128,7 +128,7 @@ def test_pose_correction(): assert np.all(np.array([mc.cdeform_field, mc.rdeform_field]) != dfield) -def test_apply_correction(): +def test_apply_correction() -> None: """Test the application of the distortion correction to the dataframe.""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -241,7 +241,7 @@ def test_apply_correction(): def test_apply_registration( transformations: Dict[Any, Any], depends_on: Dict[Any, Any], -): +) -> None: """Test the application of the distortion correction to the dataframe.""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -311,7 +311,7 @@ def test_apply_registration( ) -def test_momentum_calibration_equiscale(): +def test_momentum_calibration_equiscale() -> None: """Test the calibration using one point and the k-distance, and application to the dataframe. """ @@ -344,7 +344,7 @@ def test_momentum_calibration_equiscale(): np.testing.assert_equal(metadata["calibration"][key], value) -def test_momentum_calibration_two_points(): +def test_momentum_calibration_two_points() -> None: """Test the calibration using two k-points, and application to the dataframe.""" config = parse_config( config={"core": {"loader": "mpes"}}, diff --git a/tests/loader/flash/test_flash_loader.py b/tests/loader/flash/test_flash_loader.py index 0dad0d74..edff997e 100644 --- a/tests/loader/flash/test_flash_loader.py +++ b/tests/loader/flash/test_flash_loader.py @@ -2,6 +2,7 @@ import os from importlib.util import find_spec from pathlib import Path +from typing import Literal import pytest @@ -14,7 +15,7 @@ @pytest.fixture(name="config_file") -def fixture_config_file(): +def fixture_config_file() -> dict: """Fixture providing a configuration file for FlashLoader tests. Returns: @@ -23,7 +24,7 @@ def fixture_config_file(): return parse_config(config_path) -def test_get_channels_by_format(config_file): +def test_get_channels_by_format(config_file: dict) -> None: """ Test function to verify the 'get_channels' method in FlashLoader class for retrieving channels based on formats and index inclusion. @@ -86,10 +87,10 @@ def test_get_channels_by_format(config_file): ["online-0/fl1user3/", "express-0/fl1user3/", "FL1USER3/"], ) def test_initialize_paths( - config_file, + config_file: dict, fs, - sub_dir, -): + sub_dir: Literal["online-0/fl1user3/", "express-0/fl1user3/", "FL1USER3/"], +) -> None: """ Test the initialization of paths based on the configuration and directory structures. @@ -123,7 +124,7 @@ def test_initialize_paths( assert expected_processed_path == data_parquet_dir -def test_initialize_paths_filenotfound(config_file): +def test_initialize_paths_filenotfound(config_file: dict) -> None: """ Test FileNotFoundError during the initialization of paths. """ @@ -139,7 +140,7 @@ def test_initialize_paths_filenotfound(config_file): _, _ = fl.initialize_paths() -def test_invalid_channel_format(config_file): +def test_invalid_channel_format(config_file: dict) -> None: """ Test ValueError for an invalid channel format. """ @@ -152,7 +153,7 @@ def test_invalid_channel_format(config_file): fl.read_dataframe() -def test_group_name_not_in_h5(config_file): +def test_group_name_not_in_h5(config_file: dict) -> None: """ Test ValueError when the group_name for a channel does not exist in the H5 file. """ @@ -161,12 +162,12 @@ def test_group_name_not_in_h5(config_file): fl = FlashLoader(config=config) with pytest.raises(ValueError) as e: - fl.create_dataframe_per_file(config["core"]["paths"]["data_raw_dir"] + H5_PATH) + fl.create_dataframe_per_file(Path(config["core"]["paths"]["data_raw_dir"] + H5_PATH)) assert str(e.value.args[0]) == "The group_name for channel dldPosX does not exist." -def test_buffer_schema_mismatch(config_file): +def test_buffer_schema_mismatch(config_file: dict) -> None: """ Test function to verify schema mismatch handling in the FlashLoader's 'read_dataframe' method. diff --git a/tests/loader/sxp/test_sxp_loader.py b/tests/loader/sxp/test_sxp_loader.py index b1e5ecb1..3332f231 100644 --- a/tests/loader/sxp/test_sxp_loader.py +++ b/tests/loader/sxp/test_sxp_loader.py @@ -25,7 +25,7 @@ def fixture_config_file() -> dict: return parse_config(config=config_path, folder_config={}, user_config={}, system_config={}) -def test_get_channels_by_format(config_file: dict): +def test_get_channels_by_format(config_file: dict) -> None: """ Test function to verify the 'get_channels' method in SXPLoader class for retrieving channels based on formats and index inclusion. @@ -74,10 +74,7 @@ def test_get_channels_by_format(config_file: dict): ) -def test_initialize_paths( - config_file: dict, - fs, -): +def test_initialize_paths(config_file: dict, fs) -> None: """ Test the initialization of paths based on the configuration and directory structures. diff --git a/tests/loader/test_loaders.py b/tests/loader/test_loaders.py index 2b60e1ea..f638ba0d 100644 --- a/tests/loader/test_loaders.py +++ b/tests/loader/test_loaders.py @@ -76,7 +76,7 @@ def get_all_loaders() -> List[ParameterSet]: @pytest.mark.parametrize("loader", get_all_loaders()) -def test_if_loaders_are_children_of_base_loader(loader: BaseLoader): +def test_if_loaders_are_children_of_base_loader(loader: BaseLoader) -> None: """Test to verify that all loaders are children of BaseLoader""" if loader.__name__ != "BaseLoader": assert isinstance(loader, BaseLoader) @@ -84,7 +84,7 @@ def test_if_loaders_are_children_of_base_loader(loader: BaseLoader): @pytest.mark.parametrize("loader", get_all_loaders()) @pytest.mark.parametrize("read_type", read_types) -def test_has_correct_read_dataframe_func(loader: BaseLoader, read_type: str): +def test_has_correct_read_dataframe_func(loader: BaseLoader, read_type: str) -> None: """Test if all loaders have a valid read function implemented""" assert callable(loader.read_dataframe) @@ -169,7 +169,7 @@ def test_has_correct_read_dataframe_func(loader: BaseLoader, read_type: str): @pytest.mark.parametrize("loader", get_all_loaders()) -def test_timed_dataframe(loader: BaseLoader): +def test_timed_dataframe(loader: BaseLoader) -> None: """Test if the loaders return a correct timed dataframe Args: @@ -212,7 +212,7 @@ def test_timed_dataframe(loader: BaseLoader): @pytest.mark.parametrize("loader", get_all_loaders()) -def test_get_count_rate(loader: BaseLoader): +def test_get_count_rate(loader: BaseLoader) -> None: """Test the get_count_rate function Args: @@ -257,7 +257,7 @@ def test_get_count_rate(loader: BaseLoader): @pytest.mark.parametrize("loader", get_all_loaders()) -def test_get_elapsed_time(loader: BaseLoader): +def test_get_elapsed_time(loader: BaseLoader) -> None: """Test the get_elapsed_time function Args: @@ -301,7 +301,7 @@ def test_get_elapsed_time(loader: BaseLoader): os.remove(Path(parquet_data_dir, "buffer", file)) -def test_mpes_timestamps(): +def test_mpes_timestamps() -> None: """Function to test if the timestamps are loaded correctly""" loader_name = "mpes" loader = get_loader(loader_name) diff --git a/tests/loader/test_mirrorutil.py b/tests/loader/test_mirrorutil.py index 0349a87b..db402108 100644 --- a/tests/loader/test_mirrorutil.py +++ b/tests/loader/test_mirrorutil.py @@ -19,7 +19,7 @@ file = package_dir + "/../tests/data/loader/mpes/Scan0030_2.h5" -def test_copy_tool_folder(): +def test_copy_tool_folder() -> None: """Test the folder copy functionalty of the CopyTool""" dest_folder = tempfile.mkdtemp() gid = os.getgid() @@ -45,7 +45,7 @@ def test_copy_tool_folder(): shutil.rmtree(dest_folder) -def test_copy_tool_file(): +def test_copy_tool_file() -> None: """Test the file copy functionality of the copy tool""" dest_folder = tempfile.mkdtemp() gid = os.getgid() @@ -64,7 +64,7 @@ def test_copy_tool_file(): shutil.rmtree(dest_folder) -def test_copy_tool_cleanup(): +def test_copy_tool_cleanup() -> None: """Test the file cleanup functionality of the copy tool""" dest_folder = tempfile.mkdtemp() gid = os.getgid() diff --git a/tests/loader/test_utils.py b/tests/loader/test_utils.py index 0ce2601c..04a10748 100644 --- a/tests/loader/test_utils.py +++ b/tests/loader/test_utils.py @@ -14,7 +14,7 @@ ) -def test_split_channel_bitwise(): +def test_split_channel_bitwise() -> None: """Test split_channel_bitwise function""" output_columns = ["b", "c"] bit_mask = 2 @@ -30,7 +30,7 @@ def test_split_channel_bitwise(): pd.testing.assert_frame_equal(result.compute(), expected_output) -def test_split_channel_bitwise_raises(): +def test_split_channel_bitwise_raises() -> None: """Test split_channel_bitwise function raises""" pytest.raises( KeyError, diff --git a/tests/test_binning.py b/tests/test_binning.py index 78815276..ef1d3991 100644 --- a/tests/test_binning.py +++ b/tests/test_binning.py @@ -1,6 +1,7 @@ """This file contains code that performs several tests for the sed.binning module """ from typing import Any +from typing import cast from typing import List from typing import Sequence from typing import Tuple @@ -63,7 +64,7 @@ [bins[:1], bins[:2], bins[:3]], ids=lambda x: f"bins:{len(x)}", ) -def test_histdd_error_is_raised(_samples: np.ndarray, _bins: List[int]): +def test_histdd_error_is_raised(_samples: np.ndarray, _bins: List[int]) -> None: """Test if the correct error is raised if the bins and sample shapes do not match Args: @@ -94,7 +95,7 @@ def test_histdd_error_is_raised(_samples: np.ndarray, _bins: List[int]): if x[2] < 7 else f"ndim: {x[2]-6}-round", ) -def test_histdd_bins_as_numpy(args: Tuple[np.ndarray, np.ndarray, int]): +def test_histdd_bins_as_numpy(args: Tuple[np.ndarray, np.ndarray, int]) -> None: """Test whether the numba_histogramdd functions produces the same result as np.histogramdd if called with a list of bin edgees @@ -127,7 +128,7 @@ def test_histdd_bins_as_numpy(args: Tuple[np.ndarray, np.ndarray, int]): if x[3] < 7 else f"ndim: {x[3]-6}-round", ) -def test_histdd_ranges_as_numpy(args: Tuple[np.ndarray, tuple, tuple, int]): +def test_histdd_ranges_as_numpy(args: Tuple[np.ndarray, tuple, tuple, int]) -> None: """Test whether the numba_histogramdd functions produces the same result as np.histogramdd if called with bin numbers and ranges @@ -160,7 +161,7 @@ def test_histdd_ranges_as_numpy(args: Tuple[np.ndarray, tuple, tuple, int]): if x[3] < 7 else f"ndim: {x[3]-6}-round", ) -def test_histdd_one_bins_as_numpy(args: Tuple[np.ndarray, int, tuple, int]): +def test_histdd_one_bins_as_numpy(args: Tuple[np.ndarray, int, tuple, int]) -> None: """Test whether the numba_histogramdd functions produces the same result as np.histogramdd if called with bin numbers and ranges @@ -195,7 +196,7 @@ def test_histdd_one_bins_as_numpy(args: Tuple[np.ndarray, int, tuple, int]): ) def test_from_bins_equals_from_bin_range( args: Tuple[np.ndarray, int, tuple, np.ndarray, int], -): +) -> None: """Test whether the numba_histogramdd functions produces the same result if called with bin numbers and ranges or with bin edges. @@ -218,9 +219,7 @@ def test_from_bins_equals_from_bin_range( ], ids=lambda x: f"ndim: {x[2]}", ) -def test_numba_hist_from_bins( - args: Tuple[np.ndarray, np.ndarray, int], -): +def test_numba_hist_from_bins(args: Tuple[np.ndarray, np.ndarray, int]) -> None: """Run tests using the _hist_from_bins function without numba jit. Args: @@ -254,9 +253,7 @@ def test_numba_hist_from_bins( ], ids=lambda x: f"ndim: {x[3]}", ) -def test_numba_hist_from_bins_ranges( - args: Tuple[np.ndarray, int, tuple, int], -): +def test_numba_hist_from_bins_ranges(args: Tuple[np.ndarray, int, tuple, int]) -> None: """Run tests using the _hist_from_bins_ranges function without numba jit. Args: @@ -267,7 +264,7 @@ def test_numba_hist_from_bins_ranges( _hist_from_bin_range.py_func(sample_, bins_, np.asarray(ranges_)) -def test_bin_centers_to_bin_edges(): +def test_bin_centers_to_bin_edges() -> None: """Test the conversion from bin centers to bin edges""" stepped_array = np.concatenate( [ @@ -282,7 +279,7 @@ def test_bin_centers_to_bin_edges(): assert bin_edges[i + 1] > stepped_array[i] -def test_bin_edges_to_bin_centers(): +def test_bin_edges_to_bin_centers() -> None: """Test the conversion from bin edges to bin centers""" stepped_array = np.concatenate( [ @@ -321,7 +318,7 @@ def test_bin_edges_to_bin_centers(): def test_simplify_binning_arguments( args: Tuple[List[int], List[str], List[Tuple[float, float]]], arg_type: str, -): +) -> None: """Test the result of the _simplify_binning_arguments functions for number of bins and ranges """ @@ -402,13 +399,13 @@ def test_simplify_binning_arguments( np.testing.assert_array_equal(ranges__[i], ranges_expected[i]) -def test_bin_partition(): +def test_bin_partition() -> None: """Test bin_partition function""" # test skipping checks in bin_partition with pytest.raises(TypeError): res = bin_partition( part=sample_pdf, - bins=[10, np.array([10, 20])], + bins=[10, np.array([10, 20])], # type: ignore[arg-type] axes=columns, ranges=ranges, skip_test=True, @@ -417,7 +414,7 @@ def test_bin_partition(): res = bin_partition( part=sample_pdf, bins=bins, - axes=["x", 10], + axes=["x", 10], # type: ignore[arg-type, list-item] ranges=ranges, skip_test=True, ) @@ -455,7 +452,7 @@ def test_bin_partition(): skip_test=False, jitter=columns, ) - assert not np.allclose(res, res1) + assert not np.allclose(cast(np.ndarray, res), res1) # test jittering, dict res = bin_partition( part=sample_pdf, @@ -465,7 +462,7 @@ def test_bin_partition(): skip_test=False, jitter={axis: {"amplitude": 0.5, "mode": "normal"} for axis in columns}, ) - assert not np.allclose(res, res1) + assert not np.allclose(cast(np.ndarray, res), res1) # numpy mode with pytest.raises(ValueError): res = bin_partition( @@ -484,10 +481,10 @@ def test_bin_partition(): skip_test=False, hist_mode="numpy", ) - assert np.allclose(res, res1) + assert np.allclose(cast(np.ndarray, res), res1) -def test_bin_dataframe(): +def test_bin_dataframe() -> None: """Test bin_dataframe function""" res = bin_dataframe(df=sample_ddf, bins=bins, axes=columns, ranges=ranges) assert isinstance(res, xr.DataArray) @@ -508,7 +505,7 @@ def test_bin_dataframe(): np.testing.assert_allclose(res.values, res2.values) -def test_normalization_histogram_from_timestamps(): +def test_normalization_histogram_from_timestamps() -> None: """Test the function to generate the normalization histogram from timestamps""" time_stamped_df = sample_ddf.copy() time_stamped_df["timeStamps"] = time_stamped_df.index @@ -522,7 +519,7 @@ def test_normalization_histogram_from_timestamps(): np.testing.assert_allclose(res / res.sum(), histogram / histogram.sum(), rtol=0.001) -def test_normalization_histogram_from_timed_dataframe(): +def test_normalization_histogram_from_timed_dataframe() -> None: """Test the function to generate the normalization histogram from the timed dataframe""" res = bin_dataframe(df=sample_ddf, bins=[bins[0]], axes=[columns[0]], ranges=[ranges[0]]) histogram = normalization_histogram_from_timed_dataframe( diff --git a/tests/test_config.py b/tests/test_config.py index cee914de..cba22e4d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -31,7 +31,7 @@ ] -def test_default_config(): +def test_default_config() -> None: """Test the config loader for the default config.""" config = parse_config() assert isinstance(config, dict) @@ -43,7 +43,7 @@ def test_default_config(): assert key in config["histogram"].keys() -def test_load_dict(): +def test_load_dict() -> None: """Test the config loader for a dict.""" config_dict = {"test_entry": True} config = parse_config(config_dict) @@ -53,7 +53,7 @@ def test_load_dict(): assert config["test_entry"] is True -def test_load_config(): +def test_load_config() -> None: """Test if the config loader can handle json and yaml files.""" config_json = load_config( f"{package_dir}/../tests/data/config/config.json", @@ -64,13 +64,13 @@ def test_load_config(): assert config_json == config_yaml -def test_load_config_raise(): +def test_load_config_raise() -> None: """Test if the config loader raises an error for a wrong file type.""" with pytest.raises(TypeError): load_config(f"{package_dir}/../README.md") -def test_complete_dictionary(): +def test_complete_dictionary() -> None: """Test the merging of a config and a default config dict""" dict1 = {"key1": 1, "key2": 2, "nesteddict": {"key4": 4}} dict2 = {"key1": 2, "key3": 3, "nesteddict": {"key5": 5}} @@ -83,7 +83,7 @@ def test_complete_dictionary(): assert dict3["key1"] == 1 -def test_save_dict(): +def test_save_dict() -> None: """Test the config saver for a dict.""" with tempfile.TemporaryDirectory() as tmpdirname: for ext in ["yaml", "json"]: diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py index 21b20093..ade40db9 100644 --- a/tests/test_diagnostics.py +++ b/tests/test_diagnostics.py @@ -24,7 +24,7 @@ "ncols, backend", itertools.product([1, 2, 3, 4], ["matplotlib", "bokeh"]), ) -def test_plot_histogram(ncols: int, backend: str): +def test_plot_histogram(ncols: int, backend: str) -> None: """Test generation of data histogram Args: diff --git a/tests/test_io.py b/tests/test_io.py index 1bde7c88..0f5f7abf 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -28,7 +28,7 @@ binned_arrays, ids=lambda x: f"ndims:{len(x.shape)}", ) -def test_save_and_load_tiff_array(_da: xr.DataArray): +def test_save_and_load_tiff_array(_da: xr.DataArray) -> None: """Test the tiff saving/loading function for np.ndarrays. Args: @@ -47,7 +47,7 @@ def test_save_and_load_tiff_array(_da: xr.DataArray): binned_arrays, ids=lambda x: f"ndims:{len(x.shape)}", ) -def test_save_xarr_to_tiff(_da: xr.DataArray): +def test_save_xarr_to_tiff(_da: xr.DataArray) -> None: """Test the tiff saving function for xr.DataArrays. Args: @@ -63,7 +63,7 @@ def test_save_xarr_to_tiff(_da: xr.DataArray): binned_arrays, ids=lambda x: f"ndims:{len(x.shape)}", ) -def test_save_and_load_tiff_xarray(_da: xr.DataArray): +def test_save_and_load_tiff_xarray(_da: xr.DataArray) -> None: """Test the tiff saving/loading function for xr.DataArrays. rgs: @@ -85,7 +85,7 @@ def test_save_and_load_tiff_xarray(_da: xr.DataArray): binned_arrays, ids=lambda x: f"ndims:{len(x.shape)}", ) -def test_save_and_load_hdf5(_da: xr.DataArray): +def test_save_and_load_hdf5(_da: xr.DataArray) -> None: """Test the hdf5 saving/loading function. Args: diff --git a/tests/test_processor.py b/tests/test_processor.py index 00385670..98bdba2d 100644 --- a/tests/test_processor.py +++ b/tests/test_processor.py @@ -8,6 +8,7 @@ from importlib.util import find_spec from pathlib import Path from typing import Any +from typing import cast from typing import Dict from typing import List from typing import Tuple @@ -19,6 +20,7 @@ from sed import SedProcessor from sed.core.config import parse_config +from sed.loader.flash.loader import FlashLoader from sed.loader.loader_interface import get_loader # pylint: disable=duplicate-code @@ -48,7 +50,7 @@ biases = np.asarray(next(reader)) -def test_processor_from_dataframe(): +def test_processor_from_dataframe() -> None: """Test generation of the processor from a dataframe object""" config = {"core": {"loader": "mpes"}} dataframe, timed_dataframe, _ = loader.read_dataframe(files=files) @@ -68,7 +70,7 @@ def test_processor_from_dataframe(): ).all() -def test_processor_from_files(): +def test_processor_from_files() -> None: """Test generation of the processor from a list of files""" config = {"core": {"loader": "mpes"}} dataframe, timed_dataframe, _ = loader.read_dataframe(files=files) @@ -87,7 +89,7 @@ def test_processor_from_files(): ).all() -def test_processor_from_folders(): +def test_processor_from_folders() -> None: """Test generation of the processor from a folder""" config = {"core": {"loader": "mpes"}} dataframe, timed_dataframe, _ = loader.read_dataframe(folders=df_folder) @@ -106,7 +108,7 @@ def test_processor_from_folders(): ).all() -def test_processor_from_runs(): +def test_processor_from_runs() -> None: """Test generation of the processor from runs""" config = {"core": {"loader": "mpes"}} dataframe, timed_dataframe, _ = loader.read_dataframe(folders=df_folder, runs=runs) @@ -127,7 +129,7 @@ def test_processor_from_runs(): ).all() -def test_additional_parameter_to_loader(): +def test_additional_parameter_to_loader() -> None: """Test if additinal keyword parameter can be passed to the loader from the Processor initialiuzation. """ @@ -143,7 +145,7 @@ def test_additional_parameter_to_loader(): assert processor.files[0].find("json") > -1 -def test_repr(): +def test_repr() -> None: """test the ___repr___ method""" config = {"core": {"loader": "mpes"}} processor = SedProcessor( @@ -162,7 +164,7 @@ def test_repr(): assert processor_str.find("key1") > 0 -def test_attributes_setters(): +def test_attributes_setters() -> None: """Test class attributes and setters.""" config = {"core": {"loader": "mpes"}} processor = SedProcessor( @@ -191,7 +193,7 @@ def test_attributes_setters(): assert len(processor.files) == 2 -def test_copy_tool(): +def test_copy_tool() -> None: """Test the copy tool functionality in the processor""" config = {"core": {"loader": "mpes", "use_copy_tool": True}} processor = SedProcessor( @@ -260,7 +262,7 @@ def test_copy_tool(): "features", feature_list, ) -def test_momentum_correction_workflow(features: np.ndarray): +def test_momentum_correction_workflow(features: np.ndarray) -> None: """Test for the momentum correction workflow""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -315,7 +317,7 @@ def test_momentum_correction_workflow(features: np.ndarray): os.remove(f"sed_config_momentum_correction{len(features)}.yaml") -def test_pose_adjustment(): +def test_pose_adjustment() -> None: """Test for the pose correction and application of momentum correction workflow""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -331,11 +333,11 @@ def test_pose_adjustment(): system_config={}, ) with pytest.raises(ValueError): - processor.pose_adjustment(**adjust_params, use_correction=False, apply=True) + processor.pose_adjustment(**adjust_params, use_correction=False, apply=True) # type: ignore[arg-type] processor.bin_and_load_momentum_calibration(apply=True) # test pose adjustment - processor.pose_adjustment(**adjust_params, use_correction=False, apply=True) + processor.pose_adjustment(**adjust_params, use_correction=False, apply=True) # type: ignore[arg-type] processor = SedProcessor( folder=df_folder, @@ -354,7 +356,7 @@ def test_pose_adjustment(): apply=True, ) processor.generate_splinewarp(use_center=True) - processor.pose_adjustment(**adjust_params, apply=True) + processor.pose_adjustment(**adjust_params, apply=True) # type: ignore[arg-type] processor.apply_momentum_correction() assert "Xm" in processor.dataframe.columns assert "Ym" in processor.dataframe.columns @@ -365,7 +367,7 @@ def test_pose_adjustment(): k_coord_a = [k_distance * 0.3, k_distance * 0.8] -def test_momentum_calibration_workflow(): +def test_momentum_calibration_workflow() -> None: """Test the calibration of the momentum axes""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -418,7 +420,7 @@ def test_momentum_calibration_workflow(): os.remove("sed_config_momentum_calibration.yaml") -def test_energy_correction(): +def test_energy_correction() -> None: """Test energy correction workflow.""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -474,7 +476,7 @@ def test_energy_correction(): "energy_scale, calibration_method", itertools.product(energy_scales, calibration_methods), ) -def test_energy_calibration_workflow(energy_scale: str, calibration_method: str): +def test_energy_calibration_workflow(energy_scale: str, calibration_method: str) -> None: """Test energy calibration workflow Args: @@ -585,7 +587,7 @@ def test_energy_calibration_workflow(energy_scale: str, calibration_method: str) np.testing.assert_allclose(energy1, energy2 + (1 if energy_scale == "binding" else -1)) -def test_align_dld_sectors(): +def test_align_dld_sectors() -> None: """Test alignment of DLD sectors for flash detector""" config = df_folder + "../flash/config.yaml" processor = SedProcessor( @@ -627,12 +629,12 @@ def test_align_dld_sectors(): np.testing.assert_allclose(tof_ref_array, tof_aligned_array + sector_delays[:, np.newaxis]) # cleanup flash inermediaries - _, parquet_data_dir = processor.loader.initialize_paths() + _, parquet_data_dir = cast(FlashLoader, processor.loader).initialize_paths() for file in os.listdir(Path(parquet_data_dir, "buffer")): os.remove(Path(parquet_data_dir, "buffer", file)) -def test_append_tof_ns_axis(): +def test_append_tof_ns_axis() -> None: """Test the append_tof_ns_axis function""" processor = SedProcessor( folder=df_folder, @@ -645,7 +647,7 @@ def test_append_tof_ns_axis(): assert processor.config["dataframe"]["tof_ns_column"] in processor.dataframe -def test_delay_calibration_workflow(): +def test_delay_calibration_workflow() -> None: """Test the delay calibration workflow""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -674,7 +676,7 @@ def test_delay_calibration_workflow(): assert "delay" in processor.dataframe.columns -def test_filter_column(): +def test_filter_column() -> None: """Test the jittering function""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -699,7 +701,7 @@ def test_filter_column(): processor.filter_column("X", high, low) -def test_add_jitter(): +def test_add_jitter() -> None: """Test the jittering function""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -726,7 +728,7 @@ def test_add_jitter(): np.testing.assert_allclose(res1a, res2a) -def test_add_time_stamped_data(): +def test_add_time_stamped_data() -> None: """Test the function to add time-stamped data""" processor = SedProcessor( folder=df_folder + "../mpes/", @@ -753,7 +755,7 @@ def test_add_time_stamped_data(): np.testing.assert_array_equal(processor.attributes["time_stamped_data"][2], data) -def test_event_histogram(): +def test_event_histogram() -> None: """Test histogram plotting function""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -773,7 +775,7 @@ def test_event_histogram(): processor.view_event_histogram(dfpid=5) -def test_compute(): +def test_compute() -> None: """Test binning of final result""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -790,13 +792,13 @@ def test_compute(): ) bins = [10, 10, 10, 10] axes = ["X", "Y", "t", "ADC"] - ranges = [[0, 2048], [0, 2048], [0, 200000], [0, 50000]] + ranges = [(0, 2048), (0, 2048), (0, 200000), (0, 50000)] result = processor.compute(bins=bins, axes=axes, ranges=ranges, df_partitions=5) assert result.data.shape == tuple(bins) assert result.data.sum(axis=(0, 1, 2, 3)) > 0 -def test_compute_with_filter(): +def test_compute_with_filter() -> None: """Test binning of final result using filters""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -813,7 +815,7 @@ def test_compute_with_filter(): ) bins = [10, 10, 10, 10] axes = ["X", "Y", "t", "ADC"] - ranges = [[0, 2048], [0, 2048], [0, 200000], [0, 50000]] + ranges = [(0, 2048), (0, 2048), (0, 200000), (0, 50000)] filters = [ {"col": "X", "lower_bound": 100, "upper_bound": 200}, {"col": "index", "lower_bound": 100, "upper_bound": 200}, @@ -853,7 +855,7 @@ def test_compute_with_filter(): ) -def test_compute_with_normalization(): +def test_compute_with_normalization() -> None: """Test binning of final result with histogram normalization""" config = parse_config( config={"core": {"loader": "mpes"}}, @@ -870,7 +872,7 @@ def test_compute_with_normalization(): ) bins = [10, 10, 10, 5] axes = ["X", "Y", "t", "ADC"] - ranges = [[0, 2048], [0, 2048], [0, 200000], [650, 655]] + ranges = [(0, 2048), (0, 2048), (0, 200000), (650, 655)] result = processor.compute( bins=bins, axes=axes, @@ -898,7 +900,7 @@ def test_compute_with_normalization(): assert abs(result.sum(axis=(0, 1, 2, 3)) / result2.sum(axis=(0, 1, 2, 3)) - 1) < 0.15 -def test_get_normalization_histogram(): +def test_get_normalization_histogram() -> None: """Test the generation function for the normalization histogram""" config = parse_config( config={"core": {"loader": "mpes"}, "dataframe": {"time_stamp_alias": "timeStamps"}}, @@ -916,7 +918,7 @@ def test_get_normalization_histogram(): ) bins = [10, 10, 10, 5] axes = ["X", "Y", "t", "ADC"] - ranges = [[0, 2048], [0, 2048], [0, 200000], [650, 655]] + ranges = [(0, 2048), (0, 2048), (0, 200000), (650, 655)] with pytest.raises(ValueError): processor.get_normalization_histogram(axis="ADC") processor.compute(bins=bins, axes=axes, ranges=ranges, df_partitions=5) @@ -925,7 +927,7 @@ def test_get_normalization_histogram(): histogram1 = processor.get_normalization_histogram(axis="ADC", df_partitions=1) histogram2 = processor.get_normalization_histogram( axis="ADC", - use_time_stamps="True", + use_time_stamps=True, df_partitions=1, ) # TODO: Check why histograms are so different @@ -961,7 +963,7 @@ def test_get_normalization_histogram(): metadata["sample"]["name"] = "Sample Name" -def test_save(): +def test_save() -> None: """Test the save functionality""" config = parse_config( config={"dataframe": {"tof_binning": 1}}, @@ -985,7 +987,7 @@ def test_save(): processor.save("output.tiff") axes = ["kx", "ky", "energy", "delay"] bins = [100, 100, 200, 50] - ranges = [[-2, 2], [-2, 2], [-4, 2], [-600, 1600]] + ranges = [(-2, 2), (-2, 2), (-4, 2), (-600, 1600)] processor.compute(bins=bins, axes=axes, ranges=ranges) with pytest.raises(NotImplementedError): processor.save("output.jpeg") diff --git a/tests/test_sed.py b/tests/test_sed.py index 9795bc8a..0d422524 100644 --- a/tests/test_sed.py +++ b/tests/test_sed.py @@ -3,6 +3,6 @@ from sed import __version__ -def test_version(): +def test_version() -> None: """This function tests for the version of the package""" assert __version__ == "0.1.0" From 6bfd5d9b99b66b9efc5d4c8a7b5c9a16ffe34c42 Mon Sep 17 00:00:00 2001 From: rettigl Date: Sun, 24 Dec 2023 23:39:14 +0100 Subject: [PATCH 2/2] remove flash test notebook --- tests/loader/flash/test_data.ipynb | 82 ------------------------------ 1 file changed, 82 deletions(-) delete mode 100644 tests/loader/flash/test_data.ipynb diff --git a/tests/loader/flash/test_data.ipynb b/tests/loader/flash/test_data.ipynb deleted file mode 100644 index d1a53600..00000000 --- a/tests/loader/flash/test_data.ipynb +++ /dev/null @@ -1,82 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "!mkdir -p \"raw/hdf/express-0/fl1user3/\"\n", - "!wget \"https://syncandshare.desy.de/index.php/s/PcP2BbcE2DDLFkZ/download/FLASH1_USER3_stream_2_run43878_file1_20230130T153807.1.h5\" -O \"raw/hdf/express-0/fl1user3/FLASH1_USER3_stream_2_run43878_file1_20230130T153807.1.h5\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from sed import SedProcessor\n", - "\n", - "sp = SedProcessor(files=[43878], config=\"test_config.yaml\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sp._dataframe.compute()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "axes = ['dldPosX', 'dldPosY']\n", - "bins = [(0,900,10), (0,900,10)]\n", - "res = sp.compute(bins=bins, axes=axes)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "res.plot(cmap='terrain')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "sed-aLRHxgd9-py3.8", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.12" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}