def init(): # Set some SIE lens-model parameters and pack them into an array: l_amp = lamp # Einstein radius l_xcen = -2.5 # x position of center l_ycen = 2.5 # y position of center l_axrat = lax # minor-to-major axis ratio l_pa = lpa # major-axis position angle (degrees) c.c.w. from x axis lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) lenspar = np.asarray([l_amp, lsig, l_xcen, l_ycen, l_axrat, l_pa]) # The following lines will plot the un-lensed and lensed images side by side: (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x - xg, y - yg, gpar) lens_source = ldf.gauss_2d(x, y, lenspar) lens_source[lens_source < 0.6] = np.nan ax2.imshow(g_lensimage, **myargs, clim=(vmin, vmax)) lens = ax2.imshow(lens_source, **lensargs, clim=(vmin, vmax)) ax2.set_yticklabels([]) ax2.set_xticklabels([]) ax2.set_yticks([]) ax2.set_xticks([]) txt = ax2.text(20, 457, 'Lensed Image', size=15, color='w', zorder=5) txt.set_path_effects( [PathEffects.withStroke(linewidth=5, foreground='k')]) return lens,
def lens_object(lax,lamp=1.5,lsig=0.05,lx=0.,ly=0.,lpa=0.): # Set some SIE lens-model parameters and pack them into an array: l_amp = lamp # Einstein radius l_xcen = lx # x position of center l_ycen = ly # y position of center l_axrat = lax # minor-to-major axis ratio l_pa = lpa # major-axis position angle (degrees) c.c.w. from x axis lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) lpar2 = np.asarray([l_amp, l_xcen, l_ycen, 2., l_pa]) # rax of 2. lenspar = np.asarray([l_amp, lsig, l_xcen, l_ycen, l_axrat, l_pa]) # The following lines will plot the un-lensed and lensed images side by side: (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x-xg, y-yg, gpar) lens_source = ldf.gauss_2d(x, y, lenspar) lens_source[lens_source < 0.6] = np.nan return g_lensimage,lens_source
def update(val): g_axrat = sgaxrat.val g_xcen = sgxcen.val g_ycen = sgycen.val g_pa = sgpa.val g_sig = sgsig.val l_axrat = slaxrat.val gpar = np.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa]) lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) #g_image = ldf.gauss_2d(x, y, gpar) (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x-xg, y-yg, gpar) f.set_data(g_lensimage) fig.canvas.draw_idle()
def lensed(gamp,gsig,gx,gy,gax,gpa,name,lamp=1.5,lsig=0.05,lx=0.,ly=0.,lax=1.,lpa=0.): import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib import cm import lensdemo_funcs as ldf import fitting_ellipse as fe import matplotlib.patheffects as PathEffects # Package some image display preferences in a dictionary object, for use below: myargs = {'interpolation': 'nearest', 'origin': 'lower', 'cmap': cm.viridis} lensargs = {'interpolation': 'nearest', 'origin': 'lower', 'cmap': cm.viridis} # Make some x and y coordinate images: nx.ny = 501,501 xhilo,yhilo = [-2.5, 2.5],[-2.5, 2.5] x = (xhilo[1] - xhilo[0]) * np.outer(np.ones(ny), np.arange(nx)) / float(nx-1) + xhilo[0] y = (yhilo[1] - yhilo[0]) * np.outer(np.arange(ny), np.ones(nx)) / float(ny-1) + yhilo[0] # Set some Gaussian blob image parameters and pack them into an array: g_amp = gamp # peak brightness value g_sig = gsig # Gaussian "sigma" (i.e., size) g_xcen = gx # x position of center g_ycen = gy # y position of center g_axrat = gax # minor-to-major axis ratio g_pa = gpa # major-axis position angle (degrees) c.c.w. from x axis gpar = np.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa]) def lens_object(lax,lamp=1.5,lsig=0.05,lx=0.,ly=0.,lpa=0.): # Set some SIE lens-model parameters and pack them into an array: l_amp = lamp # Einstein radius l_xcen = lx # x position of center l_ycen = ly # y position of center l_axrat = lax # minor-to-major axis ratio l_pa = lpa # major-axis position angle (degrees) c.c.w. from x axis lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) lpar2 = np.asarray([l_amp, l_xcen, l_ycen, 2., l_pa]) # rax of 2. lenspar = np.asarray([l_amp, lsig, l_xcen, l_ycen, l_axrat, l_pa]) # The following lines will plot the un-lensed and lensed images side by side: (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x-xg, y-yg, gpar) lens_source = ldf.gauss_2d(x, y, lenspar) lens_source[lens_source < 0.6] = np.nan return g_lensimage,lens_source # defining bkgd source and lensed source (for both rax) g_image = ldf.gauss_2d(x, y, gpar) glens1,lens1 = lens_object(1.) glens2,lens2 = lens_object(2.) plt.figure(figsize=(15,6)) gs1 = gridspec.GridSpec(1,3) gs1.update(wspace=0.03) cmap = plt.cm.viridis # background source (no lens) ax1 = plt.subplot(gs1[0]) im = ax1.imshow(g_image,**myargs,clim=(0.5,2.2)) # set to make the lens the same always vmin, vmax = im.get_clim() # set to make the lens the same always ax1.set_yticklabels([]); ax1.set_xticklabels([]) ax1.set_yticks([]); ax1.set_xticks([]) txt = ax1.text(20,457,'Background Source', size=15, color='w') txt.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='k')]) # background source lensed by point-like lens ax2 = plt.subplot(gs1[1]) ax2.imshow(glens1,**myargs,clim=(vmin,vmax)) ax2.imshow(lens1,**lensargs,clim=(vmin,vmax)) cir = plt.Circle((250,250),150,fill=False,ls='--',color='C0') ax2.add_artist(cir) ax2.set_yticklabels([]); ax2.set_xticklabels([]) ax2.set_yticks([]); ax2.set_xticks([]) txt = ax2.text(20,457,'Point-like Lens', size=15, color='w') txt.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='k')]) # Listing parameters on ax1 txt = ax1.text(20,20,'amp:%s, sig:%s,\ncenter:(%s,%s),\naxrat:%s, pa:%s'\ %(gpar[0],gpar[1],gpar[2],gpar[3],gpar[4],gpar[5]), size=13, color='w') txt.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='k')]) # background source lensed by extended lens ax3 = plt.subplot(gs1[2]) ax3.imshow(glens2,**myargs,clim=(vmin,vmax)) ax3.imshow(lens2,**lensargs,clim=(vmin,vmax)) cir = plt.Circle((250,250),150,fill=False,ls='--',color='C0') ax3.add_artist(cir) ax3.set_yticklabels([]); ax3.set_xticklabels([]) ax3.set_yticks([]); ax3.set_xticks([]) txt = ax3.text(20,457,'Extended Lens', size=15, color='w') txt.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='k')]) #plt.savefig('test.png',dpi=200) # useful for troubleshooting plt.savefig('lens2-still%s.pdf'%(name),dpi=200) plt.close('all')
def lookup(par): """ PURPOSE: Calculate the flux received in a SDSSIII fiber over the intrinsic flux as a function of impact parameter, Einstein radius, axis ratio, size, etc... USAGE: f_f/f_i = lookup(par[b, q_l, P.A_l, amp, sigma_s, xcen_s, ycen_s,q_s, P.A_s]) ARGUMENTS: pars: par[0]: Einstein Radius par[1]: Axis ratio of lens par[2]: Position angle of lens (c.c.w. major-axis rotation w.r.t. x-axis) par[3]: Amplitude of source galaxy par[4]: Intermediate-axis sigma of source galaxy par[5]: x coordinate of source galaxy par[6]: y coordinate of source galaxy par[7]: Axis ratio of source par[8]: Position angle of source (c.c.w. major-axis rotation w.r.t. x-axis) RETURNS: Ratio of flux in fiber w.r.t intrinsic flux WRITTEN: Ryan A. Arneson, U. of Utah, 2010 """ #Define the pixel scale to be 0.01"/pixel myargs = { 'interpolation': 'nearest', 'origin': 'lower', 'cmap': cm.gray, 'hold': False } psf = g.gauss2d(800, 150) nx = 1600. ny = 1600. ximg = n.outer(n.ones(ny), n.arange(nx, dtype='float')) - 800. yimg = n.outer(n.arange(ny, dtype='float'), n.ones(nx)) - 800. #lpar_guess = n.asarray([b, xcen, ycen, q, P.A.]) lpar_guess = n.asarray([par[0], 0.0, 0.0, par[1], par[2]]) #gpar_guess = n.asarray([amp., sigma, xcen, ycen, q, P.A.]) gpar_guess = n.asarray([par[3], par[4], par[5], par[6], par[7], par[8]]) xg, yg = ldf.sie_grad(ximg, yimg, lpar_guess) lmodel = ldf.gauss_2d(ximg - xg, yimg - yg, gpar_guess) #lmodel = signal.fftconvolve(lmodel, psf, mode='same') #lmodel = lmodel[1:1601,1:1601] fiber = yimg for i in range(0, 1600): for j in range(0, 1600): fiber[i, j] = n.sqrt(fiber[i, j]**2 + (j - 800.)**2) for i in range(0, 1600): for j in range(0, 1600): if fiber[i, j] <= 100: fiber[i, j] = 1.0 else: fiber[i, j] = 0.0 fiber = signal.fftconvolve(fiber, psf, mode='same') fiber = fiber[1:1601, 1:1601] f_f = n.sum(lmodel * fiber) #calculate the intrinisic flux (i.e. b=0) lpar_guess[0] = 0.0 ximg = n.outer(n.ones(ny), n.arange(nx, dtype='float')) - 800. yimg = n.outer(n.arange(ny, dtype='float'), n.ones(nx)) - 800. xg, yg = ldf.sie_grad(ximg, yimg, lpar_guess) lmodel2 = ldf.gauss_2d(ximg - xg, yimg - yg, gpar_guess) #lmodel2 = signal.fftconvolve(lmodel2, psf, mode='same') #lmodel2 = lmodel2[1:1601,1:1601] f_i = n.sum(lmodel2 * fiber) mag = f_f / f_i #p.imshow(n.hstack((lmodel*fiber,lmodel2*fiber)),**myargs) return mag
g_ycen = 0.0 # y position of center g_axrat = 1.0 # minor-to-major axis ratio g_pa = 0.0 # major-axis position angle (degrees) c.c.w. from x axis gpar = np.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa]) # Set some SIE lens-model parameters and pack them into an array: l_amp = 1.5 # Einstein radius l_xcen = 0.0 # x position of center l_ycen = 0.0 # y position of center l_axrat = 1.0 # minor-to-major axis ratio l_pa = 0.0 # major-axis position angle (degrees) c.c.w. from x axis lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) #g_image = ldf.gauss_2d(x, y, gpar) (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x-xg, y-yg, gpar) f = plt.imshow(g_lensimage, **myargs) ax = plt.axes([0.25,0.1,0.65,0.03]) slaxrat = Slider(ax, 'Lens Axis Ratio', 0.0, 5.0, valinit=l_axrat) ax = plt.axes([0.25,0.15,0.65,0.03]) sgaxrat = Slider(ax, 'Gaussian Axis Ratio', 0.0, 5.0, valinit=g_axrat) ax = plt.axes([0.25,0.20,0.65,0.03]) sgpa = Slider(ax, 'Gaussian Major-Axis Angle', 0.0, 360.0, valinit=g_pa) ax = plt.axes([0.25,0.25,0.65,0.03]) sgxcen = Slider(ax, 'Gaussian X-center', -1.5, 1.5, valinit=g_xcen)
def lensed(gamp, gsig, gx, gy, gax, gpa, filename, lamp=0.1, lsig=0.05, lax=1., lpa=0.): import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib import cm import lensdemo_funcs as ldf import matplotlib.patheffects as PathEffects import matplotlib.animation as animation # Package some image display preferences in a dictionary object, for use below: myargs = { 'interpolation': 'nearest', 'aspect': 'auto', 'origin': 'lower', 'cmap': cm.viridis } lensargs = { 'interpolation': 'nearest', 'aspect': 'auto', 'origin': 'lower', 'cmap': cm.viridis } # Make some x and y coordinate images: nx.ny = 501, 501 xhilo, yhilo = [-2.5, 2.5], [-2.5, 2.5] x = (xhilo[1] - xhilo[0]) * np.outer( np.ones(ny), np.arange(nx)) / float(nx - 1) + xhilo[0] y = (yhilo[1] - yhilo[0]) * np.outer( np.arange(ny), np.ones(nx)) / float(ny - 1) + yhilo[0] # Set some Gaussian blob image parameters and pack them into an array: g_amp = gamp # peak brightness value g_sig = gsig # Gaussian "sigma" (i.e., size) g_xcen = gx # x position of center g_ycen = gy # y position of center g_axrat = gax # minor-to-major axis ratio g_pa = gpa # major-axis position angle (degrees) c.c.w. from x axis gpar = np.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa]) fig = plt.figure(figsize=(8, 4)) gs1 = gridspec.GridSpec(1, 2) gs1.update(wspace=0.035) fig.subplots_adjust(left=0.02, bottom=0.03, right=0.98, top=0.97) g_image = ldf.gauss_2d(x, y, gpar) ax1 = plt.subplot(gs1[0]) im = ax1.imshow(g_image, **myargs, clim=(0.5, 2.2)) # set to make the lens the same always vmin, vmax = im.get_clim() ax1.set_yticklabels([]) ax1.set_xticklabels([]) ax1.set_yticks([]) ax1.set_xticks([]) txt = ax1.text(20, 457, 'Background Source', size=15, color='w') txt.set_path_effects([PathEffects.withStroke(linewidth=5, foreground='k')]) ax2 = plt.subplot(gs1[1]) def init(): # Set some SIE lens-model parameters and pack them into an array: l_amp = lamp # Einstein radius l_xcen = -2.5 # x position of center l_ycen = 2.5 # y position of center l_axrat = lax # minor-to-major axis ratio l_pa = lpa # major-axis position angle (degrees) c.c.w. from x axis lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) lenspar = np.asarray([l_amp, lsig, l_xcen, l_ycen, l_axrat, l_pa]) # The following lines will plot the un-lensed and lensed images side by side: (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x - xg, y - yg, gpar) lens_source = ldf.gauss_2d(x, y, lenspar) lens_source[lens_source < 0.6] = np.nan ax2.imshow(g_lensimage, **myargs, clim=(vmin, vmax)) lens = ax2.imshow(lens_source, **lensargs, clim=(vmin, vmax)) ax2.set_yticklabels([]) ax2.set_xticklabels([]) ax2.set_yticks([]) ax2.set_xticks([]) txt = ax2.text(20, 457, 'Lensed Image', size=15, color='w', zorder=5) txt.set_path_effects( [PathEffects.withStroke(linewidth=5, foreground='k')]) return lens, def lense_it(indx): moveit = np.arange(-2.5, 2.5, 0.1) # Set some SIE lens-model parameters and pack them into an array: l_amp = lamp # Einstein radius l_xcen = moveit[indx] # x position of center l_ycen = moveit[indx] * -1 # y position of center l_axrat = lax # minor-to-major axis ratio l_pa = lpa # major-axis position angle (degrees) c.c.w. from x axis lpar = np.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) lenspar = np.asarray([l_amp, lsig, l_xcen, l_ycen, l_axrat, l_pa]) # The following lines will plot the un-lensed and lensed images side by side: (xg, yg) = ldf.sie_grad(x, y, lpar) g_lensimage = ldf.gauss_2d(x - xg, y - yg, gpar) lens_source = ldf.gauss_2d(x, y, lenspar) lens_source[lens_source < 0.6] = np.nan ax2.imshow(g_lensimage, **myargs, clim=(vmin, vmax)) lens = ax2.imshow(lens_source, **lensargs, clim=(vmin, vmax)) ax2.set_yticklabels([]) ax2.set_xticklabels([]) ax2.set_yticks([]) ax2.set_xticks([]) txt = ax2.text(20, 457, 'Lensed Source', size=15, color='w', zorder=5) txt.set_path_effects( [PathEffects.withStroke(linewidth=5, foreground='k')]) return lens, anim = animation.FuncAnimation(fig,lense_it,init_func=init,\ frames=len(np.arange(-2.5,2.5,0.1)),interval=100,blit=True) anim.save('%s.gif'%(filename),fps=20,writer='imagemagick',\ extra_args=['-vcodec','h264','-pix_fmt','yuv420p']) plt.close('all')
# Set some Gaussian blob image parameters and pack them into an array: g_amp = 1.0 # peak brightness value g_sig = 0.05 # Gaussian "sigma" (i.e., size) g_xcen = 0.0 # x position of center g_ycen = 0.0 # y position of center g_axrat = 1.0 # minor-to-major axis ratio g_pa = 0.0 # major-axis position angle (degrees) c.c.w. from x axis gpar = n.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa]) # Set some SIE lens-model parameters and pack them into an array: l_amp = 1.5 # Einstein radius l_xcen = 0.0 # x position of center l_ycen = 0.0 # y position of center l_axrat = 1.0 # minor-to-major axis ratio l_pa = 0.0 # major-axis position angle (degrees) c.c.w. from x axis lpar = n.asarray([l_amp, l_xcen, l_ycen, l_axrat, l_pa]) # Compute the lensing potential gradients: (xg, yg) = ldf.sie_grad(x, y, lpar) # Evaluate lensed Gaussian image: g_lensimage = ldf.gauss_2d(x - xg, y - yg, gpar) # Have a look: f = p.imshow(g_lensimage, **myargs) p.savefig('lens_xy1001.png') n.savetxt("pixel_data_1001.txt", g_lensimage, fmt='%d', delimiter=' ')
l_rcs = 0.00000000001 l_res = 0.00000000001 l_pas = 0.0 l_pars = np.asarray([l_xcens, l_ycens, l_qs, l_rcs,l_res,l_pas]) #---------------------------------------------------------------------- boxsize = 5.0 nn = 512 ri = np.linspace(0.0,boxsize/2.0,nn) ti = np.linspace(0.0,2.0*np.pi,nn) ri,ti = np.meshgrid(ri,ti) xi1 = ri*np.cos(ti) xi2 = ri*np.sin(ti) g_image = ldf.gauss_2d(xi1, xi2, gpar) (ai1, ai2, mui) = le_sie_subs(xi1,xi2,l_par,l_pars) yi1 = xi1+ai1 yi2 = xi2+ai2 g_lensimage = ldf.gauss_2d(yi1, yi2, gpar) #--------------------------lens images contour------------------------ levels = [0.15,0.30,0.45,0.60,0.75,0.9,1.05] lev2 = [1000] figure(num=None,figsize=(10,5),dpi=80, facecolor='w', edgecolor='k') a = axes([0.05,0.1,0.4,0.8]) a.set_xlim(-2.5,2.5) a.set_ylim(-2.5,2.5) a.contourf(xi1,xi2,g_image,levels)
# Make some x and y coordinate images: nx = 501 ny = 501 xhilo = [-2.5, 2.5] yhilo = [-2.5, 2.5] x = (xhilo[1] - xhilo[0]) * n.outer(n.ones(ny), n.arange(nx)) / float(nx - 1) + xhilo[0] y = (yhilo[1] - yhilo[0]) * n.outer(n.arange(ny), n.ones(nx)) / float(ny - 1) + yhilo[0] # Set some Gaussian blob image parameters and pack them into an array: g_amp = 1.0 # peak brightness value g_sig = 0.05 # Gaussian "sigma" (i.e., size) g_xcen = 0.0 # x position of center g_ycen = 0.0 # y position of center g_axrat = 1.0 # minor-to-major axis ratio g_pa = 0.0 # major-axis position angle (degrees) c.c.w. from x axis gpar = n.asarray([g_amp, g_sig, g_xcen, g_ycen, g_axrat, g_pa]) # Have a look at the un-lensed Gaussian image: g_image = ldf.gauss_2d(x, y, gpar) f = p.imshow(g_image, **myargs) # IMPORTANT: Kill these imshow GUIs before redisplaying, or you will get bad memory leaks! # You can kill it with the "red button", or with the following command: #p.close(f.get_figure().number) # Alternatively, if you do the following you will probably be OK redisplaying # without killing the GUI: #f.axes.hold(False) p.show()
l2_axrat except NameError: l2_axrat = 1.0 # minor-to-major axis ratio try: l2_pa except NameError: l2_pa = 0.0 # major-axis position angle (degrees) c.c.w. from x axis l2par = n.asarray([l2_amp, l2_xcen, l2_ycen, l2_axrat, l2_pa]) if l2_amp>0:print '2nd lens\nEinstein Radius: %5.3f\nCenter: (%6.4f,%6.4f)\nAxis Ratio: %5.3f\nPA: %.0f\n'%(l2_amp,l2_xcen,l2_ycen,l2_axrat,l2_pa) # Compute the lensing potential gradients: (xg, yg) = ldf.sie_grad(x, y, lpar) # Evaluate lensed Gaussian image: if g2_amp > 0: g_lensimage = ldf.gauss_2d(x-xg, y-yg, gpar)+ldf.gauss_2d(x-xg, y-yg, g2par) else: g_lensimage = ldf.gauss_2d(x-xg, y-yg, gpar) #p.figure(2) # Have a look: #f = p.imshow(g_lensimage, **myargs) #p.figure(3) # If you can recall what the parameter place values mean, # the following lines are most efficient for exploration: #gpar = n.asarray([1.0, 0.05, 0.0, 0.0, 1.0, 0.0]) #lpar = n.asarray([1.5, 0.0, 0.0, 0.7, 0.0]) (xg, yg) = ldf.sie_grad(x, y, lpar)