Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-07 Thread Sturla Molden
> for i in range(m): > for j in range(n): > found[i//4,j//4] = cond(x[i,j]) > Blah, that should be found[i//4,j//4] |= cond(x[i,j]) Sturla ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org h

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-07 Thread Sturla Molden
On 07.02.2012 15:27, eat wrote: > This is elegant and very fast as well! Just be aware that it depends on C ordered input. So: m,n = data.shape cond = lamda x : (x >= t1) & (x <= t2) x = cond(np.ascontiguousarray(data)).reshape((m//4, 4, n//4, 4)) found = np.any(np.any(x, axis=1)

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-07 Thread eat
Hi This is elegant and very fast as well! On Tue, Feb 7, 2012 at 2:57 PM, Sturla Molden wrote: > On 06.02.2012 22:27, Sturla Molden wrote: > > > > > >> > >> # Make a 4D view of this data, such that b[i,j] > >> # is a 2D block with shape (4,4) (e.g. b[0,0] is > >> # the same as a[:4, :4]). > >> b

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-07 Thread Sturla Molden
On 06.02.2012 22:27, Sturla Molden wrote: > > >> >> # Make a 4D view of this data, such that b[i,j] >> # is a 2D block with shape (4,4) (e.g. b[0,0] is >> # the same as a[:4, :4]). >> b = as_strided(a, shape=(a.shape[0]/4, a.shape[1]/4, 4, 4), >> strides=(4*a.strides[0], 4*a.strides

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread eat
Hi, Sorry for my latest post, hands way too quick ;( On Mon, Feb 6, 2012 at 9:16 PM, Moroney, Catherine M (388D) < catherine.m.moro...@jpl.nasa.gov> wrote: > Hello, > > I have to write a code to downsample an array in a specific way, and I am > hoping that > somebody can tell me how to do this w

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread eat
Hi, On Mon, Feb 6, 2012 at 9:16 PM, Moroney, Catherine M (388D) < catherine.m.moro...@jpl.nasa.gov> wrote: > Hello, > > I have to write a code to downsample an array in a specific way, and I am > hoping that > somebody can tell me how to do this without the nested do-loops. Here is > the problem

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread Sturla Molden
> > # Make a 4D view of this data, such that b[i,j] > # is a 2D block with shape (4,4) (e.g. b[0,0] is > # the same as a[:4, :4]). > b = as_strided(a, shape=(a.shape[0]/4, a.shape[1]/4, 4, 4), >strides=(4*a.strides[0], 4*a.strides[1], a.strides[0], > a.strides[1])) > Yes :-) B

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread Sturla Molden
The last t1 on each lineis of course t2. Sorry for the typo. Hard to code on an ipad ;-) Sturla Sendt fra min iPad Den 6. feb. 2012 kl. 22:12 skrev Sturla Molden : > > Something like this: > > m,n = data.shape > x = data.reshape((m,n//4,4)) > z = (x[0::4,...] >= t1) & (x[0::4,...] <= t1) >

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread Sturla Molden
Something like this: m,n = data.shape x = data.reshape((m,n//4,4)) z = (x[0::4,...] >= t1) & (x[0::4,...] <= t1) z |= (x[1::4,...] >= t1) & (x[1::4,...] <= t1) z |= (x[2::4,...] >= t1) & (x[2::4,...] <= t1) z |= (x[3::4,...] >= t1) & (x[3::4,...] <= t1) found = np.any(z, axis=2) Sturla Sendt f

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread Warren Weckesser
On Mon, Feb 6, 2012 at 2:57 PM, Sturla Molden wrote: > Short answer: Create 16 view arrays, each with a stride of 4 in both > dimensions. Test them against the conditions and combine the tests with an > |= operator. Thus you replace the nested loop with one that has only 16 > iterations. Or resha

Re: [Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread Sturla Molden
Short answer: Create 16 view arrays, each with a stride of 4 in both dimensions. Test them against the conditions and combine the tests with an |= operator. Thus you replace the nested loop with one that has only 16 iterations. Or reshape to 3 dimensions, the last with length 4, and you can do

[Numpy-discussion] avoiding loops when downsampling arrays

2012-02-06 Thread Moroney, Catherine M (388D)
Hello, I have to write a code to downsample an array in a specific way, and I am hoping that somebody can tell me how to do this without the nested do-loops. Here is the problem statement: Segment a (MXN) array into 4x4 squares and set a flag if any of the pixels in that 4x4 square meet a cer