python mount回调函数_为python回调函数设置argtype

importctypesimportmathimportitertools

MAX_FEATURE_SIZE=25FEATURE_t=ctypes.c_int

FEATURE_ptr=ctypes.POINTER(FEATURE_t)WEIGHT_t=ctypes.c_float

WEIGHT_ptr=ctypes.POINTER(WEIGHT_t)COUNT_t=ctypes.c_int

COUNT_ptr=ctypes.POINTER(COUNT_t)classFLOW_t(ctypes.Structure):_fields_=[("frm",ctypes.c_int),("to",ctypes.c_int),("amount",ctypes.c_float)]# Note that ctypes.POINTER is compatible with a ctypes array declared# as TYPE * array_len. This is equivalent to the way we can say 'char# *foo = "ABCDEF"' in C.classSIGNATURE_t(ctypes.Structure):_fields_=[("N",COUNT_t),("feature",FEATURE_ptr),("weight",WEIGHT_ptr)]FLOW_ARRAY_t=FLOW_t*(2*MAX_FEATURE_SIZE-1)CMPFUNC_t=ctypes.CFUNCTYPE(ctypes.c_float,FEATURE_ptr,FEATURE_ptr)SIGNATURE_ptr=ctypes.POINTER(SIGNATURE_t)FLOW_ptr=ctypes.POINTER(FLOW_t)# Convenience function - keeps us from having to remember all the types and parameters later ondefmake_signature(features,weights):sig=SIGNATURE_t()sig.N=len(features)sig.feature=(len(features)*FEATURE_t)(*features)sig.weight=(len(weights)*WEIGHT_t)(*weights)returnsig# We want to pass into C a custom distance function from Pythondefpy_dist_func(f1,f2):# print "f1, f2: %d, %d" % ( f1[0], f2[0] )d=distance(f1[0],f2[0])returnd# set this up as a holder for distance function between any two n-D pointsdefdistance(p0,p1):return(math.fabs(p0-p1))dist_callback=CMPFUNC_t(py_dist_func)#print "Importing emdlib"emdlib=ctypes.CDLL('emdlib.dylib')#print "Setting argtypes"emdlib.emd.argtypes=[SIGNATURE_ptr,SIGNATURE_ptr,CMPFUNC_t,FLOW_ptr,COUNT_ptr]#print "Setting restype"emdlib.emd.restype=ctypes.c_float

feature1=[0,1,2,3,4,5,6,7,8]feature2=[0,1,2,3,4,5,6,7,8]weight1=[0.275,0.296,0.002,0.131,0.208,0.048,0.058,0.098,0.455]weight2=[0.285,0.421,0.028,0.021,0.240,0.166,0.023,0.054,0.469]#print "Creating signatures"signature1=make_signature(feature1,weight1)signature2=make_signature(feature2,weight2)flow_array=FLOW_ARRAY_t()flow_size=COUNT_t()#print "Calling EMD"e=emdlib.emd(ctypes.byref(signature1),ctypes.byref(signature2),dist_callback,flow_array,ctypes.byref(flow_size))print"EMD= ",eprint"Number of FlowS",flow_size.valueprint"Flow"print"from to amount"totalFlow=0.0foriinrange(0,flow_size.value):# print "Flow from %d to %d amount :%f" %(flow_array[i].frm, flow_array[i].to, flow_array[i].amount)print" %d %d %f"%(flow_array[i].frm,flow_array[i].to,flow_array[i].amount)totalFlow=totalFlow+flow_array[i].amount## now adjust EMD to account for different signature masses and make it a metricalpha=1.0mass1=sum(weight1)mass2=sum(weight2)fList=[feature1,feature2]max_distance=0.0forp0,p1inlist(itertools.product(*fList)):# print p0,p1, distance(p0,p1), max_distancemax_distance=max(max_distance,distance(p0,p1))print"\nMax distance= %f"%max_distanceprint"Total Source = %f"%mass1print"Total Demand = %f"%mass2print"Total Flow= %f\n "%totalFlowprint"Alpha = %f\n"%alpha# emdHat = e*totalFlow+math.sqrt((mass1-mass2)*(mass1-mass2))*alpha*max_distanceemdHat=e*totalFlow+math.fabs((mass1-mass2))*alpha*max_distanceprint"Corrected Earth Movers Distance \n"print"emdHat = %f\n"%emdHat;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值