Numba TypingError: Type of variable cannot be determined

我与影子孤独终老i 提交于 2019-12-11 07:49:34

问题


This question is the follow up of this question: Why this python class is not working with numba jitclass?

Now the issue I am having that numba is unable to determine the types of the variable which I think I already explicitly defined in the specifications.

Here is the code. I have updated it with a rnd function as numba doesn't allow np.round with only two parameters.

import numpy as np
import math
from numba import jitclass 
from numba import float64,int64

spec =[
       ('spacing',float64),
       ('n_iterations',int64),
       ('np_emptyhouses',float64[:,:]),
       ('np_agenthouses',float64[:,:]),
       ('similarity_threshhold',float64),
       ('n_changes',int64)
       ]

@jitclass(spec)
class geo_schelling_update:

    def __init__(self,n_iterations,spacing,np_agenthouses,np_emptyhouses,similarity_threshhold):
        self.spacing=spacing
        self.n_iterations=n_iterations
        self.np_emptyhouses=np_emptyhouses
        self.np_agenthouses=np_agenthouses
        self.similarity_threshhold=similarity_threshhold

    def rnd(self,x,decimals):
        return np.round_(x,decimals,np.empty_like(x))

    def distance_vectorize(self,pointA1, pointA2,agent):
        x_square=np.square(pointA1-agent[0])
        y_square=np.square(pointA2-agent[1])
        dist=np.sqrt(np.array(x_square,dtype=np.float32)+np.array(y_square,dtype=np.float32))
        return self.rnd(dist,4)

    def is_unsatisfied_vectorize(self,x,y):
        race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]
        euclid_distance1=round(math.hypot(self.spacing,self.spacing),4)
        euclid_distance2=self.spacing
        total_agents=np.extract(np.logical_or(np.equal(self.rnd(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance1),np.equal(self.rnd(np.hypot((self.np_agenthouses[:,0]-(x)),(self.np_agenthouses[:,1]-(y))),4),euclid_distance2)),self.np_agenthouses[:,2])
        if total_agents.size ==0:
            return False
        else:
            return np.extract(np.equal(total_agents,race),total_agents).size<self.similarity_threshhold    

    def move_to_empty(self,x,y):
        race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]
        x_new,y_new=self.np_emptyhouses[np.random.choice(self.np_emptyhouses.shape[0],1),:][0]
        self.np_agenthouses=self.np_agenthouses[~(np.logical_and(np.equal(self.np_agenthouses[:,0],x), np.equal(self.np_agenthouses[:,1],y)))]
        self.np_agenthouses=np.vstack(np.array([self.np_agenthouses,np.array([x_new,y_new,race])]))
        self.np_emptyhouses=self.np_emptyhouses[~(np.logical_and(np.equal(self.np_emptyhouses[:,0],x_new), np.equal(self.np_emptyhouses[:,1],y_new)))]
        self.np_emptyhouses=np.vstack(np.array([self.np_emptyhouses,np.array([x,y])]))

    def update_helper(self,agent):
        if self.is_unsatisfied_vectorize(agent[0],agent[1]):
            self.move_to_empty(agent[0],agent[1])
            return 1
        else:
            return 0

    def update(self):
        for i in np.arange(self.n_iterations):
            np_oldagenthouses=self.np_agenthouses.copy()
            n_changes=0
            for row in np_oldagenthouses:
                n=self.update_helper(row)
                n_changes+=n
            print(n_changes)
            print(i)
            if n_changes == 0:
                break



np_agenthouses=np.array([[-71.8,    41.4,   2.0],
                        [-71.6, 41.4,   2.0],
                        [-71.6, 41.6,   2.0],
                        [-71.4, 41.6,   1.0],
                        [-71.6, 41.8,   1.0],
                        [-71.4, 41.8,   2.0],
                        [-71.6, 42.0,   2.0],
                        [-71.4, 42.0,   1.0],
                        [-71.4, 41.4,   2.0],
                        [-71.2, 41.4,   1.0]])

np_emptyhouses=np.array([[-71.8,  41.3],[-71.8,  41.4],[-71.5,  41.5],
                [-71.5,  41.6],[-71.7,  41.8],[-71.7,  41.9],
                [-71.5,  41.9],[-71.2,  41.4],[-71.6,  41.7]])

spacing=0.1
similarity_threshhold=0.65
n_iterations=100
schelling= geo_schelling_update(n_iterations,
                         spacing,
                         np_agenthouses,
                         np_emptyhouses,similarity_threshhold)
schelling.update()                   

Here is the error:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Use of unsupported NumPy function 'numpy.extract' or unsupported use of the function.

File "test2.py", line 42:
    def is_unsatisfied_vectorize(self,x,y):
        race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]
        ^

[1] During: typing of get attribute at C:/Users/ksharma/Documents/geoschelling/test2.py (42)

File "test2.py", line 42:
    def is_unsatisfied_vectorize(self,x,y):
        race = np.extract(np.logical_and(np.equal(self.np_agenthouses[:,0],x),np.equal(self.np_agenthouses[:,1],y)),self.np_agenthouses[:,2])[0]
        ^

[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'is_unsatisfied_vectorize') for instance.jitclass.geo_schelling_update#1747694fe48<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[2] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (60)


File "test2.py", line 60:
    def update_helper(self,agent):
        if self.is_unsatisfied_vectorize(agent[0],agent[1]):
        ^

[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'update_helper') for instance.jitclass.geo_schelling_update#1747694fe48<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[2] During: typing of call at C:/Users/ksharma/Documents/geoschelling/test2.py (71)


File "test2.py", line 71:
    def update(self):
        <source elided>
            for row in np_oldagenthouses:
                n=self.update_helper(row)
                ^

[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'update') for instance.jitclass.geo_schelling_update#1747694fe48<spacing:float64,n_iterations:int64,np_emptyhouses:array(float64, 2d, A),np_agenthouses:array(float64, 2d, A),similarity_threshhold:float64,n_changes:int64>)
[2] During: typing of call at <string> (3)

来源:https://stackoverflow.com/questions/57466531/numba-typingerror-type-of-variable-cannot-be-determined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!