def test_drop_inplace(self): stack = Raster(self.predictors) stack.drop(labels='lsat7_2000_50', in_place=True) # check that Raster object is returned self.assertIsInstance(stack, Raster) # check that RasterLayer has been dropped self.assertEqual(stack.count, 5) self.assertNotIn('lsat7_2000_50', stack.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_drop_with_copy(self): stack = Raster(self.predictors) names = stack.names result = stack.drop(labels='lsat7_2000_50', in_place=False) # check that Raster object is returned self.assertIsInstance(result, Raster) # check that RasterLayer has been dropped self.assertEqual(result.count, 5) self.assertNotIn('lsat7_2000_50', result.names) # check that original raster is unaffected self.assertEqual(stack.count, 6) self.assertEqual(stack.names, names)
def test_drop_in_memory(self): stack = Raster(self.predictors) names = stack.names result = stack.intersect(in_memory=True) result = stack.drop(labels="lsat7_2000_50", in_place=False) # check that Raster object is returned self.assertIsInstance(result, Raster) # check that RasterLayer has been dropped self.assertEqual(result.count, 5) self.assertNotIn("lsat7_2000_50", result.names) # check that original raster is unaffected self.assertEqual(stack.count, 6) self.assertEqual(stack.names, names) stack.close()
stack['new_name'].read(masked=True).mean() stack.loc['new_name'].read(masked=True).mean() stack.loc['new_name'].names # convert to pandas df = stack.to_pandas() df.head() df.columns from plotnine import * (ggplot(df.melt(id_vars=['x', 'y']), aes(x='x', y='y', fill='value')) + geom_tile(na_rm=True) + facet_wrap('variable')) # Drop a layer stack.names stack.drop(labels='lsat7_2000_50') stack.names # Modify a layer # templayer = ps.from_files(band1, mode='r+') # arr = templayer.lsat7_2000_10.read(window=Window(0, 0, 100, 100)) # arr[:] += 500 # templayer.lsat7_2000_10.write(arr, window=Window(0, 0, 100, 100)) # ras = templayer.lsat7_2000_10.read(masked=True) # plt.imshow(ras) # templayer = None # Save a stack tmp_tif = tempfile.NamedTemporaryFile().name + '.tif' newstack = stack.write(file_path=tmp_tif, nodata=-99) newstack.landsat_multiband_band1_1.read()