Skip to content

Feature/psf convolution refactor#218

Merged
Jammy2211 merged 19 commits intomainfrom
feature/psf_convolution_refactor
Feb 23, 2026
Merged

Feature/psf convolution refactor#218
Jammy2211 merged 19 commits intomainfrom
feature/psf_convolution_refactor

Conversation

@Jammy2211
Copy link
Owner

This pull request refactors how Point Spread Function (PSF) convolution is handled in the autoarray package, replacing the previous Kernel2D implementation with a new Convolver class. The changes simplify and modernize the PSF handling, remove legacy padding logic, and update interfaces throughout the dataset and simulator modules.

PSF and convolution refactor:

  • Replaced all instances of Kernel2D with Convolver throughout the codebase, including imports, class attributes, and function parameters in autoarray/dataset/imaging/dataset.py, autoarray/dataset/imaging/simulator.py, and autoarray/dataset/grids.py. [1] [2] [3] [4]
  • Updated PSF normalization and state management logic to use ConvolverState and explicitly normalize the kernel array, improving clarity and control over PSF setup.
  • Refactored methods to use Convolver's kernel and state directly, including changes to FITS input/output and simulator defaults. [1] [2] [3]

Removal of legacy padding logic:

  • Removed all code and parameters related to FFT padding (disable_fft_pad) in dataset initialization and related methods, simplifying the interface and removing unnecessary complexity. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]

Interface and method updates:

  • Updated method signatures and docstrings to reflect the removal of disable_fft_pad and the transition to Convolver, ensuring consistency and clarity for users. [1] [2]
  • Changed internal logic to reference Convolver's kernel and state in sparse operator and blurring grid calculations. [1] [2] [3]

These changes modernize the PSF handling, streamline the codebase, and make the convolution logic more robust and maintainable.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors Point Spread Function (PSF) convolution handling in the autoarray package by replacing the Kernel2D class with a new Convolver class that separates kernel data from convolution logic. The refactoring aims to simplify PSF handling by introducing a ConvolverState class to manage FFT-related state and removing legacy padding parameters.

Changes:

  • Replaced Kernel2D with a new Convolver class that wraps an Array2D kernel and manages convolution operations
  • Introduced ConvolverState to encapsulate FFT-related precomputed values (shapes, masks, FFT kernels)
  • Removed disable_fft_pad parameter from Imaging dataset and related methods throughout the codebase

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
autoarray/operators/convolver.py Complete refactoring from Kernel2D to Convolver and ConvolverState classes
autoarray/dataset/imaging/dataset.py Updated to use Convolver, removed disable_fft_pad logic, added psf_setup_state parameter
autoarray/dataset/imaging/simulator.py Changed PSF type from Kernel2D to Convolver
autoarray/dataset/grids.py Updated to use Convolver and access blurring mask through state
autoarray/dataset/preprocess.py Moved kernel rescaling logic from Kernel2D method to standalone function
test_autoarray/structures/arrays/test_kernel_2d.py File deleted (530 lines removed)
test_autoarray/operators/test_convolver.py New test file with migrated tests (315 lines added)
test_autoarray/inversion/inversion/test_abstract.py Updated tests to create Convolver from Array2D kernel
test_autoarray/dataset/imaging/test_dataset.py Updated assertions to access psf.kernel instead of psf directly
autoarray/init.py Changed import from Kernel2D to Convolver
Comments suppressed due to low confidence (8)

autoarray/operators/convolver.py:111

  • The docstring states "The Convolver is a subclass of Array2D" but Convolver is not actually a subclass of Array2D - it simply contains a kernel which is an Array2D. This misleading documentation should be corrected to accurately describe that Convolver wraps or contains an Array2D kernel.
    autoarray/operators/convolver.py:167
  • The error message contains a redundant duplicate "Convolver Convolver" instead of just "Convolver kernel". This should be corrected to "Convolver kernel must be odd" for clarity.
    autoarray/operators/convolver.py:44
  • Typo in documentation: "thi sfunction" should be "this function".
    autoarray/structures/grids/uniform_2d.py:1089
  • The error message contains a redundant duplicate "Convolver Convolver" instead of just "Convolver kernel". This should be corrected to "Convolver kernel must be odd" for clarity.
            raise exc.KernelException("Convolver Convolver must be odd")

autoarray/operators/convolver.py:160

  • When kernel is None and normalize is True, this line will raise an AttributeError trying to access self.kernel._array. Additionally, when kernel is None and use_fft is False, line 164 will raise an AttributeError trying to access self.kernel.shape_native. The Convolver should handle None kernel gracefully or the calling code should ensure kernel is never None.
    autoarray/operators/convolver.py:135
  • The docstring mentions a "mask" parameter that no longer exists in the Convolver constructor signature. The documentation should be updated to remove references to the removed "mask" parameter and the removed "header" parameter.
    autoarray/operators/convolver.py:143
  • The docstring mentions "mask" and "header" parameters (lines 131-135) that no longer exist in the Convolver constructor signature. Additionally, the "*args, **kwargs" reference on line 142 suggests these are passed to an Array2D constructor, but Convolver is no longer a subclass of Array2D. The documentation should be updated to remove these outdated parameter descriptions.
    autoarray/operators/convolver.py:167
  • The condition if not use_fft: will evaluate to True when use_fft is None (the default value), causing the odd-dimension check to be enforced even when FFT convolution is intended. This check should only apply when real-space convolution is explicitly requested. The condition should be if use_fft is False: to distinguish between False (explicitly real-space) and None (use config default, which may be FFT).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +221 to +223
psf = Convolver(
kernel=kernel,
)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When psf_path is None, kernel is set to None and passed to the Convolver constructor. However, Convolver.__init__ expects a non-None kernel parameter (it immediately accesses kernel._array if normalize is True). This will cause an AttributeError when creating an Imaging dataset without a PSF. A None check should be added, or the Convolver creation should be conditional on whether kernel is None.

Copilot uses AI. Check for mistakes.
@@ -95,7 +95,7 @@ def __init__(
psf = psf.normalized
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Convolver class does not have a normalized property or method, but line 95 attempts to access it. This will cause an AttributeError. The normalization should be handled differently, perhaps by passing normalize=True to the Convolver constructor or by calling a normalization method if one exists.

Copilot uses AI. Check for mistakes.
[[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], pixel_scales=1.0
)

psf = aa.Convolver(kernel=kernel, pixel_scales=1.0)
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Convolver constructor does not accept a pixel_scales parameter, but line 195 attempts to pass it. This will cause a TypeError. The pixel_scales should be removed from this call, as the kernel already contains pixel scale information.

Suggested change
psf = aa.Convolver(kernel=kernel, pixel_scales=1.0)
psf = aa.Convolver(kernel=kernel)

Copilot uses AI. Check for mistakes.
except exc.MaskException:
self._blurring = None
self._blurring = Grid2D.from_mask(
mask=self.psf._state.blurring_mask,
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing self.psf._state.blurring_mask directly assumes that _state has already been initialized. However, _state is None by default in the Convolver and is only set up when the Imaging constructor is called with psf_setup_state=True (which only happens in apply_mask). When Imaging is created in other contexts (e.g., from_fits, simulator), _state will be None, causing an AttributeError. The code should call self.psf.state_from(self.mask) instead to ensure the state is created if it doesn't exist.

Suggested change
mask=self.psf._state.blurring_mask,
mask=self.psf.state_from(self.mask).blurring_mask,

Copilot uses AI. Check for mistakes.
fft_shape=fft_shape,
)
psf = Convolver(
kernel=psf.kernel, state=state, normalize=use_normalized_psf
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The kernel is normalized twice when psf_setup_state=True and use_normalized_psf=True. First at line 119-121 by directly modifying psf.kernel._array, then again at line 128 by passing normalize=use_normalized_psf to the Convolver constructor. This double normalization will produce incorrect kernel values. Either remove the normalization at lines 119-121 and rely on the Convolver constructor's normalize parameter, or don't pass normalize=use_normalized_psf to the constructor.

Suggested change
kernel=psf.kernel, state=state, normalize=use_normalized_psf
kernel=psf.kernel, state=state, normalize=False

Copilot uses AI. Check for mistakes.
@Jammy2211 Jammy2211 merged commit 2220d15 into main Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants