From 8f167636a33a5e0f577cd2b1f69271b573de09e1 Mon Sep 17 00:00:00 2001 From: Leszek Grzanka Date: Mon, 25 Mar 2024 22:48:02 +0100 Subject: [PATCH] deepsource --- tests/integration/shieldhit/test_averaging.py | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/integration/shieldhit/test_averaging.py b/tests/integration/shieldhit/test_averaging.py index 505c215a..cbf3ef0e 100644 --- a/tests/integration/shieldhit/test_averaging.py +++ b/tests/integration/shieldhit/test_averaging.py @@ -17,14 +17,14 @@ def averaging_bdos_directory(main_dir) -> Generator[Path, None, None]: @pytest.fixture(scope='function') -def small_stat_bdo_pattern() -> Generator[str, None, None]: - """Part of filename denoting small statistics BDO file""" +def large_stat_bdo_pattern() -> Generator[str, None, None]: + """Part of filename denoting large statistics BDO file""" yield "_000?.bdo" @pytest.fixture(scope='function') -def large_stat_bdo_pattern() -> Generator[str, None, None]: - """Part of filename denoting large statistics BDO file""" +def small_stat_bdo_pattern() -> Generator[str, None, None]: + """Part of filename denoting small statistics BDO file""" yield "_001?.bdo" @@ -38,15 +38,14 @@ def test_aggregating_equal_stats(averaging_bdos_directory, small_stat_bdo_patter In both sets of data, the same number of primary particles was used Therefore we can use simple averaging """ - for stat_pattern in (small_stat_bdo_pattern, large_stat_bdo_pattern): bdo_file_pattern = f"{output_type}{stat_pattern}" - input_file_list = list(averaging_bdos_directory.glob(bdo_file_pattern)) + bdo_file_list = list(averaging_bdos_directory.glob(bdo_file_pattern)) - averaged_estimators = fromfilelist(input_file_list=[str(path) for path in input_file_list]) + averaged_estimators = fromfilelist(input_file_list=[str(path) for path in bdo_file_list]) assert len(averaged_estimators.pages) > 0 - list_of_estimators_for_each_file = [fromfile(str(path)) for path in input_file_list] + list_of_estimators_for_each_file = [fromfile(str(path)) for path in bdo_file_list] assert len(list_of_estimators_for_each_file) > 0 for page_id, page in enumerate(averaged_estimators.pages): @@ -58,7 +57,7 @@ def test_aggregating_equal_stats(averaging_bdos_directory, small_stat_bdo_patter if "mean" in output_type: assert np.average(list_of_entries_to_aggregate) == pytest.approx(page.data) assert np.std(list_of_entries_to_aggregate, ddof=1, axis=0) / np.sqrt( - len(input_file_list)) == pytest.approx(page.error) + len(bdo_file_list)) == pytest.approx(page.error) elif "sum" in output_type: assert np.sum(list_of_entries_to_aggregate) == pytest.approx(page.data) assert page.error is None @@ -68,3 +67,27 @@ def test_aggregating_equal_stats(averaging_bdos_directory, small_stat_bdo_patter elif "concat" in output_type: assert np.concatenate(list_of_entries_to_aggregate, axis=1) == pytest.approx(page.data) assert page.error is None + + +@pytest.mark.parametrize( + "output_type", + ["normalisation-2_aggregation-sum", "normalisation-3_aggregation-mean", "normalisation-5_aggregation-mean"]) +def test_aggregating_weighted_stats(averaging_bdos_directory, small_stat_bdo_pattern, large_stat_bdo_pattern, + output_type): + """ + For weighted statistics, we need to calculate the weighted average + The average from all files, should be approximately the same as from the large statistics file + """ + large_stat_bdo_files = list(averaging_bdos_directory.glob(f"{output_type}{large_stat_bdo_pattern}")) + all_bdo_files = list(averaging_bdos_directory.glob(f"{output_type}{small_stat_bdo_pattern}")) + large_stat_bdo_files + + averaged_estimators_all = fromfilelist(input_file_list=[str(path) for path in all_bdo_files]) + assert len(averaged_estimators_all.pages) > 0 + + averaged_estimators_large_stat = fromfilelist(input_file_list=[str(path) for path in large_stat_bdo_files]) + assert len(averaged_estimators_large_stat.pages) > 0 + + for all_stat_pages, large_stat_pages in zip(averaged_estimators_all.pages, averaged_estimators_large_stat.pages): + # the small stats should not affect the result by more than 1% + assert all_stat_pages.data == pytest.approx(large_stat_pages.data, rel=1e-2) + # assert all_stat_pages.error == pytest.approx(large_stat_pages.error, rel=1e-2)