|
1 | 1 | from __future__ import annotations |
2 | 2 | import numpy as np |
3 | | -import jax.numpy as jnp |
| 3 | + |
4 | 4 | from typing import TYPE_CHECKING, List, Union, Tuple |
5 | 5 |
|
6 | 6 | if TYPE_CHECKING: |
@@ -40,30 +40,27 @@ def convert_grid_1d( |
40 | 40 |
|
41 | 41 | grid_1d = grid_2d_util.convert_grid(grid=grid_1d) |
42 | 42 |
|
43 | | - is_numpy = True if isinstance(grid_1d, np.ndarray) else False |
44 | | - |
45 | 43 | is_native = grid_1d.shape[0] == mask_1d.shape_native[0] |
46 | 44 |
|
47 | 45 | if is_native == store_native: |
48 | | - grid_1d = grid_1d |
| 46 | + return grid_1d |
49 | 47 | elif not store_native: |
50 | | - grid_1d = grid_1d_slim_from( |
| 48 | + return grid_1d_slim_from( |
51 | 49 | grid_1d_native=grid_1d, |
52 | 50 | mask_1d=mask_1d, |
53 | 51 | ) |
54 | | - else: |
55 | | - grid_1d = grid_1d_native_from( |
56 | | - grid_1d_slim=grid_1d, |
57 | | - mask_1d=mask_1d, |
58 | | - xp=xp |
59 | | - ) |
60 | | - return np.array(grid_1d) if is_numpy else jnp.array(grid_1d) |
| 52 | + return grid_1d_native_from( |
| 53 | + grid_1d_slim=grid_1d, |
| 54 | + mask_1d=mask_1d, |
| 55 | + xp=xp |
| 56 | + ) |
61 | 57 |
|
62 | 58 |
|
63 | 59 | def grid_1d_slim_via_shape_slim_from( |
64 | 60 | shape_slim: Tuple[int], |
65 | 61 | pixel_scales: ty.PixelScales, |
66 | 62 | origin: Tuple[float] = (0.0,), |
| 63 | + xp=np |
67 | 64 | ) -> np.ndarray: |
68 | 65 | """ |
69 | 66 | This routine computes the (x) scaled coordinates at the centre of every pixel defined by a 1D shape of the |
@@ -96,13 +93,15 @@ def grid_1d_slim_via_shape_slim_from( |
96 | 93 | mask_1d=np.full(fill_value=False, shape=shape_slim), |
97 | 94 | pixel_scales=pixel_scales, |
98 | 95 | origin=origin, |
| 96 | + xp=xp |
99 | 97 | ) |
100 | 98 |
|
101 | 99 |
|
102 | 100 | def grid_1d_slim_via_mask_from( |
103 | 101 | mask_1d: np.ndarray, |
104 | 102 | pixel_scales: ty.PixelScales, |
105 | 103 | origin: Tuple[float] = (0.0,), |
| 104 | + xp=np |
106 | 105 | ) -> np.ndarray: |
107 | 106 | """ |
108 | 107 | For a grid, every unmasked pixel of its 1D mask with shape (total_pixels,) is divided into a finer uniform |
@@ -137,8 +136,8 @@ def grid_1d_slim_via_mask_from( |
137 | 136 | centres_scaled = geometry_util.central_scaled_coordinate_1d_from( |
138 | 137 | shape_slim=mask_1d.shape, pixel_scales=pixel_scales, origin=origin |
139 | 138 | ) |
140 | | - indices = jnp.arange(mask_1d.shape[0]) |
141 | | - unmasked = jnp.logical_not(mask_1d) |
| 139 | + indices = xp.arange(mask_1d.shape[0]) |
| 140 | + unmasked = xp.logical_not(mask_1d) |
142 | 141 | coords = (indices - centres_scaled[0]) * pixel_scales[0] |
143 | 142 | return coords[unmasked] |
144 | 143 |
|
|
0 commit comments