我很抱歉发布了一个很长的代码。我提取了生成和使用列表lower_lip_under_upper_list_plist
的代码部分。链接使用此列表的函数时出错。generate_t_u()
函数在time()
函数中被调用,但是time()
函数不会识别这个列表,因为它没有被传递到那里。但是如果我在这个函数中传递它,那么我也必须对main进行更改。同样,我也会得到同样的错误。在
def time(transcriptionFile) :
""" This function changes the time values in secs from the transcription file and keeps a list of start time for each phoneme. """
with open("transcriptions.txt", "r") as tFile :
timeList = []
t_u = 0.0
for line in tFile :
li = line.split()
if li :
start_time = (int(li[0]) / 10000000.)
timeList.append(start_time)
#print timeList
generate_t_u(timeList, lower_lip_under_upper_list_plist)
def generate_t_u(timeList, lower_lip_under_upper_list_plist)
""" It generates the regular time intervals t(u) values for sampling. """
i = 0
while i < len(timeList) - 1 :
# if the number is in the range
# do the calculations and move to the next number
if t_u > timeList[i] and t_u < timeList[i + 1] :
#print "\n The t_u value:", t_u, 'is between',
#print "start:", timeList[i], " and end: ", timeList[i+1]
poly = poly_coeff(timeList[i], timeList[i + 1], t_u)
Newton(poly, lower_lip_under_upper_list_plist[i], lower_lip_under_upper_list_plist[i + 1])
t_u = t_u + 0.04 # regular time interval
# if the number is at the lower boundary of the range, no need of calculation as u = 0
elif t_u == timeList[i] :
#print "\n The t_u value:", t_u, 'is on the boundary of',
#print "start:", timeList[i], " and end: ", timeList[i+1]
#print "u : 0"
lower_lip_under_upper_list_bezier(0, lower_lip_under_upper_list_plist[i], lower_lip_under_upper_list_plist[i + 1])
t_u = t_u + 0.04 # regular time interval
# if the number is at the upper boundary of the range, no need of calculation as u = 1
elif t_u == timeList[i + 1] :
#print "\n The t_u value:", t_u, 'is on the boundary of',
#print "start:", timeList[i], " and end: ", timeList[i+1]
#print " u : 1"
lower_lip_under_upper_list_bezier(1, lower_lip_under_upper_list_plist[i], lower_lip_under_upper_list_plist[i + 1])
t_u = t_u + 0.04 # regular time interval
# if the number isn't in the range, move to the next range
else :
i += 1
def Newton(poly, p0, p3) :
""" Newton's method for finding the root of a polynomial. Here the root is the 'u' value"""
poly_diff = poly_differentiate(poly)
counter = 0
epsilon = 0.000000000001
x = 0.5 # initial guess value
while True:
x_n = x - (float(poly_substitute(poly, x)) / poly_substitute(poly_diff, x))
counter += 1
if abs(x_n - x) < epsilon :
break
x = x_n
#print "\tIteration " , counter , " : ", x_n
print "u:", (x_n)
lower_lip_under_upper_list_bezier(x_n, p0, p3)
def param_lists_separate(pList) :
""" Separating the parameter values of each feature into individual lists """
v = [[inner[1][i] for outer in pList for inner in outer]
for i in range(len(pList[0][0][1]))]
lower_lip_under_upper_list_plist = v[0]
lips_part_plist = v[1]
lips_spread_plist = v[2]
jaw_open_plist = v[3]
lips_round_plist = v[4]
return lower_lip_under_upper_list_plist
def lower_lip_under_upper_list_bezier(x_n, p0, p3) :
""" Calculating sampling points using rational bezier curve equation"""
u = x_n
p1 = p0
p2 = p3
lower_lip_under_upper_list_p_u = math.pow(1 - u, 3) * p0 + 3 * u * math.pow(1 - u, 2) * p1 \
+ 3 * (1 - u) * math.pow(u, 2) * p2 + math.pow(u, 3) * p3
lower_lip_under_upper_list_p_u = lower_lip_under_upper_list_p_u * w
d = math.pow(1 - u, 3) * w + 3 * u * w * math.pow(1 - u, 2) + 3 * (1 - u) * w * math.pow(u, 2) + math.pow(u, 3) * w
lower_lip_under_upper_list_p_u = lower_lip_under_upper_list_p_u / d
print "\n p(u) values for the feature lower lip under upper list \n"
print "p(u): ", lower_lip_under_upper_list_p_u
return lower_lip_under_upper_list_p_u
if __name__ == "__main__" :
time("transcriptions.txt")
错误是:
NameError: global name 'lower_lip_under_upper_list_plist' is not defined.
此错误位于time()
函数的lower_lip_under_upper_list_bezier(0, lower_lip_under_upper_list_plist[i], lower_lip_under_upper_list_plist[i + 1])
行。在
考虑
time
函数中的这一行:lower_lip_under_upper_list_plist
未在time函数中定义,因此Python在全局范围内查找它。它也没有在那里定义,因此Python引发了一个NameError
,表示变量{顺便说一句,回溯错误消息包含一个行号,指示是什么行导致了
NameError
。这将有助于您(和我们)集中精力解决问题,包括行号。在lower_lip_under_upper_list_plist
应该是generate_t_u
的返回值吗?尝试将generate_t_u
更改为:我不确定
param_lists_separate
在哪里被调用,但看起来其中的绑定也应该是全局的。在generate_t_u
中也存在类似的问题,因为t_u
没有绑定在那里,如果它引用time
中的t_u
,那么它可能应该作为参数传入。如果有许多相关的绑定和方法作用于数据,那么您可能应该考虑创建一个将数据和操作绑定在一起的类。在考虑做一些类似以下的事情:
^{pr2}$面向对象编程最基本的原则之一是将算法与它们处理和/或需要的数据捆绑在一起。你可以在这里应用这个想法。您的全局绑定将成为对象的属性,因此它们始终可用作实例方法中
self
的属性。您可能已经注意到,不再需要传递来自lower_lip_under_upper_list_plist
之类的值。您只需将索引传递到列表中,因为列表本身是对象的属性。在将您的算法重构为
class
应该可以消除所有地方对全局变量的需要。它还将最小化参数的数量,并让您集中精力以一种干净和模块化的方式实现算法。在相关问题 更多 >
编程相关推荐