From c39b2692b3fd392a6405a108cb95c2a81099bb35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20G=C3=BCtschow?= Date: Tue, 22 Oct 2024 09:31:34 +0200 Subject: [PATCH 1/3] Add tests for merge function logging Add changelog note for pr #273 --- changelog/273.fix.md | 1 + primap2/_merge.py | 2 +- primap2/tests/test_merge.py | 40 ++++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 changelog/273.fix.md diff --git a/changelog/273.fix.md b/changelog/273.fix.md new file mode 100644 index 00000000..895794ef --- /dev/null +++ b/changelog/273.fix.md @@ -0,0 +1 @@ +The function `nir_convert_df_to_long` now retains NaN values instead of removing them. This is more consistent with the rest of our data reading where we keep NaN values to check that everything has been processed. diff --git a/primap2/_merge.py b/primap2/_merge.py index 6f7026a5..3cec575f 100644 --- a/primap2/_merge.py +++ b/primap2/_merge.py @@ -105,7 +105,7 @@ def generate_log_message(da_error: xr.DataArray, tolerance: float) -> str: scalar_dims_str = ", ".join(scalar_dims_format) da_error_dequ = da_error.squeeze(drop=True).pint.dequantify() if np.ndim(da_error_dequ.data) == 0: - errors_str = str(da_error_dequ.data) + errors_str = f"{da_error_dequ.data:.2f}" else: errors_str = da_error_dequ.to_dataframe().dropna().to_string() diff --git a/primap2/tests/test_merge.py b/primap2/tests/test_merge.py index f30f0181..fe0b61c7 100644 --- a/primap2/tests/test_merge.py +++ b/primap2/tests/test_merge.py @@ -56,7 +56,7 @@ def test_merge_fail_tolerance(opulent_ds): da_start.pr.merge(da_merge, tolerance=0.01) -def test_merge_fail_tolerance_warn(opulent_ds): +def test_merge_fail_tolerance_warn(opulent_ds, caplog): da_start = opulent_ds["CO2"] data_to_modify = opulent_ds["CO2"].pr.loc[{"area": ["ARG"]}].pr.sum("area") data_to_modify.data = data_to_modify.data * 1.09 @@ -64,6 +64,9 @@ def test_merge_fail_tolerance_warn(opulent_ds): da_result = da_start.pr.merge(da_merge, tolerance=0.01, error_on_discrepancy=False) assert_aligned_equal(da_result, da_start) + assert ( + "pr.merge error: found discrepancies larger than tolerance " "(1.00%) for " in caplog.text + ) def test_coords_not_matching_ds(opulent_ds): @@ -198,3 +201,38 @@ def test_merge_message_time_daily(opulent_ds): match=r"found discrepancies larger than tolerance \(1\.00%\) for time=2000-01-02", ): da_start.pr.merge(da_merge) + + +def test_log_formatting(minimal_ds, caplog): + da_start = minimal_ds["CO2"] + data_to_modify = ( + minimal_ds["CO2"].pr.loc[{"area": ["ARG"], "time": ["2001", "2002"]}].pr.sum("area") + ) + data_to_modify.data = data_to_modify.data * 1.09 + da_merge = minimal_ds["CO2"].pr.set("area", "ARG", data_to_modify, existing="overwrite") + + da_result = da_start.pr.merge(da_merge, tolerance=0.01, error_on_discrepancy=False) + assert_aligned_equal(da_result, da_start) + assert ( + "pr.merge error: found discrepancies larger than tolerance (1.00%) " + "for area (ISO3)=ARG, source=RAND2020:" in caplog.text + ) + assert ( + "(CO2)\n CO2\ntime \n2001-01-01 0.09" + "\n2002-01-01 0.09" in caplog.text + ) + + +def test_log_formatting_single_date(minimal_ds, caplog): + da_start = minimal_ds["CO2"] + data_to_modify = minimal_ds["CO2"].pr.loc[{"area": ["ARG"], "time": ["2000"]}].pr.sum("area") + data_to_modify.data = data_to_modify.data * 1.09 + da_merge = minimal_ds["CO2"].pr.set("area", "ARG", data_to_modify, existing="overwrite") + + da_result = da_start.pr.merge(da_merge, tolerance=0.01, error_on_discrepancy=False) + assert_aligned_equal(da_result, da_start) + assert ( + "pr.merge error: found discrepancies larger than tolerance (1.00%) " + "for time=2000, area (ISO3)=ARG, source=RAND2020:" in caplog.text + ) + assert "(CO2)\n0.09" in caplog.text From 8209c6aff40f6a4ae50e77d88e0a3201bd1ae640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20G=C3=BCtschow?= Date: Tue, 22 Oct 2024 09:33:58 +0200 Subject: [PATCH 2/3] Add changelog note --- changelog/276.improvement.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/276.improvement.md diff --git a/changelog/276.improvement.md b/changelog/276.improvement.md new file mode 100644 index 00000000..58a6a9e5 --- /dev/null +++ b/changelog/276.improvement.md @@ -0,0 +1 @@ +Add tests on log content and format for the `pr.merge()` function From d282ce307758a64e520ae4e663d5dd4cf8486ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20G=C3=BCtschow?= Date: Tue, 22 Oct 2024 13:02:46 +0200 Subject: [PATCH 3/3] Update changelog/276.improvement.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mika Pflüger --- changelog/276.improvement.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/276.improvement.md b/changelog/276.improvement.md index 58a6a9e5..755846f4 100644 --- a/changelog/276.improvement.md +++ b/changelog/276.improvement.md @@ -1 +1 @@ -Add tests on log content and format for the `pr.merge()` function +Added tests on log content and format for the `pr.merge()` function