def test_mask_wrap(): # create a test case where the outlines and the lon coordinates # are different # outline 0..359.9 outl1 = ((359, 0), (359, 1), (0, 1.), (0, 0)) outl2 = ((359, 1), (359, 2), (0, 2.), (0, 1)) outlines = [outl1, outl2] r = Regions_cls(name, numbers, names, abbrevs, outlines) # lon -180..179.9 lon = [-1.5, -0.5] lat = [0.5, 1.5] result = r.mask(lon, lat, xarray=False) assert np.all(np.isnan(result)) # this is the wron wrapping result = r.mask(lon, lat, xarray=False, wrap_lon=180) assert np.all(np.isnan(result)) expected = expected_mask() # determine the wrap automatically result = r.mask(lon, lat, xarray=False, wrap_lon=True) assert np.allclose(result, expected, equal_nan=True) # determine the wrap by hand result = r.mask(lon, lat, xarray=False, wrap_lon=360) assert np.allclose(result, expected, equal_nan=True)
def test_Regions_cls_deprection_warning(): with pytest.warns( FutureWarning, match= "Using 'Regions_cls' is deprecated, please use 'Regions' instead.", ): Regions_cls(name, numbers, names, abbrevs, outlines)
def test_centroids_deprection_warning(): centroids = [[0, 0], [1, 1]] with pytest.warns(FutureWarning, match="Specifying 'centroids' is deprecated"): Regions_cls(name, numbers, names, abbrevs, outlines, centroids=centroids)
def test_user_defined_centroid(): centroids = [[0, 0], [1, 1]] test_regions_centroids = Regions_cls(name, numbers, names, abbrevs, outlines, centroids=centroids) assert test_regions_centroids.centroids == centroids
def crop(self, image: xa.DataArray, regions: Regions_cls ) -> xa.DataArray: region_mask = regions.mask( image, lat_name=image.dims[-2], lon_name=image.dims[-1] ) return image.where( region_mask == 0 )
from pytest import raises import xarray as xr # ============================================================================= name = 'Example' numbers = [0, 1] names = ['Unit Square1', 'Unit Square2'] abbrevs = ['uSq1', 'uSq2'] outl1 = ((0, 0), (0, 1), (1, 1.), (1, 0)) outl2 = ((0, 1), (0, 2), (1, 2.), (1, 1)) outlines = [outl1, outl2] r1 = Regions_cls(name, numbers, names, abbrevs, outlines) lon = [0.5, 1.5] lat = [0.5, 1.5] # in this example the result looks: # a fill # b fill def expected_mask(a=0, b=1, fill=np.NaN): return np.array([[a, fill], [b, fill]]) def test_create_mask_contains():
warnings.filterwarnings(message="Using 'Regions_cls'", action="ignore") # ============================================================================= # set up the testing regions name = "Example" numbers = [0, 1] names = ["Unit Square1", "Unit Square2"] abbrevs = ["uSq1", "uSq2"] outl1 = ((0, 0), (0, 1), (1, 1.0), (1, 0)) outl2 = ((0, 1), (0, 2), (1, 2.0), (1, 1)) outlines = [outl1, outl2] r1 = Regions_cls(name, numbers, names, abbrevs, outlines) numbers = [1, 2] names = {1: "Unit Square1", 2: "Unit Square2"} abbrevs = {1: "uSq1", 2: "uSq2"} poly1 = Polygon(outl1) poly2 = Polygon(outl2) poly = {1: poly1, 2: poly2} r2 = Regions_cls(name, numbers, names, abbrevs, poly) # numbers as array r3 = Regions_cls(name, np.array(numbers), names, abbrevs, poly) # =============================================================================