0

I have been trying to experiment with the geostatistics library`, one of its file has the following function,

def krige( data, covfct, grid, method='simple', N=0, nugget=0 ):
    '''
    Krige an <Nx2> array of points representing a grid.

    Use either simple or ordinary kriging, some number N
    of neighboring points, and a nugget value.
    '''
    if method == 'simple':
        k = lambda d, c, u, N, nug: simple( d, c, u, N, nug ) 
    elif method == 'ordinary':
        k = lambda d, c, u, N, nug: ordinary( d, c, u, N, nug )
    print('method = ',method)
    M = len( grid )
    est, kstd = np.zeros(( M, 1 )), np.zeros(( M, 1 ))
    for i in range( M ):
        est[i], kstd[i] = k( data, covfct, grid[i], N, nugget )
    return est, kstd

It seems to me that it contains two possible functions:, simple and ordinary, through lambda. Does u after lambda correspond to grid[i] in est[i], kstd[i] = k( data, covfct, grid[i], N, nugget )?

I run the example code given here, given here. For instance,

import unittest
from geostatsmodels import kriging 
import numpy as np

data = np.array([[0,0,1],[1,0,3],[0,1,3],[1,1,4]])
hs = np.array([0.5,1.0,1.5,2.0])
bw = 0.5
u = [0.5,0.5]
N = 2
eps = 0.0001
kv = kriging.krige(data,kriging.spherical,hs,bw,u,N)

It gives the following error message, I do not understand what does it mean?

enter image description here

user288609
  • 12,465
  • 26
  • 85
  • 127

1 Answers1

3
if method == 'simple':
    k = lambda d, c, u, N, nug: simple( d, c, u, N, nug ) 
elif method == 'ordinary':
    k = lambda d, c, u, N, nug: ordinary( d, c, u, N, nug )

If any of conditions are not met, k never exists so it gives the UnboundLocalError.

kv = kriging.krige(data,kriging.spherical,hs,bw,u,N) here you are passing bw = 0.5 which is neither 'simple' nor 'ordinary'. you need to pass required argument to function.

Rahul
  • 10,830
  • 4
  • 53
  • 88
  • Although this doesn't seem possible - the ```method``` parameter has a default argument of ```simple``` so that condition should always *pass* – wwii Jul 15 '17 at 04:32
  • You're using the word _default_, yet you don't seem to understand that it means "only if no other value is specified". – John Gordon Jul 15 '17 at 04:41
  • Thank you all. So what can be the reason that cause the unboundlocal error. My sense is the usage of K, but I don't know how to correct it. Because logically it seems OK. – user288609 Jul 15 '17 at 13:43
  • Logically it doesn't seem ok. The 'krige' function only accepts 4th argument as string either 'simple' or 'ordinary'. You are passing bw which is 0.5. That is not acceptable. Change bw = 'ordinary' or change your function krige. – Rahul Aug 02 '17 at 07:20