Skip to content

Commit 7bd9e4d

Browse files
Jammy2211Jammy2211
authored andcommitted
lots of JAX stuff cleaned up
1 parent ed3461c commit 7bd9e4d

3 files changed

Lines changed: 24 additions & 37 deletions

File tree

autoarray/inversion/inversion/abstract.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def __init__(
7575

7676
self.xp = xp
7777

78+
79+
7880
@property
7981
def data(self):
8082
return self.dataset.data

autoarray/structures/grids/uniform_2d.py

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import annotations
2-
import jax.numpy as jnp
32
import numpy as np
43
from pathlib import Path
54
from typing import List, Optional, Tuple, Union
@@ -28,6 +27,7 @@ def __init__(
2827
store_native: bool = False,
2928
over_sample_size: Union[int, Array2D] = 4,
3029
over_sampled: Optional[Grid2D] = None,
30+
xp=np,
3131
*args,
3232
**kwargs,
3333
):
@@ -163,9 +163,10 @@ def __init__(
163163
grid_2d=values,
164164
mask_2d=mask,
165165
store_native=store_native,
166+
xp=xp
166167
)
167168

168-
super().__init__(values)
169+
super().__init__(values, xp=xp)
169170

170171
self.mask = mask
171172

@@ -690,7 +691,7 @@ def blurring_grid_from(
690691
over_sample_size=over_sample_size,
691692
)
692693

693-
def subtracted_from(self, offset: Tuple[(float, float), np.ndarray]) -> "Grid2D":
694+
def subtracted_from(self, offset: Tuple[(float, float), np.ndarray], xp=np) -> "Grid2D":
694695

695696
mask = Mask2D(
696697
mask=self.mask,
@@ -699,10 +700,10 @@ def subtracted_from(self, offset: Tuple[(float, float), np.ndarray]) -> "Grid2D"
699700
)
700701

701702
return Grid2D(
702-
values=self - jnp.array(offset),
703+
values=self - xp.array(offset),
703704
mask=mask,
704705
over_sample_size=self.over_sample_size,
705-
over_sampled=self.over_sampled - jnp.array(offset),
706+
over_sampled=self.over_sampled - xp.array(offset),
706707
)
707708

708709
@property
@@ -847,7 +848,7 @@ def squared_distances_to_coordinate_from(
847848
coordinate
848849
The (y,x) coordinate from which the squared distance of every grid (y,x) coordinate is computed.
849850
"""
850-
squared_distances = jnp.square(self.array[:, 0] - coordinate[0]) + jnp.square(
851+
squared_distances = self.xp.square(self.array[:, 0] - coordinate[0]) + self.xp.square(
851852
self.array[:, 1] - coordinate[1]
852853
)
853854

@@ -867,7 +868,7 @@ def distances_to_coordinate_from(
867868
squared_distance = self.squared_distances_to_coordinate_from(
868869
coordinate=coordinate
869870
)
870-
distances = jnp.sqrt(squared_distance.array)
871+
distances = self.xp.sqrt(squared_distance.array)
871872
return Array2D(values=distances, mask=self.mask)
872873

873874
def grid_2d_radial_projected_shape_slim_from(
@@ -1017,50 +1018,32 @@ def shape_native_scaled_interior(self) -> Tuple[float, float]:
10171018
of the grid's (y,x) values, whereas the `shape_native_scaled` uses the uniform geometry of the grid and its
10181019
``pixel_scales``, which means it has a buffer at each edge of half a ``pixel_scale``.
10191020
"""
1020-
if isinstance(self, jnp.ndarray):
1021-
return (
1022-
np.amax(self.array[:, 0]) - np.amin(self.array[:, 0]),
1023-
np.amax(self.array[:, 1]) - np.amin(self.array[:, 1]),
1024-
)
1025-
else:
1026-
return (
1027-
np.amax(self[:, 0]) - np.amin(self[:, 0]),
1028-
np.amax(self[:, 1]) - np.amin(self[:, 1]),
1029-
)
1021+
return (
1022+
np.amax(self[:, 0]) - np.amin(self[:, 0]),
1023+
np.amax(self[:, 1]) - np.amin(self[:, 1]),
1024+
)
10301025

10311026
@property
10321027
def scaled_minima(self) -> Tuple:
10331028
"""
10341029
The (y,x) minimum values of the grid in scaled units, buffed such that their extent is further than the grid's
10351030
extent.
10361031
"""
1037-
if isinstance(self, jnp.ndarray):
1038-
return (
1039-
jnp.amin(self.array[:, 0]).astype("float"),
1040-
jnp.amin(self.array[:, 1]).astype("float"),
1041-
)
1042-
else:
1043-
return (
1044-
np.amin(self[:, 0]).astype("float"),
1045-
np.amin(self[:, 1]).astype("float"),
1046-
)
1032+
return (
1033+
np.amin(self[:, 0]).astype("float"),
1034+
np.amin(self[:, 1]).astype("float"),
1035+
)
10471036

10481037
@property
10491038
def scaled_maxima(self) -> Tuple:
10501039
"""
10511040
The (y,x) maximum values of the grid in scaled units, buffed such that their extent is further than the grid's
10521041
extent.
10531042
"""
1054-
if isinstance(self, jnp.ndarray):
1055-
return (
1056-
jnp.amax(self.array[:, 0]).astype("float"),
1057-
jnp.amax(self.array[:, 1]).astype("float"),
1058-
)
1059-
else:
1060-
return (
1061-
np.amax(self[:, 0]).astype("float"),
1062-
np.amax(self[:, 1]).astype("float"),
1063-
)
1043+
return (
1044+
np.amax(self[:, 0]).astype("float"),
1045+
np.amax(self[:, 1]).astype("float"),
1046+
)
10641047

10651048
def extent_with_buffer_from(self, buffer: float = 1.0e-8) -> List[float]:
10661049
"""

test_autoarray/inversion/inversion/test_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def test__inversion_imaging__via_mapper(
114114
assert inversion.log_det_curvature_reg_matrix_term == pytest.approx(10.6674, 1.0e-4)
115115
assert inversion.mapped_reconstructed_image == pytest.approx(np.ones(9), 1.0e-4)
116116

117+
fff
118+
117119

118120
def test__inversion_imaging__via_regularizations(
119121
masked_imaging_7x7_no_blur,

0 commit comments

Comments
 (0)