class TestToCrs(TestCase): def setUp(self) -> None: # test inputs predictors = [ nc.band1, nc.band2, nc.band3, nc.band4, nc.band5, nc.band7 ] self.stack = Raster(predictors) # test results self.stack_prj = None def tearDown(self) -> None: self.stack.close() self.stack_prj.close() def test_to_crs_defaults(self): self.stack_prj = self.stack.to_crs({"init": "EPSG:4326"}) # check raster object self.assertIsInstance(self.stack_prj, Raster) self.assertEqual(self.stack_prj.count, self.stack.count) self.assertEqual(self.stack_prj.read(masked=True).count(), 1012061) # test nodata value is recognized self.assertEqual( self.stack_prj.read(masked=True).min(), self.stack.read(masked=True).min()) self.assertEqual( self.stack_prj.read(masked=True).max(), self.stack.read(masked=True).max()) def test_to_crs_custom_nodata(self): self.stack_prj = self.stack.to_crs({"init": "EPSG:4326"}, nodata=-999) # check raster object self.assertIsInstance(self.stack_prj, Raster) self.assertEqual(self.stack_prj.count, self.stack.count) self.assertEqual(self.stack_prj.read(masked=True).count(), 1012061) # test nodata value is recognized self.assertEqual( self.stack_prj.read(masked=True).min(), self.stack.read(masked=True).min()) self.assertEqual( self.stack_prj.read(masked=True).max(), self.stack.read(masked=True).max()) def test_to_crs_in_memory(self): self.stack_prj = self.stack.to_crs({"init": "EPSG:4326"}, in_memory=True) # check raster object self.assertIsInstance(self.stack_prj, Raster)
class TestSample(TestCase): def setUp(self) -> None: predictors = [nc.band1, nc.band2, nc.band3, nc.band4, nc.band5, nc.band7] self.stack = Raster(predictors) self.strata = Raster(nc.strata) def tearDown(self) -> None: self.stack.close() self.strata.close() def test_sample_strata(self): # extract using a strata raster and returning two arrays size = 100 categories = self.strata.read(masked=True).flatten() categories = categories[~categories.mask] n_categories = np.unique(categories).shape[0] n_samples = size * n_categories X, xy = self.stack.sample(size=size, strata=self.strata, return_array=True) self.assertEqual(X.shape, (n_samples, 6)) self.assertEqual(xy.shape, (n_samples, 2)) # extract using a strata raster and returning a dataframe samples = self.stack.sample(size=size, strata=self.strata, return_array=False) self.assertEqual(samples.shape, (n_samples, 7)) def test_sample_no_strata(self): size = 100 X, xy = self.stack.sample(size=size, return_array=True) self.assertEqual(X.shape, (size, 6)) self.assertEqual(xy.shape, (size, 2)) samples = self.stack.sample(size=size, return_array=False) self.assertEqual(samples.shape, (size, 7))
# spatial cross-validation from sklearn.cluster import KMeans # create 10 spatial clusters based on clustering of the training data point x,y coordinates clusters = KMeans(n_clusters=34, n_jobs=-1) clusters.fit(df_polygons.geometry.bounds.iloc[:, 0:2]) # cross validate scores = cross_validate( lr, X, y, groups=clusters.labels_, scoring='accuracy', cv=3, n_jobs=1) scores['test_score'].mean() # prediction df = stack.read(as_df=True, masked=True) result = stack.predict(estimator=lr, dtype='int16', nodata=0) result = stack.predict(estimator=lr, dtype='int16', nodata=0, as_df=True) result_prob = stack.predict_proba(estimator=lr) result.names result_prob.names result.plot() plt.show() result_prob.plot() plt.show() # sampling # extract training data using a random sample
ep.plot_rgb(rgb432, extent=extent, ax=axes[0,0], stretch=True, title='Verdadero color') manzana.boundary.plot(ax=axes[0,0], color='white', alpha=0.5, linewidth=1) # Plot falso color ep.plot_rgb(rgb843, extent=extent, ax=axes[0,1], stretch=True, title='Falso color') manzana.boundary.plot(ax=axes[0,1], color='white', alpha=0.5, linewidth=1) # Plot Class Keras ep.plot_bands(result_keras.read(), extent=extent, ax=axes[1,0], cmap='RdYlGn', alpha=0.8, title="Clasificación ANN", cbar=False) manzana.boundary.plot(ax=axes[1,0], color='white', alpha=0.5, linewidth=1) # Plot class KNeigh # Plot Class Keras ep.plot_bands(result_neigh.read(), extent=extent, ax=axes[1,1], cmap='RdYlGn', alpha=0.8, title="Clasificación KNeighbors", cbar=False) manzana.boundary.plot(ax=axes[1,1], color='white', alpha=0.5, linewidth=1) # Plot ndvi ep.plot_bands(raster_ndvi.read(), extent=extent, ax=axes[2,0], cmap='RdYlGn', alpha=0.8, title="NDVI", cbar=False) manzana.boundary.plot(ax=axes[2,0], color='white', alpha=0.5, linewidth=1) # Plot EVI ep.plot_bands(raster_evi.read(), extent=extent, ax=axes[2,1], cmap='RdYlGn', alpha=0.8, title="EVI", cbar=False) manzana.boundary.plot(ax=axes[2,1], color='white', alpha=0.5, linewidth=1) plt.tight_layout() plt.show() # %% df_ndvi = raster_ndvi.extract_vector(training) df_ndvi.rename(columns={df_ndvi.columns[0]: 'id'}, inplace=True) df_evi = raster_evi.extract_vector(training) df_evi.rename(columns={df_evi.columns[0]: 'id'}, inplace=True)