RACS-tools 4.3.2__tar.gz → 4.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: racs-tools
3
- Version: 4.3.2
3
+ Version: 4.4.0
4
4
  Summary: Useful scripts for RACS.
5
5
  License: BSD
6
6
  License-File: LICENSE
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "racs-tools"
3
- version = "4.3.2" # A placeholder
3
+ version = "4.4.0" # A placeholder
4
4
  description = "Useful scripts for RACS."
5
5
  authors = ["Alec Thomson"]
6
6
  license = "BSD"
@@ -7,6 +7,7 @@ import gc
7
7
  from typing import Literal, NamedTuple
8
8
 
9
9
  import numpy as np
10
+ import scipy.fft
10
11
  import scipy.signal
11
12
  from astropy import convolution, units
12
13
  from astropy import units as u
@@ -177,22 +178,39 @@ def convolve(
177
178
  ###
178
179
 
179
180
  # Now we do the convolution
180
- nanflag = np.isnan(image).any()
181
+ nan_mask = np.isnan(image)
182
+ nanflag = nan_mask.any()
181
183
 
182
184
  if nanflag:
183
- # Create a mask for the NaNs
184
- mask = np.isnan(image).astype(np.int16)
185
185
  logger.warning(
186
- f"Image contains {mask.sum()} ({mask.sum() / mask.size * 100:0.1f}%) NaNs"
186
+ f"Image contains {nan_mask.sum()} "
187
+ f"({nan_mask.sum() / nan_mask.size * 100:0.1f}%) NaNs"
187
188
  )
188
189
  image = np.nan_to_num(image)
190
+ else:
191
+ del nan_mask
189
192
 
190
193
  nx = image.shape[0]
191
194
  ny = image.shape[1]
192
195
 
193
- # The coordinates in FT domain:
194
- u_image = np.fft.fftfreq(nx, d=dx.to(units.rad).value).astype(np.complex64)
195
- v_image = np.fft.fftfreq(ny, d=dy.to(units.rad).value).astype(np.complex64)
196
+ # The image is real, so half of its spectrum is redundant. Transforming in
197
+ # the image's own precision and keeping only that half is what makes this
198
+ # affordable on a wide image: a single-precision plane never gets promoted
199
+ # to complex128, and the last axis is stored once rather than twice.
200
+ #
201
+ # Matched on kind and width rather than identity, because FITS data arrives
202
+ # big-endian (">f4") and `np.dtype(">f4") == np.float32` is False, which
203
+ # would send every real image down the double-precision path.
204
+ real_dtype = (
205
+ np.float32
206
+ if image.dtype.kind == "f" and image.dtype.itemsize <= 4
207
+ else np.float64
208
+ )
209
+
210
+ # The coordinates in FT domain. Only the non-redundant v are needed, to
211
+ # match the output of the real-input transform below.
212
+ u_image = np.fft.fftfreq(nx, d=dx.to(units.rad).value).astype(real_dtype)
213
+ v_image = np.fft.rfftfreq(ny, d=dy.to(units.rad).value).astype(real_dtype)
196
214
 
197
215
  [g_final, g_ratio] = gaussft.gaussft(
198
216
  bmin_in=old_beam.minor.to(units.deg).value,
@@ -204,40 +222,34 @@ def convolve(
204
222
  u=u_image,
205
223
  v=v_image,
206
224
  )
207
- g_final = g_final.astype(np.complex64)
208
225
  del u_image
209
226
  del v_image
210
227
 
211
- # Perform the x-ing in the FT domain
212
- im_f = np.fft.fft2(image).astype(np.complex64)
228
+ # Perform the x-ing in the FT domain. The taper is applied in place, and
229
+ # the spectrum is consumed by its own inverse, so neither is duplicated.
230
+ im_f = scipy.fft.rfft2(image.astype(real_dtype, copy=False))
213
231
  del image
214
-
215
- # Now convolve with the desired Gaussian:
216
- M = np.multiply(im_f, g_final).astype(np.complex64)
232
+ im_f *= g_final
233
+ im_conv = scipy.fft.irfft2(im_f, s=(nx, ny), overwrite_x=True).astype(
234
+ np.float32, copy=False
235
+ )
217
236
  del im_f
218
- im_conv = np.fft.ifft2(M).astype(np.complex64)
219
- im_conv = np.real(im_conv).astype(np.float32)
220
- del M
221
237
 
222
238
  if nanflag:
223
- # Convert the mask to the FT domain
224
- mask_f = np.fft.fft2(mask).astype(np.complex64)
225
- del mask
226
- # Multiply the mask by the FT of the Gaussian
227
- M = np.multiply(mask_f, g_final).astype(np.complex64)
239
+ # Convolve the NaN mask the same way, to find where the NaNs spread to
240
+ mask_f = scipy.fft.rfft2(nan_mask.astype(real_dtype))
241
+ del nan_mask
242
+ mask_f *= g_final
228
243
  del g_final
244
+ mask_conv = scipy.fft.irfft2(mask_f, s=(nx, ny), overwrite_x=True)
229
245
  del mask_f
230
- # Invert the FT of the mask
231
- mask_conv = np.fft.ifft2(M).astype(np.complex64)
232
- mask_conv = np.real(mask_conv).astype(np.float32)
233
- del M
234
246
  # Use approx values to find the NaNs
235
247
  # Need this to get around numerical issues
236
248
  mask_conv = ~(mask_conv + 1 < 2)
237
249
  logger.warning(
238
250
  f"Convolved image contains {mask_conv.sum()} ({mask_conv.sum() / mask_conv.size * 100:0.1f}%) NaNs"
239
251
  )
240
- im_conv[mask_conv > 0] = np.nan
252
+ im_conv[mask_conv] = np.nan
241
253
 
242
254
  return ConvolutionResult(image=im_conv, scaling_factor=g_ratio)
243
255
 
@@ -25,6 +25,10 @@ def gaussft(
25
25
  """
26
26
  Compute the Fourier transform of a 2D Gaussian for convolution.
27
27
 
28
+ The returned taper is real and takes the dtype of ``u`` and ``v``, so a
29
+ single-precision grid gives a single-precision taper. It is filled in one
30
+ pass, meaning only the taper itself is ever allocated at image size.
31
+
28
32
  Parameters:
29
33
  bmin_in (float): Intrinsic psf BMIN (degrees)
30
34
  bmaj_in (float): Intrinsic psf BMAJ (degrees)
@@ -58,28 +62,29 @@ def gaussft(
58
62
  bmin_in_rad / (2.0 * np.sqrt(2.0 * np.log(2.0))),
59
63
  )
60
64
 
61
- u_cosbpa, u_sinbpa = u * np.cos(bpa_rad), u * np.sin(bpa_rad)
62
- u_cosbpa_in, u_sinbpa_in = u * np.cos(bpa_in_rad), u * np.sin(bpa_in_rad)
63
-
64
- v_cosbpa, v_sinbpa = v * np.cos(bpa_rad), v * np.sin(bpa_rad)
65
- v_cosbpa_in, v_sinbpa_in = v * np.cos(bpa_in_rad), v * np.sin(bpa_in_rad)
66
-
67
65
  g_amp = np.sqrt(2.0 * np.pi * sx * sy)
68
66
 
69
67
  dg_amp = np.sqrt(2.0 * np.pi * sx_in * sy_in)
70
68
 
71
69
  g_ratio = g_amp / dg_amp
72
70
 
73
- # Vectorized calculation of ur, vr, g_arg, and dg_arg
74
- ur = u_cosbpa[:, np.newaxis] - v_sinbpa[np.newaxis, :]
75
- vr = u_sinbpa[:, np.newaxis] + v_cosbpa[np.newaxis, :]
76
- g_arg = -2.0 * np.pi**2 * ((sx * ur) ** 2 + (sy * vr) ** 2)
71
+ cos_bpa, sin_bpa = np.cos(bpa_rad), np.sin(bpa_rad)
72
+ cos_bpa_in, sin_bpa_in = np.cos(bpa_in_rad), np.sin(bpa_in_rad)
73
+ two_pi_sq = 2.0 * np.pi**2
74
+
75
+ g_final = np.empty((u.size, v.size), dtype=u.dtype)
76
+ for i in range(u.size):
77
+ u_cosbpa, u_sinbpa = u[i] * cos_bpa, u[i] * sin_bpa
78
+ u_cosbpa_in, u_sinbpa_in = u[i] * cos_bpa_in, u[i] * sin_bpa_in
79
+ for j in range(v.size):
80
+ ur = u_cosbpa - v[j] * sin_bpa
81
+ vr = u_sinbpa + v[j] * cos_bpa
82
+ g_arg = -two_pi_sq * ((sx * ur) ** 2 + (sy * vr) ** 2)
77
83
 
78
- ur_in = u_cosbpa_in[:, np.newaxis] - v_sinbpa_in[np.newaxis, :]
79
- vr_in = u_sinbpa_in[:, np.newaxis] + v_cosbpa_in[np.newaxis, :]
80
- dg_arg = -2.0 * np.pi**2 * ((sx_in * ur_in) ** 2 + (sy_in * vr_in) ** 2)
84
+ ur_in = u_cosbpa_in - v[j] * sin_bpa_in
85
+ vr_in = u_sinbpa_in + v[j] * cos_bpa_in
86
+ dg_arg = -two_pi_sq * ((sx_in * ur_in) ** 2 + (sy_in * vr_in) ** 2)
81
87
 
82
- # Vectorized calculation of g_final
83
- g_final = g_ratio * np.exp(g_arg - dg_arg)
88
+ g_final[i, j] = g_ratio * np.exp(g_arg - dg_arg)
84
89
 
85
90
  return g_final, g_ratio
File without changes
File without changes
File without changes