def test_append_with_copy(self): """Same tests as above but create a new Raster rather than append in place """ # append another Raster containing a single layer with identical name stack = Raster(self.predictors) band7_mean = stack["lsat7_2000_70"].read(masked=True).mean() result = stack.append(Raster(nc.band7), in_place=False) # check that original is untouched self.assertEqual(stack.count, 6) # check that result contains appended raster self.assertEqual(list(result.names)[5], "lsat7_2000_70_1") self.assertEqual(list(result.names)[-1], "lsat7_2000_70_2") # check that band 7 stats are the same after appending self.assertEqual( result.lsat7_2000_70_1.read(masked=True).mean(), result.lsat7_2000_70_2.read(masked=True).mean(), band7_mean, ) # append a multiband raster result = stack.append(Raster(nc.multiband), in_place=False) self.assertEqual(list(result.names)[6], "landsat_multiband_1") stack.close() # append multiple rasters stack = Raster(self.predictors) new_stack = stack.append( [Raster(nc.band5), Raster(nc.band7)], in_place=False) self.assertEqual(new_stack.count, 8)
def test_append_inplace(self): # append another Raster containing a single layer with identical name stack = Raster(self.predictors) band7_mean = stack.loc['lsat7_2000_70'].read(masked=True).mean() stack.append(Raster(nc.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 = Raster(self.predictors) stack.append(Raster(nc.multiband)) self.assertEqual(stack.names[6], 'landsat_multiband_1')
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_append_inplace(self): """Append another Raster containing a single layer with identical name This test should cause the Raster object to automatically rename the duplicated names as "lsat7_2000_70_1", "lsat7_2000_70_2", etc. Appending a multi-band raster should result in a new layer with the multi-band name "landsat_multiband_1", "landsat_multiband_2", etc. A list of Rasters can be passed to append() to append multiple rasters """ # append a single band raster with the same name stack = Raster(self.predictors) band7_mean = stack["lsat7_2000_70"].read(masked=True).mean() stack.append(Raster(nc.band7), in_place=True) self.assertEqual(list(stack.names)[5], "lsat7_2000_70_1") self.assertEqual(list(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 = Raster(self.predictors) stack.append(Raster(nc.multiband), in_place=True) self.assertEqual(list(stack.names)[6], "landsat_multiband_1") stack.close() # append multiple rasters stack = Raster(self.predictors) stack.append([Raster(nc.band5), Raster(nc.band7)], in_place=True) self.assertEqual(stack.count, 8)
def test_append_with_copy(self): # append another Raster containing a single layer with identical name stack = Raster(self.predictors) band7_mean = stack.loc['lsat7_2000_70'].read(masked=True).mean() result = stack.append(Raster(nc.band7), in_place=False) # check that original is untouched self.assertEqual(stack.count, 6) # check that result contains appended raster self.assertEqual(result.names[5], 'lsat7_2000_70_1') self.assertEqual(result.names[-1], 'lsat7_2000_70_2') # check that band 7 stats are the same after appending self.assertEqual( result.lsat7_2000_70_1.read(masked=True).mean(), result.lsat7_2000_70_2.read(masked=True).mean(), band7_mean) # append a multiband raster result = stack.append(Raster(nc.multiband), in_place=False) self.assertEqual(result.names[6], 'landsat_multiband_1')
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)) stack.names print(stack.testme.read(masked=True).mean()) print(stack.lsat7_2000_70_1.read(masked=True).mean()) print(stack.lsat7_2000_70_2.read(masked=True).mean()) stack.append(Raster(nc.multiband)) stack.names print(stack.testme.read(masked=True).mean()) print(stack.landsat_multiband_band1_band1.read(masked=True).mean()) # Rename layers stack.names print(stack.lsat7_2000_30.read(masked=True).mean()) stack.rename({'lsat7_2000_30': 'new_name'}) stack.names
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(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 stack.append(Raster(band7)) stack.names print(stack.testme.read(masked=True).mean()) print(stack.lsat7_2000_70_1.read(masked=True).mean()) print(stack.lsat7_2000_70_2.read(masked=True).mean()) stack.append(Raster(multiband)) stack.names print(stack.testme.read(masked=True).mean()) print(stack.landsat_multiband_band1_band1.read(masked=True).mean()) # Rename layers stack.names print(stack.lsat7_2000_30.read(masked=True).mean()) stack.rename({'lsat7_2000_30': 'new_name'}) stack.names