Skip to content

Geometry

This module is used as for calculating geometric shapes on coordinate arrays, used to generate apertures. Each shape has two functions. One that generates a hard-edged oversampled one for more accurate aperture shapes, but is not differentiable/jittable. These are the circle, square, etc functions. The other type are differentiable and jittable in order to gain dynamic aperture that can be fit to data. These instead calculate the distance of each pixel from an edge and 'clip' the values between 0-1 for distances above a specified threshold. These functions are denoted with a soft_ prefix, ie soft_circle, soft_square, etc.

There are also some other helper functions for scaling and downsampling.

TODO: Little tutorial on generating apertures.


circle

Calculates a soft-edged circle via downsampling. This function is not differentiable.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the circle on.

required
radius float

The radius of the circle.

required
invert bool = False

Whether to invert the circle.

False

Returns:

Name Type Description
circle Array

The circle.

Source code in src/dLux/utils/geometry.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def circle(coords: Array, radius: float, invert: bool = False) -> Array:
    """
    Calculates a soft-edged circle via downsampling. This function is not
    differentiable.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the circle on.
    radius : float
        The radius of the circle.
    invert : bool = False
        Whether to invert the circle.

    Returns
    -------
    circle : Array
        The circle.
    """
    if invert:
        return (circ_distance(coords, radius) > 0).astype(float)
    return (circ_distance(coords, radius) < 0).astype(float)
square

Calculates a soft-edged square via downsampling. This function is not differentiable.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the square on.

required
width float

The width of the square.

required
invert bool = False

Whether to invert the square.

False

Returns:

Name Type Description
square Array

The square.

Source code in src/dLux/utils/geometry.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def square(coords: Array, width: float, invert: bool = False) -> Array:
    """
    Calculates a soft-edged square via downsampling. This function is not
    differentiable.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the square on.
    width : float
        The width of the square.
    invert : bool = False
        Whether to invert the square.

    Returns
    -------
    square : Array
        The square.
    """
    if invert:
        return (square_distance(coords, width) > 0).astype(float)
    return (square_distance(coords, width) < 0).astype(float)
rectangle

Calculates a soft-edged rectangle via downsampling. This function is not differentiable.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the rectangle on.

required
width float

The width of the rectangle.

required
height float

The height of the rectangle.

required

Returns:

Name Type Description
rectangle Array

The rectangle.

Source code in src/dLux/utils/geometry.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def rectangle(
    coords: Array, width: float, height: float, invert: bool = False
) -> Array:
    """
    Calculates a soft-edged rectangle via downsampling. This function is not
    differentiable.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the rectangle on.
    width : float
        The width of the rectangle.
    height : float
        The height of the rectangle.

    Returns
    -------
    rectangle : Array
        The rectangle.
    """
    if invert:
        return (rectangle_distance(coords, width, height) > 0).astype(float)
    return (rectangle_distance(coords, width, height) < 0).astype(float)
reg_polygon

Calculates a soft-edged regular polygon via downsampling. This function is not differentiable.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the polygon on.

required
rmax float

The radius of the polygon.

required
nsides int

The number of sides of the polygon.

required
invert bool = False

Whether to invert the polygon.

False

Returns:

Name Type Description
polygon Array

The polygon.

Source code in src/dLux/utils/geometry.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def reg_polygon(
    coords: Array, rmax: float, nsides: int, invert: bool = False
) -> Array:
    """
    Calculates a soft-edged regular polygon via downsampling. This function is not
    differentiable.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the polygon on.
    rmax : float
        The radius of the polygon.
    nsides : int
        The number of sides of the polygon.
    invert : bool = False
        Whether to invert the polygon.

    Returns
    -------
    polygon : Array
        The polygon.
    """
    if invert:
        return (reg_polygon_distance(coords, nsides, rmax) > 0).astype(float)
    return (reg_polygon_distance(coords, nsides, rmax) < 0).astype(float)
spider

Calculates a soft-edged spider via downsampling. This function is not differentiable.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the spider on.

required
width float

The width of the spider.

required
angles Array

The angles of the spider.

required

Returns:

Name Type Description
spider Array

The spider.

Source code in src/dLux/utils/geometry.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def spider(coords: Array, width: float, angles: Array) -> Array:
    """
    Calculates a soft-edged spider via downsampling. This function is not
    differentiable.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the spider on.
    width : float
        The width of the spider.
    angles : Array
        The angles of the spider.

    Returns
    -------
    spider : Array
        The spider.
    """
    angles = np.array(angles) if not isinstance(angles, np.ndarray) else angles
    calc_fn = vmap(lambda angle: spider_distance(coords, width, angle) < 0)
    return (
        ~lax.reduce(calc_fn(angles), np.array(False), lax.bitwise_or, (0,))
    ).astype(float)
combine

Combines multiple arrays by multiplying them together, and downsampling the output.

Parameters:

Name Type Description Default
arrays Array

The arrays to be combined. Should have shape (n_arrays, npix, npix).

required
oversample int = 1

The amount to downsample the output by.

1
sum bool = False

Whether to sum the arrays instead of multiplying them.

False
Source code in src/dLux/utils/geometry.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def combine(arrays: Array, oversample: int = 1, sum: bool = False) -> Array:
    """
    Combines multiple arrays by multiplying them together, and downsampling the output.

    Parameters
    ----------
    arrays : Array
        The arrays to be combined. Should have shape (n_arrays, npix, npix).
    oversample : int = 1
        The amount to downsample the output by.
    sum : bool = False
        Whether to sum the arrays instead of multiplying them.
    """
    method = np.sum if sum else np.prod
    array = np.array(arrays)
    if oversample == 1:
        return method(array, 0)
    return dlu.downsample(method(array, 0), oversample)
soft_circle

Calculates a soft-edged circle differentiably. The 'clip_dist' parameter defines the distance from the edge to 'soften' up to. A large clip_dist will result in a circle with a very soft edge, while a small clip_dist will result in a circle with a very hard edge.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the circle on.

required
radius float

The radius of the circle.

required
clip_dist float = 0.1

The distance from the edge to 'soften' up to. circle.

0.1
invert bool = False

Whether to invert the circle.

False

Returns:

Name Type Description
circle Array

The softened circle.

Source code in src/dLux/utils/geometry.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def soft_circle(
    coords: Array, radius: float, clip_dist: float = 0.1, invert: bool = False
) -> Array:
    """
    Calculates a soft-edged circle differentiably. The 'clip_dist' parameter defines
    the distance from the edge to 'soften' up to. A large clip_dist will result in a
    circle with a very soft edge, while a small clip_dist will result in a circle with
    a very hard edge.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the circle on.
    radius : float
        The radius of the circle.
    clip_dist : float = 0.1
        The distance from the edge to 'soften' up to.
        circle.
    invert : bool = False
        Whether to invert the circle.

    Returns
    -------
    circle : Array
        The softened circle.
    """
    distances = -circ_distance(coords, radius)
    return soften(distances, clip_dist, invert)
soft_square

Calculates a soft-edged square differentiably. The 'clip_dist' parameter defines the distance from the edge to 'soften' up to. A large clip_dist will result in a square with a very soft edge, while a small clip_dist will result in a square with a very hard edge.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the square on.

required
width float

The width of the square.

required
clip_dist float = 0.1

The distance from the edge to 'soften' up to. square.

0.1
invert bool = False

Whether to invert the square.

False

Returns:

Name Type Description
square Array

The softened square.

Source code in src/dLux/utils/geometry.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def soft_square(
    coords: Array, width: float, clip_dist: float = 0.1, invert: bool = False
) -> Array:
    """
    Calculates a soft-edged square differentiably. The 'clip_dist' parameter defines
    the distance from the edge to 'soften' up to. A large clip_dist will result in a
    square with a very soft edge, while a small clip_dist will result in a square with
    a very hard edge.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the square on.
    width : float
        The width of the square.
    clip_dist : float = 0.1
        The distance from the edge to 'soften' up to.
        square.
    invert : bool = False
        Whether to invert the square.

    Returns
    -------
    square : Array
        The softened square.
    """
    distances = -square_distance(coords, width)
    return soften(distances, clip_dist, invert)
soft_rectangle

Calculates a soft-edged rectangle differentiably. The 'clip_dist' parameter defines the distance from the edge to 'soften' up to. A large clip_dist will result in a rectangle with a very soft edge, while a small clip_dist will result in a rectangle with a very hard edge.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the rectangle on.

required
width float

The width of the rectangle.

required
height float

The height of the rectangle.

required
clip_dist float = 0.1

The distance from the edge to 'soften' up to.

0.1

Returns:

Name Type Description
rectangle Array

The softened rectangle.

Source code in src/dLux/utils/geometry.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def soft_rectangle(
    coords: Array,
    width: float,
    height: float,
    clip_dist: float = 0.1,
    invert: bool = False,
) -> Array:
    """
    Calculates a soft-edged rectangle differentiably. The 'clip_dist' parameter defines
    the distance from the edge to 'soften' up to. A large clip_dist will result in a
    rectangle with a very soft edge, while a small clip_dist will result in a rectangle
    with a very hard edge.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the rectangle on.
    width : float
        The width of the rectangle.
    height : float
        The height of the rectangle.
    clip_dist : float = 0.1
        The distance from the edge to 'soften' up to.

    Returns
    -------
    rectangle : Array
        The softened rectangle.
    """
    distances = -rectangle_distance(coords, width, height)
    return soften(distances, clip_dist, invert)
soft_reg_polygon

Calculates a soft-edged regular polygon differentiably. The 'clip_dist' parameter defines the distance from the edge to 'soften' up to. A large clip_dist will result in a polygon with a very soft edge, while a small clip_dist will result in a polygon with a very hard edge.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the polygon on.

required
radius float

The radius of the polygon.

required
nsides int

The number of sides of the polygon.

required
clip_dist float = 0.1

The distance from the edge to 'soften' up to.

0.1
invert bool = False

Whether to invert the polygon.

False

Returns:

Name Type Description
polygon Array

The softened polygon.

Source code in src/dLux/utils/geometry.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
def soft_reg_polygon(
    coords: Array,
    radius: float,
    nsides: int,
    clip_dist: float = 0.1,
    invert: bool = False,
) -> Array:
    """
    Calculates a soft-edged regular polygon differentiably. The 'clip_dist' parameter
    defines the distance from the edge to 'soften' up to. A large clip_dist will result
    in a polygon with a very soft edge, while a small clip_dist will result in a polygon
    with a very hard edge.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the polygon on.
    radius : float
        The radius of the polygon.
    nsides : int
        The number of sides of the polygon.
    clip_dist : float = 0.1
        The distance from the edge to 'soften' up to.
    invert : bool = False
        Whether to invert the polygon.

    Returns
    -------
    polygon : Array
        The softened polygon.
    """
    distances = -reg_polygon_distance(coords, nsides, radius)
    return soften(distances, clip_dist, invert)
soft_spider

Calculates a soft-edged spider differentiably. The 'clip_dist' parameter defines the distance from the edge to 'soften' up to. A large clip_dist will result in a spider with a very soft edge, while a small clip_dist will result in a spider with a very hard edge.

Parameters:

Name Type Description Default
coords Array

The coordinates to calculate the spider on.

required
width float

The width of the spider.

required
angles Array

The angles of the spider.

required
clip_dist float = 0.1

The distance from the edge to 'soften' up to.

0.1
invert bool = False

Whether to invert the spider.

False

Returns:

Name Type Description
spider Array

The softened spider.

Source code in src/dLux/utils/geometry.py
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def soft_spider(
    coords: Array,
    width: float,
    angles: Array,
    clip_dist: float = 0.1,
    invert: bool = False,
) -> Array:
    """
    Calculates a soft-edged spider differentiably. The 'clip_dist' parameter defines
    the distance from the edge to 'soften' up to. A large clip_dist will result in a
    spider with a very soft edge, while a small clip_dist will result in a spider with
    a very hard edge.

    Parameters
    ----------
    coords : Array
        The coordinates to calculate the spider on.
    width : float
        The width of the spider.
    angles : Array
        The angles of the spider.
    clip_dist : float = 0.1
        The distance from the edge to 'soften' up to.
    invert : bool = False
        Whether to invert the spider.

    Returns
    -------
    spider : Array
        The softened spider.
    """
    angles = np.array(angles) if not isinstance(angles, np.ndarray) else angles
    spider_fn = vmap(lambda angle: spider_distance(coords, width, angle))
    spiders = -spider_fn(angles).min(axis=0)
    return soften(spiders, clip_dist, invert)