Skip to content

Geometry¤

combine

dLux.utils.geometry.combine(arrays, oversample=1, use_sum=False) ¤

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
use_sum bool = False

Whether to sum the arrays instead of multiplying them.

False

Returns:

Name Type Description
array Array

The combined array, optionally downsampled by oversample.

Source code in dLux/utils/geometry.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def combine(
    arrays: Array,
    oversample: int = 1,
    use_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.
    use_sum : bool = False
        Whether to sum the arrays instead of multiplying them.

    Returns
    -------
    array : Array
        The combined array, optionally downsampled by `oversample`.
    """
    method = np.sum if use_sum else np.prod
    array = np.array(arrays)
    if oversample == 1:
        return method(array, 0)
    return dlu.downsample(method(array, 0), oversample)
circle

dLux.utils.geometry.circle(coords, radius, invert=False) ¤

Calculates a hard-edged circle. 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 dLux/utils/geometry.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def circle(coords: Array, radius: float, invert: bool = False) -> Array:
    """
    Calculates a hard-edged circle. 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

dLux.utils.geometry.square(coords, width, invert=False) ¤

Calculates a hard-edged square. 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 dLux/utils/geometry.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def square(coords: Array, width: float, invert: bool = False) -> Array:
    """
    Calculates a hard-edged square. 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

dLux.utils.geometry.rectangle(coords, width, height, invert=False) ¤

Calculates a hard-edged rectangle. 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
invert bool = False

Whether to invert the rectangle.

False

Returns:

Name Type Description
rectangle Array

The rectangle.

Source code in dLux/utils/geometry.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def rectangle(
    coords: Array, width: float, height: float, invert: bool = False
) -> Array:
    """
    Calculates a hard-edged rectangle. 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.
    invert : bool = False
        Whether to invert 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

dLux.utils.geometry.reg_polygon(coords, rmax, nsides, invert=False) ¤

Calculates a hard-edged regular polygon. 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 dLux/utils/geometry.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def reg_polygon(coords: Array, rmax: float, nsides: int, invert: bool = False) -> Array:
    """
    Calculates a hard-edged regular polygon. 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

dLux.utils.geometry.spider(coords, width, angles) ¤

Calculates a hard-edged spider. 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 in degrees.

required

Returns:

Name Type Description
spider Array

The spider.

Source code in dLux/utils/geometry.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def spider(coords: Array, width: float, angles: Array) -> Array:
    """
    Calculates a hard-edged spider. 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 in degrees.

    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)
    spiders = ~lax.reduce(calc_fn(angles), np.array(False), lax.bitwise_or, (0,))
    return spiders.astype(float)
soft_circle

dLux.utils.geometry.soft_circle(coords, radius, clip_dist=0.1, invert=False) ¤

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.

0.1
invert bool = False

Whether to invert the circle.

False

Returns:

Name Type Description
circle Array

The softened circle.

Source code in dLux/utils/geometry.py
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
251
252
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.
    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

dLux.utils.geometry.soft_square(coords, width, clip_dist=0.1, invert=False) ¤

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.

0.1
invert bool = False

Whether to invert the square.

False

Returns:

Name Type Description
square Array

The softened square.

Source code in dLux/utils/geometry.py
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
281
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.
    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

dLux.utils.geometry.soft_rectangle(coords, width, height, clip_dist=0.1, invert=False) ¤

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
invert bool = False

Whether to invert the rectangle.

False

Returns:

Name Type Description
rectangle Array

The softened rectangle.

Source code in dLux/utils/geometry.py
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
314
315
316
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.
    invert : bool = False
        Whether to invert the rectangle.

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

dLux.utils.geometry.soft_reg_polygon(coords, radius, nsides, clip_dist=0.1, invert=False) ¤

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 dLux/utils/geometry.py
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
349
350
351
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

dLux.utils.geometry.soft_spider(coords, width, angles, clip_dist=0.1, invert=False) ¤

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 dLux/utils/geometry.py
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
386
387
388
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)