python - ValueError: could not broadcast input array from shape (80,269,269) into shape (80,269,234) -
datai
of shape (80,336,336)
z= np.zeros([80,800,800], dtype= datai.dtype) v=np.zeros_like(z) centres = [(100,200),(400,100), (600,300), (500,400), (600,500)] zooms = [[1,1,1], [1,0.6,0.6], [1,0.7,0.7], [1,0.8,0.8], [1,0.9,0.9]] cent,zoom in zip(centres,zooms): dataz = scipy.ndimage.zoom(datai, zoom, order=3) dimz = dataz.shape off = [max(c-s//2,0) c,s in zip(cent,dimz)] z[0:dataz.shape[0],off[0]:off[0]+dimz[1],off[1]:off[1]+dimz[2]]=dataz
i need place scaled data in big array z
. giving center positions, need place data (if overlaps, take average of values)
but following error:
traceback (most recent call last): file "<stdin>", line 1, in <module> z[0:dataz.shape[0],off[0]:off[0]+dimz[1],off[1]:off[1]+dimz[2]]=dataz valueerror: not broadcast input array shape (80,269,269) shape (80,269,234)
how solve error?
the problem slicing can go beyond array extend, in case elements in extend of array returned:
>>> arr = np.ones((3, 3)) >>> arr[2:10, 2:10] # doesn't have shape (8, 8)! array([[ 1.]])
so calculation goes beyond extend , z[0:dataz.shape[0],off[0]:off[0]+dimz[1],off[1]:off[1]+dimz[2]]
isn't shape expect be!
i added debug statements:
from scipy import ndimage # better import submodules scipy # did cent, zoom in zip(centres, zooms): dataz = ndimage.zoom(datai, zoom, order=3) dimz = dataz.shape off = [max(c - s // 2, 0) c, s in zip(cent, dimz)] print(off) print(np.s_[0:dataz.shape[0],off[0]:off[0]+dimz[1],off[1]:off[1]+dimz[2]]) z[0 : dataz.shape[0], off[0] : off[0] + dimz[1], off[1] : off[1] + dimz[2]] = dataz
this printed:
[60, 32] (slice(0, 80, none), slice(60, 396, none), slice(32, 368, none)) [360, 0] (slice(0, 80, none), slice(360, 562, none), slice(0, 202, none)) [560, 183] (slice(0, 80, none), slice(560, 795, none), slice(183, 418, none)) [460, 266] (slice(0, 80, none), slice(460, 729, none), slice(266, 535, none)) [560, 349] (slice(0, 80, none), slice(560, 862, none), slice(349, 651, none))
and mentioned earlier applies here: slice(560, 862, none)
goes beyond extend , lack 62
items in second dimension:
valueerror: not broadcast input array shape (80,302,302) shape (80,240,302)
easy fix: increase size of z
!
a related note: know //
has higher precedence -
?
max(c-s//2,0) # equivalent max(c-(s//2), 0)
Comments
Post a Comment