def test_silent(self): """ Test that the silent method does not return any output in console """ fn_img = datasets.get_path("landsat_B4") # let's capture stdout # cf https://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call class Capturing(list): def __enter__(self): self._stdout = sys.stdout sys.stdout = self._stringio = StringIO() return self def __exit__(self, *args): self.extend(self._stringio.getvalue().splitlines()) del self._stringio # free up some memory sys.stdout = self._stdout with Capturing() as output1: img = si.SatelliteImage(fn_img) # check the metadata reading outputs to console assert len(output1) > 0 with Capturing() as output2: img = si.SatelliteImage(fn_img, silent=True) # check nothing outputs to console assert len(output2) == 0
def test_init(self): """ Test that inputs work properly in SatelliteImage class init """ fn_img = datasets.get_path("landsat_B4") # from filename, checking option img = si.SatelliteImage(fn_img, read_from_fn=False) img = si.SatelliteImage(fn_img) assert isinstance(img, si.SatelliteImage) # from SatelliteImage img2 = si.SatelliteImage(img) assert isinstance(img2, si.SatelliteImage) # from Raster r = gr.Raster(fn_img) img3 = si.SatelliteImage(r) assert isinstance(img3, si.SatelliteImage) assert np.logical_and.reduce((np.array_equal(img.data, img2.data, equal_nan=True), np.array_equal(img2.data, img3.data, equal_nan=True))) assert np.logical_and.reduce((np.all(img.data.mask == img2.data.mask), np.all(img2.data.mask == img3.data.mask)))
def test_init(self): """ Test that inputs work properly in DEM class init """ fn_img = xdem.examples.get_path("longyearbyen_ref_dem") # from filename dem = DEM(fn_img) assert isinstance(dem, DEM) # from DEM dem2 = DEM(dem) assert isinstance(dem2, DEM) # from Raster r = gr.Raster(fn_img) dem3 = DEM(r) assert isinstance(dem3, DEM) # from SatelliteImage with warnings.catch_warnings(): warnings.filterwarnings( "ignore", "Parse metadata from file not implemented") img = si.SatelliteImage(fn_img) dem4 = DEM(img) assert isinstance(dem4, DEM) list_dem = [dem, dem2, dem3, dem4] attrs = [ at for at in r._get_rio_attrs() if at not in ['name', 'dataset_mask', 'driver'] ] all_attrs = attrs + si.satimg_attrs + xdem.dem.dem_attrs for attr in all_attrs: attrs_per_dem = [idem.__getattribute__(attr) for idem in list_dem] assert all(at == attrs_per_dem[0] for at in attrs_per_dem) assert np.logical_and.reduce((np.array_equal(dem.data, dem2.data, equal_nan=True), np.array_equal(dem2.data, dem3.data, equal_nan=True), np.array_equal(dem3.data, dem4.data, equal_nan=True))) assert np.logical_and.reduce( (np.all(dem.data.mask == dem2.data.mask), np.all(dem2.data.mask == dem3.data.mask), np.all(dem3.data.mask == dem4.data.mask)))
def test_copy(self) -> None: """ Test that the copy method works as expected for SatelliteImage. In particular when copying r to r2: - if r.data is modified and r copied, the updated data is copied - if r is copied, r.data changed, r2.data should be unchanged """ # Open dataset, update data and make a copy r = si.SatelliteImage(datasets.get_path("landsat_B4")) r.data += 5 r2 = r.copy() # Objects should be different (not pointing to the same memory) assert r is not r2 # Check the object is a SatelliteImage assert isinstance(r2, geoutils.satimg.SatelliteImage) # check all immutable attributes are equal # georaster_attrs = ['bounds', 'count', 'crs', 'dtypes', 'height', 'indexes', 'nodata', # 'res', 'shape', 'transform', 'width'] # satimg_attrs = ['satellite', 'sensor', 'product', 'version', 'tile_name', 'datetime'] # using list directly available in Class attrs = [ at for at in r._get_rio_attrs() if at not in ["name", "dataset_mask", "driver"] ] all_attrs = attrs + si.satimg_attrs for attr in all_attrs: assert r.__getattribute__(attr) == r2.__getattribute__(attr) # Check data array assert geoutils.misc.array_equal(r.data, r2.data, equal_nan=True) # Check dataset_mask array assert np.all(r.data.mask == r2.data.mask) # Check that if r.data is modified, it does not affect r2.data r.data += 5 assert not geoutils.misc.array_equal(r.data, r2.data, equal_nan=True)