def test_abs(self, tmp_path): # TEST the absolute threshold in_path = self.create_test_consec_data(tmp_path) e = EventDetector(in_path) # BELOW Q10 e.detect( variable="precip", time_period="month", hilo="low", method="abs", value=0.3 ) assert ( len(e.thresh.month) == 12 ), f"Expected the threshold \ calculation to be 12 (one for each month)" # all monthly thresholds should be 10 got = e.thresh.precip.mean(dim=["lat", "lon"]).values assert all( got == 0.3 ), f"\ Expected all monthly threshold values in e.thresh to be 0.3. Got: {got}" # longest run should be 4 (below 0.3) runs = e.calculate_runs() assert runs.max().values == 4, f"Expected the longest run to be 4 (below 0.3)"
def test_q90(self, tmp_path): # TEST the mean threshold in_path = self.create_test_consec_data(tmp_path) e = EventDetector(in_path) # ABOVE Q90 e.detect(variable="precip", time_period="month", hilo="high", method="q90") assert ( len(e.thresh.month) == 12 ), f"Expected the threshold \ calculation to be 12 (one for each month)" # all monthly thresholds should be 10 got = e.thresh.precip.mean(dim=["lat", "lon"]).values assert all( got == 10 ), f"\ Expected all monthly threshold values in e.thresh to be 10. Got: {got}" # longest run should be 0 (exceeding Q90) runs = e.calculate_runs() assert ( runs.max().values == 0 ), f"Expected the longest run to be 0 (exceeding q90)"
def test_number_of_consecutive_events(self, tmp_path): in_path = self.create_test_consec_data(tmp_path) e = EventDetector(in_path) e.detect(variable='precip', time_period='dayofyear', hilo='low', method='std') # check boolean dtype assert e.exceedences.dtype == np.dtype('bool'), f"Expected a\ boolean array but got: {e.exceedences.dtype}" # check the threshold calculation assert e.thresh.precip.max().values == 10, f"Expected max to be 10\ got: {e.thresh.precip.max().values}" # test run size runs = e.calculate_runs() assert runs.max().values == 4, f"Expected max run length to be 4\ Got: {runs.max().values}" # ensure they are of the correct time slice max_run_time = runs.where(runs == runs.max(), drop=True).squeeze() expected = pd.to_datetime('2000-04-30') got = pd.to_datetime(max_run_time.time.values) assert got == expected, f"Expected\ the maximum time slice to be: {expected} Got: {got}" # ensure the second largest timeslice is second_largest_run_time = runs.where(runs == 3, drop=True).squeeze() expected = pd.to_datetime('2000-11-30') got = pd.to_datetime(second_largest_run_time.time.values) assert got == expected, f"Expected\
def test_initialise_event_detector(self, tmp_path): _create_dummy_precip_data(tmp_path) data_dir = tmp_path / 'data' / 'interim' precip_dir = data_dir / 'chirps_preprocessed' / 'chirps_kenya.nc' e = EventDetector(precip_dir) assert 'precip' in [v for v in e.ds.variables], f"\ Expected precip to be a variable.\ Got: {[v for v in e.ds.variables]}" assert e.ds.precip.shape == (36, 30, 30), f"\
def test_q10(self, tmp_path): # TEST the q10 threshold in_path = self.create_test_consec_data(tmp_path) e = EventDetector(in_path) # BELOW Q10 e.detect(variable='precip', time_period='month', hilo='low', method='q10') assert len(e.thresh.month) == 12, f"Expected the threshold \ calculation to be 12 (one for each month)" # all monthly thresholds should be 10 got = e.thresh.precip.mean(dim=['lat', 'lon']).values assert all(got == 10), f"\ Expected all monthly threshold values in e.thresh to be 10. Got: {got}" # longest run should be 0 (below Q10) runs = e.calculate_runs() assert runs.max( ).values == 4, f"Expected the longest run to be 4 (below q10)"
def test_initialise_event_detector(self, tmp_path): _create_dummy_precip_data(tmp_path) data_dir = tmp_path / "data" / "interim" precip_dir = data_dir / "chirps_preprocessed" / "data_kenya.nc" e = EventDetector(precip_dir) assert "precip" in [ v for v in e.ds.variables ], f"\ Expected precip to be a variable.\ Got: {[v for v in e.ds.variables]}" assert e.ds.precip.shape == ( 36, 30, 30, ), f"\
from pathlib import Path import sys sys.path.append('..') from src.analysis import EventDetector data_dir = Path('data') chirps_dir = data_dir / 'interim' / 'chirps_preprocessed' / 'chirps_kenya.nc' e = EventDetector(chirps_dir) variable = 'precip' hilo = 'low' method = 'std' e.detect(variable=variable, time_period='dayofyear', hilo=hilo, method=method) # plot the output exceed = e.reapply_mask_to_boolean_xarray('precip', e.exceedences) fig, ax = plt.subplots() df = exceed.sum(dim=['lat', 'lon']).to_dataframe() df.plot(ax=ax, label=f'{variable}_{hilo}_{method}', legend=True) df.rolling(24).mean().plot(ax=ax, label='24mth mean') ax.set_title(f'Number of Pixels with {variable} \ {"below" if hilo == "low" else "above"} {method}') fig.savefig('.png')
from pathlib import Path from src.analysis import EventDetector data_dir = Path("data") interim_dir = data_dir / "interim" p_dir = interim_dir / "chirps_preprocessed" / "chirps_19812019_kenya.nc" v_dir = interim_dir / "vhi_preprocessed" / "vhi_preprocess_kenya.nc" e = EventDetector(p_dir) e.detect(variable="precip", time_period="dayofyear", hilo="low", method="std") runs = e.calculate_runs()