def ext_shear_direction(data_class, lens_model_class, kwargs_lens, strength_multiply=10): """ :param kwargs_data: :param kwargs_psf: :param kwargs_options: :param lens_result: :param source_result: :param lens_light_result: :param else_result: :return: """ x_grid, y_grid = data_class.coordinates shear = ExternalShear() f_x_shear, f_y_shear = 0, 0 for i, lens_model in enumerate(lens_model_class.lens_model_list): if lens_model == 'SHEAR': kwargs = kwargs_lens[i] f_x_shear, f_y_shear = shear.derivatives( x_grid, y_grid, e1=kwargs['e1'] * strength_multiply, e2=kwargs['e2'] * strength_multiply) x_shear = x_grid - f_x_shear y_shear = y_grid - f_y_shear f_x_foreground, f_y_foreground = 0, 0 for i, lens_model in enumerate(lens_model_class.lens_model_list): if lens_model == 'FOREGROUND_SHEAR': kwargs = kwargs_lens[i] f_x_foreground, f_y_foreground = shear.derivatives( x_grid, y_grid, e1=kwargs['e1'] * strength_multiply, e2=kwargs['e2'] * strength_multiply) x_foreground = x_grid - f_x_foreground y_foreground = y_grid - f_y_foreground center_x = np.mean(x_grid) center_y = np.mean(y_grid) radius = (np.max(x_grid) - np.min(x_grid)) / 4 circle_shear = util_maskl.mask_sphere(x_shear, y_shear, center_x, center_y, radius) circle_foreground = util_maskl.mask_sphere(x_foreground, y_foreground, center_x, center_y, radius) f, ax = plt.subplots(1, 1, figsize=(16, 8), sharex=False, sharey=False) im = ax.matshow(np.log10(data_class.data), origin='lower', alpha=0.5) im = ax.matshow(util.array2image(circle_shear), origin='lower', alpha=0.5, cmap="jet") im = ax.matshow(util.array2image(circle_foreground), origin='lower', alpha=0.5) #f.show() return f, ax
def mass_fraction_within_radius(self, kwargs_lens, center_x, center_y, theta_E, numPix=100): """ computes the mean convergence of all the different lens model components within a spherical aperture :param kwargs_lens: lens model keyword argument list :param center_x: center of the aperture :param center_y: center of the aperture :param theta_E: radius of aperture :return: list of average convergences for all the model components """ x_grid, y_grid = util.make_grid(numPix=numPix, deltapix=2. * theta_E / numPix) x_grid += center_x y_grid += center_y mask = mask_util.mask_sphere(x_grid, y_grid, center_x, center_y, theta_E) kappa_list = [] for i in range(len(kwargs_lens)): kappa = self.LensModel.kappa(x_grid, y_grid, kwargs_lens, k=i) kappa_mean = np.sum(kappa * mask) / np.sum(mask) kappa_list.append(kappa_mean) return kappa_list
def mask_point_source(self, x_pos, y_pos, x_grid, y_grid, radius, i=0): """ :param x_pos: :param y_pos: :param i: :param kernel: :param image: :return: a mask of the size of the image with cutouts around the position """ mask = np.ones_like(x_grid) for k in range(len(x_pos)): if k != i: mask_point = 1 - mask_util.mask_sphere(x_grid, y_grid, x_pos[k], y_pos[k], radius) mask *= mask_point return mask
def mask_point_source(x_pos, y_pos, x_grid, y_grid, radius, i=0): """ :param x_pos: x-position of list of point sources :param y_pos: y-position of list of point sources :param x_grid: x-coordinates of grid :param y_grid: y-coordinates of grid :param i: index of point source not to mask out :param radius: radius to mask out other point sources :return: a mask of the size of the image with cutouts around the position """ mask = np.ones_like(x_grid) for k in range(len(x_pos)): if k != i: mask_point = 1 - mask_util.mask_sphere(x_grid, y_grid, x_pos[k], y_pos[k], radius) mask *= mask_point return util.array2image(mask)
def moments(I_xy_input, x, y): """ compute quadrupole moments from a light distribution :param I_xy: light distribution :param x: x-coordinates of I_xy :param y: y-coordinates of I_xy :return: Q_xx, Q_xy, Q_yy """ I_xy = copy.deepcopy(I_xy_input) background = np.minimum(0, np.min(I_xy)) I_xy -= background x_ = np.sum(I_xy * x) y_ = np.sum(I_xy * y) r = (np.max(x) - np.min(x)) / 3. mask = mask_util.mask_sphere(x, y, center_x=x_, center_y=y_, r=r) Q_xx = np.sum(I_xy * mask * (x - x_)**2) Q_xy = np.sum(I_xy * mask * (x - x_) * (y - y_)) Q_yy = np.sum(I_xy * mask * (y - y_)**2) return Q_xx, Q_xy, Q_yy, background / np.mean(I_xy)
def half_light_radius(lens_light, x_grid, y_grid, center_x=0, center_y=0): """ :param lens_light: array of surface brightness :param x_grid: x-axis coordinates :param y_gird: y-axis coordinates :param center_x: center of light :param center_y: center of light :return: """ lens_light[lens_light < 0] = 0 total_flux_2 = np.sum(lens_light) / 2. r_max = np.max(np.sqrt((x_grid - center_x)**2 + (y_grid - center_y)**2)) for i in range(1000): r = i / 500. * r_max mask = mask_util.mask_sphere(x_grid, y_grid, center_x, center_y, r) flux_enclosed = np.sum(np.array(lens_light) * mask) if flux_enclosed > total_flux_2: return r return -1
def radial_profile(light_grid, x_grid, y_grid, center_x=0, center_y=0, n=None): """ :param light_grid: array of surface brightness :param x_grid: x-axis coordinates :param y_gird: y-axis coordinates :param center_x: center of light :param center_y: center of light :param n: number of discrete steps :return: """ r_max = np.max(np.sqrt((x_grid - center_x)**2 + (y_grid - center_y)**2)) if n is None: n = int(np.sqrt(len(x_grid))) I_r = np.zeros(n) I_enclosed = 0 r = np.linspace(1. / n * r_max, r_max, n) for i, r_i in enumerate(r): mask = mask_util.mask_sphere(x_grid, y_grid, center_x, center_y, r_i) flux_enclosed = np.sum(np.array(light_grid) * mask) I_r[i] = flux_enclosed - I_enclosed I_enclosed = flux_enclosed return I_r, r
def light2mass_interpol(lens_light_model_list, kwargs_lens_light, numPix=100, deltaPix=0.05, subgrid_res=5, center_x=0, center_y=0): """ takes a lens light model and turns it numerically in a lens model (with all lensmodel quantities computed on a grid). Then provides an interpolated grid for the quantities. :param kwargs_lens_light: lens light keyword argument list :param numPix: number of pixels per axis for the return interpolation :param deltaPix: interpolation/pixel size :param center_x: center of the grid :param center_y: center of the grid :param subgrid: subgrid for the numerical integrals :return: """ # make sugrid x_grid_sub, y_grid_sub = util.make_grid(numPix=numPix * 5, deltapix=deltaPix, subgrid_res=subgrid_res) import lenstronomy.Util.mask as mask_util mask = mask_util.mask_sphere(x_grid_sub, y_grid_sub, center_x, center_y, r=1) x_grid, y_grid = util.make_grid(numPix=numPix, deltapix=deltaPix) # compute light on the subgrid lightModel = LightModel(light_model_list=lens_light_model_list) flux = lightModel.surface_brightness(x_grid_sub, y_grid_sub, kwargs_lens_light) flux_norm = np.sum(flux[mask == 1]) / np.sum(mask) flux /= flux_norm from lenstronomy.LensModel.numerical_profile_integrals import ConvergenceIntegrals integral = ConvergenceIntegrals() # compute lensing quantities with subgrid convergence_sub = flux f_x_sub, f_y_sub = integral.deflection_from_kappa(convergence_sub, x_grid_sub, y_grid_sub, deltaPix=deltaPix / float(subgrid_res)) f_sub = integral.potential_from_kappa(convergence_sub, x_grid_sub, y_grid_sub, deltaPix=deltaPix / float(subgrid_res)) # interpolation function on lensing quantities x_axes_sub, y_axes_sub = util.get_axes(x_grid_sub, y_grid_sub) from lenstronomy.LensModel.Profiles.interpol import Interpol_func interp_func = Interpol_func() interp_func.do_interp(x_axes_sub, y_axes_sub, f_sub, f_x_sub, f_y_sub) # compute lensing quantities on sparser grid x_axes, y_axes = util.get_axes(x_grid, y_grid) f_ = interp_func.function(x_grid, y_grid) f_x, f_y = interp_func.derivatives(x_grid, y_grid) # numerical differentials for second order differentials from lenstronomy.LensModel.numeric_lens_differentials import NumericLens lens_differential = NumericLens(lens_model_list=['INTERPOL']) kwargs = [{ 'grid_interp_x': x_axes_sub, 'grid_interp_y': y_axes_sub, 'f_': f_sub, 'f_x': f_x_sub, 'f_y': f_y_sub }] f_xx, f_xy, f_yx, f_yy = lens_differential.hessian( x_grid, y_grid, kwargs) kwargs_interpol = { 'grid_interp_x': x_axes, 'grid_interp_y': y_axes, 'f_': util.array2image(f_), 'f_x': util.array2image(f_x), 'f_y': util.array2image(f_y), 'f_xx': util.array2image(f_xx), 'f_xy': util.array2image(f_xy), 'f_yy': util.array2image(f_yy) } return kwargs_interpol