Numba:“此错误通常是由于传递了命名函数不支持的类型的参数造成的”

2024-04-26 23:23:01 发布

您现在位置:Python中文网/ 问答频道 /正文

当使用Numba的@jitclass decorator对对象调用类方法时,看起来 “self”参数不受支持。我不知道该如何处理这里的错误。 尽管调用了其他numpy函数,但所有的编译都很好,但是各个方法都没有抖动

spec = [('raster',numba.float32[:,:]),('height', numba.int32),('width', numba.int32),('azis', numba.int64[:]),('grid',numba.int64),('rough',numba.float64[:,:]),('maxrange',numba.float64[:,:]),('aziratio',numba.float64[:,:]),('labels',numba.float64[:,:])]

@jitclass(spec)
class raster_class(object):
    def __init__(self,raster):
        self.raster = raster
        self.height =self.raster.shape[0]
        self.width = self.raster.shape[1]
        self.azis =  np.arange(0,170,10)
        self.grid = 500
        x = np.int(self.height/self.grid)
        y = np.int(self.width/self.grid)
        self.rough = np.zeros((x,y))
        self.maxrange = np.zeros((x,y)) 
        self.aziratio = np.zeros((x,y))
        self.labels = np.zeros((x,y))


    def detrend(self):
        raster -= ndimage.gaussian_filter(self.raster,sigma=40)
        return raster


    def SR(self,image):
        image = image[~np.isnan(image)] # remove nan's
        image = np.ndarray.flatten(image)
        mean = np.mean(image)

        return np.sqrt((1/(len(image)-1))*np.sum((image-mean)**2))
    def getRange(self,mat):
    # fits an anisotropic variogram model and returns the effective range for a given azimuth
        m,n = mat.shape
        vals = np.reshape(mat,(m*n,1))
        coords = []
        for i in range(m):
            for j in range(n):
                coords.append((i,j))
                coords = np.array(coords)
                response = np.hstack((coords,vals))
                response = response[~np.isnan(response[:,-1])]
                response = response[response[:,-1] != 0]
                response = response[~np.isnan(response[:,-1])]
                coords = response[:,:2]
                response = response[:,2]
                response += np.random.normal(0,scale=0.25,size=response.shape[0]) #add noise to prevent same values

         azi_r = []
         for azi in self.azis:
            DV =  DirectionalVariogram(coords,response,azimuth=azi,tolerance=15,maxlag=250,n_lags=20) 
            azi_r.append(DV.cof[0])

        major = np.argmax(azi_r)
        large_range = azi_r[major]
        major = azis[major]

        if major  >= 90:
            perp = major - 90
        else:
            perp = major + 90
        minor = azis.index(perp)
        minor_range = azi_r(minor)
        ratio = large_range/minor_range
        return ratio,large_range

    def iterate(self):
        for i in range(0,self.height-self.grid,self.grid):
            for j in range(0,self.width-self.grid,self.grid):
                image = self.raster[i:i+self.grid,j:j+self.grid]
                indi = int(i/self.grid)
                indj = int(j/self.grid)
                roughness = self.SR(image)
                ratio,range_ = self.getRange(image)
                self.azi_ratio[indi,indj] = ratio
                self.largest_range[indi,indj] = range_
                self.response_rough[indi,indj] = roughness

if __name__ == "__main__":
    brooks = np.load("brooks_dem.npy")
    brooks_class = raster_class(brooks)
    time = time.time()
    brooks_class.iterate()
    end_time = time.time() - time
    hours = end_time/3600
    print("Computation Took {} Hours".format(hours))

错误消息

This error is usually caused by passing an argument of a type that is unsupported by the 
named function.
[1] During: typing of intrinsic-call at /home/dunbar/DEM/processraster.py (35)

File "processraster.py", line 35:
    def SR(self,image):
        image = image[~np.isnan(image)] # remove nan's
        ^

[1] During: resolving callee type: BoundFunction((<class 
'numba.types.misc.ClassInstanceType'>, 'SR') for 
instance.jitclass.raster_class#55ac81be91b8<raster:array(float32, 2d, 
A),height:int32,width:int32,azis:array(int64, 1d, A),grid:int64,rough:array(float64, 2d, 
A),maxrange:array(float64, 2d, A),aziratio:array(float64, 2d, A),labels:array(float64, 2d, 
A)>)
[2] During: typing of call at /home/dunbar/DEM/processraster.py (81)


File "processrabster.py", line 81:
    def iterate(self):
        <source elided>
                indj = int(j/self.grid)
                roughness = self.SR(image)
                ^

[1] During: resolving callee type: BoundFunction((<class 
'numba.types.misc.ClassInstanceType'>, 'iterate') for 
instance.jitclass.raster_class#55ac81be91b8<raster:array(float32, 2d, 
A),height:int32,width:int32,azis:array(int64, 1d, A),grid:int64,rough:array(float64, 2d, 
A),maxrange:array(float64, 2d, A),aziratio:array(float64, 2d, A),labels:array(float64, 2d, 
A)>)
[2] During: typing of call at <string> (3)


File "<string>", line 3:
<source missing, REPL/exec in use?>

Tags: imageselffortimeresponsenprangearray
1条回答
网友
1楼 · 发布于 2024-04-26 23:23:01

问题似乎出在SR方法中,但不幸的是,jitclass错误消息除此之外信息量不大。但是,由于这是一个静态方法,调试它的一个非常简单的方法是将其作为独立函数进行测试,即从类中取出SR,删除self参数,添加@njit装饰器,并在任意二维数组上运行SR

当我这样做时,我发现以下两个问题:

  1. image[~np.isnan(image)]是一种“高级”索引形式,因为它使用布尔数组作为输入。Numbaonly supports advanced indexing on a single dimension,但是image是二维的

  2. 您调用flatten就像从其ndarray类调用函数一样,即np.ndarray.flatten(image),但Numba只识别更标准的方法调用image.flatten()

您可以通过切换两行的顺序来修复点#1,首先写入image = image.flatten()(或者image = image.ravel(),因为不需要复制),然后再写入image = image[~np.isnan(image)]

幸运的是,对于您的特定应用程序,不需要执行这些操作,因为SR方法似乎可以替换为对^{}的调用,这是由Numba支持的

更一般地说,我赞同这样的评论,即使用Numba编译这样一个大型类并不是真正的预期用途(至少目前是这样);最好通过分析识别一些瓶颈,并专门编译这些瓶颈

相关问题 更多 >