Skip to content

Commit fb2f394

Browse files
authored
Merge pull request #398 from PyAutoLabs/feature/jax-interp-2d
refactor: DatasetInterp delegates to aa.interp_2d; expose xp
2 parents e3cdd22 + d0d9ed5 commit fb2f394

1 file changed

Lines changed: 121 additions & 87 deletions

File tree

Lines changed: 121 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,121 @@
1-
import numpy as np
2-
from typing import Tuple
3-
4-
from autoconf import cached_property
5-
6-
import autoarray as aa
7-
8-
9-
class DatasetInterp:
10-
def __init__(self, dataset: aa.Imaging):
11-
"""
12-
An ellipse interpolator, which contains a dataset (e.g. the image data and noise-map) and performs interpo.aiton
13-
calculations used for ellipse fitting.
14-
15-
This object is used by the input to the `FitEllipse` object, which fits the dataset with ellipses and quantifies
16-
the goodness-of-fit via a residual map, likelihood, chi-squared and other quantities.
17-
18-
The following quantities of the ellipse data are interpolated and used for the following tasks:
19-
20-
- `data`: The image data, which shows the signal that is analysed and fitted with ellipses.
21-
22-
- `noise_map`: The RMS standard deviation error in every pixel, which is used to compute the chi-squared value
23-
and likelihood of a fit.
24-
25-
The `data` and `noise_map` are typically the same images of a galaxy used to perform standard light-profile
26-
fitting.
27-
28-
Parameters
29-
----------
30-
dataset
31-
The imaging data, containing the image data, noise map.
32-
"""
33-
self.dataset = dataset
34-
35-
@cached_property
36-
def points_interp(self) -> Tuple[np.ndarray, np.ndarray]:
37-
"""
38-
The points on which the interpolation from the 2D grid of data is performed.
39-
"""
40-
41-
x = self.dataset.mask.derive_grid.all_false.native[0, :, 1]
42-
y = np.flip(self.dataset.mask.derive_grid.all_false.native[:, 0, 0])
43-
44-
return (x, y)
45-
46-
@cached_property
47-
def mask_interp(self) -> "interpolate.RegularGridInterpolator":
48-
"""
49-
Returns a 2D interpolation of the mask, which is used to determine whether inteprolated values use a masked
50-
pixel for the interpolation and thus should not be included in a fit.
51-
"""
52-
from scipy import interpolate
53-
54-
return interpolate.RegularGridInterpolator(
55-
points=self.points_interp,
56-
values=np.float64(self.dataset.data.mask),
57-
bounds_error=False,
58-
fill_value=0.0,
59-
)
60-
61-
@cached_property
62-
def data_interp(self) -> "interpolate.RegularGridInterpolator":
63-
"""
64-
Returns a 2D interpolation of the data, which is used to evaluate the data at any point in 2D space.
65-
"""
66-
from scipy import interpolate
67-
68-
return interpolate.RegularGridInterpolator(
69-
points=self.points_interp,
70-
values=np.float64(self.dataset.data.native),
71-
bounds_error=False,
72-
fill_value=0.0,
73-
)
74-
75-
@cached_property
76-
def noise_map_interp(self) -> "interpolate.RegularGridInterpolator":
77-
"""
78-
Returns a 2D interpolation of the noise-map, which is used to evaluate the noise-map at any point in 2D space.
79-
"""
80-
from scipy import interpolate
81-
82-
return interpolate.RegularGridInterpolator(
83-
points=self.points_interp,
84-
values=np.float64(self.dataset.noise_map.native),
85-
bounds_error=False,
86-
fill_value=0.0,
87-
)
1+
import numpy as np
2+
from typing import Tuple
3+
4+
from autoconf import cached_property
5+
6+
import autoarray as aa
7+
8+
9+
class DatasetInterp:
10+
def __init__(self, dataset: aa.Imaging):
11+
"""
12+
An ellipse interpolator, which contains a dataset (e.g. the image data and noise-map) and performs interpo.aiton
13+
calculations used for ellipse fitting.
14+
15+
This object is used by the input to the `FitEllipse` object, which fits the dataset with ellipses and quantifies
16+
the goodness-of-fit via a residual map, likelihood, chi-squared and other quantities.
17+
18+
The following quantities of the ellipse data are interpolated and used for the following tasks:
19+
20+
- `data`: The image data, which shows the signal that is analysed and fitted with ellipses.
21+
22+
- `noise_map`: The RMS standard deviation error in every pixel, which is used to compute the chi-squared value
23+
and likelihood of a fit.
24+
25+
The `data` and `noise_map` are typically the same images of a galaxy used to perform standard light-profile
26+
fitting.
27+
28+
Parameters
29+
----------
30+
dataset
31+
The imaging data, containing the image data, noise map.
32+
"""
33+
self.dataset = dataset
34+
35+
@cached_property
36+
def points_interp(self) -> Tuple[np.ndarray, np.ndarray]:
37+
"""
38+
The points on which the interpolation from the 2D grid of data is performed.
39+
"""
40+
41+
x = self.dataset.mask.derive_grid.all_false.native[0, :, 1]
42+
y = np.flip(self.dataset.mask.derive_grid.all_false.native[:, 0, 0])
43+
44+
return (x, y)
45+
46+
def mask_interp(self, points, xp=np):
47+
"""
48+
Returns a 2D interpolation of the mask, which is used to determine whether inteprolated values use a masked
49+
pixel for the interpolation and thus should not be included in a fit.
50+
51+
Backwards-compatibility note: this used to be a cached property that
52+
returned a scipy ``RegularGridInterpolator`` instance, also callable on
53+
``(N, 2)`` points. Existing call sites in ``fit_ellipse.py`` that read
54+
``self.interp.mask_interp(points)`` continue to work; the sentinel
55+
check ``self.interp.mask_interp is not None`` also continues to pass
56+
(a bound method is never None).
57+
58+
Parameters
59+
----------
60+
points
61+
An ``(N, 2)`` array of query coordinates. Out-of-bounds queries
62+
return 0.0 (treat outside-the-grid as unmasked).
63+
xp
64+
Array namespace (``numpy`` or ``jax.numpy``). Defaults to
65+
``numpy``.
66+
"""
67+
x_axis, y_axis = self.points_interp
68+
return aa.interp_2d(
69+
points,
70+
x_axis,
71+
y_axis,
72+
np.float64(self.dataset.data.mask),
73+
fill_value=0.0,
74+
xp=xp,
75+
)
76+
77+
def data_interp(self, points, xp=np):
78+
"""
79+
Returns a 2D interpolation of the data, which is used to evaluate the data at any point in 2D space.
80+
81+
Parameters
82+
----------
83+
points
84+
An ``(N, 2)`` array of query coordinates. Out-of-bounds queries
85+
return 0.0.
86+
xp
87+
Array namespace (``numpy`` or ``jax.numpy``). Defaults to
88+
``numpy``.
89+
"""
90+
x_axis, y_axis = self.points_interp
91+
return aa.interp_2d(
92+
points,
93+
x_axis,
94+
y_axis,
95+
np.float64(self.dataset.data.native),
96+
fill_value=0.0,
97+
xp=xp,
98+
)
99+
100+
def noise_map_interp(self, points, xp=np):
101+
"""
102+
Returns a 2D interpolation of the noise-map, which is used to evaluate the noise-map at any point in 2D space.
103+
104+
Parameters
105+
----------
106+
points
107+
An ``(N, 2)`` array of query coordinates. Out-of-bounds queries
108+
return 0.0.
109+
xp
110+
Array namespace (``numpy`` or ``jax.numpy``). Defaults to
111+
``numpy``.
112+
"""
113+
x_axis, y_axis = self.points_interp
114+
return aa.interp_2d(
115+
points,
116+
x_axis,
117+
y_axis,
118+
np.float64(self.dataset.noise_map.native),
119+
fill_value=0.0,
120+
xp=xp,
121+
)

0 commit comments

Comments
 (0)