Beispiel #1
0
    def deflect(self, xim, yim, lens):
        """
            Calculate deflection following Keeton,Mao,Witt 2000.
            
            Parameters:
            xim, yim
                  2D Arrays of image coordinates we're going to lens,
                  probably generated by np.meshgrid.
            lens
                  A lens object; we use this to shift the coordinate system
                  to be centered on the lens.
            """

        ximage, yimage = xim.copy(), yim.copy()

        ximage -= lens.x['value']
        yimage -= lens.y['value']

        if not np.isclose(lens.PA['value'], 0.):
            r, theta = cart2pol(ximage, yimage)
            ximage, yimage = pol2cart(r, theta - (lens.PA['value'] * deg2rad))

        # KMW2000, altered for our coordinate convention.
        g, thg = self.shear['value'], (self.shearangle['value'] -
                                       lens.PA['value']) * deg2rad
        dxs = -g * np.cos(2 * thg) * ximage - g * np.sin(2 * thg) * yimage
        dys = -g * np.sin(2 * thg) * ximage + g * np.cos(2 * thg) * yimage

        if not np.isclose(lens.PA['value'], 0.):
            r, theta = cart2pol(dxs, dys)
            dxs, dys = pol2cart(r, theta + (lens.PA['value'] * deg2rad))

        self.deflected_x = dxs
        self.deflected_y = dys
Beispiel #2
0
 def deflect(self,xim,yim,lens):
       """
       Calculate deflection following Keeton,Mao,Witt 2000.
       
       Parameters:
       xim, yim
             2D Arrays of image coordinates we're going to lens,
             probably generated by np.meshgrid.
       lens
             A lens object; we use this to shift the coordinate system
             to be centered on the lens.
       """
       
       ximage,yimage = xim.copy(), yim.copy()
       
       ximage -= lens.x['value']; yimage -= lens.y['value']
       
       if not np.isclose(lens.PA['value'], 0.):
             r,theta = cart2pol(ximage,yimage)
             ximage,yimage = pol2cart(r,theta-(lens.PA['value']*deg2rad))
             
       # KMW2000, altered for our coordinate convention.
       g,thg = self.shear['value'], (self.shearangle['value']-lens.PA['value'])*deg2rad
       dxs = -g*np.cos(2*thg)*ximage - g*np.sin(2*thg)*yimage
       dys = -g*np.sin(2*thg)*ximage + g*np.cos(2*thg)*yimage
       
       if not np.isclose(lens.PA['value'], 0.):
             r,theta = cart2pol(dxs,dys)
             dxs,dys = pol2cart(r,theta+(lens.PA['value']*deg2rad))
             
       self.deflected_x = dxs; self.deflected_y = dys
Beispiel #3
0
    def deflect(self, xim, yim, Dd, Ds, Dds):
        """
            Follow Kormann+1994 for the lensing deflections.
            
            Parameters:
            xim, yim
                  2D Arrays of image coordinates we're going to lens,
                  probably generated by np.meshgrid.
            Dd, Ds, Dds
                  Distances to the lens, source and between the source
                  and lens (units don't matter as long as they're the 
                  same). Can't be calculated only from lens due to source
                  distances.
            """
        if self._altered:  # Only redo if something is new.
            ximage, yimage = xim.copy(), yim.copy()  # for safety.

            f = 1. - self.e['value']
            fprime = np.sqrt(1. - f**2.)

            # K+94 parameterizes in terms of LOS velocity dispersion and then
            # basically the Einstein radius.
            sigma = ((self.M['value'] * Ds * G * Msun * c**2.) /
                     (4 * np.pi**2. * Dd * Dds * Mpc))**(1 / 4.)
            Xi0 = 4 * np.pi * (sigma / c)**2. * (Dd * Dds / Ds)

            # Flip units, the recenter and rotate grid to lens center and major axis
            ximage *= arcsec2rad
            yimage *= arcsec2rad
            ximage -= (self.x['value'] * arcsec2rad)
            yimage -= (self.y['value'] * arcsec2rad)
            if not np.isclose(self.PA['value'], 0.):
                r, theta = cart2pol(ximage, yimage)
                ximage, yimage = pol2cart(r,
                                          theta - (self.PA['value'] * deg2rad))
            phi = np.arctan2(yimage, ximage)

            # Calculate the deflections, account for e=0 (the SIS), which has
            # cancelling infinities. K+94 eq 27a.
            if np.isclose(f, 1.):
                dxs = -(Xi0 / Dd) * np.cos(phi)
                dys = -(Xi0 / Dd) * np.sin(phi)
            else:
                dxs = -(Xi0 / Dd) * (np.sqrt(f) / fprime) * np.arcsinh(
                    np.cos(phi) * fprime / f)
                dys = -(Xi0 / Dd) * (np.sqrt(f) / fprime) * np.arcsin(
                    np.sin(phi) * fprime)

            # Rotate and shift back to sky frame
            if not np.isclose(self.PA['value'], 0.):
                r, theta = cart2pol(dxs, dys)
                dxs, dys = pol2cart(r, theta + (self.PA['value'] * deg2rad))
            dxs *= rad2arcsec
            dys *= rad2arcsec

            self.deflected_x = dxs
            self.deflected_y = dys
            self._altered = False
Beispiel #4
0
 def deflect(self,xim,yim,Dd,Ds,Dds):
       """
       Follow Kormann+1994 for the lensing deflections.
       
       Parameters:
       xim, yim
             2D Arrays of image coordinates we're going to lens,
             probably generated by np.meshgrid.
       Dd, Ds, Dds
             Distances to the lens, source and between the source
             and lens (units don't matter as long as they're the 
             same). Can't be calculated only from lens due to source
             distances.
       """
       if self._altered: # Only redo if something is new.
             ximage, yimage = xim.copy(), yim.copy() # for safety.
       
             f = 1. - self.e['value']
             fprime = np.sqrt(1. - f**2.)
       
             # K+94 parameterizes in terms of LOS velocity dispersion and then
             # basically the Einstein radius.
             sigma = ((self.M['value']*Ds*G*Msun*c**2.)/(4*np.pi**2. * Dd*Dds*Mpc))**(1/4.)
             Xi0 = 4*np.pi * (sigma/c)**2. * (Dd*Dds/Ds)
       
             # Flip units, the recenter and rotate grid to lens center and major axis
             ximage *= arcsec2rad; yimage *= arcsec2rad
             ximage -= (self.x['value']*arcsec2rad)
             yimage -= (self.y['value']*arcsec2rad)
             if not np.isclose(self.PA['value'], 0.):
                   r,theta = cart2pol(ximage,yimage)
                   ximage,yimage = pol2cart(r,theta-(self.PA['value']*deg2rad))
             phi = np.arctan2(yimage,ximage)
       
             # Calculate the deflections, account for e=0 (the SIS), which has
             # cancelling infinities. K+94 eq 27a.
             if np.isclose(f, 1.):
                   dxs = -(Xi0/Dd)*np.cos(phi)
                   dys = -(Xi0/Dd)*np.sin(phi)
             else:
                   dxs = -(Xi0/Dd)*(np.sqrt(f)/fprime)*np.arcsinh(np.cos(phi)*fprime/f)
                   dys = -(Xi0/Dd)*(np.sqrt(f)/fprime)*np.arcsin(np.sin(phi)*fprime)
       
             # Rotate and shift back to sky frame
             if not np.isclose(self.PA['value'], 0.):
                   r,theta = cart2pol(dxs,dys)
                   dxs,dys = pol2cart(r,theta+(self.PA['value']*deg2rad))
             dxs *= rad2arcsec; dys *= rad2arcsec
       
             self.deflected_x = dxs
             self.deflected_y = dys
             self._altered = False
Beispiel #5
0
	def Zphase_MahajanOSA(self):
		phasemap = np.zeros((self.PARAMS['1'],self.PARAMS['1']))
		phasemap = phasemap + phasemap*1j 
		sizeoffield = self.PARAMS['4']

		B = 0.2 # fraction if the diffuse component;
		A = 1 - B # raction of directed component;
		peakx = 0 # normalized location of peak reflectance in x-direction
		peaky = 0 # normalized location of peak reflectance in x-direction
		p = 0 # 0.047 # set to zero to turn off the amplitude factor (Values from Burns et al in mm^(-2)

		self.PARAMS['6'] = 0

		# ----------Joel's Method --------------------------------
		nx = z_adjust(np.arange(1, self.PARAMS['1'] + 1), sizeoffield, self.PARAMS['1'])
		ny = z_adjust(np.arange(1, self.PARAMS['1'] + 1), sizeoffield, self.PARAMS['1'])
		xx, yy = np.meshgrid(nx, ny)

		angle, norm_radius = utils.cart2pol(xx,yy)
		norm_radius = norm_radius / (float(self.PARAMS['2']) / 2.)
		r = norm_radius

		phase = utils.computePhase(self.c, angle, r)
		d = np.sqrt((peakx - xx)**2 + (peaky - yy)**2);
		SCfactor = B + A * 10**(-p * ((self.PARAMS['2'])**2) * d**2)

		phase_result = SCfactor * np.exp(-1j * 2 * np.pi * phase / self.PARAMS['5'])

		phasemap[np.where(norm_radius <= 1)] = phase_result[np.where(norm_radius <= 1)]

		self.PARAMS['6'] = np.sum((norm_radius <= 1).astype(int))


		# #-------- Austin's Method ---------------------------------
		# newsize = self.PARAMS['1']
		# for ny in np.arange(1, self.PARAMS['1'] + 1):
		# 	for nx in np.arange(1, self.PARAMS['1'] + 1):
		# 		xpos = (((float(nx) - 1.) * (float(sizeoffield) / float(newsize))) - (sizeoffield / 2.))
		# 		ypos = (((float(ny) - 1.) * (float(sizeoffield) / float(newsize))) - (sizeoffield / 2.))

		# 		angle, norm_radius = utils.cart2pol(xpos,ypos)	
		# 		norm_radius = float(norm_radius) / (float(self.PARAMS['2']) / 2.)
		# 		r = norm_radius

				
		# 		if norm_radius > 1:
		# 			pass
		# 		else:
		# 			phase = utils.computePhase(self.c, angle, r)
		# 			d = np.sqrt((peakx - xpos)**2 + (peaky - ypos)**2);
		# 			SCfactor = B + A * 10**(-p * ((self.PARAMS['2'])**2) * d**2)
		# 			phasemap[nx,ny] = SCfactor * np.exp(-1j * 2 * np.pi * phase / self.PARAMS['5'])
		# 			self.PARAMS['6'] += 1

		return phasemap
Beispiel #6
0
    def update(self, steering):
        """
        Update the positions and headings
        steering: vector of (x, y) steering
        """
        self.accelerations = steering * self.dt

        # old heading
        _, old_headings = cart2pol(self.velocities)

        new_velocities = self.velocities + self.accelerations * self.dt

        # new velocity in polar coordinates
        speeds, new_headings = cart2pol(new_velocities)

        # calculate heading diff
        headings_diff = np.radians(
            180 -
            (180 - np.degrees(new_headings) + np.degrees(old_headings)) % 360)

        # if the heading diff is too big, we cut the turn at max_turn or min_turn
        # use np.where
        new_headings = np.where(headings_diff < -self.max_turn,
                                old_headings - self.max_turn, new_headings)

        new_headings = np.where(self.max_turn < headings_diff,
                                old_headings + self.max_turn, new_headings)

        # if the speed is too slow or too big, we adjust it
        # use np.where
        speeds = np.where(speeds < self.min_speed, self.min_speed, speeds)
        speeds = np.where(speeds > self.max_speed, self.max_speed, speeds)

        # set the new velocity
        # modify pol2cart
        self.velocities = pol2cart(speeds, new_headings)

        self.headings = new_headings
        # move
        self.positions += self.velocities * self.dt

        self.adjust_positions_to_boundaries()
Beispiel #7
0
	def Zwave_MahajanOSA(self):
		newsize = np.ceil(self.PARAMS['7']*self.PARAMS['2']).astype(int)
		waveabermap = np.zeros((newsize, newsize))
		sizeoffield = self.PARAMS['2']

		self.PARAMS['6'] = 0

		#------ Joel's Method -------------------------------------------------------------
		nx = z_adjust(np.arange(1, newsize+1), sizeoffield, newsize)
		ny = z_adjust(np.arange(1, newsize+1), sizeoffield, newsize)

		xx, yy = np.meshgrid(nx, ny)

		angle, norm_radius = utils.cart2pol(xx,yy)
		norm_radius = norm_radius / (self.PARAMS['3'] / 2)
		r = norm_radius

		phase = utils.computePhase(self.c, angle, r)
		waveabermap[np.where(norm_radius > self.PARAMS['2']/self.PARAMS['3'])] = float('nan')
		waveabermap[np.where(norm_radius <= self.PARAMS['2']/self.PARAMS['3'])] = phase[np.where(norm_radius <= self.PARAMS['2']/self.PARAMS['3'])]


		# #---- Austin's Method --------------------------------------------------------------
		# for ny in range(newsize):
		# 	for nx in range(newsize):
		# 		xpos = ((nx - 1) * (sizeoffield / newsize) - (sizeoffield / 2))
		# 		print(xpos)
		# 		ypos = ((ny - 1) * (sizeoffield / newsize) - (sizeoffield / 2))

		# 		angle, norm_radius = utils.cart2pol(xpos,ypos)
		# 		norm_radius = norm_radius / (self.PARAMS['3'] / 2)
		# 		r = norm_radius

		# 		if norm_radius > self.PARAMS['2']/self.PARAMS['3']:
		# 			waveabermap[nx,ny] = float('nan')
		# 		else:
		# 			phase = utils.computePhase(self.c, angle, r)
		# 			waveabermap[nx,ny] = phase
		# 			self.PARAMS['6'] += 1

		return waveabermap
Beispiel #8
0
vecY = np.array([0])
vecSd = np.array([0.5])

mdlPrms = rmp_deg_pixel_x_y_s(vecY, vecX, vecSd, (int(pix), int(pix)),
                              -fovHeight / 2., fovHeight / 2., -fovHeight / 2.,
                              fovHeight / 2.)
mdlPrms = np.stack(mdlPrms, axis=1)

prf_ntr = crt_2D_gauss(pix, pix, mdlPrms[:, 0], mdlPrms[:, 1], mdlPrms[:, 2])

# %% apply gradient tp prf
gridX, gridY = np.meshgrid(
    np.linspace(-pix / 2., pix / 2., pix, endpoint=False),
    np.linspace(-pix / 2., pix / 2., pix, endpoint=False))

_, gridRad = cart2pol(gridX, gridY)

gradOtw = np.copy(np.ones(gridRad.shape) * gridRad.max() - gridRad)
gradInw = np.copy(gridRad)

gradOtw = np.divide(gradOtw, np.sum(gradOtw, axis=(0, 1)))
gradInw = np.divide(gradInw, np.sum(gradInw, axis=(0, 1)))

prf_otw = cnvl_grad_prf(prf_ntr, gradOtw, Normalize=True)
prf_inw = cnvl_grad_prf(prf_ntr, gradInw, Normalize=True)

# %% save new prfs to disk
fig, ax = plt.subplots()
cax = plt.imshow(prf_otw, cmap="viridis")
varFigName = os.path.join("/media/sf_D_DRIVE/MotDepPrf/Presentation/figures",
                          "Figure_prfshift", "prf_otw" + ".svg")
import itertools
import os
import numpy as np
import config_MotDepPrf as cfg
from PIL import Image
from utils import createBinCircleMask, getDistIma, assignBorderVals, cart2pol

# %% create the radial sine wave pattern
# get cartesian coordinates which are needed to define the textures
x, y = np.meshgrid(
    np.linspace(-cfg.fovHeight / 2., cfg.fovHeight / 2., cfg.pix / 2.),
    np.linspace(-cfg.fovHeight / 2., cfg.fovHeight / 2., cfg.pix / 2.))

# get polar coordinates which are needed to define the textures
theta, radius = cart2pol(x, y)
# define the phase for inward/outward conditin
phase = np.linspace(0., 2. * np.pi, cfg.nFrames / cfg.cycPerSec)

# get the array that divides field in angular cycles
polCycles = np.sin(cfg.angularCycles * theta)
polCycles[np.greater_equal(polCycles, 0)] = 1
polCycles[np.less(polCycles, 0)] = -1

# get radial sine wave gratings for main conditions
stimTexture = np.zeros(
    (cfg.pix / 2., cfg.pix / 2., cfg.nFrames / cfg.cycPerSec))
for ind, ph in enumerate(phase):
    ima = np.sin(cfg.spatFreq * 2. * np.pi * radius - ph)
    stimTexture[..., ind] = ima
Beispiel #10
0
def test_cart_pol_conv():
    print(ut.pol2cart((2, np.pi / 2)))
    print(ut.cart2pol((5, 5)))