[Numpy-discussion] Overlap arrays with "transparency"
Michael S. Gilbert
Mon May 18 10:38:25 CDT 2009
On Mon, 18 May 2009 05:37:09 -0700 (PDT), Cristi Constantin wrote:
> Good day.
> I am working on this algorithm for a few weeks now, so i tried almost everything...
> I want to overlap / overwrite 2 matrices, but completely ignore some values (in this case ignore 0)
> Let me explain:
> a = [
> [1, 2, 3, 4, 5],
> [5,5,5] ]
> b = [
> [2,2,2,2] ]
> Then, we have:
> a over b = [
> [5,5,5,2] ]
> b over a = [
> 5,5,5] ]
> That means, completely overwrite one list of arrays over the other, not matter what values one has, not matter the size, just ignore 0 values on overwriting.
> I checked the documentation, i just need some tips.
> TempA = []
> One For Cicle in here to get the Element data...
> Data = vElem.data # This is a list of numpy ndarrays.
> for nr_row in range( len(Data) ): # For each numpy ndarray (row) in Data.
> NData = Data[nr_row] # New data, to be written over old data.
> OData = TempA[nr_row:nr_row+1] or [] # This is old data. Can be numpy ndarray, or empty list.
> OData = OData
> # NData must completely eliminate transparent pixels... here comes the algorithm... No algorithm yet.
> if len(NData) >= len(OData):
> # If new data is longer than old data, old data will be completely overwritten.
> TempA[nr_row:nr_row+1] = [NData]
> else: # Old data is longer than new data ; old data cannot be null.
> TempB = np.copy(OData)
> TempB.put( range(len(NData)), NData )
> #TempB[0:len(NData)-1] = NData # This returns "ValueError: shape mismatch: objects cannot be broadcast to a single shape"
> TempA[nr_row:nr_row+1] = [TempB]
> del TempB
> The result is stored inside TempA as list of numpy arrays.
> I would use 2D arrays, but they are slower than Python Lists containing Numpy arrays. I need to do this overwrite in a very big loop and every delay is very important.
> I tried to create a masked array where all "zero" values are ignored on overlap, but it doesn't work. Masked or not, the "transparent" values are still overwritten.
> Please, any suggestion is useful.
your code will certainly be slow if you do no preallocate memory for
your arrays. and i would suggest using numpy's array class instead of
a = numpy.array( a )
b = numpy.array( b )
c = numpy.zeros( ( max( ( len(a[:,0]) , len(b[:,0]) ) ) ,
max( ( len(a[0,:]) , len(b[0,:]) ) ) , int )
More information about the Numpy-discussion