Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def test_max_points_smaller_min_points(self):
        with self.assertRaises(ValueError) as e:
            ok = OrdinaryKriging(self.V, min_points=3, max_points=5)
            ok.max_points = 2

        self.assertEqual(str(e.exception),
                         'max_points can\'t be smaller than min_points.')
Ejemplo n.º 3
0
 def test_max_points_negative(self):
     with self.assertRaises(ValueError) as e:
         ok = OrdinaryKriging(self.V, max_points=10)
         ok.max_points = - 2
         
     self.assertEqual(
         str(e.exception), 'max_points can\'t be negative.'
     )
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
    def test_mode_settings(self):
        # estimate mode
        ok = OrdinaryKriging(self.V, mode='estimate')
        self.assertIsNotNone(ok._prec_g)
        self.assertIsNotNone(ok._prec_dist)

        # exact mode
        ok.mode = 'exact'
        self.assertIsNone(ok._prec_g)
        self.assertIsNone(ok._prec_dist)
Ejemplo n.º 6
0
 def test_mode_unknown(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, mode='foo')
         
     self.assertEqual(
         str(e.exception), "mode has to be one of 'exact', 'estimate'."
     )
Ejemplo n.º 7
0
 def test_max_points_type_check(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, max_points=16.0)
     
     self.assertEqual(
         str(e.exception), 'max_points has to be an integer.'
     )
Ejemplo n.º 8
0
 def test_min_points_larger_max_points(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, min_points=10, max_points=5)
     
     self.assertEqual(
         str(e.exception), 'min_points can\'t be larger than max_points.'
     )
Ejemplo n.º 9
0
 def test_min_points_negative(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, min_points=-2)
     
     self.assertEqual(
         str(e.exception), 'min_points can\'t be negative.'
     )
Ejemplo n.º 10
0
 def test_solver_AttributeError(self):
     with self.assertRaises(AttributeError) as e:
         OrdinaryKriging(self.V, solver='peter')
         
     self.assertEqual(
         str(e.exception), "solver has to be ['inv', 'numpy', 'scipy']"
     )
Ejemplo n.º 11
0
 def test_precision_ValueError(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, precision=0)
         
     self.assertEqual(
         str(e.exception), 'The precision has be be > 1'
     )
Ejemplo n.º 12
0
 def test_precision_TypeError(self):
     with self.assertRaises(TypeError) as e:
         OrdinaryKriging(self.V, precision='5.5')
         
     self.assertEqual(
         str(e.exception), 'precision has to be of type int'
     )
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
    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)]
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    def test_coordinates_with_duplicates(self):
        c = self.c.copy()

        # create two duplicates
        c[14] = c[42]
        c[8] = c[42]

        V = Variogram(c, self.v)
        ok = OrdinaryKriging(V)

        # two instances should be removed
        self.assertEqual(len(ok.coords), 50 - 2)
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
 def test_max_points_smaller_min_points(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, min_points=10, max_points=5)
         self.assertEqual(
             str(e), 'max_points can\'t be larger smaller min_points.')
Ejemplo n.º 19
0
 def test_min_points_type_check(self):
     with self.assertRaises(ValueError) as e:
         OrdinaryKriging(self.V, min_points=4.0)
         self.assertEqual(str(e), 'min_points has to be an integer.')
Ejemplo n.º 20
0
 def test_coordinates_and_values(self):
     ok = OrdinaryKriging(self.V)
     assert_array_almost_equal(self.c, ok.coords)