"""
Pupil Maps
----------
Functions for the creation of pupil maps and masks.
"""
import numpy
[docs]def circle(radius, size, circle_centre=(0, 0), origin="middle"):
"""
Create a 2-D array: elements equal 1 within a circle and 0 outside.
The default centre of the coordinate system is in the middle of the array:
circle_centre=(0,0), origin="middle"
This means:
if size is odd : the centre is in the middle of the central pixel
if size is even : centre is in the corner where the central 4 pixels meet
origin = "corner" is used e.g. by psfAnalysis:radialAvg()
Examples: ::
circle(1,5) circle(0,5) circle(2,5) circle(0,4) circle(0.8,4) circle(2,4)
00000 00000 00100 0000 0000 0110
00100 00000 01110 0000 0110 1111
01110 00100 11111 0000 0110 1111
00100 00000 01110 0000 0000 0110
00000 00000 00100
circle(1,5,(0.5,0.5)) circle(1,4,(0.5,0.5))
.-->+
| 00000 0000
| 00000 0010
+V 00110 0111
00110 0010
00000
Parameters:
radius (float) : radius of the circle
size (int) : size of the 2-D array in which the circle lies
circle_centre (tuple): coords of the centre of the circle
origin (str) : where is the origin of the coordinate system
in which circle_centre is given;
allowed values: {"middle", "corner"}
Returns:
ndarray (float64) : the circle array
"""
# (2) Generate the output array:
C = numpy.zeros((size, size))
# (3.a) Generate the 1-D coordinates of the pixel's centres:
# coords = numpy.linspace(-size/2.,size/2.,size) # Wrong!!:
# size = 5: coords = array([-2.5 , -1.25, 0. , 1.25, 2.5 ])
# size = 6: coords = array([-3. , -1.8, -0.6, 0.6, 1.8, 3. ])
# (2015 Mar 30; delete this comment after Dec 2015 at the latest.)
# Before 2015 Apr 7 (delete 2015 Dec at the latest):
# coords = numpy.arange(-size/2.+0.5, size/2.-0.4, 1.0)
# size = 5: coords = array([-2., -1., 0., 1., 2.])
# size = 6: coords = array([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])
coords = numpy.arange(0.5, size, 1.0)
# size = 5: coords = [ 0.5 1.5 2.5 3.5 4.5]
# size = 6: coords = [ 0.5 1.5 2.5 3.5 4.5 5.5]
# (3.b) Just an internal sanity check:
if len(coords) != size:
raise exceptions.Bug("len(coords) = {0}, ".format(len(coords)) +
"size = {0}. They must be equal.".format(size) +
"\n Debug the line \"coords = ...\".")
# (3.c) Generate the 2-D coordinates of the pixel's centres:
x, y = numpy.meshgrid(coords, coords)
# (3.d) Move the circle origin to the middle of the grid, if required:
if origin == "middle":
x -= size / 2.
y -= size / 2.
# (3.e) Move the circle centre to the alternative position, if provided:
x -= circle_centre[0]
y -= circle_centre[1]
# (4) Calculate the output:
# if distance(pixel's centre, circle_centre) <= radius:
# output = 1
# else:
# output = 0
mask = x * x + y * y <= radius * radius
C[mask] = 1
# (5) Return:
return C