function - python functools.partial with fixed array valued argument -
i'm trying vectorize following function on argument tiling
:
def find_tile(x,tiling): """ calculates index of closest element of 'tiling' 'x'. tiling: array of grid positions x: variable of same type elements of tiling """ return np.argmin(np.linalg.norm(tiling - x, axis=1))
for instance, non-vectorized version of function can accept following arguments
tiling = np.array( [[i,j] in xrange(3) j in xrange(3)] ) x = np.array([1.2, 2.7])
i'm interested in finding fastest possible vectorisation, such x
remains single vector , can pass list of arguments tiling
so tried defining multiple tilings using generator:
tilings = (tiling + np.random.uniform(0,1,2) j in xrange(3))
and using map
, functools.partial
:
map(functools.partial(find_tile, x=x), tilings)
apparently, there's problem x
being array or something, since i'm getting error:
traceback (most recent call last): file "main.py", line 43, in <module> inds = map(functools.partial(find_tile, x=x), ts) typeerror: find_tile() got multiple values keyword argument 'x'
can explain me how around it?
also, there alternative , faster way (possibly re-writing function find_tile
?)
you passing in x
keyword argument. map()
passes in each element tilings
in positional argument. however, since first positional argument x
, clashes keyword argument. using name keyword argument not prevent same name being filled positional argument.
don't use keyword argument x
; pass in positional argument partial()
:
map(functools.partial(find_tile, x), tilings)
now each element tilings
passed in second positional argument , call works.
Comments
Post a Comment