Пример #1
0
def test_rfactor_benchmark_single_year(
    station,
    year,
    rain_benchmark_closure,
    intensity_method,
    erosivity_benchmark_data,
    erosivity_benchmark_matlab_clone_data,
):
    """Run the erosivity/rfactor calculation for single year/station combinations"""
    rain = rain_benchmark_closure(station, year)

    if intensity_method == maximum_intensity:
        eros_benchmark = erosivity_benchmark_data
    else:
        eros_benchmark = erosivity_benchmark_matlab_clone_data

    erosivity = compute_erosivity(rain, intensity_method)
    erosivity_reference = eros_benchmark[(eros_benchmark["year"] == year) & (
        eros_benchmark["station"] == station)]

    pd.testing.assert_frame_equal(erosivity, erosivity_reference)

    # using support function provides the same output
    erosivity_support_func = _compute_erosivity(
        rain, intensity_method=intensity_method)
    erosivity_support_func.index = erosivity_support_func["datetime"]
    pd.testing.assert_frame_equal(
        erosivity.drop(columns=["tag", "station", "year"]),
        erosivity_support_func)
Пример #2
0
def test_erosivity_rain_single_yearstation(dummy_rain):
    """Erosivity calculation need to work on rain input dataframe with datetime,
    rain_mm and station only for a given year/station."""
    erosivity = compute_erosivity(dummy_rain)

    # year column added when not existing
    assert "year" in erosivity.columns
    assert erosivity["year"][0] == 2018

    # station column preserved
    assert erosivity["station"][0] == "P01_001"

    # tag column added when not existing
    assert "tag" in erosivity.columns
    assert erosivity["tag"][0] == "P01_001_2018"
Пример #3
0
def test_erosivity_rain_single_yearstation_missing_column(dummy_rain):
    """Erosivity calculation need on rain input dataframe a datetime,
    rain_mm and station column."""

    with pytest.raises(RFactorKeyError) as excinfo:
        compute_erosivity(dummy_rain[["rain_mm", "datetime"]])
    assert "should contain 'datetime', 'rain_mm' and 'station'" in str(
        excinfo.value)
    with pytest.raises(RFactorKeyError) as excinfo:
        compute_erosivity(dummy_rain[["station", "datetime"]])
    assert "should contain 'datetime', 'rain_mm' and 'station'" in str(
        excinfo.value)
    with pytest.raises(RFactorKeyError) as excinfo:
        compute_erosivity(dummy_rain[["station", "rain_mm"]])
    assert "should contain 'datetime', 'rain_mm' and 'station'" in str(
        excinfo.value)
Пример #4
0
def test_rfactor_full_benchmark(rain_benchmark_data, erosivity_benchmark_data):
    """Run the full benchmark data set"""
    erosivity = compute_erosivity(rain_benchmark_data)
    pd.testing.assert_frame_equal(erosivity, erosivity_benchmark_data)
Пример #5
0
def test_erosivity_existing_tag(dummy_rain):
    """Existing tag is not overwritten. If not tag, new one is created."""
    dummy_rain["tag"] = "MY_UNIQUE_TAG"
    erosivity = compute_erosivity(dummy_rain)
    assert "tag" in erosivity.columns
    assert erosivity["tag"][0] == "MY_UNIQUE_TAG"
Пример #6
0
def test_erosivity_rain_single_yearstation_wrong_rain_dtype(dummy_rain):
    """Erosivity calculation with wrong datetime dtype returns error."""
    dummy_rain["rain_mm"] = "0.44"
    with pytest.raises(RFactorTypeError) as excinfo:
        compute_erosivity(dummy_rain)
    assert "'rain_mm' column needs to be of a float" in str(excinfo.value)
Пример #7
0
def test_erosivity_rain_single_yearstation_wrong_station_dtype(dummy_rain):
    """Erosivity calculation with wrong station dtype returns error."""
    dummy_rain["station"] = 44
    with pytest.raises(RFactorTypeError) as excinfo:
        compute_erosivity(dummy_rain)
    assert "'station' column needs to be of a str/object" in str(excinfo.value)
Пример #8
0
def test_erosivity_rain_single_yearstation_wrong_datetime_dtype(dummy_rain):
    """Erosivity calculation with wrong datetime dtype returns error."""
    dummy_rain["datetime"] = dummy_rain["datetime"].astype(str)
    with pytest.raises(RFactorTypeError) as excinfo:
        compute_erosivity(dummy_rain)
    assert "'datetime' column needs to be of a datetime" in str(excinfo.value)