|
1 | | -import numpy as np |
2 | | -from functools import wraps |
3 | | - |
4 | | - |
5 | | -from typing import List, Union |
6 | | - |
7 | | -from autoarray.structures.arrays.irregular import ArrayIrregular |
8 | | -from autoarray.structures.arrays.uniform_1d import Array1D |
9 | | -from autoarray.structures.arrays.uniform_2d import Array2D |
10 | | -from autoarray.structures.decorators.abstract import AbstractMaker |
11 | | -from autoarray.structures.grids.uniform_1d import Grid1D |
12 | | -from autoarray.structures.grids.irregular_2d import Grid2DIrregular |
13 | | -from autoarray.structures.grids.uniform_2d import Grid2D |
14 | | - |
15 | | - |
16 | | -class ArrayMaker(AbstractMaker): |
17 | | - def via_grid_2d(self, result) -> Union[Array2D, List[Array2D]]: |
18 | | - """ |
19 | | - Convert the result of a decorated function which receives as input a `Grid2D` object to an `Array2D` object. |
20 | | -
|
21 | | - If the result returns a list, a list of `Array2D` objects is returned. |
22 | | -
|
23 | | - Parameters |
24 | | - ---------- |
25 | | - result |
26 | | - The input result (e.g. of a decorated function) that is converted to an Array2D or list of Array2D objects. |
27 | | - """ |
28 | | - |
29 | | - if not isinstance(result, list): |
30 | | - return Array2D(values=result, mask=self.mask) |
31 | | - return [Array2D(values=res, mask=self.mask) for res in result] |
32 | | - |
33 | | - def via_grid_2d_irr(self, result) -> Union[ArrayIrregular, List[ArrayIrregular]]: |
34 | | - """ |
35 | | - Convert the result of a decorated function which receives as input a `Grid2DIrregular` object to an `ArrayIrregular` |
36 | | - object. |
37 | | -
|
38 | | - If the result returns a list, a list of `ArrayIrregular` objects is returned. |
39 | | -
|
40 | | - Parameters |
41 | | - ---------- |
42 | | - result |
43 | | - The input result (e.g. of a decorated function) that is converted to an ArrayIrregular or list of |
44 | | - ArrayIrregular objects. |
45 | | - """ |
46 | | - if not isinstance(result, list): |
47 | | - return ArrayIrregular(values=result) |
48 | | - return [ArrayIrregular(values=res) for res in result] |
49 | | - |
50 | | - def via_grid_1d(self, result) -> Union[Array1D, List[Array1D]]: |
51 | | - """ |
52 | | - Convert the result of a decorated function which receives as input a `Grid1D` object to an `Array1D` object. |
53 | | -
|
54 | | - If the result returns a list, a list of `Array1D` objects is returned. |
55 | | -
|
56 | | - Parameters |
57 | | - ---------- |
58 | | - result |
59 | | - The input result (e.g. of a decorated function) that is converted to an Array1D or list of Array1D objects. |
60 | | - """ |
61 | | - if not isinstance(result, list): |
62 | | - return Array1D(values=result, mask=self.mask) |
63 | | - return [Array1D(values=res, mask=self.mask) for res in result] |
64 | | - |
65 | | - |
66 | | -def to_array(func): |
67 | | - """ |
68 | | - Homogenize the inputs and outputs of functions that take 1D or 2D grids of coordinates and return a 1D ndarray |
69 | | - which is converted to an `Array2D`, `ArrayIrregular` or `Array1D` object. |
70 | | -
|
71 | | - Parameters |
72 | | - ---------- |
73 | | - func |
74 | | - A function which computes a set of values from a 1D or 2D grid of coordinates. |
75 | | -
|
76 | | - Returns |
77 | | - ------- |
78 | | - A function that has its outputs homogenized to `Array2D`, `ArrayIrregular` or `Array1D` objects. |
79 | | - """ |
80 | | - |
81 | | - @wraps(func) |
82 | | - def wrapper( |
83 | | - obj: object, |
84 | | - grid: Union[np.ndarray, Grid2D, Grid2DIrregular, Grid1D], |
85 | | - xp=np, |
86 | | - *args, |
87 | | - **kwargs, |
88 | | - ) -> Union[np.ndarray, Array1D, Array2D, ArrayIrregular, List]: |
89 | | - """ |
90 | | - This decorator homogenizes the input of a "grid_like" 2D structure (`Grid2D`, `Grid2DIrregular` or `Grid1D`) |
91 | | - into a function which outputs an array-like structure (`Array2D`, `ArrayIrregular` or `Array1D`). |
92 | | -
|
93 | | - It allows these classes to be interchangeably input into a function, such that the grid is used to evaluate |
94 | | - the function at every (y,x) coordinates of the grid using specific functionality of the input grid. |
95 | | -
|
96 | | - The grid_like objects `Grid2D` and `Grid2DIrregular` are input into the function as a slimmed 2D ndarray array |
97 | | - of shape [total_coordinates, 2] where the second dimension stores the (y,x) |
98 | | -
|
99 | | - There are three types of consistent data structures and therefore decorated function mappings: |
100 | | -
|
101 | | - - Uniform (`Grid2D` -> `Array`): 2D structures defined on a uniform grid of data points. Both structures are |
102 | | - defined according to a `Mask2D`, which the maker object ensures is passed through self consistently. |
103 | | -
|
104 | | - - Irregular (`Grid2DIrregular` -> `ArrayIrregular`: 2D structures defined on an irregular grid of data points, |
105 | | - Neither structure is defined according to a mask and the maker sures the lack of a mask does not prevent the |
106 | | - function from being evaluated. |
107 | | -
|
108 | | - - 1D (`Grid1D` -> `Array1D`): 1D structures defined on a 1D grid of data points. These project the 1D grid |
109 | | - to a 2D grid to ensure the function can be evaluated, and then deproject the 2D grid back to a 1D grid to |
110 | | - ensure the output data structure is consistent with the input grid. |
111 | | -
|
112 | | - Parameters |
113 | | - ---------- |
114 | | - obj |
115 | | - An object whose function uses grid_like inputs to compute quantities at every coordinate on the grid. |
116 | | - grid |
117 | | - A grid_like object of coordinates on which the function values are evaluated. |
118 | | -
|
119 | | - Returns |
120 | | - ------- |
121 | | - The function values evaluated on the grid with the same structure as the input grid_like object. |
122 | | - """ |
123 | | - return ArrayMaker(func=func, obj=obj, grid=grid, xp=xp, *args, **kwargs).result |
124 | | - |
125 | | - return wrapper |
| 1 | +import numpy as np |
| 2 | +from functools import wraps |
| 3 | +from typing import List, Union |
| 4 | + |
| 5 | +from autoarray.structures.arrays.irregular import ArrayIrregular |
| 6 | +from autoarray.structures.arrays.uniform_2d import Array2D |
| 7 | +from autoarray.structures.decorators.abstract import AbstractMaker |
| 8 | +from autoarray.structures.grids.irregular_2d import Grid2DIrregular |
| 9 | +from autoarray.structures.grids.uniform_2d import Grid2D |
| 10 | + |
| 11 | + |
| 12 | +class ArrayMaker(AbstractMaker): |
| 13 | + def via_grid_2d(self, result) -> Union[Array2D, List[Array2D]]: |
| 14 | + if not isinstance(result, list): |
| 15 | + return Array2D(values=result, mask=self.mask) |
| 16 | + return [Array2D(values=res, mask=self.mask) for res in result] |
| 17 | + |
| 18 | + def via_grid_2d_irr(self, result) -> Union[ArrayIrregular, List[ArrayIrregular]]: |
| 19 | + if not isinstance(result, list): |
| 20 | + return ArrayIrregular(values=result) |
| 21 | + return [ArrayIrregular(values=res) for res in result] |
| 22 | + |
| 23 | + |
| 24 | +def to_array(func): |
| 25 | + """ |
| 26 | + Homogenize the inputs and outputs of functions that take 2D grids of coordinates and return a 1D ndarray |
| 27 | + which is converted to an `Array2D` or `ArrayIrregular` object. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + func |
| 32 | + A function which computes a set of values from a 2D grid of coordinates. |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + A function that has its outputs homogenized to `Array2D` or `ArrayIrregular` objects. |
| 37 | + """ |
| 38 | + |
| 39 | + @wraps(func) |
| 40 | + def wrapper( |
| 41 | + obj: object, |
| 42 | + grid: Union[np.ndarray, Grid2D, Grid2DIrregular], |
| 43 | + xp=np, |
| 44 | + *args, |
| 45 | + **kwargs, |
| 46 | + ) -> Union[np.ndarray, Array2D, ArrayIrregular, List]: |
| 47 | + return ArrayMaker(func=func, obj=obj, grid=grid, xp=xp, *args, **kwargs).result |
| 48 | + |
| 49 | + return wrapper |
0 commit comments