<div dir="ltr"><div><div>A small improvement:<br><br># Dimensions<br>N = 100<br>M = 200<br><br># Coordinates of the centre<br>x0 = 40<br>y0 = 70<br><br>x, y = np.meshgrid(np.arange(N) - x0, np.arange(M) - y0, sparse=True)<br>

<br># Center at 40, 70, radius 20<br>out_circle = (x * x + y * y < 20**2)<br><br>out_ring = out_circle - (x * x + y * y < 10**2)<br><br>plt.imshow(out_circle)<br>plt.figure()<br>plt.imshow(out_ring)<br>plt.show()<br>

<br></div>Using sparse you can avoid the repetition of the arrays, getting the same functionality. Also, if the image is big, you can delegate the computations of out_circle and out_ring to numexpr for speed.<br><br></div>

/David.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 11 February 2014 22:16, Daπid <span dir="ltr"><<a href="mailto:davidmenhur@gmail.com" target="_blank">davidmenhur@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Here it is an example:<br><br>import numpy as np<br>import pylab as plt<br><br>N = 100<br>

M = 200<br>x, y = np.meshgrid(np.arange(N), np.arange(M))<br><br></div><div># Center at 40, 70, radius 20<br>
</div><div>x -= 40<br>y -= 70<br>out_circle = (x * x + y * y < 20**2)<br><br></div><div>out_ring = out_circle -  (x * x + y * y < 10**2)</div><div><br></div>plt.imshow(out_circle)<br>plt.figure()<br>plt.imshow(out_ring)<br>


plt.show()<br><br></div>Note that I have avoided taking the costly square root of each element by just taking the square of the radius. It can also be generalised to ellipses or rectangles, if you need it.<br><br></div><div>


Also, don't use 0 as a False value, and don't force it to be a 0. Instead, use "if not ring:"<br></div><div><br><br></div>/David<br><div><br><br><div><div><br></div></div></div></div><div class="HOEnZb">

<div class="h5"><div class="gmail_extra">
<br><br><div class="gmail_quote">On 11 February 2014 21:56, Wolfgang Draxinger <span dir="ltr"><<a href="mailto:Wolfgang.Draxinger@physik.uni-muenchen.de" target="_blank">Wolfgang.Draxinger@physik.uni-muenchen.de</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I implemented the following helper function to create masking arrays:<br>
<br>
def gen_mask(self, ring, sector):<br>
        in_segment = None<br>
        if ring == 0:<br>
                radius = self.scales()[0]<br>
                def in_center_circle(xy):<br>
                        dx = xy[0] - self.xy[0]<br>
                        dy = xy[1] - self.xy[1]<br>
                        r = math.sqrt( dx**2 + dy**2 )<br>
                        return r < radius<br>
                in_segment = in_center_circle<br>
        else:<br>
                angle = ( self.a_sector(sector, ring),<br>
                          self.a_sector( (sector+1) % self.n_sectors(), ring) )<br>
                radii = self.scales()[ring:ring+1]<br>
                def in_segment_ring(xy):<br>
                        dx = xy[0] - self.xy[0]<br>
                        dy = xy[1] - self.xy[1]<br>
                        r = math.sqrt( dx**2 + dy**2 )<br>
                        a = math.atan2( dy, dx )<br>
                        return r >= radii[0] and r < radii[1] \<br>
                           and a >= angle[0] and a < angle[1]<br>
                in_segment = in_segment_ring<br>
<br>
        width,height = self.arr.shape<br>
<br>
        mask = numpy.zeros(shape=(width, height), dtype=numpy.bool)<br>
        for y in range(height):<br>
                for x in range(width):<br>
                        mask[x][y] = in_segment((x,y))<br>
        return mask<br>
<br>
self.scales() generates a list of radius scaling factors.<br>
self.a_sector() gives the dividing angle between sectors "sector" and<br>
"sector+1" on the given ring.<br>
<br>
The function works, it generates the masks as I need it. The problem is<br>
- of course - that it's quite slow due to the inner loops the perform<br>
the geometrical test if the given element of the array self.arr is<br>
withing the bounds of the ring-sector for which the mask is generated.<br>
<br>
I wonder if you guys have some ideas, on how I could accelerate this.<br>
This can be perfectly well constructed by boolean combination of<br>
elementary geometrical objects. For example a ring would be<br>
<br>
ring(p, r0, r1): disk(p, r1) xor disk(p, r0) # where r0 < r1<br>
<br>
The sector would then be<br>
<br>
ring_sector(p, r, s): ring(p, r, r + ...) and sector(p, s, s+1)<br>
<br>
I'd like to avoid doing this using some C helper routine. I'm looking<br>
for something that is the most efficient when it comes to<br>
"speedup"/"time invested to develop this".<br>
<br>
<br>
Cheers,<br>
<br>
Wolfgang<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>