def test_rename_inplace(self): stack = Raster(self.predictors) band3_stats = stack.lsat7_2000_30.mean() # rename band 3 stack.rename(names={"lsat7_2000_30": "new_name"}, in_place=True) # check that renaming occurred in Raster self.assertEqual(stack.names[2], "new_name") self.assertNotIn("lsat7_2000_30", stack.names) # check that Raster layer properties also renamed self.assertIn("new_name", dir(stack)) self.assertNotIn("lsat7_2000_30", dir(stack)) # check that internal name of RasterLayer was also renamed self.assertEqual(stack.iloc[2].names[0], "new_name") # check that the RasterLayer attached to the new name is the same self.assertEqual(stack["new_name"].mean(), band3_stats) self.assertEqual(stack["new_name"].mean(), band3_stats) self.assertEqual(stack.new_name.mean(), band3_stats) self.assertEqual(stack.iloc[2].mean(), band3_stats) # check that a new Raster object derived from the renamed data # have the right names new_raster = Raster(src=stack.iloc[2]) self.assertIn("new_name", new_raster.names)
def test_rename_with_copy(self): stack = Raster(self.predictors) names = list(stack.names) band3_stats = stack.lsat7_2000_30.mean() # rename band 3 result = stack.rename(names={"lsat7_2000_30": "new_name"}, in_place=False) # check that original is untouched self.assertEqual(list(stack.names), names) # check that renaming occurred in Raster self.assertEqual(list(result.names)[2], "new_name") self.assertNotIn("lsat7_2000_30", result.names) # check that Raster layer properties also renamed self.assertIn("new_name", dir(result)) self.assertNotIn("lsat7_2000_30", dir(result)) # check that internal name of RasterLayer was also renamed self.assertEqual(result.iloc[2].name, "new_name") # check that the RasterLayer attached to the new name is the same self.assertEqual(result["new_name"].mean(), band3_stats) self.assertEqual(result["new_name"].mean(), band3_stats) self.assertEqual(result.new_name.mean(), band3_stats) self.assertEqual(result.iloc[2].mean(), band3_stats) # check that a new Raster object derived from the renamed data # have the right names new_raster = Raster(src=result.iloc[2]) self.assertIn("new_name", new_raster.names)
def test_indexing(self): stack = Raster(self.predictors + [self.multiband]) # replace band 1 with band 7 band7_mean = stack.loc['lsat7_2000_70'].read(masked=True).mean() stack.iloc[0] = Raster(self.band7).iloc[0] self.assertEqual(stack.iloc[0].read(masked=True).mean(), band7_mean) self.assertEqual(stack.loc['lsat7_2000_10'].read(masked=True).mean(), band7_mean) self.assertEqual(stack['lsat7_2000_10'].read(masked=True).mean(), band7_mean) self.assertEqual( stack.lsat7_2000_10.read(masked=True).mean(), band7_mean) # append another Raster containing a single layer stack.append(Raster(self.band7)) self.assertEqual(stack.names[5], 'lsat7_2000_70_1') self.assertEqual(stack.names[-1], 'lsat7_2000_70_2') self.assertEqual( stack.lsat7_2000_70_1.read(masked=True).mean(), stack.lsat7_2000_70_2.read(masked=True).mean(), band7_mean) # append a multiband raster stack.append(Raster(self.multiband)) self.assertEqual(stack.names[6], 'landsat_multiband_1_1') self.assertEqual(stack.names[12], 'landsat_multiband_1_2') # rename layers band3_mean = stack.lsat7_2000_30.read(masked=True).mean() stack.rename({'lsat7_2000_30': 'new_name'}) self.assertEqual(stack['new_name'].read(masked=True).mean(), band3_mean) self.assertEqual(stack.new_name.read(masked=True).mean(), band3_mean) self.assertEqual(stack.loc['new_name'].read(masked=True).mean(), band3_mean) self.assertEqual(stack.iloc[-1].read(masked=True).mean(), band3_mean) # Drop a layer stack.names stack.drop(labels='lsat7_2000_50') self.assertNotIn('lsat7_2000_50', stack.names)
def test_subseting(self): stack = Raster(self.predictors + [nc.multiband]) # RasterLayer indexing which returns a RasterLayer self.assertIsInstance(stack.iloc[0], RasterLayer) self.assertIsInstance(stack.loc['lsat7_2000_10'], RasterLayer) self.assertIsInstance(stack.lsat7_2000_10, RasterLayer) self.assertListEqual( stack.iloc[0:3], stack.loc[['lsat7_2000_10', 'lsat7_2000_20', 'lsat7_2000_30']]) # RasterStack subsetting subset_raster = stack[['lsat7_2000_10', 'lsat7_2000_70']] self.assertListEqual(subset_raster.names, ['lsat7_2000_10', 'lsat7_2000_70']) self.assertEqual(subset_raster.lsat7_2000_10.read(masked=True).mean(), 80.56715262406088) self.assertEqual(subset_raster.lsat7_2000_70.read(masked=True).mean(), 59.17773813401238) # subsetting after name change stack.rename({'lsat7_2000_10': 'testme'}) expected_names = [ 'testme', 'lsat7_2000_20', 'lsat7_2000_30', 'lsat7_2000_40', 'lsat7_2000_50', 'lsat7_2000_70', 'landsat_multiband_1', 'landsat_multiband_2', 'landsat_multiband_3', 'landsat_multiband_4', 'landsat_multiband_5' ] self.assertListEqual(stack.names, expected_names) self.assertListEqual(stack.iloc[0].names, ['testme']) self.assertListEqual( stack[['testme', 'lsat7_2000_20']].names, ['testme', 'lsat7_2000_20']) # check that RasterLayer internal name is carried over to new Raster self.assertListEqual(Raster(stack.iloc[0]).names, ['testme'])
def rename_in_memory(self): # Create a fake 3-band image for testing arr = np.random.rand(3, 64, 64) file = NamedTemporaryFile(prefix="test", suffix=".tif").name with rasterio.open(file, "w", width=64, height=64, count=3, dtype=np.float32) as dst: dst.write(arr) r = Raster(file) in_memory = r.aggregate((32, 32), in_memory=True) renamed = r.rename(dict(zip(in_memory.names, ["Red", "Green", "Blue"]))) self.assertListEqual(list(renamed.names), ["Red", "Green", "Blue"])
def rename_multiband(self): # Create a fake 3-band image for testing arr = np.random.rand(3, 64, 64) file = NamedTemporaryFile(prefix="test", suffix=".tif").name layer_name = os.path.basename(file).split(".")[0] layer_names = ["_".join([layer_name, str(i)]) for i in [1, 2 ,3]] with rasterio.open(file, "w", width=64, height=64, count=3, dtype=np.float32) as dst: dst.write(arr) r = Raster(file) self.assertListEqual(list(r.names), layer_names) renamed = r.rename(dict(zip(r.names, ["Red", "Green", "Blue"]))) self.assertListEqual(list(renamed.names), ["Red", "Green", "Blue"])
# RasterLayer indexing which returns a RasterLayer stack.iloc[0] # index stack.iloc[0:3] # slice stack.loc['lsat7_2000_10'] # key stack.loc[('lsat7_2000_10', 'lsat7_2000_20')] # list or tuple of keys stack.lsat7_2000_10 # attribute # RasterStack subsetting subset_raster = stack[['lsat7_2000_10', 'lsat7_2000_70']] subset_raster.names subset_raster.lsat7_2000_10 print(subset_raster.lsat7_2000_10.read(masked=True).mean()) print(subset_raster.lsat7_2000_70.read(masked=True).mean()) # subsetting after name change stack.rename({'lsat7_2000_10': 'testme'}) stack.names stack.iloc[-1].names stack[['testme', 'lsat7_2000_20']].names Raster(layers=stack.iloc[-1]).names # Replace a layer print(stack.iloc[0].read(masked=True).mean()) stack.iloc[0] = Raster(nc.band7).iloc[0] print(stack.iloc[0].read(masked=True).mean()) print(stack.loc['testme'].read(masked=True).mean()) print(stack['testme'].read(masked=True).mean()) print(stack.testme.read(masked=True).mean()) # Add a layer new_raster = stack.append(Raster(nc.band7))
def nre_fun(x, y): nre = (x - y) / (x + y) return nre ############################################################################## # RASTER ############################################################################## raw_fp = "C:/Users/linds/NOAA/shapefile_training/data_raw/" # file path to raw data folder raster_fp = raw_fp + 'white_small_sub.tif' # combine with tif file stack = Raster(raster_fp) stack.names stack.rename({'white_small_sub_1': 'coastal'}) stack.rename({'white_small_sub_2': 'blue'}) stack.rename({'white_small_sub_3': 'green'}) stack.rename({'white_small_sub_4': 'yellow'}) stack.rename({'white_small_sub_5': 'red'}) stack.rename({'white_small_sub_6': 'rededge'}) stack.rename({'white_small_sub_7': 'NIR1'}) stack.rename({'white_small_sub_8': 'NIR2'}) # Check the dimensions and bands print(stack.shape) # dimensions print(stack.count) # bands ############################################################################## # SHAPEFILE ##############################################################################
class TestInit(TestCase): def setUp(self) -> None: # inputs self.predictors = [nc.band1, nc.band2, nc.band3, nc.band4, nc.band5, nc.band7] # test results self.stack = None def tearDown(self) -> None: self.stack.close() def test_initiation_files(self): # test init from list of file paths self.stack = Raster(self.predictors) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 6) def test_initiation_file(self): # test init from single file path self.stack = Raster(nc.band1) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 1) def test_initiation_datasetreader(self): # test init from single rasterio.io.datasetreader with rasterio.open(nc.band1) as src: self.stack = Raster(src) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 1) def test_initiation_list_datasetreader(self): # test init from list of rasterio.io.datasetreader objects srcs = [] for f in self.predictors: srcs.append(rasterio.open(f)) self.stack = Raster(srcs) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 6) def test_initiation_band(self): # test init from single rasterio.band object with rasterio.open(nc.band1) as src: band = rasterio.band(src, 1) self.stack = Raster(band) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 1) def test_initiation_list_bands(self): # test init from list of rasterio.band objects bands = [] for f in self.predictors: src = rasterio.open(f) bands.append(rasterio.band(src, 1)) self.stack = Raster(bands) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 6) def test_initiation_rasterlayer(self): # test init from a single RasterLayer object with rasterio.open(nc.band1) as src: band = rasterio.band(src, 1) layer = RasterLayer(band) self.stack = Raster(layer) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 1) def test_initiation_list_rasterlayer(self): # test init from a list of RasterLayer objects layers = [] for f in self.predictors: src = rasterio.open(f) band = rasterio.band(src, 1) layers.append(RasterLayer(band)) self.stack = Raster(layers) self.assertIsInstance(self.stack, Raster) self.assertEqual(self.stack.count, 6) def test_initiation_array(self): # check initiation of single-band raster from file arr = np.zeros((100, 100)) self.stack = Raster(arr) # check output is written to tempfile self.assertTrue(os.path.exists(self.stack.iloc[0].file)) # check some operations on the created raster layer_name = list(self.stack.names)[0] self.stack = self.stack.rename({layer_name: 'new_layer'}) self.assertEqual(list(self.stack.names)[0], 'new_layer') # check initiation from array in memory arr = np.zeros((100, 100)) self.stack = Raster(arr, in_memory=True) layer_name = list(self.stack.names)[0] self.stack = self.stack.rename({layer_name: 'new_layer'}) self.assertEqual(list(self.stack.names)[0], 'new_layer')
aoi_file = os.path.join(path_shapes, 'aoi.geojson') # Formato geográfico manzana_file = os.path.join(path_shapes, 'manzana.geojson') # Formato geográfico img_name = img_train.split('_')[0] # Cargar la imagen como un objeto Raster Dataframe y el shape de aoi como Geodaataframe stack = Raster(img_file) training = gpd.read_file(aoi_file) manzana = gpd.read_file(manzana_file) bandsio = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12'] bandsio_names = ['B2 (Blue)', 'B3 (Green)', 'B4 (Red)', 'B5 (Veg. red edge)', 'B6 (Veg. red edge)', 'B7 (Veg. red edge)', 'B8 (NIR)', 'B8A (Narrow NIR)', 'B11 (SWIR)', 'B12 (SWIR)'] # Mapea los nombres de bandas de entrada [stack.rename({name: bandsio[idx]}, in_place=True) for idx, name in enumerate(stack.names)] extent = es.rio.plot.plotting_extent(stack) # %% [markdown] # ## Se define la selección de bandas de entrenamineto y salida para todo el proceso # %% bandsout = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12'] bandsout_names = ['B2 (Blue)', 'B3 (Green)', 'B4 (Red)', 'B5 (Veg. red edge)', 'B6 (Veg. red edge)', 'B7 (Veg. red edge)', 'B8 (NIR)', 'B8A (Narrow NIR)', 'B11 (SWIR)', 'B12 (SWIR)'] # %% [markdown] """ ## Anáisis exploratorio de datos Despliegue de imágenes y análisis exploratorio