From f042b1c264007f51915bcd6d8b1ab9dbc929a955 Mon Sep 17 00:00:00 2001 From: mcampos16 Date: Wed, 20 May 2026 17:44:50 +0200 Subject: [PATCH 1/2] Avoid warnings raised in group_by --- src/jade/post/manipulate_tally.py | 2 ++ tests/post/test_manipulate_tally.py | 26 ++++++++++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/jade/post/manipulate_tally.py b/src/jade/post/manipulate_tally.py index 818f784a..2950440a 100644 --- a/src/jade/post/manipulate_tally.py +++ b/src/jade/post/manipulate_tally.py @@ -181,6 +181,8 @@ def groupby(tally: pd.DataFrame, by: str, action: str) -> pd.DataFrame: # Valid both for sum and mean err = ( np.sqrt(np.sum((subset_error * subset_value) ** 2)) / subset_value.sum() + if subset_value.sum() != 0 + else 0 ) rows.append(err) error = pd.Series(rows, name="Error") diff --git a/tests/post/test_manipulate_tally.py b/tests/post/test_manipulate_tally.py index 0ce0fc83..2640970b 100644 --- a/tests/post/test_manipulate_tally.py +++ b/tests/post/test_manipulate_tally.py @@ -18,9 +18,8 @@ divide_by_bin, format_decimals, gaussian_broadening, - volume, - mass, groupby, + mass, no_action, no_concat, ratio, @@ -30,6 +29,7 @@ subtract_tallies, sum_tallies, tof_to_energy, + volume, ) @@ -200,27 +200,28 @@ def test_add_column_with_dict(): pd.testing.assert_frame_equal(result, expected) -def test_groupby(): +def test_groupby(recwarn): data = { - "Energy": [1, 1, 2, 2], - "Value": [1, 2, 3, 4], - "Error": [0.1, 0.2, 0.3, 0.1], + "Energy": [1, 1, 2, 2, 3, 3], + "Value": [1, 2, 3, 4, 0, 0], + "Error": [0.1, 0.2, 0.3, 0.1, 0.0, 0.0], } df = pd.DataFrame(data) + result = groupby(df.copy(), "Energy", "sum") - assert (result["Value"] == [3, 7]).all() + assert (result["Value"] == [3, 7, 0]).all() assert ( result["Error"].iloc[0] == np.sqrt((0.1 * 1) ** 2 + (0.2 * 2) ** 2) / result["Value"].iloc[0] ) result = groupby(df.copy(), "Energy", "mean") - assert (result["Value"] == [1.5, 3.5]).all() + assert (result["Value"] == [1.5, 3.5, 0.0]).all() result = groupby(df.copy(), "Energy", "max") - assert (result["Value"] == [2, 4]).all() - assert (result["Error"] == [0.2, 0.1]).all() + assert (result["Value"] == [2, 4, 0]).all() + assert (result["Error"] == [0.2, 0.1, 0.0]).all() result = groupby(df.copy(), "Energy", "min") - assert (result["Value"] == [1, 3]).all() - assert (result["Error"] == [0.1, 0.3]).all() + assert (result["Value"] == [1, 3, 0]).all() + assert (result["Error"] == [0.1, 0.3, 0.0]).all() result = groupby(df.copy(), "all", "sum") assert result["Value"].iloc[0] == 10 assert ( @@ -229,6 +230,7 @@ def test_groupby(): / result["Value"].iloc[0] ) assert len(result) == 1 + assert len(recwarn) == 0 # If any warning is raised, the test fails def test_delete_cols(): From da3e23f945cd3a1bd686e56e48dff1b6bea686ff Mon Sep 17 00:00:00 2001 From: mcampos16 Date: Wed, 20 May 2026 18:13:11 +0200 Subject: [PATCH 2/2] Avoid division warning for additional division --- src/jade/post/manipulate_tally.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/jade/post/manipulate_tally.py b/src/jade/post/manipulate_tally.py index 2950440a..1f4515a8 100644 --- a/src/jade/post/manipulate_tally.py +++ b/src/jade/post/manipulate_tally.py @@ -167,7 +167,9 @@ def groupby(tally: pd.DataFrame, by: str, action: str) -> pd.DataFrame: # Valid both for sum and mean error = pd.Series( np.sqrt(((tally["Error"] * tally["Value"]) ** 2).sum()) - / tally["Value"].sum(), + / tally["Value"].sum() + if tally["Value"].sum() != 0 + else 0, name="Error", ) else: