def get_positions(filepath, colour1=None, colour2=None, colour_limits=None, fits_file=None, fig_dir='.', addsavename='', returnflux=False, ignore_points=()): sex = read_sextractor_output(filepath) ignore_indexes = [] for xignore, yignore in ignore_points: ignore_indexes += list(np.where(sex.X_IMAGE == xignore)[0] & np.where(sex.Y_IMAGE == yignore)[0]) sex = sex.drop(ignore_indexes) x = sex.X_IMAGE y = sex.Y_IMAGE flux = sex.FLUX_ISO plt.figure() if fits_file is not None: plot_image(fits_file, vmin=0, vmax=0.35) plt.scatter(x, y, marker='o', c='b', alpha=0.2) if colour_limits is not None: (x1, x2), (y1, y2) = colour_limits mask = (colour1 > x1) & (colour1 < x2) & (colour2 > y1) & (colour2 < y2) x = x[mask] y = y[mask] flux = flux[mask] plt.scatter(x, y, marker='+', c='g', alpha=0.9) plt.xlim(min(x), max(x)) plt.ylim(min(y), max(y)) plt.savefig(os.path.join(fig_dir, 'cluster_members{}'.format(addsavename))) if returnflux: return x, y, flux else: return x, y
def plot_img_pos(pars, pix_scale=1., threshold=0.8, fits_file=None, img_xobs=None, img_yobs=None, d=None): xsrcA, ysrcA, sigsrcA, xlens, ylens, blens, qlens, plens = pars fig_dir = 'Figures/lensed_quasar/' sa = (0, 100) # search area is 2000 pixels to 5000 pixels # Define source positions as a Guassian surface brightness profile X1, Y1, Q1, P1, S1, srcs = {}, {}, {}, {}, {}, {} X1['A'] = pymc.Uniform('X1A', 0., 100., value=xsrcA) Y1['A'] = pymc.Uniform('Y1A', 0., 100., value=ysrcA) Q1['A'] = pymc.Uniform('Q1A', 0.2, 1., value=1.) P1['A'] = pymc.Uniform('P1A', -180., 180., value=0.) S1['A'] = pymc.Uniform('N1A', 0., 6., value=sigsrcA) srcs['A'] = SBObjects.Gauss('', {'x': X1['A'], 'y': Y1['A'], 'q': Q1['A'],'pa': P1['A'], 'sigma': S1['A']}) # Define lens mass model LX = pymc.Uniform('lx', 0., 100., value=xlens) LY = pymc.Uniform('ly', 0., 100., value=ylens) LB = pymc.Uniform('lb', 0., 100., value=blens) LQ = pymc.Uniform('lq', 0.2, 1., value=qlens) LP = pymc.Uniform('lp', -180., 180., value=plens) XB = pymc.Uniform('xb', -0.2, 0.2, value=0.) XP = pymc.Uniform('xp', -180., 180., value=0.) lens = MassModels.SIE('', {'x': LX, 'y': LY, 'b': LB, 'q': LQ, 'pa': LP}) shear = MassModels.ExtShear('',{'x':LX,'y':LY,'b':XB,'pa':XP}) lenses = [lens] x, y = np.meshgrid(np.arange(sa[0], sa[1], pix_scale), np.arange(sa[0], sa[1], pix_scale)) image_plane, image_coords_pred = {}, {} for name in ['A']: x_src, y_src = pylens.getDeflections(lenses, [x, y], d=d[name]) image_plane[name] = srcs[name].pixeval(x_src, y_src) plt.figure() plt.imshow(image_plane[name], interpolation='nearest', origin='lower') plt.xlabel('%dx - %d pixels' % (pix_scale, sa[0])) plt.ylabel('%dy - %d pixels' % (pix_scale, sa[0])) plt.savefig(os.path.join(ROOT_DIR, fig_dir, 'image_plane%s.png' % name)) image_coords_pred[name] = np.add(np.multiply(np.where(image_plane[name] > threshold), pix_scale)[::-1], sa[0]) lnlike, img_xpred_compare, img_ypred_compare = calc_lnlike(image_coords_pred[name], img_xobs[name], img_yobs[name]) print(img_xpred_compare, img_xobs[name], lnlike) print(img_ypred_compare, img_yobs[name], lnlike) colors = (col for col in ['#1f77b4', '#2ca02c', '#9467bd', '#17becf', '#e377c2']) fig = plt.figure('image_and_position') plot_image(fits_file, figname='image_and_position', vmax=10.) plt.xlim(sa[0], sa[1]) plt.ylim(sa[0], sa[1]) for name in ['A']: plt.scatter(image_coords_pred[name][0], image_coords_pred[name][1], marker='.', alpha=0.3, c=next(colors)) plt.savefig(os.path.join(ROOT_DIR, fig_dir, 'image_with_predicted_image_plane.png'))
def plot_img_pos(pars, pix_scale=1., threshold=0.8, fits_file=None, img_xobs=None, img_yobs=None, d=None, fig_dir=None): names = img_xobs.keys() sa = (2000, 4500) # search area is 2000 pixels to 5000 pixels xsrc, ysrc, sigsrc = {}, {}, {} for i, name in enumerate(names): xsrc[name], ysrc[name], sigsrc[name] = pars[3 * i:3 * (i + 1)] xlens, ylens, blens, qlens, plens = pars[-5:] # Define source positions as a Guassian surface brightness profile X1, Y1, Q1, P1, S1, srcs = {}, {}, {}, {}, {}, {} for name in names: X1[name] = pymc.Uniform('X1%s' % name, 0., 5000., value=xsrc[name]) Y1[name] = pymc.Uniform('Y1%s' % name, 0., 5000., value=ysrc[name]) Q1[name] = pymc.Uniform('Q1%s' % name, 0.2, 1., value=1.) P1[name] = pymc.Uniform('P1%s' % name, -180., 180., value=0.) S1[name] = pymc.Uniform('N1%s' % name, 0., 10000., value=sigsrc[name]) srcs[name] = SBObjects.Gauss( '', { 'x': X1[name], 'y': Y1[name], 'q': Q1[name], 'pa': P1[name], 'sigma': S1[name] }) # Define lens mass model LX = pymc.Uniform('lx', 0., 5000., value=xlens) LY = pymc.Uniform('ly', 0., 5000., value=ylens) LB = pymc.Uniform('lb', 0., 5000., value=blens) LQ = pymc.Uniform('lq', 0.2, 1., value=qlens) LP = pymc.Uniform('lp', -180., 180., value=plens) XB = pymc.Uniform('xb', -0.2, 0.2, value=0.) XP = pymc.Uniform('xp', -180., 180., value=0.) lens = MassModels.SIE('', {'x': LX, 'y': LY, 'b': LB, 'q': LQ, 'pa': LP}) shear = MassModels.ExtShear('', {'x': LX, 'y': LY, 'b': XB, 'pa': XP}) lenses = [lens] x, y = np.meshgrid(np.arange(sa[0], sa[1], pix_scale), np.arange(sa[0], sa[1], pix_scale)) image_plane, image_coords_pred = {}, {} for name in names: x_src, y_src = pylens.getDeflections(lenses, [x, y], d=d[name]) image_plane[name] = srcs[name].pixeval(x_src, y_src) plt.figure() plt.imshow(image_plane[name], interpolation='nearest', origin='lower') plt.xlabel('%dx - %d pixels' % (pix_scale, sa[0])) plt.ylabel('%dy - %d pixels' % (pix_scale, sa[0])) plt.savefig(os.path.join(ROOT_DIR, fig_dir, 'image_plane%s.png' % name)) image_indexes_pred = np.where(image_plane[name] > threshold) image_coords_pred[name] = np.array( [x[image_indexes_pred], y[image_indexes_pred]]) print(name, image_coords_pred[name]) colors = (col for col in ['#1f77b4', '#2ca02c', '#9467bd', '#17becf', '#e377c2', 'lime']) markers = (marker for marker in ['x', 'o', '*', '+', 'v', 'D']) fig = plt.figure('image_and_position', figsize=(13, 13)) plot_image(fits_file, figname='image_and_position') plt.xlim(sa[0], sa[1]) plt.ylim(sa[0], sa[1]) for name in names: plt.scatter(img_xobs[name], img_yobs[name], marker=next(markers), c='white', label="%s obs" % name, alpha=0.8) plt.scatter(image_coords_pred[name][0], image_coords_pred[name][1], marker='.', alpha=0.5, c=next(colors), label=name) plt.legend(loc='upper right') plt.savefig( os.path.join(ROOT_DIR, fig_dir, 'image_with_predicted_image_plane.png'))
def plot_source_and_pred_lens_positions(pars, img_xobs, img_yobs, d, fig_dir, threshold=0.01, plotimage=False, fits_file=None, mass_pos=None, flux_dependent_b=False, masses_flux=None, sa=(2000, 4500), pix_scale=10.): if plotimage: fig = plt.figure('image_and_position', figsize=(13, 13)) plot_image(fits_file, figname='image_and_position') names = img_xobs.keys() xsrc, ysrc, sigsrc = {}, {}, {} for i, name in enumerate(names): xsrc[name], ysrc[name], sigsrc[name] = pars[3 * i: 3 * (i + 1)] xlens, ylens, blens, qlens, plens = pars[-7:-2] # Define source positions as a Guassian surface brightness profile X1, Y1, Q1, P1, S1, srcs = {}, {}, {}, {}, {}, {} for name in names: X1[name] = pymc.Uniform('X1%s' % name, 0., 5000., value=xsrc[name]) Y1[name] = pymc.Uniform('Y1%s' % name, 0., 5000., value=ysrc[name]) Q1[name] = pymc.Uniform('Q1%s' % name, 0.2, 1., value=1.) P1[name] = pymc.Uniform('P1%s' % name, -180., 180., value=0.) S1[name] = pymc.Uniform('N1%s' % name, 0., 10000., value=sigsrc[name]) srcs[name] = SBObjects.Gauss('', {'x': X1[name], 'y': Y1[name], 'q': Q1[name],'pa': P1[name], 'sigma': S1[name]}) # Define lens mass model LX = pymc.Uniform('lx', 0., 5000., value=xlens) LY = pymc.Uniform('ly', 0., 5000., value=ylens) LB = pymc.Uniform('lb', 0., 5000., value=blens) LQ = pymc.Uniform('lq', 0., 1., value=qlens) LP = pymc.Uniform('lp', -180., 180., value=plens) lens = MassModels.SIE('', {'x': LX, 'y': LY, 'b': LB, 'q': LQ, 'pa': LP}) lenses = [lens] if flux_dependent_b: for (lx, ly), flux in zip(mass_pos, masses_flux): slope, intercept = pars[-2:] LX = pymc.Uniform('lx', 0., 5000., value=lx) LY = pymc.Uniform('ly', 0., 5000., value=ly) LB = slope * np.log(flux) + intercept lens = MassModels.SIS('', {'x': LX, 'y': LY, 'b': LB}) lenses += [lens] print('lens einstein radius', lens.b) else: for b, (lx, ly) in zip(pars[-2:], mass_pos): LX = pymc.Uniform('lx', 0., 5000., value=lx) LY = pymc.Uniform('ly', 0., 5000., value=ly) LB = pymc.Uniform('lb', 0., 5000., value=b) lens = MassModels.SIS('', {'x': LX, 'y': LY, 'b': LB}) lenses += [lens] print('lens einstein radius', lens.b) colors = (col for col in ['#1f77b4', '#2ca02c', '#9467bd', '#17becf', '#e377c2', '#ADFF2F']) markers = (marker for marker in ['x', 'o', '*', '+', 'v', 'D']) x_src, y_src = {}, {} image_plane, image_coords_pred = {}, {} X1, Y1, Q1, P1, S1, srcs = {}, {}, {}, {}, {}, {} x, y = np.meshgrid(np.arange(sa[0], sa[1], pix_scale), np.arange(sa[0], sa[1], pix_scale)) plt.xlim(sa[0], sa[1]) plt.ylim(sa[0], sa[1]) for name in names: plt.figure('image_and_position') col = next(colors) plt.scatter(img_xobs[name], img_yobs[name], marker=next(markers), c='white', label="%s obs" % name, alpha=0.8) plt.scatter(x_src[name], y_src[name], marker='.', alpha=0.5, c=col, label="%s pred src" % name) # Get Image plane x_src_all, y_src_all = pylens.getDeflections(lenses, [x, y], d=d[name]) image_plane[name] = srcs[name].pixeval(x_src_all, y_src_all) image_indexes_pred = np.where(image_plane[name] > threshold) image_coords_pred[name] = np.array([x[image_indexes_pred], y[image_indexes_pred]]) image_plane_norm = (image_plane[name] - image_plane[name].min())/(image_plane[name].max() - image_plane[name].min()) rgba = np.zeros((len(image_coords_pred[name][0]), 4)) rgba[:, 0:3] = list(int(col[i:i+2], 16)/256. for i in (1, 3, 5)) rgba[:, 3] = 0.8 * np.array(image_plane_norm[image_indexes_pred]) plt.scatter(image_coords_pred[name][0], image_coords_pred[name][1], marker='x', color=rgba, label="%s pred img" % name) plt.figure(name) plt.title(name) plt.imshow(image_plane[name], interpolation='nearest', origin='lower') print(x_src, y_src) plt.figure('image_and_position') plt.legend(loc='upper right') plt.savefig(os.path.join(fig_dir, 'image_with_contours_and_images.png'))
def plot_source_and_pred_lens_positions(pars, img_xobs, img_yobs, d, fig_dir, threshold=0.01, plotimage=False, fits_file=None): if plotimage: fig = plt.figure(figsize=(13, 13)) plot_image(fits_file, fig) names = img_xobs.keys() try: xlens, ylens, blens, qlens, plens = pars bshear, pshear = 0., 0. except ValueError: # includes shear xlens, ylens, blens, qlens, plens, bshear, pshear = pars # Define lens mass model LX = pymc.Uniform('lx', 0., 5000., value=xlens) LY = pymc.Uniform('ly', 0., 5000., value=ylens) LB = pymc.Uniform('lb', 0., 5000., value=blens) LQ = pymc.Uniform('lq', 0.2, 1., value=qlens) LP = pymc.Uniform('lp', -180., 180., value=plens) XB = pymc.Uniform('xb', -200., 200., value=bshear) XP = pymc.Uniform('xp', -180., 180., value=pshear) lens = MassModels.SIE('', {'x': LX, 'y': LY, 'b': LB, 'q': LQ, 'pa': LP}) lenses = [lens] if len(pars) == 7: shear = MassModels.ExtShear('',{'x':LX,'y':LY,'b':XB,'pa':XP}) lenses += [shear] colors = (col for col in ['#1f77b4', '#2ca02c', '#9467bd', '#17becf', '#e377c2', 'lime']) markers = (marker for marker in ['x', 'o', '*', '+', 'v', 'D']) x_src, y_src = {}, {} image_plane, image_coords_pred = {}, {} X1, Y1, Q1, P1, S1, srcs = {}, {}, {}, {}, {}, {} print(float(lens.x), float(lens.y), float(lens.b), float(lens.q), float(lens.pa)) sa = (2000, 4500) pix_scale = 10. x, y = np.meshgrid(np.arange(sa[0], sa[1], pix_scale), np.arange(sa[0], sa[1], pix_scale)) plt.xlim(sa[0], sa[1]) plt.ylim(sa[0], sa[1]) for name in names: x_src[name], y_src[name] = pylens.getDeflections(lenses, [img_xobs[name], img_yobs[name]], d[name]) xs = np.median(x_src[name]) ys = np.median(y_src[name]) lnlike = -0.5 * (x_src[name].var() + y_src[name].var()) print(float(lens.x), float(lens.y), float(lens.b), float(lens.q), float(lens.pa)) print(name, xs, ys, lnlike) col = next(colors) plt.scatter(img_xobs[name], img_yobs[name], marker=next(markers), c='white', label="%s obs" % name, alpha=0.8) plt.scatter(x_src[name], y_src[name], marker='.', alpha=0.5, c=col, label="%s pred src" % name) # CALC IMG POS # Assume gaussian surface brightness at (xs, ys) X1[name] = pymc.Uniform('X1%s' % name, 0., 5000., value=xs) Y1[name] = pymc.Uniform('Y1%s' % name, 0., 5000., value=ys) Q1[name] = pymc.Uniform('Q1%s' % name, 0.2, 1., value=1.) P1[name] = pymc.Uniform('P1%s' % name, -180., 180., value=0.) S1[name] = pymc.Uniform('N1%s' % name, 0., 10000., value=6.) srcs[name] = SBObjects.Gauss('', {'x': X1[name], 'y': Y1[name], 'q': Q1[name], 'pa': P1[name], 'sigma': S1[name]}) # Get Image plane x_src_all, y_src_all = pylens.getDeflections(lenses, [x, y], d=d[name]) image_plane[name] = srcs[name].pixeval(x_src_all, y_src_all) image_indexes_pred = np.where(image_plane[name] > threshold) image_coords_pred[name] = np.array([x[image_indexes_pred], y[image_indexes_pred]]) plt.scatter(image_coords_pred[name][0], image_coords_pred[name][1], marker='x', alpha=0.5, c=col, label="%s pred img" % name) print(x_src, y_src) plt.legend(loc='upper right') plt.savefig(os.path.join(fig_dir, 'image_with_contours_and_images.png'))