def test_resampling(self): monthly_in, _, _ = _make_dataset(size=(10, 10)) monthly = BasePreProcessor.resample_time(monthly_in, resample_length='M') assert len(monthly_in.time) == len(monthly.time)
# ------------------------------------------------------------------------------ # Test PREPROCESSING # ------------------------------------------------------------------------------ from src.preprocess.base import BasePreProcessor b = BasePreProcessor() ds1_kenya = b.chop_roi(ds1, inverse_lat=True) ds2_kenya = b.chop_roi(ds2, inverse_lat=True) # concat across initialisation dates ds_kenya = xr.concat([ds1_kenya, ds2_kenya], dim="initialisation_date") stacked = ds_kenya.stack(time=("initialisation_date", "forecast_horizon")) # stack each individually k1 = ds1_kenya.stack(time=("initialisation_date", "forecast_horizon")) k2 = ds2_kenya.stack(time=("initialisation_date", "forecast_horizon")) # test selectors stacked.sel(forecast_horizon=np.timedelta64(28, "D")) stacked.sel(initialisation_date="1997-01-01") stacked.swap_dims({"time": "valid_time"}).sel(valid_time="1997-04") # test regridding ref_ds = b.load_reference_grid(path_to_grid=Path("data/interim/chirps_preprocessed.nc")) ds1_kenya_regrid = b.regrid( ds1_kenya.rename({"latitude": "lat", "longitude": "lon"}), ref_ds ) # test resampling time b.resample_time()
# engineer.engineer(test_year=1994, target_variable='VHI') # wrong shapes! datasets = engineer._get_preprocessed_files() ds_list = [xr.open_dataset(ds) for ds in datasets] dims_list = [[dim for dim in ds.dims] for ds in ds_list] variable_list = [[var for var in ds.variables if var not in dims_list[i]][0] for i, ds in enumerate(ds_list)] da_list = [ds[variable_list[i]] for i, ds in enumerate(ds_list)] pp = BasePreProcessor(data_path) c_ds = ds_list[0] e_ds = ds_list[1] v_ds = ds_list[2] v_ds = pp.resample_time(v_ds) c_ds = pp.regrid(c_ds, v_ds) c_ds = pp.resample_time(c_ds) v_ds.to_netcdf(vhi_path.home() / vhi_path.parent / "vhi_kenya_regrid.nc") v_ds.to_netcdf(chirps_path.home() / chirps_path.parent / "chirps_kenya_regrid.nc") # engineer process engineer._get_preprocessed_files engineer._make_dataset engineer.stratify_xy engineer.stratify_xy engineer._train_test_split engineer.stratify_xy