Skip to content

Commit 1ec232c

Browse files
authored
Merge pull request #225 from Jammy2211/feature/documentation-geometry
docs: refactor and complete docstrings for autoarray/geometry
2 parents 52894f3 + b8e1f12 commit 1ec232c

File tree

4 files changed

+224
-121
lines changed

4 files changed

+224
-121
lines changed

autoarray/geometry/abstract_2d.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@
55
class AbstractGeometry2D:
66
@property
77
def extent(self) -> Tuple[float, float, float, float]:
8+
"""
9+
The extent of the 2D geometry in scaled units, returned as (x_min, x_max, y_min, y_max).
10+
11+
This format matches the ``extent`` argument of ``matplotlib.pyplot.imshow``, with x and y
12+
swapped relative to the usual (y,x) PyAutoArray convention. Subclasses must implement this.
13+
"""
814
raise NotImplementedError
915

1016
@property
1117
def extent_square(self) -> Tuple[float, float, float, float]:
1218
"""
13-
Returns an extent where the y and x distances are the same.
19+
The extent of the 2D geometry in scaled units, expanded so that the y and x ranges are equal.
20+
21+
This ensures that a uniform grid with square pixels can be laid over this extent, which is
22+
useful for constructing interpolation grids used in visualization. The centre of the extent
23+
is preserved; whichever axis has the smaller range is expanded symmetrically to match the
24+
larger one.
1425
15-
This ensures that a uniform grid with square pixels can be laid over this extent, such that an
16-
`interpolation_grid` can be computed which has square pixels. This benefits visualization.
26+
Returns
27+
-------
28+
(x_min, x_max, y_min, y_max)
29+
The square extent in scaled units following the matplotlib ``imshow`` convention.
1730
"""
1831

1932
y_mean = 0.5 * (self.extent[2] + self.extent[3])

autoarray/geometry/geometry_2d.py

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,15 @@ def central_pixel_coordinates(self) -> Tuple[float, float]:
121121
@property
122122
def central_scaled_coordinates(self) -> Tuple[float, float]:
123123
"""
124-
Returns the central scaled coordinates of a 2D geometry (and therefore a 2D data structure like an ``Array2D``)
125-
from the shape of that data structure.
124+
The central (y,x) coordinates of the 2D geometry in scaled units.
125+
126+
Computed from the shape and pixel scales of the geometry, shifted by the origin. This is
127+
the scaled-unit coordinate that sits at the geometric centre of the grid.
128+
129+
Returns
130+
-------
131+
Tuple[float, float]
132+
The (y,x) scaled coordinates of the geometric centre.
126133
"""
127134
return geometry_util.central_scaled_coordinate_2d_from(
128135
shape_native=self.shape_native,
@@ -134,24 +141,25 @@ def pixel_coordinates_2d_from(
134141
self, scaled_coordinates_2d: Tuple[float, float]
135142
) -> Tuple[int, int]:
136143
"""
137-
Convert a 2D (y,x) scaled coordinate to a 2D (y,x) pixel coordinate, which are returned as integers such that
138-
they do not include the decimal offset from each pixel's top-left corner relative to the input scaled coordinate.
144+
Convert a 2D (y,x) scaled coordinate to a 2D (y,x) pixel coordinate, returned as integers that
145+
identify the pixel containing the scaled coordinate (without sub-pixel decimal offsets).
139146
140-
The conversion is performed according to the 2D geometry on a uniform grid, where the pixel coordinate origin
141-
is at the top left corner, such that the pixel [0,0] corresponds to the highest (most positive) y scaled
142-
coordinate and lowest (most negative) x scaled coordinate on the gird.
147+
The conversion is performed according to the 2D geometry on a uniform grid, where the pixel
148+
coordinate origin is at the top-left corner, such that pixel [0,0] corresponds to the highest
149+
(most positive) y scaled coordinate and lowest (most negative) x scaled coordinate on the grid.
143150
144-
The scaled coordinate is defined by an origin and coordinates are shifted to this origin before computing their
145-
1D grid pixel coordinate values.
151+
The geometry's origin is applied so that scaled coordinates are correctly shifted before the
152+
pixel index calculation.
146153
147154
Parameters
148155
----------
149156
scaled_coordinates_2d
150-
The 2D (y,x) coordinates in scaled units which are converted to pixel coordinates.
157+
The 2D (y,x) coordinates in scaled units to convert to pixel coordinates.
151158
152159
Returns
153160
-------
154-
A 2D (y,x) pixel-value coordinate.
161+
Tuple[int, int]
162+
The 2D (y,x) integer pixel coordinates.
155163
"""
156164
return geometry_util.pixel_coordinates_2d_from(
157165
scaled_coordinates_2d=scaled_coordinates_2d,
@@ -164,23 +172,24 @@ def scaled_coordinates_2d_from(
164172
self, pixel_coordinates_2d: Tuple[float, float]
165173
) -> Tuple[float, float]:
166174
"""
167-
Convert a 2D (y,x) pixel coordinates to a 2D (y,x) scaled values.
175+
Convert a 2D (y,x) pixel coordinate to a 2D (y,x) scaled coordinate.
168176
169-
The conversion is performed according to a 2D geometry on a uniform grid, where the pixel coordinate origin is at
170-
the top left corner, such that the pixel [0,0] corresponds to the highest (most positive) y scaled coordinate
171-
and lowest (most negative) x scaled coordinate on the gird.
177+
The conversion is performed according to the 2D geometry on a uniform grid, where the pixel
178+
coordinate origin is at the top-left corner, such that pixel [0,0] corresponds to the highest
179+
(most positive) y scaled coordinate and lowest (most negative) x scaled coordinate on the grid.
172180
173-
The scaled coordinate is defined by an origin and coordinates are shifted to this origin before computing their
174-
1D grid pixel coordinate values.
181+
The geometry's origin is applied so that the returned scaled coordinate is correctly shifted
182+
relative to the coordinate system's centre.
175183
176184
Parameters
177185
----------
178-
scaled_coordinates_2d
179-
The 2D (y,x) coordinates in scaled units which are converted to pixel coordinates.
186+
pixel_coordinates_2d
187+
The 2D (y,x) pixel coordinates to convert to scaled units.
180188
181189
Returns
182190
-------
183-
A 2D (y,x) pixel-value coordinate.
191+
Tuple[float, float]
192+
The 2D (y,x) coordinates in scaled units.
184193
"""
185194

186195
return geometry_util.scaled_coordinates_2d_from(
@@ -266,6 +275,11 @@ def grid_pixels_2d_from(self, grid_scaled_2d: Grid2D) -> Grid2D:
266275
----------
267276
grid_scaled_2d
268277
A grid of (y,x) coordinates in scaled units.
278+
279+
Returns
280+
-------
281+
Grid2D
282+
A grid of (y,x) pixel-value coordinates as floats including the sub-pixel decimal offset.
269283
"""
270284
from autoarray.structures.grids.uniform_2d import Grid2D
271285

@@ -283,7 +297,7 @@ def grid_pixel_centres_2d_from(self, grid_scaled_2d: Grid2D) -> Grid2D:
283297
returned as integers such that they map directly to the pixel they are contained within.
284298
285299
The pixel coordinate origin is at the top left corner of the grid, such that the pixel [0,0] corresponds to
286-
higher y scaled coordinate value and lowest x scaled coordinate.
300+
highest y scaled coordinate value and lowest x scaled coordinate.
287301
288302
The scaled coordinate origin is defined by the class attribute origin, and coordinates are shifted to this
289303
origin before computing their 1D grid pixel indexes.
@@ -292,6 +306,12 @@ def grid_pixel_centres_2d_from(self, grid_scaled_2d: Grid2D) -> Grid2D:
292306
----------
293307
grid_scaled_2d
294308
The grid of (y,x) coordinates in scaled units.
309+
310+
Returns
311+
-------
312+
Grid2D
313+
A grid of (y,x) integer pixel coordinates identifying which pixel each scaled coordinate
314+
falls within.
295315
"""
296316
from autoarray.structures.grids.uniform_2d import Grid2D
297317

@@ -309,7 +329,7 @@ def grid_pixel_indexes_2d_from(self, grid_scaled_2d: Grid2D) -> Array2D:
309329
Convert a grid of (y,x) scaled coordinates to an array of pixel 1D indexes, which are returned as integers.
310330
311331
The pixel coordinate origin is at the top left corner of the grid, such that the pixel [0,0] corresponds to
312-
higher y scaled coordinate value and lowest x scaled coordinate.
332+
highest y scaled coordinate value and lowest x scaled coordinate.
313333
314334
For example:
315335
@@ -324,6 +344,12 @@ def grid_pixel_indexes_2d_from(self, grid_scaled_2d: Grid2D) -> Array2D:
324344
----------
325345
grid_scaled_2d
326346
The grid of (y,x) coordinates in scaled units.
347+
348+
Returns
349+
-------
350+
Array2D
351+
A 1D array of flat pixel indexes, where each value is the integer index of the pixel
352+
(counting left-to-right, top-to-bottom) that the corresponding scaled coordinate falls in.
327353
"""
328354

329355
from autoarray.structures.arrays.uniform_2d import Array2D
@@ -342,7 +368,7 @@ def grid_scaled_2d_from(self, grid_pixels_2d: Grid2D) -> Grid2D:
342368
Convert a grid of (y,x) pixel coordinates to a grid of (y,x) scaled values.
343369
344370
The pixel coordinate origin is at the top left corner of the grid, such that the pixel [0,0] corresponds to
345-
higher y scaled coordinate value and lowest x scaled coordinate.
371+
highest y scaled coordinate value and lowest x scaled coordinate.
346372
347373
The scaled coordinate origin is defined by the class attribute origin, and coordinates are shifted to this
348374
origin before computing their 1D grid pixel indexes.
@@ -351,6 +377,11 @@ def grid_scaled_2d_from(self, grid_pixels_2d: Grid2D) -> Grid2D:
351377
----------
352378
grid_pixels_2d
353379
The grid of (y,x) coordinates in pixels.
380+
381+
Returns
382+
-------
383+
Grid2D
384+
A grid of (y,x) coordinates in scaled units.
354385
"""
355386
from autoarray.structures.grids.uniform_2d import Grid2D
356387

autoarray/geometry/geometry_2d_irregular.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ def __init__(
1111
scaled_minima: Tuple[float, float],
1212
):
1313
"""
14-
A 2D geometry, representing an irregular grid of (y,x) coordinates.
14+
A 2D geometry for an irregular (non-uniform) grid of (y,x) coordinates.
1515
16-
This class is used for defining the extent of the irregular grid when visualizing it.
16+
Unlike `Geometry2D` which is derived from a regular pixel grid with a fixed `shape_native`
17+
and `pixel_scales`, this class stores pre-computed extent information directly. It is used
18+
by `Grid2DIrregular` and similar structures to define their bounding box for visualization
19+
and coordinate-space calculations.
20+
21+
Because the coordinates are irregular there is no uniform pixel scale, so the geometry
22+
is defined purely by the scaled extent (minima, maxima) of the point distribution.
1723
1824
Parameters
1925
----------
2026
shape_native_scaled
21-
The 2D scaled shape of the geometry defining the full extent of this object.
27+
The (y, x) extent of the geometry in scaled units, i.e.
28+
(scaled_maxima[0] - scaled_minima[0], scaled_maxima[1] - scaled_minima[1]).
2229
scaled_maxima
23-
The maximum (y,x) scaled coordinates of the 2D geometry.
30+
The maximum (y,x) scaled coordinates of the irregular grid.
2431
scaled_minima
25-
The minimum (y,x) scaled coordinates of the 2D geometry.
32+
The minimum (y,x) scaled coordinates of the irregular grid.
2633
"""
2734
self.shape_native_scaled = shape_native_scaled
2835
self.scaled_maxima = scaled_maxima

0 commit comments

Comments
 (0)