I have an array with about 64,000 features (labeled arbitrarily 0-64,000), and I'm trying to write a function that will compare each feature to each of its neighbors. To do this iteratively takes a prohibitive amount of time. I'm trying to expedite the process by creating a nested function that will be applied to each feature using pandas.DataFrame.apply() using the following code:
def textureRemover(pix, labeledPix, ratio):
counter = 0
numElements = numpy.amax(labeledPix)
maxSize = numpy.count_nonzero(labeledPix)
allElements = pandas.DataFrame(numpy.array(list(range(numElements))))
def func(regionID):
...
allElements.apply(func, axis = 1)
return pix
Where func() needs access to the parameters of, and variables defined within, textureRemover() and each call of func() will alter the very large arrays pix and labeledPix
I've tried using the line: global pix, labeledPix, counter, ratio, maxSize. However, If use this line in textureRemover() I receive an error that variables cannot be parameters and global. And if I use the line within func() I receive an error that these variables are undefined.
How can I make func() able to access and modify these variables?
applywill improve performance over iteration.The second is how to use apply with a function that takes multiple parameters. These should be split into two different questions, the latter of which has an answer here, hope this helpspix[...] = ...should just work insidefunc.