Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_error_map_source(self):
sourceModel = LightModel(light_model_list=['UNIFORM', 'UNIFORM'])
kwargs_data = sim_util.data_configure_simple(numPix=10, deltaPix=1, exposure_time=1, background_rms=1)
data_class = ImageData(**kwargs_data)
psf_type = "GAUSSIAN"
fwhm = 0.9
kwargs_psf = {'psf_type': psf_type, 'fwhm': fwhm}
psf_class = PSF(**kwargs_psf)
imageModel = ImageLinearFit(data_class=data_class, psf_class=psf_class, lens_model_class=None,
point_source_class=None, source_model_class=sourceModel)
x_grid, y_grid = util.make_grid(numPix=10, deltapix=1)
error_map = imageModel.error_map_source(kwargs_source=[{'amp': 1}, {'amp': 1}], x_grid=x_grid, y_grid=y_grid, cov_param=np.array([[1, 0], [0, 1]]))
assert error_map[0] == 2
# data specifics
sigma_bkg = 0.05 # background noise per pixel
exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit)
numPix = 10 # cutout pixel size
deltaPix = 0.5 # pixel size in arcsec (area per pixel = deltaPix**2)
fwhm = 0.5 # full width half max of PSF
# PSF specification
self.kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg)
data_class = ImageData(**self.kwargs_data)
kwargs_psf_gaussian = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'truncation': 5, 'pixel_size': deltaPix}
psf_gaussian = PSF(**kwargs_psf_gaussian)
self.kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': psf_gaussian.kernel_point_source}
psf_class = PSF(**self.kwargs_psf)
# 'EXERNAL_SHEAR': external shear
kwargs_shear = {'gamma1': 0.01, 'gamma2': 0.01} # gamma_ext: shear strength, psi_ext: shear angel (in radian)
e1, e2 = param_util.phi_q2_ellipticity(0.2, 0.8)
kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': e1, 'e2': e2}
lens_model_list = ['SPEP', 'SHEAR']
self.kwargs_lens = [kwargs_spemd, kwargs_shear]
lens_model_class = LensModel(lens_model_list=lens_model_list)
self.LensModel = lens_model_class
# list of light profiles (for lens and source)
# 'SERSIC': spherical Sersic profile
kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0}
# 'SERSIC_ELLIPSE': elliptical Sersic profile
phi, q = 0.2, 0.9
e1, e2 = param_util.phi_q2_ellipticity(phi, q)
npt.assert_almost_equal(np.sum(kernel_point_source), 1, decimal=8)
deltaPix = 0.05 # pixel size of image
numPix = 40 # number of pixels per axis
subsampling_res = 4 # subsampling scale factor (in each dimension)
fwhm = 0.3 # FWHM of the PSF kernel
fwhm_object = 0.2 # FWHM of the Gaussian source to be convolved
# create Gaussian/Pixelized kernels
# first we create the sub-sampled kernel
kernel_point_source_subsampled = kernel_util.kernel_gaussian(kernel_numPix=11 * subsampling_res + 1,
deltaPix=deltaPix / subsampling_res, fwhm=fwhm)
kwargs_pixel_subsampled = {'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source_subsampled,
'point_source_supersampling_factor': subsampling_res}
psf_pixel_subsampled = PSF(**kwargs_pixel_subsampled)
kernel_point_source /= np.sum(kernel_point_source)
kwargs_pixel = {'psf_type': 'PIXEL',
'kernel_point_source': kernel_point_source}
psf_pixel = PSF(**kwargs_pixel)
kernel_point_source = psf_pixel.kernel_point_source
kernel_point_source_new = psf_pixel_subsampled.kernel_point_source
npt.assert_almost_equal(np.sum(kernel_point_source), np.sum(kernel_point_source_new), decimal=8)
npt.assert_almost_equal(np.sum(kernel_point_source), 1, decimal=8)
psf_none = PSF(psf_type='NONE')
kernel_super = psf_none.kernel_point_source_supersampled(supersampling_factor=5)
npt.assert_almost_equal(kernel_super, psf_none.kernel_point_source, decimal=9)
def test_force_positive_source_surface_brightness(self):
kwargs_likelihood = {'force_minimum_source_surface_brightness': True}
kwargs_model = {'source_light_model_list': ['SERSIC']}
kwargs_constraints = {}
param_class = Param(kwargs_model, **kwargs_constraints)
kwargs_data = sim_util.data_configure_simple(numPix=10, deltaPix=0.1, exposure_time=1, background_rms=0.1)
data_class = ImageData(**kwargs_data)
kwargs_psf = {'psf_type': 'NONE'}
psf_class = PSF(**kwargs_psf)
kwargs_sersic = {'amp': -1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0}
source_model_list = ['SERSIC']
kwargs_source = [kwargs_sersic]
source_model_class = LightModel(light_model_list=source_model_list)
imageModel = ImageModel(data_class, psf_class, lens_model_class=None, source_model_class=source_model_class)
image_sim = sim_util.simulate_simple(imageModel, [], kwargs_source)
kwargs_data['image_data'] = image_sim
kwargs_data_joint = {'multi_band_list': [[kwargs_data, kwargs_psf, {}]], 'multi_band_type': 'single-band'}
likelihood = LikelihoodModule(kwargs_data_joint=kwargs_data_joint, kwargs_model=kwargs_model, param_class=param_class, **kwargs_likelihood)
logL = likelihood.logL(args=param_class.kwargs2args(kwargs_source=kwargs_source), verbose=True)
assert logL <= -10**10
def test_psf_error_map(self):
deltaPix = 1.
fwhm = 5.6
kwargs = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'truncation': 5, 'pixel_size': deltaPix}
psf_kernel = PSF(**kwargs)
error_map = psf_kernel.psf_error_map
assert error_map.all() == 0
# data specifics
sigma_bkg = 0.05 # background noise per pixel
exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit)
numPix = 10 # cutout pixel size
deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2)
fwhm = 0.5 # full width half max of PSF
# PSF specification
kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg)
data_class = ImageData(**kwargs_data)
kwargs_psf_gaussian = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix}
psf = PSF(**kwargs_psf_gaussian)
kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': psf.kernel_point_source}
psf_class = PSF(**kwargs_psf)
kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1}
lens_model_list = ['SPEP']
self.kwargs_lens = [kwargs_spemd]
lens_model_class = LensModel(lens_model_list=lens_model_list)
kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0}
# 'SERSIC_ELLIPSE': elliptical Sersic profile
kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0,
'e1': 0.1, 'e2': 0.1}
lens_light_model_list = ['SERSIC']
self.kwargs_lens_light = [kwargs_sersic]
lens_light_model_class = LightModel(light_model_list=lens_light_model_list)
source_model_list = ['SERSIC_ELLIPSE']
self.kwargs_source = [kwargs_sersic_ellipse]
source_model_class = LightModel(light_model_list=source_model_list)
def setup(self):
# data specifics
sigma_bkg = 0.05 # background noise per pixel
exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit)
numPix = 10 # cutout pixel size
deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2)
fwhm = 0.5 # full width half max of PSF
# PSF specification
kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg)
data_class = ImageData(**kwargs_data)
kwargs_psf_gaussian = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix}
psf = PSF(**kwargs_psf_gaussian)
kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': psf.kernel_point_source}
psf_class = PSF(**kwargs_psf)
kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1}
lens_model_list = ['SPEP']
self.kwargs_lens = [kwargs_spemd]
lens_model_class = LensModel(lens_model_list=lens_model_list)
kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0}
# 'SERSIC_ELLIPSE': elliptical Sersic profile
kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0,
'e1': 0.1, 'e2': 0.1}
lens_light_model_list = ['SERSIC']
self.kwargs_lens_light = [kwargs_sersic]
lens_light_model_class = LightModel(light_model_list=lens_light_model_list)
source_model_list = ['SERSIC_ELLIPSE']
# data specifics
sigma_bkg = 0.05 # background noise per pixel
exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit)
numPix = 10 # cutout pixel size
deltaPix = 0.05 # pixel size in arcsec (area per pixel = deltaPix**2)
fwhm = 0.5 # full width half max of PSF
# PSF specification
self.kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg)
data_class = ImageData(**self.kwargs_data)
kwargs_psf_gaussian = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix, 'truncation': 3}
psf_gaussian = PSF(**kwargs_psf_gaussian)
self.kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': psf_gaussian.kernel_point_source, 'psf_error_map': np.zeros_like(psf_gaussian.kernel_point_source)}
psf_class = PSF(**self.kwargs_psf)
# 'EXTERNAL_SHEAR': external shear
kwargs_shear = {'gamma1': 0.01, 'gamma2': 0.01} # gamma_ext: shear strength, psi_ext: shear angel (in radian)
kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1}
lens_model_list = ['SPEP', 'SHEAR']
self.kwargs_lens = [kwargs_spemd, kwargs_shear]
lens_model_class = LensModel(lens_model_list=lens_model_list)
kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0}
# 'SERSIC_ELLIPSE': elliptical Sersic profile
kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0,
'e1': 0.1, 'e2': 0.1}
lens_light_model_list = ['SERSIC']
self.kwargs_lens_light = [kwargs_sersic]
lens_light_model_class = LightModel(light_model_list=lens_light_model_list)
source_model_list = ['SERSIC_ELLIPSE']
# data specifics
sigma_bkg = 0.05 # background noise per pixel
exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit)
numPix = 10 # cutout pixel size
deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2)
fwhm = 0.5 # full width half max of PSF
# PSF specification
kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, exp_time, sigma_bkg)
data_class = ImageData(**kwargs_data)
kwargs_psf_gaussian = {'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix}
psf = PSF(**kwargs_psf_gaussian)
kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': psf.kernel_point_source}
psf_class = PSF(**kwargs_psf)
kwargs_spemd = {'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1}
lens_model_list = ['SPEP']
self.kwargs_lens = [kwargs_spemd]
lens_model_class = LensModel(lens_model_list=lens_model_list)
kwargs_sersic = {'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0}
# 'SERSIC_ELLIPSE': elliptical Sersic profile
kwargs_sersic_ellipse = {'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0,
'e1': 0.1, 'e2': 0.1}
lens_light_model_list = ['SERSIC']
self.kwargs_lens_light = [kwargs_sersic]
lens_light_model_class = LightModel(light_model_list=lens_light_model_list)
source_model_list = ['SERSIC_ELLIPSE']
self.kwargs_source = [kwargs_sersic_ellipse]
source_model_class = LightModel(light_model_list=source_model_list)
def test_raise(self):
psf = PSF(psf_type='PIXEL', kernel_point_source=np.ones((3, 3)))
psf.psf_type = 'WRONG'
with self.assertRaises(ValueError):
PSF(psf_type='GAUSSIAN')
with self.assertRaises(ValueError):
PSF(psf_type='PIXEL')
with self.assertRaises(ValueError):
PSF(psf_type='PIXEL', kernel_point_source=np.ones((2, 2)))
with self.assertRaises(ValueError):
PSF(psf_type='WRONG')
with self.assertRaises(ValueError):
PSF(psf_type='PIXEL', kernel_point_source=np.ones((3, 3)), psf_error_map=np.ones((5, 5)))
psf.kernel_point_source_supersampled(supersampling_factor=3)
with self.assertRaises(ValueError):
psf = PSF(psf_type='PIXEL', kernel_point_source=np.ones((3, 3)))
psf.psf_type = 'WRONG'
psf.kernel_point_source_supersampled(supersampling_factor=3)
with self.assertRaises(ValueError):
psf = PSF(psf_type='GAUSSIAN', fwhm=100, pixel_size=0.0001)
psf.kernel_point_source_supersampled(supersampling_factor=3)
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger a warning.
kernel_point_source_subsampled = np.ones((9, 9))