class TestVariogramMethods(unittest.TestCase):
    def setUp(self):
        # set up default values, whenever c and v are not important
        np.random.seed(42)
        self.c = np.random.gamma(10, 4, (30, 2))
        np.random.seed(42)
        self.v = np.random.normal(10, 4, 30)

        self.V = Variogram(self.c, self.v, normalize=False, n_lags=10)

    def test_clone_method(self):
        # copy variogram
        copy = self.V.clone()

        # test against bins and experimental
        assert_array_almost_equal(copy.experimental, self.V.experimental)
        assert_array_almost_equal(copy.bins, self.V.bins)

    def test_data_no_force(self):
        lags, var = self.V.data(n=10, force=False)

        assert_array_almost_equal(
            lags, [0., 4.7, 9.4, 14.1, 18.8, 23.5, 28.2, 32.9, 37.6, 42.3],
            decimal=2)

        assert_array_almost_equal(var, [
            0., 11.82, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97
        ],
                                  decimal=2)

    def test_data_with_force(self):
        # should work if _dist is corccupted
        self.V._dist = self.V._dist * 5.
        self.V.cof = None
        lags, var = self.V.data(n=10, force=True)

        assert_array_almost_equal(
            lags, [0., 4.7, 9.4, 14.1, 18.8, 23.5, 28.2, 32.9, 37.6, 42.3],
            decimal=2)

        assert_array_almost_equal(var, [
            0., 11.82, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97
        ],
                                  decimal=2)

    def test_data_normalized(self):
        V = self.V.clone()

        V.normalize = True

        lags, var = V.data(n=5, force=True)

        assert_array_almost_equal(lags, [0., 10.58, 21.15, 31.73, 42.3],
                                  decimal=2)

        assert_array_almost_equal(var, [0., 13.97, 13.97, 13.97, 13.97],
                                  decimal=2)

    def test_parameter_property_matern(self):
        V = self.V.clone()

        # test matern
        param = [42.3, 15.42, 0.11, 0.]
        V.set_model('matern')
        assert_array_almost_equal(V.parameters, param, decimal=2)

    def test_parameter_property_stable(self):
        V = self.V.clone()

        # test stable
        param = [42.3, 15.79, 0.45, 0.]
        V.set_model('stable')
        assert_array_almost_equal(V.parameters, param, decimal=2)
示例#2
0
class TestVariogramMethods(unittest.TestCase):
    def setUp(self):
        # set up default values, whenever c and v are not important
        np.random.seed(42)
        self.c = np.random.gamma(10, 4, (30, 2))
        np.random.seed(42)
        self.v = np.random.normal(10, 4, 30)

        self.V = Variogram(self.c, self.v, normalize=False, n_lags=10)

    def test_get_empirical(self):
        bins = self.V.bins
        exp = self.V.experimental

        emp_x, emp_y = self.V.get_empirical()

        # test
        assert_array_almost_equal(bins, emp_x)
        assert_array_almost_equal(exp, emp_y)

    def test_get_empirical_center(self):
        V = Variogram(self.c, self.v)

        # overwrite bins
        V.bins = [4, 8, 9, 12, 15]
        emp_x, emp_y = V.get_empirical(bin_center=True)

        assert_array_almost_equal(emp_x, [2., 6., 8.5, 10.5, 13.5])

    def test_clone_method(self):
        # copy variogram
        copy = self.V.clone()

        # test against bins and experimental
        assert_array_almost_equal(copy.experimental, self.V.experimental)
        assert_array_almost_equal(copy.bins, self.V.bins)

    def test_data_no_force(self):
        lags, var = self.V.data(n=10, force=False)

        assert_array_almost_equal(
            lags, [0., 4.7, 9.4, 14.1, 18.8, 23.5, 28.2, 32.9, 37.6, 42.3],
            decimal=2)

        assert_array_almost_equal(var, [
            0., 11.82, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97
        ],
                                  decimal=2)

    def disabled_test_data_with_force(self):
        # Distance can no longer be explicitly set
        # it would require setting the whole MetricSpace, with a
        # non-sparse diagonal matrix

        # should work if _dist is corccupted
        self.V._dist = self.V._dist * 5.
        self.V.cof = None
        lags, var = self.V.data(n=10, force=True)

        assert_array_almost_equal(
            lags, [0., 4.7, 9.4, 14.1, 18.8, 23.5, 28.2, 32.9, 37.6, 42.3],
            decimal=2)

        assert_array_almost_equal(var, [
            0., 11.82, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97, 13.97
        ],
                                  decimal=2)

    def test_data_normalized(self):
        V = self.V.clone()

        V.normalize = True

        lags, var = V.data(n=5, force=True)

        assert_array_almost_equal(lags, [0., 10.58, 21.15, 31.73, 42.3],
                                  decimal=2)

        assert_array_almost_equal(var, [0., 13.97, 13.97, 13.97, 13.97],
                                  decimal=2)

    def test_parameter_property_matern(self):
        V = self.V.clone()

        # test matern
        param = [42.3, 16.2, 0.1, 0.]
        V.set_model('matern')
        assert_array_almost_equal(V.parameters, param, decimal=2)

    def test_parameter_property_stable(self):
        V = self.V.clone()

        # test stable
        param = [42.3, 15.79, 0.45, 0.]
        V.set_model('stable')
        assert_array_almost_equal(V.parameters, param, decimal=2)
示例#3
0
class TestPyKrigeInterface(unittest.TestCase):
    def setUp(self):
        # use real sample data in the interface
        df = pd.read_csv(os.path.join(os.path.dirname(__file__), 'sample.csv'))
        self.c = df[['x', 'y']].values
        self.v = df.z.values

        self.V = Variogram(self.c,
                           self.v,
                           model='matern',
                           normalize=False,
                           use_nugget=True)

        if not PYKRIGE_AVAILABLE:
            print('PyKrige not found, will skip all pykrige interface tests')

    def test_model_interface(self):
        if not PYKRIGE_AVAILABLE:  # pragma: no cover
            return True
        # get the function
        model = pykrige_interface.pykrige_model(self.V)

        # use the transform function.
        xi = np.arange(1, 85)
        yi = self.V.transform(xi)

        assert_array_almost_equal(yi, model([], xi), decimal=6)

    def test_model_interface_from_list(self):
        if not PYKRIGE_AVAILABLE:  # pragma: no cover
            return True

        # get the function
        model = pykrige_interface.pykrige_model(self.V)

        # use the transform function
        xi = list(range(1, 85))
        yi = self.V.transform(np.array(xi))

        assert_array_almost_equal(yi, model([], xi), decimal=6)

    def test_parameters(self):
        if not PYKRIGE_AVAILABLE:  # pragma: no cover
            return True

        p = pykrige_interface.pykrige_params(self.V)
        params = self.V.parameters

        self.assertAlmostEqual(p[0], params[1], places=4)
        self.assertAlmostEqual(p[1], params[0], places=4)
        self.assertAlmostEqual(p[2], params[2], places=4)

    def test_as_kwargs(self):
        if not PYKRIGE_AVAILABLE:  # pragma: no cover
            return True

        args = pykrige_interface.pykrige_as_kwargs(self.V)
        pars = pykrige_interface.pykrige_params(self.V)

        # test
        self.assertEqual(args['variogram_model'], 'custom')
        assert_array_almost_equal(pars, args['variogram_parameters'])

        xi = np.arange(1, 80)
        yi = self.V.transform(xi)
        assert_array_almost_equal(yi,
                                  args['variogram_function']([], xi),
                                  decimal=6)

    def test_as_kwargs_adjust_maxlag(self):
        if not PYKRIGE_AVAILABLE:  # pragma: no cover
            return True

        V = self.V.clone()

        # now maxlag should be changed
        args = pykrige_interface.pykrige_as_kwargs(V, adjust_maxlag=True)

        # should be None
        self.assertIsNone(V.maxlag)

        # transform should change
        xi = np.arange(1, 20)
        yi = V.transform(xi)

        # test changed values
        assert_array_almost_equal(yi, args['variogram_function']([], xi))

    def test_as_kwargs_adjust_nlags(self):
        if not PYKRIGE_AVAILABLE:  # pragma: no cover
            return True

        args = pykrige_interface.pykrige_as_kwargs(self.V, adjust_nlags=True)

        self.assertEqual(args['nlags'], self.V.n_lags)