In the application I am creating I have this list:
scores = [[232178, 0], [167883, 1], [174491, 2], [890, 3], [190860, 4], [137581, 5], [2126, 6], [200983, 7]]
Then I want to sort them based on their first element of each element of that list and access that index.
sorted(scores, key=lambda x: x[0])[0][1]
However when I run it, it gives an error:
NameError: global name '<lambda>' is not defined
I am using a conda environment with python 3.7.4
Any help would be appreciated!
Code used: Given a video of messed up frames, use each frame to find the next closest frame to build the original video
import cv2
import numpy as np
from numba import jit, cuda
import copy
@jit(target_backend='cuda')
def foo():
images = []
for i in range(1,10):#3941):
print(f'i:{i}')
filename = "extracted/" + str(i).zfill(4) + '.png'
image = cv2.imread(filename)
height, width, layers = image.shape
images.append(image)
print('loaded images')
return images, width, height
@jit(target_backend='cuda')
def bar(images_list, iwidth, iheight, where):
images_new = [images_list.pop(3)]#3157)]
c = 0
while len(images) > 1:
scores = []
old_image = images_new[-1]
print(f'where:{where} c:{c}')
for i in range(len(images_list)):
scores.append([np.sum((images_list[i][where] - old_image[where])**2), i])
new = sorted(scores, key=lambda x: x[0])
images_new.append(images_list.pop(new[0][1]))
c += 1
print(f'where:{where} sorted images')
video = cv2.VideoWriter(f'videoat{where}.avi', 0, 10, (iwidth, iheight))
for image in images_new:
video.write(image)
print(f'where:{where} made video')
cv2.destroyAllWindows()
video.release()
print(f'where:{where} done')
images, width, height = foo()
bar(copy.deepcopy(images),width,height,50)
The key
argument to sorted
is not supported by Numba.
Specifically, it looks like it might be partially supported (maybe it works if you pass a function defined by def
), but your error message suggests that Numba tries to use the argument, but assumes its __name__
attribute is also the name of a valid global variable. The __name__
attribute of a function defined by a lambda expression, though, is <lambda>
, not any global variable.
(There are probably other reasons it is not supported. Other places in the documentation mention lack of support for **kwargs
, for example. Even though lambda expressions are commonly used for the key
argument, it seems odd to document that it is completely unsupported if the only restriction would be to use functions defined by a def
statement.)