def krig(vario, grid, **kwargs): # get the grid xx, yy = grid ok = OrdinaryKriging(vario, **kwargs) field = ok.transform(xx.flatten(), yy.flatten()) sigma = ok.sigma return field.reshape(xx.shape), sigma.reshape(xx.shape)
def ordinary_kriging(x, y, z, grid, model='spherical', estimator='matheron', n_lags=15, maxlag='median', min_points=5, max_points=15, mode='exact', precision=1000, **settings): # build coordinates coords = __point_array(x, y) # fit a Variogram V = Variogram(coords, z, model=model, estimator=estimator, n_lags=n_lags, maxlag=maxlag, normalize=False) # get the shape and build the Kriging shape = grid[0].shape ok = OrdinaryKriging(V, min_points=min_points, max_points=max_points, mode=mode, precision=precision) # apply return ok.transform(grid[0].flatten(), grid[1].flatten()).reshape(shape)
def setUp(self): # Generate some random but spatially correlated data # with a range of ~20 np.random.seed(42) c = np.random.sample((50, 2)) * 60 np.random.seed(42) v = np.random.normal(10, 4, 50) V = Variogram(c, v).describe() V["effective_range"] = 20 OK = OrdinaryKriging(V, coordinates=c, values=v) self.c = np.random.sample((500, 2)) * 60 self.v = OK.transform(self.c) self.c = self.c[~np.isnan(self.v), :] self.v = self.v[~np.isnan(self.v)]
def test_ordinary(self): if not GSTOOLS_AVAILABLE: # pragma: no cover return True x = np.array([self.c[0][0]]) y = np.array([self.c[0][1]]) # run ordinary kriging with skgstat ok = OrdinaryKriging(self.V, min_points=3) sk_res = ok.transform(x, y) # get the gstools Krige class krige = self.V.to_gs_krige() gs_res, _ = krige.structured([x, y]) # test assert_array_almost_equal(sk_res.flatten(), gs_res.flatten(), decimal=1)
class TestPerformance(unittest.TestCase): """ The TestPerformance class is not a real unittest. It will always be true. It does apply some benchmarking, which could be included into the testing framework, as soon as the OrdinaryKriging class is finalized. From that point on, new code should not harm the performance significantly. """ def setUp(self): # define the target field def func(x, y): return np.sin(0.02 * np.pi * y) * np.cos(0.02 * np.pi * x) # create a grid self.grid_x, self.grid_y = np.mgrid[0:100:100j, 0:100:100j] # sample the field np.random.seed(42) self.x = np.random.randint(100, size=300) np.random.seed(1337) self.y = np.random.randint(100, size=300) self.z = func(self.x, self.y) # build the Variogram and Kriging class self.V = Variogram(list(zip(self.x, self.y)), self.z, model='gaussian', n_lags=15, maxlag=0.4) self.ok = OrdinaryKriging(self.V, min_points=2, max_points=5, perf=True) def _run_benchmark(self, points): xi = self.grid_x.flatten()[:points] yi = self.grid_y.flatten()[:points] # run res = self.ok.transform(xi, yi) self.ok.perf_dist *= 1000 self.ok.perf_mat *= 1000 self.ok.perf_solv *= 1000 print('Benchmarking OrdinaryKriging...') print('-------------------------------') print('Points:', points) print('Solver:', self.ok.solver) print('Mode:', self.ok.mode) print('Build distance matrix: %.1f ms (%.4f ms each)' % (np.sum(self.ok.perf_dist), np.std(self.ok.perf_dist))) print('Build variogram matrix: %.1f ms (%.4f ms each)' % (np.sum(self.ok.perf_mat), np.std(self.ok.perf_mat))) print('Solve kriging matrix: %.1f ms (%.4f ms each)' % (np.sum(self.ok.perf_solv), np.std(self.ok.perf_solv))) print('---------------------------------------------') def test_20points_exact(self): self.ok.mode = 'exact' self.ok.solver = 'inv' self._run_benchmark(points=20) def test_100points_exact(self): self.ok.mode = 'exact' self.ok.solver = 'inv' self._run_benchmark(points=100) def test_20points_estimate(self): self.ok.mode = 'estimate' self.ok.solver = 'inv' self._run_benchmark(points=20) def test_100points_estimate(self): self.ok.mode = 'estimate' self.ok.solver = 'inv' self._run_benchmark(points=100)