日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/10 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python mount回调函数_为python回调函数设置argtype 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我對Python很在行,所以希望我能正確地表達(dá)這個問題。在

整個問題涉及從Python調(diào)用C例程。我可以通過把一些相關(guān)的問題/答案湊在一起來接近,但我似乎不能把事情安排得很好。有兩個方面:第一個是用指針調(diào)用C例程,第二個是使用回調(diào)函數(shù)。在

背景

Rubner提供了一個用C[EMD C code location]編寫的EMD例程,他還提供了兩個調(diào)用EMD例程的示例C程序。我正在嘗試開發(fā)一個Python例程,作為example2.c的替代,它將調(diào)用EMD例程。(是的,我熟悉EMD的OpenCV實現(xiàn)。)

為了方便起見,下面是我想從python調(diào)用的emd.c代碼的頭文件:/* DEFINITIONS */

#define MAX_SIG_SIZE 100

#define MAX_ITERATIONS 500

#define INFINITY 1e20

#define EPSILON 1e-6

/*****************************************************************************/

/* feature_t SHOULD BE MODIFIED BY THE USER TO REFLECT THE FEATURE TYPE */

typedef int feature_t;

/* typedef struct { int X,Y,Z; } feature_t;*/

/*typedef struct { int X; } feature_t; */

/*****************************************************************************/

typedef struct

{

int n; /* Number of features in the signature */

feature_t *Features; /* Pointer to the features vector */

float *Weights; /* Pointer to the weights of the features */

} signature_t;

typedef struct

{

int from; /* Feature number in signature 1 */

int to; /* Feature number in signature 2 */

float amount; /* Amount of flow from "from" to "to" */

} flow_t;

float emd(signature_t *Signature1, signature_t *Signature2,

float (*func)(feature_t *, feature_t *),

flow_t *Flow, int *FlowSize);

#endif

最后,這里是我迄今為止拼湊起來的python代碼。我認(rèn)為(但不確定)我已經(jīng)正確地設(shè)置了結(jié)構(gòu)。(請注意,這是Rubner emd.c代碼中可能的功能結(jié)構(gòu)的簡化版本。我最終想讓整個工作正常,但現(xiàn)在我開始變得簡單了。)我遇到的第一個問題是調(diào)用函數(shù)的argtypes中的某個地方。我嘗試過一些變體,但是在網(wǎng)上可以找到的例子非常少,我遇到了困難。在

^{pr2}$

如果有任何關(guān)于我哪里出錯的建議,我們將不勝感激。在

我肯定會遇到的第二個問題是返回指針的argtypes;這里的任何建議都將受到歡迎。在

提前謝謝。在

------------更新(工作)代碼import ctypes

import math

import itertools

MAX_FEATURE_SIZE = 25

FEATURE_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)

class FLOW_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.

class SIGNATURE_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 on

def make_signature(features, weights):

sig = SIGNATURE_t()

sig.N = len(features)

sig.feature = (len(features) * FEATURE_t)(*features)

sig.weight = (len(weights) * WEIGHT_t)(*weights)

return sig

# We want to pass into C a custom distance function from Python

def py_dist_func(f1,f2):

# print "f1, f2: %d, %d" % ( f1[0], f2[0] )

d= distance(f1[0],f2[0])

return d

# set this up as a holder for distance function between any two n-D points

def distance(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= ", e

print "Number of FlowS", flow_size.value

print "Flow"

print "from to amount"

totalFlow=0.0

for i in range(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 metric

alpha=1.0

mass1=sum(weight1)

mass2=sum(weight2)

fList=[feature1,feature2]

max_distance= 0.0

for p0, p1 in list(itertools.product(*fList)):

# print p0,p1, distance(p0,p1), max_distance

max_distance = max(max_distance, distance(p0, p1))

print "\nMax distance= %f" % max_distance

print "Total Source = %f" % mass1

print "Total Demand = %f" % mass2

print "Total Flow= %f\n " % totalFlow

print "Alpha = %f\n" %alpha

# emdHat = e*totalFlow+math.sqrt((mass1-mass2)*(mass1-mass2))*alpha*max_distance

emdHat = e*totalFlow+math.fabs((mass1-mass2))*alpha*max_distance

print "Corrected Earth Movers Distance \n"

print "emdHat = %f\n" % emdHat;

總結(jié)

以上是生活随笔為你收集整理的python mount回调函数_为python回调函数设置argtype的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。