slitronomy.Util package

Submodules

slitronomy.Util.metrics_util module

slitronomy.Util.metrics_util.QOR(truth, model, noise_map)

computes Quality Of Reconstruction

slitronomy.Util.metrics_util.SDR(truth, model)

computes Source Distortion Ratio

slitronomy.Util.metrics_util.SSIM(truth, model)

computes Structure Similarity Index

slitronomy.Util.metrics_util.chi2_nu(normalized_residuals, num_data_points)

computes the reduced chi2 from a normalized residual map

slitronomy.Util.metrics_util.chi2_threshold(normalized_residuals, sigma_threshold=3)

computes the reduced chi2 from a normalized residual map

slitronomy.Util.metrics_util.total_mag(flux_map, zero_point)

slitronomy.Util.plot_util module

class slitronomy.Util.plot_util.ReNormColormapAdaptor(base, cmap_norm, orig_norm=None)

Bases: matplotlib.colors.Colormap

Colormap adaptor that uses another Normalize instance for the colormap than applied to the mappable.

credits : https://stackoverflow.com/questions/26253947/logarithmic-colormap-in-matplotlib

slitronomy.Util.plot_util.log_cmap(cmap_name, vmin, vmax)
slitronomy.Util.plot_util.nice_colorbar(mappable, position='right', pad=0.1, size='5%', label=None, fontsize=12, invisible=False, divider_kwargs={}, colorbar_kwargs={}, label_kwargs={})
slitronomy.Util.plot_util.nice_colorbar_residuals(mappable, res_map, vmin, vmax, position='right', pad=0.1, size='5%', invisible=False, label=None, fontsize=16)
slitronomy.Util.plot_util.std_colorbar(mappable, label=None, fontsize=12, label_kwargs={}, **colorbar_kwargs)
slitronomy.Util.plot_util.std_colorbar_residuals(mappable, res_map, vmin, vmax, label=None, fontsize=12, label_kwargs={}, **colorbar_kwargs)

slitronomy.Util.solver_plotter module

class slitronomy.Util.solver_plotter.SolverPlotter(solver_class, show_now=True)

Bases: object

plot_final(image)
plot_init(image)
plot_results(log_scale=False, vmin_image=None, vmax_image=None, normalised_res=True, vmin_source=None, vmax_source=None, vmin_res=-6, vmax_res=6, cmap_image=None, cmap_source=None, cmap_residuals=None, fontsize=12, with_history=True, unconvolved=False, point_source_add=False)
static plot_source_residuals_comparison(source_truth, source_model_list, name_list, vmin_res=-0.5, vmax_res=-0.5, cmap='cubehelix', fontsize=12)

given a true source, plot residuals of a list of source model

plot_step(image, iter_1, iter_2=None, iter_3=None)
static quick_imshow(image, title=None, show_now=False, **kwargs)

slitronomy.Util.solver_tracker module

class slitronomy.Util.solver_tracker.SolverTracker(solver_class, verbose=False)

Bases: object

finalize()

convert to numpy array for practicality / plots

init()
save(S=None, S_next=None, HG=None, HG_next=None, P=None, print_bool=False, iteration_text=None)
track

slitronomy.Util.util module

slitronomy.Util.util.Downsample(image, factor=1)

resizes image with nx x ny to nx/factor x ny/factor :param image: 2d image with shape (nx,ny) :param factor: integer >=1 :return:

slitronomy.Util.util.Upsample(image, factor=1)
slitronomy.Util.util.array2cube(array, n_1, n_23)
slitronomy.Util.util.array2image(array, nx=0, ny=0)

returns the information contained in a 1d array into an n*n 2d array (only works when lenght of array is n**2) Credits to S. Birrer (lenstronomy)

Parameters:array (array of size n**2) – image values
Returns:2d array
Raises:AttributeError, KeyError
slitronomy.Util.util.cube2array(cube)
slitronomy.Util.util.dirac_impulse(num_pix)

returns the 2d array of a Dirac impulse at the center of the image

Returns:2d numpy array
slitronomy.Util.util.exponential_decrease(curr_value, init_value, min_value, num_iter, num_iter_at_min_value)

Computes a exponentially decreasing value, for a given loop index, starting at a specified value.

Parameters:
  • curr_value (float) – Current value to be updated
  • init_value (float) – Value at iteration 0.
  • min_value (float) – Minimum value, reached at iteration num_iter - num_iter_at_min_value - 1.
  • num_iter (int) – Total number of iterations.
  • num_iter_at_min_value (int) – Number of iteration for which the returned value equals min_value.
Returns:

Exponentially decreased value.

Return type:

float

Raises:

ValueError – If num_iter - num_iter_at_min_value < 1, cannot compute the value.

slitronomy.Util.util.generate_initial_guess(num_pix, n_scales, transform, inverse_transform, formulation='analysis', guess_type='background_rms', background_rms=None, noise_map=None, noise_map_synthesis=None, seed=None)

Generates a random image and its transform for sparse optimization initialisation. This supports both analysis and synthesis types of initial guess.

Parameters:
  • num_pix (int) – Number of side pixels.
  • n_scales (int) – Number of decomposition scales, consistent with the transform operator.
  • transform (callable) – Operator (e.g. wavelet transform) for transformed random guess.
  • inverse_transform (callable) – Inverse operator (e.g. wavelet inverse transform) for direct space random guess.
  • formulation (str, 'analysis') – ‘analysis’ generates the random image in direct space, then transforms it. ‘synthesis’ generates the random coefficients in transformed space, then inverse transforms it for direct space image.
  • guess_type (str, 'background_rms') – ‘background_rms’ uses the background noise RMS value for random image generate. ‘noise_map’ uses the whole noise (diagonal) covariance for random image generate.
  • background_rms (float, None) – Background noise RMS value. Required if guess_type is ‘background_rms’
  • noise_map (array_like, None) – Noise map, diagonal noise covariance per pixel. Required if guess_type is ‘noise_map’ and formulation is ‘analysis’.
  • noise_map_synthesis (array_like, None) – Diagonal noise covariance per pixel, in transformed space. Required if guess_type is ‘noise_map’ and formulation is ‘synthesis’.
Returns:

  • array_like – Random image.
  • array_like – Transform of the above array, after call to the transform callable.

slitronomy.Util.util.generate_initial_guess_simple(num_pix, transform, background_rms, seed=None)

Generates a random image and its transform for sparse optimization initialisation.

Parameters:
  • num_pix (int) – Number of side pixels.
  • transform (callable) – Operator (e.g. wavelet transform) for transformed random image.
  • background_rms (float) – Background noise RMS value.
Returns:

  • array_like – Random image, normal distribution with std dev background_rms.
  • array_like – Transform of the above array, after call to the transform callable.

slitronomy.Util.util.hard_threshold(array, thresh)
slitronomy.Util.util.image2array(image)

returns the information contained in a 2d array into an n*n 1d array Credits to S. Birrer (lenstronomy)

Parameters:array (array of size (n,n)) – image values
Returns:1d array
Raises:AttributeError, KeyError
slitronomy.Util.util.index_1d_to_2d(i, num_pix)
slitronomy.Util.util.index_2d_to_1d(x, y, num_pix)
slitronomy.Util.util.linear_decrease(curr_value, init_value, min_value, num_iter, num_iter_at_min_value)

Computes a linearly decreasing value, for a given loop index, starting at a specified value.

Parameters:
  • curr_value (float) – Current value to be updated
  • init_value (float) – Value at iteration 0.
  • min_value (float) – Minimum value, reached at iteration num_iter - num_iter_at_min_value - 1.
  • num_iter (int) – Total number of iterations.
  • num_iter_at_min_value (int) – Number of iteration for which the returned value equals min_value.
Returns:

Linearly decreased value.

Return type:

float

Raises:

ValueError – If num_iter - num_iter_at_min_value < 1, cannot compute the value.

slitronomy.Util.util.make_grid(numPix, deltapix, subgrid_res=1, left_lower=False)

Credits to S. Birrer (lenstronomy)

Parameters:
  • numPix – number of pixels per axis
  • deltapix – pixel size
  • subgrid_res – sub-pixel resolution (default=1)
Returns:

x, y position information in two 1d arrays

slitronomy.Util.util.regridding_error_map_squared(mag_map=None, data_image=None, image_pixel_scale=None, source_pixel_scale=None, noise_map2_prefactor=None)

Computes the regridding error map as defined in Suyu et al. 2009 (https://ui.adsabs.harvard.edu/abs/2009ApJ…691..277S/abstract) The error is a 2D array corresponding to the noise variance sigma^2 per pixel.

Parameters:
  • mag_map (array_like, optional) – Magnification map, as a 2D array.
  • data_image (array_like, optional) – Imaging data, as a 2D array.
  • image_pixel_scale (float, optional) – Pixel scale of image plane grid.
  • source_pixel_scale (None, optional) – Pixel scale of source plane grid.
  • noise_map2_prefactor (None, optional) – Prefactor pre-computed. If provided, this won’t be computed again. This is the second returned array of this function.
Returns:

  • array_like – Full regridding error map.
  • array_like – Part of the map that is independent of the magnification, for speedup next call to the function.

slitronomy.Util.util.soft_threshold(array, thresh)
slitronomy.Util.util.spectral_norm(num_pix, operator, inverse_operator, num_iter=20, tol=1e-10, seed=None)

compute spectral norm from operator and its inverse

Sometimes referred to as the Lipschitz constant, it is essentially the greatest singular value of a matrix/operator (in this case the Starlet transform operator) This is particularly important for determining the optimal amplitude of gradient descent step in a minimization problem.

See e.g. http://fourier.eng.hmc.edu/e161/lectures/algebra/node12.html

slitronomy.Util.util.starlet_transorm(array, num_scales)

Module contents