Beispiel #1
0
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        assert data_path.exists(), f"file not created correctly"
        spi = SPI(data_path / "data_kenya.nc")
        variable = "precip"
        spi.fit(variable=variable)

        coords = [c for c in spi.index.coords]
        vars_ = [v for v in spi.index.variables if v not in coords]
        assert "SPI3" in vars_, f"Expecting `v` variable in" "`self.index`"

        assert spi.index.SPI3.isel(time=slice(0, 2)).isnull().all(), (
            ""
            "The first two timesteps should be nan because using SPI3")

        assert not spi.index.SPI3.isel(time=slice(0, 3)).isnull().all(), (
            ""
            "The first two timesteps but not 3rd should be nan SPI3")

        assert np.isclose(
            spi.index.SPI3.mean(), 0,
            atol=0.01), (""
                         "Expect the mean SPI value to be close to 0 because"
                         "converted to a standard normal distribution")
        assert np.isclose(spi.index.SPI3.std(), 1, atol=0.01), (
            ""
            "Expect the `std()` SPI value to be close to 1 because"
            "converted to a standard normal distribution")
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        ci = ConditionIndex(data_path / "data_kenya.nc")
        variable = "precip"
        ci.fit(variable=variable)

        coords = [c for c in ci.index.coords]
        vars_ = [v for v in ci.index.variables if v not in coords]
        assert "precip_condition_index_1" in vars_, (
            f"Expecting `precip_condition_index_1` variable in"
            "`self.index`")

        # MIN-MAX of 0, 100
        assert np.isclose(
            float(ci.index[vars_[0]].max().values), 100
        ), f"Expected to have max value of 100. Got: {ci.index[vars_[0]].max()}"
        assert np.isclose(
            float(ci.index[vars_[0]].min().values), 0
        ), f"Expected to have min value of 0. Got: {ci.index[vars_[0]].min()}"

        assert (np.isclose(
            ci.index[vars_[0]].max(dim="time"),
            100)).all(), "Expect all times to have a max value of 100"
        assert (
            np.isclose(ci.index[vars_[0]].max(dim=["lat", "lon"]), 100)
        ).all(), "Expect all pixels (lat, lon) to have a max value of 100"
        assert (np.isclose(
            ci.index[vars_[0]].min(dim="time"),
            0)).all(), "Expect all times to have a min value of 0"
        assert (np.isclose(
            ci.index[vars_[0]].min(dim=["lat", "lon"]),
            0)).all(), "Expect all pixels (lat, lon) to have a min value of 0"
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date='2000-01-01',
                                              end_date='2010-01-01')
        spi = SPI(data_path / 'chirps_kenya.nc')
        variable = 'precip'
        spi.fit(variable=variable)

        coords = [c for c in spi.index.coords]
        vars_ = [v for v in spi.index.variables if v not in coords]
        assert "SPI3" in vars_, f"Expecting `v` variable in"\
            "`self.index`"

        assert spi.index.SPI3.isel(time=slice(0, 2)).isnull().all(), "" \
            "The first two timesteps should be nan because using SPI3"

        assert not spi.index.SPI3.isel(time=slice(0, 3)).isnull().all(), "" \
            "The first two timesteps but not 3rd should be nan SPI3"

        assert np.isclose(spi.index.SPI3.mean(), 0, atol=0.01), ""\
            "Expect the mean SPI value to be close to 0 because" \
            "converted to a standard normal distribution"
        assert np.isclose(spi.index.SPI3.std(), 1, atol=0.01), ""\
            "Expect the `std()` SPI value to be close to 0 because" \
            "converted to a standard normal distribution"
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(
            tmp_path, start_date="2000-01-01", end_date="2010-01-01"
        )
        di = DecileIndex(data_path / "chirps_kenya.nc")
        variable = "precip"
        di.fit(variable=variable)

        coords = [c for c in di.index.coords]
        vars_ = [v for v in di.index.variables if v not in coords]
        assert "quintile" in vars_, f"Expecting `quintile` variable in" "`self.index`"

        vals = np.unique(di.index.quintile.values)
        assert all(np.isin(np.arange(1, 6), vals)), (
            f"Expect quintiles to" f" be from 1 - 5. Got: {vals}"
        )

        assert di.index.rank_norm.min().values == 0, (
            f"Expect minmum "
            f"rank_norm to be 0. Got: {di.index.rank_norm.min().values}"
        )

        assert di.index.rank_norm.max().values == 100, (
            f"Expect max "
            f"rank_norm to be 100. Got: {di.index.rank_norm.max().values}"
        )
Beispiel #5
0
 def test_initialisation(self, tmp_path):
     data_path = _create_dummy_precip_data(tmp_path)
     b = BaseIndices(data_path / "chirps_kenya.nc")
     assert isinstance(
         b.ds,
         xr.Dataset), (f"expected to have loaded"
                       f"`ds` as an attribute in `BaseIndices` object")
Beispiel #6
0
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        spi = SPI(data_path / "data_kenya.nc")
        assert spi.name == "spi", f"Expected name" f"to be `spi` got: {spi.name}"

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            spi.index
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        ma = MovingAverage(data_path / 'chirps_kenya.nc')
        assert ma.name == '3month_moving_average', f"Expected name"\
            f"to be `3month_moving_average` got: {ma.name}"

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            ma.index
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        a = AnomalyIndex(data_path / 'chirps_kenya.nc')
        assert a.name == 'rainfall_anomaly_index', f"Expected name"\
            f"to be `rainfall_anomaly_index` got: {a.name}"

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            a.index
Beispiel #9
0
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        czi = ChinaZIndex(data_path / 'chirps_kenya.nc')
        assert czi.name == 'china_z_index', f"Expected name"\
            f"to be `china_z_index` got: {czi.name}"

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            czi.index
Beispiel #10
0
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        pni = PercentNormalIndex(data_path / 'chirps_kenya.nc')
        assert pni.name == 'percent_normal_index', f"Expected name"\
            f"to be `percent_normal_index` got: {pni.name}"

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            pni.index
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        dsi = DroughtSeverityIndex(data_path / 'chirps_kenya.nc')
        assert dsi.name == 'drought_severity_index', f"Expected name"\
            f"to be `drought_severity_index` got: {dsi.name}"

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            dsi.index
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        di = DecileIndex(data_path / "data_kenya.nc")
        assert di.name == "decile_index", (
            f"Expected name"
            f"to be `decile_index` got: {di.name}")

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            di.index
Beispiel #13
0
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        zsi = ZScoreIndex(data_path / "chirps_kenya.nc")
        assert zsi.name == "z_score_index", (
            f"Expected name"
            f"to be `z_score_index` got: {zsi.name}")

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            zsi.index
    def test_initialisation(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        vdi = VegetationDeficitIndex(data_path / "data_kenya.nc")
        assert vdi.name == "vegetation_deficit_index", (
            "Expected name"
            f"to be `vegetation_deficit_index` got: {vdi.name}")

        with pytest.raises(AttributeError):
            # assert error raised because haven't fit
            vdi.index
Beispiel #15
0
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        ai = AnomalyIndex(data_path / "chirps_kenya.nc")
        variable = "precip"
        ai.fit(variable=variable)

        coords = [c for c in ai.index.coords]
        vars_ = [v for v in ai.index.variables if v not in coords]
        assert "RAI" in vars_, f"Expecting `RAI` variable in `self.index`"
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date='2000-01-01',
                                              end_date='2010-01-01')
        ma = MovingAverage(data_path / 'chirps_kenya.nc')
        variable = 'precip'
        ma.fit(variable=variable)

        vars_ = [v for v in ma.index.data_vars]
        assert "precip_3month_moving_average" in vars_, \
            "Expecting `precip_3month_moving_average` variable in"\
            "`self.index`"
    def test_apply_over_period(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        ds = xr.open_dataset(data_path / 'chirps_kenya.nc')

        got = apply_over_period(ds,
                                xr.Dataset.mean,
                                in_variable='precip',
                                out_variable='mean',
                                time_str='month')
        expected = (ds.groupby('time.month').mean(dim='time').rename(
            {'precip': 'mean'}))
        assert all(got == expected), f"Expected: {expected}\nGot: {got}"
Beispiel #18
0
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        ma = MovingAverage(data_path / "data_kenya.nc")
        variable = "precip"
        ma.fit(variable=variable)

        vars_ = [v for v in ma.index.data_vars]
        assert "precip_3month_moving_average" in vars_, (
            "Expecting `precip_3month_moving_average` variable in"
            "`self.index`")
Beispiel #19
0
    def test_apply_over_period(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        ds = xr.open_dataset(data_path / "data_kenya.nc")

        got = apply_over_period(
            ds,
            xr.Dataset.mean,
            in_variable="precip",
            out_variable="mean",
            time_str="month",
        )
        expected = ds.groupby("time.month").mean(dim="time").rename(
            {"precip": "mean"})
        assert all(got == expected), f"Expected: {expected}\nGot: {got}"
Beispiel #20
0
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date='2000-01-01',
                                              end_date='2010-01-01')
        czi = ChinaZIndex(data_path / 'chirps_kenya.nc')
        variable = 'precip'
        czi.fit(variable=variable)

        coords = [c for c in czi.index.coords]
        vars_ = [v for v in czi.index.variables if v not in coords]
        assert "CZI" in vars_, f"Expecting `CZI` variable in `self.index`"
        assert np.isclose(czi.index.CZI.median().values, 0, atol=0.1), "" \
            "Because values are normally distributed expect CZI median to be" \
            "close to 0"
Beispiel #21
0
    def test_save(self, tmp_path):
        file_path = _create_dummy_precip_data(tmp_path)
        data_path = tmp_path / "data"

        if not data_path.exists():
            data_path.mkdir(parents=True, exist_ok=True)

        b = BaseIndices(file_path / "chirps_kenya.nc")
        b.name = "SPI"
        b.index = xr.open_dataset(file_path / "chirps_kenya.nc")
        b.save(data_path)

        assert (data_path / "analysis" / "indices" /
                "SPI.nc").exists(), "Expected to have created a new `.nc` file"
    def test_save(self, tmp_path):
        file_path = _create_dummy_precip_data(tmp_path)
        data_path = tmp_path / 'data'

        if not data_path.exists():
            data_path.mkdir(parents=True, exist_ok=True)

        b = BaseIndices(file_path / 'chirps_kenya.nc')
        b.name = 'SPI'
        b.index = xr.open_dataset(file_path / 'chirps_kenya.nc')
        b.save(data_path)

        assert (data_path / 'analysis' / 'indices' / 'SPI.nc').exists(), \
            "Expected to have created a new `.nc` file"
Beispiel #23
0
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(
            tmp_path, start_date='2000-01-01', end_date='2010-01-01'
        )
        pni = PercentNormalIndex(data_path / 'chirps_kenya.nc')
        variable = 'precip'
        pni.fit(variable=variable)

        coords = [c for c in pni.index.coords]
        vars_ = [v for v in pni.index.variables if v not in coords]
        assert "PNI" in vars_, f"Expecting `v` variable in"\
            "`self.index`"

        assert np.isclose(pni.index.PNI.mean().values, 100, atol=5), ""\
            "Expect the mean PNI value to be 100 (100% is NORMAL)"
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        dsi = DroughtSeverityIndex(data_path / "chirps_kenya.nc")
        variable = "precip"
        dsi.fit(variable=variable)

        coords = [c for c in dsi.index.coords]
        vars_ = [v for v in dsi.index.variables if v not in coords]
        assert "DSI" in vars_, f"Expecting `v` variable in" "`self.index`"

        assert (dsi.index.DSI.max() <= 4) and (dsi.index.DSI.min() >= -4), (
            ""
            "Range of valid DSI values from -4 to 4")
    def test_fit_modified(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        czi = ChinaZIndex(data_path / "chirps_kenya.nc")
        variable = "precip"
        czi.fit(variable=variable, modified=True)

        coords = [c for c in czi.index.coords]
        vars_ = [v for v in czi.index.variables if v not in coords]
        assert "MCZI" in vars_, f"Expecting `CZI` variable in `self.index`"

        assert np.isclose(czi.index.MCZI.median().values, 0, atol=0.1), (
            ""
            "Because values are normally distributed expect CZI median to be"
            "close to 0")
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        vdi = VegetationDeficitIndex(data_path / "data_kenya.nc")
        variable = "precip"
        vdi.fit(variable=variable)

        vars_ = [v for v in vdi.index.data_vars]
        assert "VCI3M_index" in vars_, (f"Expecting `VCI3M_index` variable in"
                                        "`self.index`")

        vals = np.unique(vdi.index.VCI3M_index.values)
        assert all(np.isin(np.arange(1, 6),
                           vals)), (f"Expect quintiles to"
                                    f" be from 1 - 5. Got: {vals}")
Beispiel #27
0
    def test_fit(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path,
                                              start_date="2000-01-01",
                                              end_date="2010-01-01")
        zsi = ZScoreIndex(data_path / "chirps_kenya.nc")
        variable = "precip"
        zsi.fit(variable=variable)

        coords = [c for c in zsi.index.coords]
        vars_ = [v for v in zsi.index.variables if v not in coords]
        assert "ZSI" in vars_, f"Expecting `ZSI` variable in" "`self.index`"

        assert np.isclose(
            zsi.index.ZSI.mean(), 0,
            atol=0.01), (""
                         "Expect the mean ZScoreIndex value to be close to 0")
    def test_rolling_cumsum(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        ds = xr.open_dataset(data_path / 'chirps_kenya.nc')

        cumsum3 = rolling_cumsum(ds, rolling_window=3)
        cumsum6 = rolling_cumsum(ds, rolling_window=6)

        # assert start in 2nd month (CENTRED WINDOW)
        got = pd.to_datetime(cumsum3.isel(time=0).time.values)
        expected = pd.to_datetime('1999-02-28')
        assert got == expected, "Expected minimum time of Cumsum to be:" \
            f"{expected} because centered window with size=3. Got: {got}"

        # assert start in 4th month
        got = pd.to_datetime(cumsum6.isel(time=0).time.values)
        expected = pd.to_datetime('1999-04-30')
        assert got == expected, "Expected minimum time of Cumsum to be:"\
            f"{expected} because centered window with size=6. Got: {got}"
Beispiel #29
0
    def test_create_shape_aligned_climatology_season(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        ds = xr.open_dataset(data_path / "data_kenya.nc")

        time_period = "season"
        ds_window = rolling_cumsum(ds, rolling_window=3)
        climatology = ds_window.groupby(f"time.{time_period}").mean(dim="time")

        assert climatology.precip.shape[0] == 4, (f"There should be 4 seasons"
                                                  f"in the climatology")

        clim = create_shape_aligned_climatology(ds_window,
                                                climatology,
                                                variable="precip",
                                                time_period=time_period)
        assert ds_window["precip"].shape == clim["precip"].shape, (
            f"Expected"
            f"the shape of `ds_window` ({ds_window['precip'].shape}) to =="
            f"`clim` ({clim['precip'].shape})")
    def test_create_shape_aligned_climatology_season(self, tmp_path):
        data_path = _create_dummy_precip_data(tmp_path)
        ds = xr.open_dataset(data_path / 'chirps_kenya.nc')

        time_period = 'season'
        ds_window = rolling_cumsum(ds, rolling_window=3)
        climatology = (ds_window.groupby(f'time.{time_period}').mean(
            dim='time'))

        assert climatology.precip.shape[0] == 4, f"There should be 4 seasons" \
            f"in the climatology"

        clim = create_shape_aligned_climatology(ds_window,
                                                climatology,
                                                variable='precip',
                                                time_period=time_period)
        assert ds_window['precip'].shape == clim['precip'].shape, f"Expected" \
            f"the shape of `ds_window` ({ds_window['precip'].shape}) to ==" \
            f"`clim` ({clim['precip'].shape})"