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/changelog/276.improvement.md b/changelog/276.improvement.md new file mode 100644 index 00000000..755846f4 --- /dev/null +++ b/changelog/276.improvement.md @@ -0,0 +1 @@ +Added tests on log content and format for the `pr.merge()` function 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