Skip to content

Commit e1ec21e

Browse files
authored
Merge pull request #259 from PyAutoLabs/feature/lazy-imports
perf: defer scipy imports to reduce import time
2 parents ff0f45e + a31da2b commit e1ec21e

File tree

17 files changed

+80
-54
lines changed

17 files changed

+80
-54
lines changed

autoarray/dataset/plot/imaging_plots.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from typing import Optional
22

3-
import matplotlib.pyplot as plt
43

5-
from autoarray.plot.utils import subplot_save, conf_subplot_figsize, tight_layout
4+
from autoarray.plot.utils import subplots, subplot_save, conf_subplot_figsize, tight_layout
65

76

87
def subplot_imaging_dataset(
@@ -51,7 +50,7 @@ def subplot_imaging_dataset(
5150

5251
from autoarray.plot.array import plot_array
5352

54-
fig, axes = plt.subplots(3, 3, figsize=conf_subplot_figsize(3, 3))
53+
fig, axes = subplots(3, 3, figsize=conf_subplot_figsize(3, 3))
5554
axes = axes.flatten()
5655

5756
plot_array(
@@ -172,7 +171,7 @@ def subplot_imaging_dataset_list(
172171
from autoarray.plot.array import plot_array
173172

174173
n = len(dataset_list)
175-
fig, axes = plt.subplots(n, 3, figsize=conf_subplot_figsize(n, 3))
174+
fig, axes = subplots(n, 3, figsize=conf_subplot_figsize(n, 3))
176175
if n == 1:
177176
axes = [axes]
178177
for i, dataset in enumerate(dataset_list):

autoarray/dataset/plot/interferometer_plots.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import numpy as np
22
from typing import Optional
33

4-
import matplotlib.pyplot as plt
54

65
from autoarray.plot.array import plot_array
76
from autoarray.plot.grid import plot_grid
87
from autoarray.plot.yx import plot_yx
9-
from autoarray.plot.utils import subplot_save, hide_unused_axes, conf_subplot_figsize, tight_layout
8+
from autoarray.plot.utils import subplots, subplot_save, hide_unused_axes, conf_subplot_figsize, tight_layout
109
from autoarray.structures.grids.irregular_2d import Grid2DIrregular
1110

1211

@@ -39,7 +38,7 @@ def subplot_interferometer_dataset(
3938
use_log10
4039
Apply log10 normalisation to image panels.
4140
"""
42-
fig, axes = plt.subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
41+
fig, axes = subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
4342
axes = axes.flatten()
4443

4544
plot_grid(dataset.data.in_grid, ax=axes[0], title="Visibilities", xlabel="", ylabel="")
@@ -117,7 +116,7 @@ def subplot_interferometer_dirty_images(
117116
use_log10
118117
Apply log10 normalisation.
119118
"""
120-
fig, axes = plt.subplots(1, 3, figsize=conf_subplot_figsize(1, 3))
119+
fig, axes = subplots(1, 3, figsize=conf_subplot_figsize(1, 3))
121120

122121
plot_array(
123122
dataset.dirty_image,

autoarray/fit/plot/fit_imaging_plots.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from typing import Optional
22

3-
import matplotlib.pyplot as plt
43

54
from autoarray.plot.array import plot_array
6-
from autoarray.plot.utils import subplot_save, symmetric_vmin_vmax, hide_unused_axes, conf_subplot_figsize, tight_layout
5+
from autoarray.plot.utils import subplots, subplot_save, symmetric_vmin_vmax, hide_unused_axes, conf_subplot_figsize, tight_layout
76

87

98
def subplot_fit_imaging(
@@ -43,7 +42,7 @@ def subplot_fit_imaging(
4342
grid, positions, lines
4443
Optional overlays forwarded to every panel.
4544
"""
46-
fig, axes = plt.subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
45+
fig, axes = subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
4746
axes = axes.flatten()
4847

4948
plot_array(

autoarray/fit/plot/fit_interferometer_plots.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import numpy as np
22
from typing import Optional
33

4-
import matplotlib.pyplot as plt
54

65
from autoarray.plot.array import plot_array
76
from autoarray.plot.yx import plot_yx
8-
from autoarray.plot.utils import subplot_save, symmetric_vmin_vmax, hide_unused_axes, conf_subplot_figsize, tight_layout
7+
from autoarray.plot.utils import subplots, subplot_save, symmetric_vmin_vmax, hide_unused_axes, conf_subplot_figsize, tight_layout
98

109

1110
def subplot_fit_interferometer(
@@ -40,7 +39,7 @@ def subplot_fit_interferometer(
4039
Not used here (UV-plane residuals are scatter plots); kept for API
4140
consistency.
4241
"""
43-
fig, axes = plt.subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
42+
fig, axes = subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
4443
axes = axes.flatten()
4544

4645
uv = fit.dataset.uv_distances / 10**3.0
@@ -135,7 +134,7 @@ def subplot_fit_interferometer_dirty_images(
135134
residuals_symmetric_cmap
136135
Centre residual colour scale symmetrically around zero.
137136
"""
138-
fig, axes = plt.subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
137+
fig, axes = subplots(2, 3, figsize=conf_subplot_figsize(2, 3))
139138
axes = axes.flatten()
140139

141140
plot_array(

autoarray/inversion/inversion/inversion_util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from autoarray.settings import Settings
66

77
from autoarray import exc
8-
from autoarray.util.fnnls import fnnls_cholesky
98

109

1110
def curvature_matrix_diag_via_psf_weighted_noise_from(
@@ -281,6 +280,8 @@ def reconstruction_positive_only_from(
281280

282281
try:
283282

283+
from autoarray.util.fnnls import fnnls_cholesky
284+
284285
return fnnls_cholesky(
285286
curvature_reg_matrix,
286287
(data_vector).T,

autoarray/inversion/mesh/interpolator/delaunay.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import numpy as np
2-
import scipy.spatial
3-
from scipy.spatial import cKDTree, Delaunay, Voronoi
42

53
from autoconf import cached_property
64

@@ -12,6 +10,7 @@
1210

1311
def scipy_delaunay(points_np, query_points_np, areas_factor):
1412
"""Compute Delaunay simplices (simplices_padded) and Voronoi areas in one call."""
13+
from scipy.spatial import Delaunay
1514

1615
max_simplices = 2 * points_np.shape[0]
1716

@@ -182,6 +181,8 @@ def pix_indexes_for_sub_slim_index_delaunay_from(
182181
# Case 2: Outside → KDTree NN
183182
# ---------------------------
184183
if outside_mask.any():
184+
from scipy.spatial import cKDTree
185+
185186
tree = cKDTree(delaunay_points)
186187
_, idx = tree.query(data_grid[outside_mask], k=1)
187188
out[outside_mask, 0] = idx.astype(np.int32)
@@ -202,6 +203,7 @@ def scipy_delaunay_matern(points_np, query_points_np):
202203
typically of shape (Q, 3), where each row gives the indices of the
203204
Delaunay mesh vertices ("pixels") associated with that query point.
204205
"""
206+
from scipy.spatial import Delaunay
205207

206208
max_simplices = 2 * points_np.shape[0]
207209

autoarray/inversion/plot/inversion_plots.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
from pathlib import Path
55
from typing import Optional, Union
66

7-
import matplotlib.pyplot as plt
87
from autoconf import conf
98

109
from autoarray.inversion.mappers.abstract import Mapper
1110
from autoarray.plot.array import plot_array
12-
from autoarray.plot.utils import numpy_grid, numpy_lines, numpy_positions, subplot_save, hide_unused_axes, conf_subplot_figsize, tight_layout
11+
from autoarray.plot.utils import subplots, numpy_grid, numpy_lines, numpy_positions, subplot_save, hide_unused_axes, conf_subplot_figsize, tight_layout
1312
from autoarray.inversion.plot.mapper_plots import plot_mapper
1413
from autoarray.structures.arrays.uniform_2d import Array2D
1514

@@ -53,7 +52,7 @@ def subplot_of_mapper(
5352
"""
5453
mapper = inversion.cls_list_from(cls=Mapper)[mapper_index]
5554

56-
fig, axes = plt.subplots(3, 4, figsize=conf_subplot_figsize(3, 4))
55+
fig, axes = subplots(3, 4, figsize=conf_subplot_figsize(3, 4))
5756
axes = axes.flatten()
5857

5958
# panel 0: data subtracted
@@ -279,7 +278,7 @@ def subplot_mappings(
279278
)
280279
mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes)
281280

282-
fig, axes = plt.subplots(2, 2, figsize=conf_subplot_figsize(2, 2))
281+
fig, axes = subplots(2, 2, figsize=conf_subplot_figsize(2, 2))
283282
axes = axes.flatten()
284283

285284
# panel 0: data subtracted

autoarray/inversion/plot/mapper_plots.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import logging
22
from typing import Optional
33

4-
import matplotlib.pyplot as plt
54

65
from autoarray.plot.array import plot_array
76
from autoarray.plot.inversion import plot_inversion_reconstruction
8-
from autoarray.plot.utils import numpy_grid, numpy_lines, subplot_save, conf_subplot_figsize, tight_layout
7+
from autoarray.plot.utils import subplots, numpy_grid, numpy_lines, subplot_save, conf_subplot_figsize, tight_layout
98

109
logger = logging.getLogger(__name__)
1110

@@ -114,7 +113,7 @@ def subplot_image_and_mapper(
114113
lines
115114
Lines to overlay on both panels.
116115
"""
117-
fig, axes = plt.subplots(1, 2, figsize=conf_subplot_figsize(1, 2))
116+
fig, axes = subplots(1, 2, figsize=conf_subplot_figsize(1, 2))
118117

119118
plot_array(
120119
image,

autoarray/mask/mask_2d_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22
import warnings
3-
from scipy.ndimage import binary_dilation
43
from typing import Tuple
54

65
from autoarray import exc
@@ -465,7 +464,6 @@ def min_false_distance_to_edge(mask: np.ndarray) -> Tuple[int, int]:
465464
from typing import Tuple
466465

467466
import numpy as np
468-
from scipy.ndimage import binary_dilation
469467

470468

471469
def required_shape_for_kernel(
@@ -608,6 +606,8 @@ def blurring_mask_2d_from(
608606
)
609607

610608
# Pixels within kernel footprint of any unmasked pixel
609+
from scipy.ndimage import binary_dilation
610+
611611
near_unmasked_padded = binary_dilation(unmasked_padded, structure=structure)
612612
near_unmasked = near_unmasked_padded[
613613
pad_y : pad_y + mask_2d.shape[0],

autoarray/operators/convolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import numpy as np
99
from pathlib import Path
10-
import scipy
1110
from typing import Optional, Tuple, Union
1211
import warnings
1312

@@ -116,6 +115,8 @@ class determines how masked real-space data are embedded into a padded array,
116115
full_shape = tuple(
117116
s1 + s2 - 1 for s1, s2 in zip(mask_shape, self.kernel.shape_native)
118117
)
118+
import scipy.fft
119+
119120
fft_shape = tuple(scipy.fft.next_fast_len(s, real=True) for s in full_shape)
120121

121122
self.fft_shape = fft_shape

0 commit comments

Comments
 (0)