Skip to content

Commit

Permalink
deepsource
Browse files Browse the repository at this point in the history
  • Loading branch information
grzanka committed Mar 25, 2024
1 parent 64b05a7 commit 8f16763
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions tests/integration/shieldhit/test_averaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 8f16763

Please sign in to comment.