def getFeatures(self):
     features = []
     # Preprocess the ch1 image -- let's just try an abs
     img = self.ch1
     hist, bins = np.histogram(img.ravel(), 256, [0, np.max(img)])
     #plt.hist(img.ravel(),256,[0,np.max(img)]); plt.show()
     zero = np.argmax(hist)
     img = img - bins[zero]
     ch1 = np.abs(img)
     ch1 = ndimage.gaussian_filter(ch1, sigma=2)
     ch6 = self.ch6
     ch6 = ndimage.gaussian_filter(ch6, sigma=2)
     features = np.concatenate([features, gaussfitter.gaussfit(ch1)])
     features = np.concatenate([features, gaussfitter.gaussfit(ch6)])
     return features
Exemple #2
0
    def test_masked_fit(self):
        inpars = [0, 1, 64, 64, 8, 8, 0]
        in_data = gf.twodgaussian(inpars, shape=(128, 128))
        masked_data = np.ma.array(in_data)

        # fit should be independent of masked data value
        masked_data[64, 64] = 1e6
        masked_data[64, 64] = np.ma.masked
        fit1 = gf.gaussfit(masked_data)

        masked_data[64, 64] = 0
        masked_data[64, 64] = np.ma.masked
        fit2 = gf.gaussfit(masked_data)

        for v1, v2 in zip(fit1, fit2):
            self.assertAlmostEqual(v1, v2)
    def test_masked_fit(self):
        inpars = [0, 1, 64, 64, 8, 8]
        in_data = gf.twodgaussian(inpars, shape=(128,128), rotate=False)
        masked_data = np.ma.array(in_data)

        # fit should be independent of masked data value
        masked_data[64,64] = 1e6
        masked_data[64,64] = np.ma.masked
        fit1 = gf.gaussfit(masked_data, rotate=False)

        masked_data[64,64] = 0
        masked_data[64,64] = np.ma.masked
        fit2 = gf.gaussfit(masked_data, rotate=False)

        for v1, v2 in zip(fit1, fit2):
            self.assertAlmostEqual(v1, v2)
 def test_simple_fit(self):
     # fit a 2D gaussian function centered at (64, 64) with width 8
     # inpars = [height,amplitude,center_x,center_y,width_x,width_y,rota]
     inpars = [0, 1, 64, 64, 8, 8,]
     in_data = gf.twodgaussian(inpars, shape=(128, 128), rotate=False)
     fit = gf.gaussfit(in_data, rotate=False)
     for inval, outval in zip(inpars, fit):
         self.assertAlmostEqual(inval, outval)
Exemple #5
0
 def test_simple_fit(self):
     # fit a 2D gaussian function centered at (64, 64) with width 8
     # inpars = [height,amplitude,center_x,center_y,width_x,width_y,rota]
     inpars = [0, 1, 64, 64, 8, 8, 0]
     in_data = gf.twodgaussian(inpars, shape=(128, 128))
     fit = gf.gaussfit(in_data)
     for inval, outval in zip(inpars, fit):
         self.assertAlmostEqual(inval, outval)
def getfit(frame):
    if np.max(frame) != np.max(np.abs(frame)):
        onoroff = -1
    else:
        onoroff = 1

    pars = gfit.gaussfit(frame * onoroff)
    f = gfit.twodgaussian(pars)
    return f, pars, onoroff
Exemple #7
0
 def __call__(self, im):
     kernel = self.reference[::-1, ::-1]
     cor = fftconvolve(im, kernel, mode='same')
     y, x = find_max_star(cor)
     g = gaussfit(cor[max(0, y - 40):min(y + 40, cor.shape[0]),
                      max(0, x - 40):min(x + 40, cor.shape[1])])
     shiftx = np.rint(cor.shape[1] / 2.) - max(0, x - 40) - g[2]
     shifty = np.rint(cor.shape[0] / 2.) - max(0, y - 40) - g[3]
     #shifts.append((shifty,shiftx))
     return (shifty, shiftx)
Exemple #8
0
def shift_est(psf_stack): 
    """Estimates shifts (see SPRITE paper, section 3.4.1., subsection 'Subpixel shifts').
    
    Calls:
    
    * gaussfitter.gaussfit
    * :func:`utils.compute_centroid`
    """
    shap = psf_stack.shape
    U = zeros((shap[2],2))
    param=gaussfitter.gaussfit(psf_stack[:,:,0],returnfitimage=False)
    #(centroid_ref,Wc) = compute_centroid(psf_stack[:,:,0],(param[3]+param[4])/2)
    centroid_out = zeros((shap[2],2))
    for i in range(0,shap[2]):
        param=gaussfitter.gaussfit(psf_stack[:,:,i],returnfitimage=False)
        (centroid,Wc) = compute_centroid(psf_stack[:,:,i],(param[3]+param[4])/2)
        U[i,0] = centroid[0,0]-double(shap[0])/2
        U[i,1] = centroid[0,1]-double(shap[1])/2
        centroid_out[i,0]  = centroid[0,0]
        centroid_out[i,1]  = centroid[0,1]
    return U,centroid_out
Exemple #9
0
def shift_est(psf_stack):
    """Estimates shifts (see SPRITE paper, section 3.4.1., subsection 'Subpixel shifts').
    
    Calls:
    
    * gaussfitter.gaussfit
    * :func:`utils.compute_centroid`
    """
    shap = psf_stack.shape
    U = zeros((shap[2], 2))
    param = gaussfitter.gaussfit(psf_stack[:, :, 0], returnfitimage=False)
    #(centroid_ref,Wc) = compute_centroid(psf_stack[:,:,0],(param[3]+param[4])/2)
    centroid_out = zeros((shap[2], 2))
    for i in range(0, shap[2]):
        param = gaussfitter.gaussfit(psf_stack[:, :, i], returnfitimage=False)
        (centroid, Wc) = compute_centroid(psf_stack[:, :, i],
                                          (param[3] + param[4]) / 2)
        U[i, 0] = centroid[0, 0] - double(shap[0]) / 2
        U[i, 1] = centroid[0, 1] - double(shap[1]) / 2
        centroid_out[i, 0] = centroid[0, 0]
        centroid_out[i, 1] = centroid[0, 1]
    return U, centroid_out
Exemple #10
0
 def gaussfit2d(self,img,returnfitimage=True,return_all=False):
     """ 
         2d gaussian params: (height, amplitude, center_x, center_y, width_x, width_y, theta) 
     """
     x_dim,y_dim = img.shape
     limitedmin = [False,False,True,True,True,True,True]
     limitedmax = [False,False,True,True,True,True,True]
     minpars = [0.0, 0.0, 0, 0, x_dim*0.1, y_dim*0.1, 0.0]
     maxpars = [0.0, 0.0, x_dim, y_dim, x_dim*0.8, y_dim*0.8, 360.0]
     usemoment= np.array([True,True,False,False,False,False,True],dtype='bool')
     #usemoment=np.array([],dtype='bool')
     params = [0.0, 0.0, x_dim/2, y_dim/2, x_dim/3, y_dim/3, 0.0]
     img = nd.filters.gaussian_filter(img,0.2)
     
     if returnfitimage:
         params,img = gaussfit(img,params=params,returnfitimage=True,limitedmin=limitedmin,\
                               limitedmax=limitedmax,minpars=minpars,maxpars=maxpars,usemoment=usemoment)
         return params,img
     elif return_all:
         params,errors = gaussfit(img,params=params,return_all=True,limitedmin=limitedmin,\
                                  limitedmax=limitedmax,minpars=minpars,maxpars=maxpars,usemoment=usemoment)
         return params,errors
Exemple #11
0
def gaussBoxFit(img, outLength):

    # ~ Given an image and extent from the central pixel, finds the brightest point and then returns the parameters of a gaussian curve fit to those

    bbout = findBrightBox(img, outLength)
    pixSet = bbout[0]
    sideLength = (outLength * 2) + 1
    if checkOnePeak(pixSet) == True:
        xes = np.arange((outLength * 2) + 1)
        bits = gaussfit((pixSet), return_error='True', circle='True')
        #drawGaussCircle(image, (bbout[2]+bits[0][2], bbout[1]+bits[0][3]), bits[0][4])
        return (bits)
    else:
        return ([np.zeros(5), np.zeros(5)])
def gaborfit(data,err=None,params=(),fixed=np.repeat(False,9),
             limitedmin=[False,False,False,False,True,True,True,True,True],
             limitedmax=[False,False,False,False,False,False,True,False,True],
             minpars=[0.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.0, 0.01, 0.0],
             maxpars=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 360.0, 0.0, 360.0],
             quiet=True,returnmp=False,return_all=False,returnfitimage=False,**kwargs):
    """ gabor params = (height,amplitude,center_x,center_y,width_x,width_y,theta,lambda,phi) """

    """ gaussian params=(height, amplitude, center_x, center_y, width_x, width_y, theta) """
    if len(params) == 0:
        params,_errors = gaussfit(data,limitedmin=limitedmin[:7],limitedmax=limitedmax[:7],\
                                  minpars=minpars[:7],maxpars=maxpars[:7],return_all=True)
        spatial_freq = math.sqrt(params[2]**2+params[3]**2)
        phase = 0.0
    
        params = np.append(params, np.array([spatial_freq, phase],dtype='float'))

    def mpfitfun(data,err):
        if err is None:
            def f(p,fjac=None): return [0,np.ravel(data-twodgabor(p)\
                    (*np.indices(data.shape)))]
        else:
            def f(p,fjac=None): return [0,np.ravel((data-twodgabor(p)\
                    (*np.indices(data.shape)))/err)]
        return f
    
    parinfo = [ 
                {'n':0,'value':params[0],'limits':[minpars[0],maxpars[0]],'limited':[limitedmin[0],limitedmax[0]],'fixed':fixed[0],'parname':"HEIGHT",'error':0},
                {'n':1,'value':params[1],'limits':[minpars[1],maxpars[1]],'limited':[limitedmin[1],limitedmax[1]],'fixed':fixed[1],'parname':"AMPLITUDE",'error':0},
                {'n':2,'value':params[2],'limits':[minpars[2],maxpars[2]],'limited':[limitedmin[2],limitedmax[2]],'fixed':fixed[2],'parname':"XSHIFT",'error':0},
                {'n':3,'value':params[3],'limits':[minpars[3],maxpars[3]],'limited':[limitedmin[3],limitedmax[3]],'fixed':fixed[3],'parname':"YSHIFT",'error':0},
                {'n':4,'value':params[4],'limits':[minpars[4],maxpars[4]],'limited':[limitedmin[4],limitedmax[4]],'fixed':fixed[4],'parname':"XWIDTH",'error':0},
                {'n':5,'value':params[5],'limits':[minpars[5],maxpars[5]],'limited':[limitedmin[5],limitedmax[5]],'fixed':fixed[5],'parname':"YWIDTH",'error':0},
                {'n':6,'value':params[6],'limits':[minpars[6],maxpars[6]],'limited':[limitedmin[6],limitedmax[6]],'fixed':fixed[6],'parname':"ROTATION",'error':0},
                {'n':7,'value':params[7],'limits':[minpars[7],maxpars[7]],'limited':[limitedmin[7],limitedmax[7]],'fixed':fixed[7],'parname':"SPATIALFREQ",'error':0},
                {'n':8,'value':params[8],'limits':[minpars[8],maxpars[8]],'limited':[limitedmin[8],limitedmax[8]],'fixed':fixed[8],'parname':"PHASE",'error':0} ]
    
    mp = mpfit(mpfitfun(data,err),parinfo=parinfo,quiet=quiet)
        
    if returnmp:
        returns = (mp)
    elif return_all == 0:
        returns = mp.params
    elif return_all == 1:
        returns = mp.params,mp.perror
    if returnfitimage:
        fitimage = twodgabor(mp.params)(*np.indices(data.shape))
        returns = (returns,fitimage)
    return returns
    
Exemple #13
0
def subreg(reference, images):
    bfixpix(reference,np.isnan(reference),n=8)
    kernel=reference[::-1,::-1]
    shifts=[]
    for im in images:
        bfixpix(im,np.isnan(im),n=8)
        cor = fftconvolve(im,kernel,mode='same')
        y,x=find_max_star(cor)
        g=gaussfit(cor[max(0, y-40):min(y+40, cor.shape[0]),
                       max(0, x-40):min(x+40, cor.shape[1])])
        shiftx=np.rint(cor.shape[1]/2.) - max(0,x-40)-g[2]
        shifty=np.rint(cor.shape[0]/2.) - max(0,y-40)-g[3]
        shifts.append((shifty,shiftx))
    return shifts
Exemple #14
0
def gaussBoxFitPoint(img, point, outLength):
    '''Given a point in an image and outLength to test, returns a 2d gaussian fit to the object around the point and its errors'''

    #print(point)
    bbout = getBrightBox(img, point, outLength)
    pixSet = bbout[0]
    # ~ sideLength = (outLength*2)+1
    if checkOnePeak(pixSet) == True:
        # ~ xes = np.arange((outLength*2)+1)
        bits = gaussfit((pixSet), return_error='True', circle='True')
        # ~ drawGaussCircle(img, (bbout[2]+bits[0][2], bbout[1]+bits[0][3]), bits[0][4])
        return ([bits[0], bits[1]])
    else:
        return ([np.zeros(5), np.zeros(5)])
def fitgaussian(sta, f_size=10):
    max_i = np.unravel_index(np.argmax(np.abs(sta)), sta.shape)
    try:
        sta, max_i_cut = msc.cut_around_center(sta, max_i, f_size)
    except ValueError as e:
        if str(e).startswith('Frame is out'):
            raise ValueError('Fit failed.')
    fit_frame = sta[..., max_i_cut[-1]]
    # Parameters are in the format:
    # (height,amplitude,center_x,center_y,width_x,width_y,rota)
    pars = gfit.gaussfit(fit_frame)
#    f = gfit.twodgaussian(pars)
    pars_out = pars
    pars_out[2:4] = pars[2:4] - [f_size, f_size] + max_i[:2]
    return pars_out
Exemple #16
0
def photon_finder_gaussians(image, centers):

    #commented for circle=1

    param_names = [
        'angle', 'd_angle', 'offset', 'd_offset', 'amplitude', 'd_amplitude',
        'xo', 'd_xo', 'yo', 'd_yo', 'sx', 'd_sx', 'sy', 'd_sy'
    ]
    res = {name: [] for name in param_names}

    for c in centers:
        xo, yo = tuple(map(int, c))
        fit, cov_p, infodict, errmsg = gaussfit(image[xo - 4:xo + 4,
                                                      yo - 4:yo + 4],
                                                circle=0,
                                                return_all=1)
        N = np.size(image[xo - 4:xo + 4, yo - 4:yo + 4])
        n = len(fit)
        s_sq = (infodict['fvec']**2).sum() / (
            N - n)  # infodict already contains f(x)-y, which is needed
        cov = cov_p * s_sq
        stds = [np.sqrt(cov[i][i]) for i in range(len(fit))]
        #params = dict(zip(param_names, np.round(fit, 6)))
        params = dict(zip(param_names, np.round(fit, 7)))
        #const, amp, x, y, s, angle = fit
        const, amp, x, y, sx, sy, angle = fit
        x += xo
        y += yo
        res['offset'].append(const)
        res['amplitude'].append(amp)
        res['xo'].append(x)
        res['yo'].append(y)
        #res['s'].append(s)
        res['sy'].append(sy)
        res['sx'].append(sx)
        res['angle'].append(angle)
        res['d_offset'].append(stds[0])
        res['d_amplitude'].append(stds[1])
        res['d_xo'].append(stds[2])
        res['d_yo'].append(stds[3])
        res['d_sx'].append(stds[4])
        res['d_sy'].append(stds[5])
        res['d_angle'].append(stds[6])

    fits = pd.DataFrame(res)
    #positions = pd.DataFrame(centers, columns=['xo', 'yo'])

    return fits
Exemple #17
0
def compute_centroid(im,sigw=None,nb_iter=4):
    """ Computes centroid.
    
    #TODO: would be interesting to compare with Sam's moments based computation
    
    Calls:
    
    * gaussfitter.gaussfit
    """
    if sigw is None:
        param=gaussfitter.gaussfit(im,returnfitimage=False)
        #print param
        sigw = (param[3]+param[4])/2
    sigw = float(sigw)
    n1 = im.shape[0]
    n2 = im.shape[1]
    rx = array(range(0,n1))
    ry = array(range(0,n2))
    Wc = ones((n1,n2))
    centroid = zeros((1,2))
    # Four iteration loop to compute the centroid
    i=0
    for i in range(0,nb_iter):

        xx = npma.outerproduct(rx-centroid[0,0],ones(n2))
        yy = npma.outerproduct(ones(n1),ry-centroid[0,1])
        W = npma.exp(-(xx**2+yy**2)/(2*sigw**2))
        centroid = zeros((1,2))
        # Estimate Centroid
        Wc = copy(W)
        if i == 0:Wc = ones((n1,n2))
        totx=0.0
        toty=0.0
        cx=0
        cy=0

        for cx in range(0,n1):
            centroid[0,0] += (im[cx,:]*Wc[cx,:]).sum()*(cx)
            totx += (im[cx,:]*Wc[cx,:]).sum()
        for cy in range(0,n2):
            centroid[0,1] += (im[:,cy]*Wc[:,cy]).sum()*(cy)
            toty += (im[:,cy]*Wc[:,cy]).sum()
        centroid = centroid*array([1/totx,1/toty])


    return (centroid,Wc)
Exemple #18
0
def compute_centroid(im, sigw=None, nb_iter=4):
    """ Computes centroid.
    
    #TODO: would be interesting to compare with Sam's moments based computation
    
    Calls:
    
    * gaussfitter.gaussfit
    """
    if sigw is None:
        param = gaussfitter.gaussfit(im, returnfitimage=False)
        #print param
        sigw = (param[3] + param[4]) / 2
    sigw = float(sigw)
    n1 = im.shape[0]
    n2 = im.shape[1]
    rx = array(range(0, n1))
    ry = array(range(0, n2))
    Wc = ones((n1, n2))
    centroid = zeros((1, 2))
    # Four iteration loop to compute the centroid
    i = 0
    for i in range(0, nb_iter):

        xx = npma.outerproduct(rx - centroid[0, 0], ones(n2))
        yy = npma.outerproduct(ones(n1), ry - centroid[0, 1])
        W = npma.exp(-(xx**2 + yy**2) / (2 * sigw**2))
        centroid = zeros((1, 2))
        # Estimate Centroid
        Wc = copy(W)
        if i == 0: Wc = ones((n1, n2))
        totx = 0.0
        toty = 0.0
        cx = 0
        cy = 0

        for cx in range(0, n1):
            centroid[0, 0] += (im[cx, :] * Wc[cx, :]).sum() * (cx)
            totx += (im[cx, :] * Wc[cx, :]).sum()
        for cy in range(0, n2):
            centroid[0, 1] += (im[:, cy] * Wc[:, cy]).sum() * (cy)
            toty += (im[:, cy] * Wc[:, cy]).sum()
        centroid = centroid * array([1 / totx, 1 / toty])

    return (centroid, Wc)
from astropy.io import fits
import aplpy
import pylab

import pyregion
import gaussfitter

cube = SpectralCube.read(dpath('W51Ku_BD_h2co_v30to90_natural_contsub.image.fits')).with_spectral_unit(u.km/u.s, velocity_convention='radio')

r = pyregion.open(rpath('W51_22_emission.reg'))
scube = cube.subcube_from_ds9region(pyregion.ShapeList([x for x in r if 'text' in x.attr[1] and x.attr[1]['text']=='W51NorthCore']))


vr = [56,61]
gaussfits = [gaussfitter.gaussfit(
    np.nan_to_num((scube[scube.closest_spectral_channel(v*u.km/u.s),:,:]/scube.max()).value),
    return_error=True )  for v in np.arange(vr[0],vr[1]+0.5,0.5)]

centroids = np.array([gf[0][2:4] for gf in gaussfits])
ecentroids = np.array([gf[1][2:4] for gf in gaussfits])

fig = pl.figure(1)
fig.clf()
ax = fig.gca()
sc = ax.scatter(centroids[:,0], centroids[:,1], marker='o', c=np.arange(vr[0],vr[1]+0.5,0.5), s=100)

# sc doesn't have facecolors until it is drawn
pl.draw()
itr = zip(centroids[:,0], centroids[:,1], ecentroids[:,0], ecentroids[:,1], sc.get_facecolors())

print itr
 def test_elliptical_fit(self):
     inpars = [0, 1, 64, 64, 4, 12, 30]
     in_data = gf.twodgaussian(inpars, shape=(128, 128))
     fit = gf.gaussfit(in_data)
     for inval, outval in zip(inpars, fit):
         self.assertAlmostEqual(inval, outval)
Exemple #21
0
    except ValueError:
        continue

    fit_frame = sta[:, :, max_i[2]]

    if np.max(fit_frame) != np.max(np.abs(fit_frame)):
        onoroff = -1
    else:
        onoroff = 1

    Y, X = np.meshgrid(np.arange(fit_frame.shape[1]),
                       np.arange(fit_frame.shape[0]))

    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', '.*divide by zero*.', RuntimeWarning)
        pars = gfit.gaussfit(fit_frame * onoroff)
        f = gfit.twodgaussian(pars)
        Z = f(X, Y)

    # Correcting for Mahalonobis dist.
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', '.*divide by zero*.', RuntimeWarning)
        Zm = np.log((Z - pars[0]) / pars[1])
    Zm[np.isinf(Zm)] = np.nan
    Zm = np.sqrt(Zm * -2)

    ax = plt.subplot(1, 2, 1)
    plf.subplottext('A', ax, x=0, y=1.3)

    vmax = np.abs(fit_frame).max()
    vmin = -vmax
Exemple #22
0
#epoch1reproj = FITS_tools.hcongrid.hcongrid(epoch1[0].data, epoch1header, epoch3header)
epoch1reproj, footprint = reproject.reproject(epoch1[0], epoch3header)

scalefactor = (beam3.sr/beam1.sr).value

xshift,yshift, ex, ey = image_registration.chi2_shift(epoch3[0].data, epoch1reproj*scalefactor, err=0.001)

center = coordinates.SkyCoord(290.92443*u.deg, 14.515755*u.deg, frame='fk5')
xx,yy = wcs1.wcs_world2pix([[center.fk4.ra.deg, center.fk4.dec.deg]], 0)[0]
subim1 = epoch1[0].data[yy-10:yy+10, xx-10:xx+10]
wcs1sub = wcs1[yy-10:yy+10, xx-10:xx+10]
xx,yy = wcs3.wcs_world2pix([[center.fk5.ra.deg, center.fk5.dec.deg]], 0)[0]
wcs3sub = wcs3[yy-10:yy+10, xx-10:xx+10]
subim3 = epoch3[0].data[yy-10:yy+10, xx-10:xx+10]
gf1 = gaussfitter.gaussfit(subim1)
gf3 = gaussfitter.gaussfit(subim3)

dx1,dy1 = gf1[2:4]
dx3,dy3 = gf3[2:4]
dx_gf, dy_gf = dx3-dx1, dy3-dy1

epoch1matched_fft = image_registration.fft_tools.shift2d(epoch1reproj*scalefactor, -xshift, -yshift)
epoch1matched = image_registration.fft_tools.shift2d(epoch1reproj*scalefactor, dx_gf, dy_gf)

diff =  epoch3[0].data - epoch1matched
diffhdu = fits.PrimaryHDU(data=diff, header=epoch3header)
diffhdu.writeto(paths.dpath("Cband_Epoch3-Epoch1.fits"), clobber=True, output_verify='fix')

smooth_beam = beam1.deconvolve(beam3)
kernel = smooth_beam.major
Exemple #23
0
def main():
	
	file = open(outputFolder+'onOff.csv', "w")
	
	header = 'Unidad\t'+'OnOff\t'+'PeakFrame''\n'
	file.write(header)
	maxDataSTAValue = 0
	minDataSTAValue = 0
	for unitFile in sorted(os.listdir(sourceFolder)):
		unitName = unitFile.rsplit('_', 1)[0]
		if os.path.isdir(sourceFolder+unitFile):
			# The STA matrix is named as M8a_lineal/sta_array_M8a.mat
			staMatrixFile = scipy.io.loadmat(sourceFolder+unitFile+'/sta_array_'+unitName+'.mat')
			staMatrix = staMatrixFile['STA_array']
			staMatrix = staMatrix[:,:,1:18]
			
			dataUnit, coordinates = rfe.loadSTACurve(sourceFolder,unitFile,unitName)
			
			print unitName
			print 'coordinates',coordinates
			
			media = numpy.mean(staMatrix)
			maximo = numpy.amax(staMatrix)
			minimo = numpy.amin(staMatrix)
			maximaDistancia = numpy.absolute(maximo-media)
			minimaDistancia = numpy.absolute(minimo-media) 
			if maximaDistancia > minimaDistancia:
				frame = numpy.where(maximo==staMatrix)[2][0]
				linea = '"'+unitName+'"\t"On"\t"'+ str(frame + 1) + '\"\n'
			else:
				frame = numpy.where(minimo==staMatrix)[2][0]
				linea = '"'+unitName+'"\t"Off"\t"'+ str(frame + 1) + '\"\n'
				
			data = gf.moments(staMatrix[:,:,frame],circle=0,rotate=1,vheight=1)
			print 'frame',frame
			print 'height',data[0]
			print 'amplitude',data[1]
			print 'x',data[2]
			print 'y',data[3]
			print 'width_x',data[4]
			print 'width_y',data[5]
			print 'rotation',data[6]
			
			curva = staMatrix[data[3],data[2],:]
			
			from numpy import linspace,exp
			from numpy.random import randn
			import matplotlib.pyplot as plt
			from scipy.interpolate import UnivariateSpline
			x = linspace(1, len(curva), len(curva))
			s = UnivariateSpline(x, curva, s=1)
			xs = linspace(1, len(curva), len(curva)*10)
			ys = s(xs)
			plt.plot(x, curva,'r')
			plt.plot(xs, ys,'b')
			plt.savefig(outputFolder+unitName+"_spline.png")
			plt.close()
			
				
			dataFit = gf.gaussfit(staMatrix[:,:,frame],autoderiv=1, \
				 return_all=1,circle=0,fixed=numpy.repeat(False,7), \
				 limitedmin=[False,False,False,False,True,True,True], \
				 limitedmax=[False,False,False,False,False,False,True], \
				 usemoment=[1,1,1,1],minpars=numpy.repeat(0,7),maxpars=[0,0,0,0,0,0,360], \
				 rotate=1,vheight=1,quiet=True,returnmp=False, \
				 returnfitimage=True)

			fig = plt.figure(1, figsize=(10,10))
			ax = fig.add_subplot(111)
			plt.imshow(dataFit[1])

			plt.savefig(outputFolder+unitName+".png")
			plt.close()
				
			file.write(linea)
	file.close
	return 0
Exemple #24
0
from __future__ import division
import pydc1394 as fw
from time import sleep, time
import numpy as np
import pylab as pl
import matplotlib.cm as cm

import gaussfitter as gf

if __name__ == "__main__":
    filename = 'img_343444'
    # filename = 'img'
    data = np.load('%s.npy' %filename)

    fit = gf.gaussfit(data)
    cmap = cm.gist_gray
    cmap = cm.jet

    fig = pl.figure(num=1, figsize=(11.69, 8.27))
    pl.subplot(221)
    pl.imshow(data, cmap=cmap)
    pl.title('Image')
    pl.xlabel('x')
    pl.ylabel('y')

    pl.subplot(224)
    fdata = np.ravel((gf.twodgaussian(fit,0,1,1)\
                (*np.indices(data.shape)))).reshape(np.shape(data))
    pl.imshow(fdata)
    pl.xlabel('Fitted data')
def star_analysis(target_x, target_y):
    snippet_size = 50 # sub_image half_width

    full_image_data = pyfits.getdata(image_filepath)
    
    if (target_x < snippet_size) or (target_x > full_image_data.shape[1] - snippet_size) or (target_y < snippet_size) or (target_y > full_image_data.shape[0] - snippet_size):
        print "You must select a position at least", snippet_size, "pixels from the edge of the image. Aborting."
        return
    
    sub_image_data = full_image_data[target_y-snippet_size:target_y+snippet_size, target_x-snippet_size:target_x+snippet_size]

    # Run the 2D gaussian fit
    gauss_fit_results = gaussfit(sub_image_data)
    # gauss_fit_results has the format:
    # (height, amplitude, x, y, width_x, width_y, rotation angle), image
    gauss_fit_height = gauss_fit_results[0]
    gauss_fit_amplitude = gauss_fit_results[1]
    gauss_fit_x_center = gauss_fit_results[2]
    gauss_fit_y_center = gauss_fit_results[3]
    gauss_fit_x_width = gauss_fit_results[4]
    gauss_fit_y_width = gauss_fit_results[5]
    gauss_fit_x_fwhm = 2.35482*gauss_fit_x_width
    gauss_fit_y_fwhm = 2.35482*gauss_fit_y_width
    gauss_fit_fwhm = (gauss_fit_x_fwhm + gauss_fit_y_fwhm)/2.
    gauss_fit_ecc = (abs(1 - gauss_fit_x_fwhm/gauss_fit_y_fwhm))

    sub_image_data = sub_image_data - gauss_fit_height

    z = []
    x = []
    y = []
    for i in range(len(sub_image_data)):
        i_pixel = i + 0
        for j in range(len(sub_image_data[i])):
            if sub_image_data[i][j] != 0:
                j_pixel = j + 0
                x.append(j_pixel)
                y.append(i_pixel)
                z.append(sub_image_data[i][j])


    r = ((x - gauss_fit_x_center)**2 + (y - gauss_fit_y_center)**2)**0.5

    fit_curve_r = linspace(0, snippet_size, 400)
    gauss_fit_curve = gauss_fit_height + gauss_fit_amplitude * exp(-1* (fit_curve_r)**2 / (2 * ( (gauss_fit_x_width + gauss_fit_y_width)/2.)**2))



    fit_params = fit_moffat(gauss_fit_x_center, gauss_fit_y_center, z, x, y)
    central_height, center_x, center_y, alpha, beta = fit_params
    fwhm_moffat = 2*alpha*sqrt(2**(1/beta) - 1)

    rad = sqrt( (x-center_x)**2 + (y-center_y)**2 )
    h = where( rad < 3 * fwhm_moffat )
    z_cropped = array(z)[h]
    x_cropped = array(x)[h]
    y_cropped = array(y)[h]
    r_cropped = array(r)[h]

    fit_params = fit_moffat(center_x, center_y, z_cropped, x_cropped, y_cropped)

    central_height, center_x, center_y, alpha, beta = fit_params
    fwhm_moffat = 2*alpha*sqrt(2**(1/beta) - 1)

    r2 = fit_curve_r**2
    moffat_fit_curve = (central_height * (1 + (r2/(alpha**2)))**(-beta))


    h_annulus = where((rad > 10 * fwhm_moffat) * (rad < 12 * fwhm_moffat))
    z_annulus = array(z)[h_annulus]
    r_annulus = array(r)[h_annulus]


    clf()
    scatter(r_cropped, z_cropped, color="red", alpha=0.5)
    # scatter(r_annulus, z_annulus, color="orange", alpha=0.1)
    plot(fit_curve_r, gauss_fit_curve, color="blue", label="Gaussian Fit FWHM=" + str(round(gauss_fit_fwhm, 3)))
    plot(fit_curve_r, moffat_fit_curve, color="green", label="Moffat Fit FWHM=" + str(round(fwhm_moffat, 3)))
#     plot(fit_curve_r, median(z_annulus)*ones(fit_curve_r.size), color="green", label="Sky Background=" + str(round(median(z_annulus), 3)))
    xlim(0, fwhm_moffat*3.06)
    ylim(max(gauss_fit_height+gauss_fit_amplitude, central_height)*-0.1, max(gauss_fit_height+gauss_fit_amplitude, central_height)*1.05)
    xlabel("Radius [px]")
    ylabel("Flux Counts [ADU]")
    legend(loc=1)
    show()
    return
Exemple #26
0
        # rr = radius grid
        rr = ((xx-rp*3)**2+(yy-rp*3)**2)**0.5
        # mask = pixels within 1 beam radius
        mask = rr<rp
        if mask.sum() and np.isfinite(co[mask]).sum() > 12:
            peaks[freq][name] = co[mask].max()
            valleys[freq][name] = co.min()
            params = [co.min(), co[mask].max(), 3*rp, 3*rp, 2.0, 2.0, 45.0]

            # fit NaN-containing images
            co[~np.isfinite(co)] = 0.0

            try:
                mp, fitimg = gaussfitter.gaussfit(co, params=params,
                                                  err=error[freq],
                                                  returnmp=True, rotate=True,
                                                  vheight=True, circle=False,
                                                  returnfitimage=True)
            except:
                mpfits[freq][name] = ()
                gpars[freq][name] = np.array([np.nan]*7)
                gparerrs[freq][name] = np.array([np.nan]*7)
                continue

            mpfits[freq][name] = mp
            gpars[freq][name] = mp.params
            gparerrs[freq][name] = mp.perror
            gfits[freq][name] = fitimg

            wcsX,wcsY = wcs.wcs_pix2world(mp.params[2]+int(xc-3*rp), mp.params[3]+int(yc-3*rp), 0)
            center_coord = coordinates.SkyCoord(wcsX*u.deg, wcsY*u.deg,
Exemple #27
0
    for i in range(ring_sizes2[0], ring_sizes2[1]):
        plt.subplot(4, 2, 3)
        masked = mf.ringmask(sta, max_i, i)
        if not masked[1]:
            plt.plot(np.mean(masked[0], axis=(0, 1)), label=str(i))
    plt.legend()

    # %% Fit 2D Gaussian
    #    fit_frame = sta[:, :, max_i[2]]
    #    f_size = 20
    #
    #    if f_size is not 0:
    #        fit_frame = fit_frame[max_i[0]-f_size:max_i[0]+f_size+1,
    #                              max_i[1]-f_size:max_i[1]+f_size+1]

    pars = gfit.gaussfit(fit_frame)

    f = gfit.twodgaussian(pars)

    Y, X = np.meshgrid(np.arange(fit_frame.shape[1]),
                       np.arange(fit_frame.shape[0]))
    Z = f(X, Y)
    # Correcting for Mahalonobis dist.
    # Using a second variable Zm2 to not break how it currently works and
    # easily revert
    Zm2 = np.log((Z - pars[0]) / pars[1])
    Zm2[np.isinf(Zm2)] = np.nan
    Zm = np.sqrt(Zm2 * -2)
    # To workaround negative values from before, remove minus from Zm comparisons to fix this
    #    Zm = -Zm
    #    Zmr = np.ceil(Zm)
Exemple #28
0
def plotcheckersurround(exp_name, stim_nr, filename=None, spikecutoff=1000,
                        ratingcutoff=4, staqualcutoff=0, inner_b=2,
                        outer_b=4):

    """
    Divides into center and surround by fitting 2D Gaussian, and plot
    temporal components.

    spikecutoff:
        Minimum number of spikes to include.

    ratingcutoff:
        Minimum spike sorting rating to include.

    staqualcutoff:
        Minimum STA quality (as measured by z-score) to include.

    inner_b:
        Defined limit between receptive field center and surround
        in units of sigma.

    outer_b:
        Defined limit of the end of receptive field surround.
    """

    exp_dir = iof.exp_dir_fixer(exp_name)
    stim_nr = str(stim_nr)
    if filename:
        filename = str(filename)

    if not filename:
        savefolder = 'surroundplots'
        label = ''
    else:
        label = filename.strip('.npz')
        savefolder = 'surroundplots_' + label

    _, metadata = asc.read_spikesheet(exp_name)
    px_size = metadata['pixel_size(um)']

    data = iof.load(exp_name, stim_nr, fname=filename)

    clusters = data['clusters']
    stas = data['stas']
    stx_h = data['stx_h']
    exp_name = data['exp_name']
    stimname = data['stimname']
    max_inds = data['max_inds']
    frame_duration = data['frame_duration']
    filter_length = data['filter_length']
    quals = data['quals'][-1, :]

    spikenrs = data['spikenrs']

    c1 = np.where(spikenrs > spikecutoff)[0]
    c2 = np.where(clusters[:, 2] <= ratingcutoff)[0]
    c3 = np.where(quals > staqualcutoff)[0]

    choose = [i for i in range(clusters.shape[0]) if ((i in c1) and
                                                      (i in c2) and
                                                      (i in c3))]
    clusters = clusters[choose]
    stas = list(np.array(stas)[choose])
    max_inds = list(np.array(max_inds)[choose])

    clusterids = plf.clusters_to_ids(clusters)

    t = np.arange(filter_length)*frame_duration*1000

    # Determine frame size so that the total frame covers
    # an area large enough i.e. 2*700um
    f_size = int(700/(stx_h*px_size))

    del data

    for i in range(clusters.shape[0]):

        sta_original = stas[i]
        max_i_original = max_inds[i]

        try:
            sta, max_i = mf.cut_around_center(sta_original,
                                              max_i_original, f_size)
        except ValueError:
            continue

        fit_frame = sta[:, :, max_i[2]]

        if np.max(fit_frame) != np.max(np.abs(fit_frame)):
            onoroff = -1
        else:
            onoroff = 1



        Y, X = np.meshgrid(np.arange(fit_frame.shape[1]),
                           np.arange(fit_frame.shape[0]))

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore',
                                    '.*divide by zero*.', RuntimeWarning)
            pars = gfit.gaussfit(fit_frame*onoroff)
            f = gfit.twodgaussian(pars)
            Z = f(X, Y)

        # Correcting for Mahalonobis dist.
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore',
                                    '.*divide by zero*.', RuntimeWarning)
            Zm = np.log((Z-pars[0])/pars[1])
        Zm[np.isinf(Zm)] = np.nan
        Zm = np.sqrt(Zm*-2)

        ax = plt.subplot(1, 2, 1)

        plf.stashow(fit_frame, ax)
        ax.set_aspect('equal')

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=UserWarning)
            warnings.filterwarnings('ignore', '.*invalid value encountered*.')
            ax.contour(Y, X, Zm, [inner_b, outer_b],
                       cmap=plf.RFcolormap(('C0', 'C1')))

        barsize = 100/(stx_h*px_size)
        scalebar = AnchoredSizeBar(ax.transData,
                                   barsize, '100 µm',
                                   'lower left',
                                   pad=1,
                                   color='k',
                                   frameon=False,
                                   size_vertical=.2)
        ax.add_artist(scalebar)

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore',
                                    '.*invalid value encountered in*.',
                                    RuntimeWarning)
            center_mask = np.logical_not(Zm < inner_b)
            center_mask_3d = np.broadcast_arrays(sta,
                                                 center_mask[..., None])[1]
            surround_mask = np.logical_not(np.logical_and(Zm > inner_b,
                                                          Zm < outer_b))
            surround_mask_3d = np.broadcast_arrays(sta,
                                                   surround_mask[..., None])[1]

        sta_center = np.ma.array(sta, mask=center_mask_3d)
        sta_surround = np.ma.array(sta, mask=surround_mask_3d)

        sta_center_temporal = np.mean(sta_center, axis=(0, 1))
        sta_surround_temporal = np.mean(sta_surround, axis=(0, 1))

        ax1 = plt.subplot(1, 2, 2)
        l1 = ax1.plot(t, sta_center_temporal,
                      label='Center\n(<{}σ)'.format(inner_b),
                      color='C0')
        sct_max = np.max(np.abs(sta_center_temporal))
        ax1.set_ylim(-sct_max, sct_max)
        ax2 = ax1.twinx()
        l2 = ax2.plot(t, sta_surround_temporal,
                      label='Surround\n({}σ<x<{}σ)'.format(inner_b, outer_b),
                      color='C1')
        sst_max = np.max(np.abs(sta_surround_temporal))
        ax2.set_ylim(-sst_max, sst_max)
        plf.spineless(ax1)
        plf.spineless(ax2)
        ax1.tick_params('y', colors='C0')
        ax2.tick_params('y', colors='C1')
        plt.xlabel('Time[ms]')
        plt.axhline(0, linestyle='dashed', linewidth=1)

        lines = l1+l2
        labels = [line.get_label() for line in lines]
        plt.legend(lines, labels, fontsize=7)
        plt.title('Temporal components')
        plt.suptitle(f'{exp_name}\n{stimname}\n{clusterids[i]}')

        plt.subplots_adjust(wspace=.5, top=.85)

        plotpath = os.path.join(exp_dir, 'data_analysis',
                                stimname, savefolder)
        if not os.path.isdir(plotpath):
            os.makedirs(plotpath, exist_ok=True)

        plt.savefig(os.path.join(plotpath, clusterids[i])+'.svg',
                    format='svg', dpi=300)
        plt.close()
    print(f'Plotted checkerflicker surround for {stimname}')
import gaussfitter

input_parameters = [0, 1, 25, 25, 3, 6, 30]
print("Input parameters are: {0}".format(input_parameters))
example_gaussian = gaussfitter.twodgaussian(input_parameters, shape=[50,50],)

moments = gaussfitter.moments(example_gaussian, circle=False, rotate=True, vheight=True)

print("These are the input guesses computed using 'moments':")
print(moments)

print("This is from the gaussian fit; it works well:")
print(gaussfitter.gaussfit(example_gaussian))
def compact_fit():
  params,gaussdata=g.gaussfit(img,returnfitimage=True) #gives height, amp, center_x, center_y, widthx, widthy, rotation
  height,amp,cx,cy,widthx,widthy,rot=params
  return height,amp,cx,cy,widthx,widthy,rot,gaussdata
Exemple #31
0
def peak_attribs_image(image,
                       peak_width=None,
                       target_locations=None,
                       medfilt_radius=None,
                       xc_filter=True,
                       progress_object=PyFaceProgress(),
                       kill_edges=True,
                       kill_duplicates=True):
    """
    Characterizes the peaks in an image.

        Parameters:
        ----------

        peak_width : int (optional)
                expected peak width.  Affects characteristic fitting window.
                Too big, and you'll include other peaks in the measurement.  
                Too small, and you'll get spurious peaks around your peaks.
                Default is None (attempts to auto-detect)

        target_locations : numpy array (n x 2)
                array of n target locations.  If left as None, will create 
                target locations by locating peaks on the average image of the stack.
                default is None (peaks detected from average image)

        medfilt_radius : int (optional)
                median filter window to apply to smooth the data
                (see scipy.signal.medfilt)
                if 0, no filter will be applied.
                default is set to 5

        Returns:
        -------

        2D numpy array:
        - One row per peak
        - 7 columns:
          0,1 - location
          2 - height
          3,4 - long and short axis length
          5 - orientation
          6 - eccentricity
          7,8 - skew

    """
    try:
        import cv
    except:
        try:
            import cv2.cv as cv
        except:
            print 'Module %s:' % sys.modules[__name__]
            print 'OpenCV is not available, the peak characterization functions will not work.'
            return None
    if target_locations is None:
        # target locations should be a list of arrays.  Each element in the
        #    list corresponds to a recursion level in peak finding.  The peak
        #    width can change with each recursion.
        target_locations = two_dim_findpeaks(image,
                                             peak_width=peak_width,
                                             medfilt_radius=medfilt_radius,
                                             xc_filter=xc_filter,
                                             kill_edges=kill_edges,
                                             kill_duplicates=kill_duplicates)
    imsize = image.shape[0]

    total_peaks = target_locations.shape[0]
    rlt = np.zeros((total_peaks, 9))

    if progress_object is not None:
        progress_object.initialize("Characterizing peaks", total_peaks)

    rlt_offset = 0

    if peak_width is None:
        peak_width = estimate_peak_width(image)

    r = int(np.ceil(peak_width / 2))
    roi = np.zeros((r * 2, r * 2))
    mask = draw_mask((r * 2, r * 2), r, [(r, r)])
    for c in target_locations:
        peak_left = int(c[0] - r)
        peak_top = int(c[1] - r)
        peak_right = int(c[0] + r)
        peak_bottom = int(c[1] + r)
        #if bxmin<0: bxmin=0; bxmax=peak_width
        #if bymin<0: bymin=0; bymax=peak_width
        #if bxmax>imsize: bxmax=imsize; bxmin=imsize-peak_width
        #if bymax>imsize: bymax=imsize; bymin=imsize-peak_width
        # skip peaks that are too close to edges.
        if (peak_right) > image.shape[1] + r / 4 or (
                peak_bottom) > image.shape[0] + r / 4:
            if progress_object is not None:
                progress_object.increment()
            continue
        # set the neighborhood of the peak to zero so we go look elsewhere
        #  for other peaks
        x = np.array(np.arange(peak_left, peak_right), dtype=np.integer)
        y = np.array(np.arange(peak_top, peak_bottom), dtype=np.integer)
        xv, yv = np.meshgrid(x, y)
        roi[:, :] = image[xv.clip(0, image.shape[0] - 1),
                          yv.clip(0, image.shape[1] - 1)] * mask
        #roi[0:,:]=image[bymin:bymax,bxmin:bxmax]
        # skip frames with significant dead pixels (corners of
        #    rotated images, perhaps
        #if np.average(roi)< 0.5*np.average(image):
        #rlt[loc,:2] = (c[1], c[0])
        #continue
        ms = cv.Moments(cv.fromarray(roi))
        # output from get_characteristics is:
        # x, y, height, long_axis, short_axis, orientation, eccentricity, skew_x, skew_y
        rlt[rlt_offset] = get_characteristics(ms)

        # order for these is:
        # amp, xShift, yShift, xWidth, height, yWidth, Rotation
        #  WTF???  Why is this different from return order!?
        # I'm a control freak...
        limit_min = [True, True, True, True, True, True, True]
        limit_max = [True, True, True, True, True, True, True]

        # 30 pixels seems like a hell of a lot for a peak...
        max_width = 30
        max_height = 1.2 * np.max(roi)
        ctr = np.array(roi.shape) / 2
        min_height = np.mean(image) / 1.5

        x = rlt[rlt_offset][0]
        y = rlt[rlt_offset][1]
        amp = image[int(peak_left + x), int(peak_top + y)]
        long_axis = rlt[rlt_offset][3]
        short_axis = rlt[rlt_offset][4]
        orientation = rlt[rlt_offset][5]
        height = 0
        params = [amp, x, y, long_axis, height, short_axis, orientation]

        minpars = [min_height, x - 2, y - 2, 0, 0, 0, 0]
        maxpars = [max_height, x + 2, y + 2, max_width, 0, max_width, 360]

        # TODO: could use existing moments or parameters to speed up...

        amp, fit_x, fit_y, width_x, height, width_y, rot = gaussfit(
            roi,
            limitedmin=limit_min,
            limitedmax=limit_max,
            maxpars=maxpars,
            minpars=minpars,
            params=params)
        # x and y are the locations within the ROI.  Add the coordinates of
        #    the top-left corner of our ROI on the global image.
        rlt[rlt_offset, :2] = (np.array([peak_left, peak_top]) +
                               np.array([fit_x, fit_y]))
        #rlt[loc,:2] = np.array([y,x])
        # insert the height
        rlt[rlt_offset, 2] = amp + height
        # TODO: compare this with OpenCV moment calculation above:
        #  (this is using the gaussfitter value)
        rlt[rlt_offset, 5] = rot
        rlt_offset += 1
        if progress_object is not None:
            progress_object.increment()
    # chuck outliers based on median of height
    d = np.abs(rlt[:, 2] - np.median(rlt[:, 2]))
    mdev = np.median(d)
    s = d / mdev if mdev else 0
    # kill outliers based on height
    rlt = rlt[np.logical_and(s < 10, rlt[:, 2] < image.max() * 1.3)]
    # kill peaks that are too close to other peaks
    # the minimum width is 1.5 times the smallest peak width.
    #min_width = 1.5*target_locations[-1][0][3]
    #rlt = kill_duplicates(rlt,min_width)
    return rlt
Exemple #32
0
def peak_attribs_image(image, peak_width=None, target_locations=None, medfilt_radius=None, xc_filter=True,
                       progress_object=PyFaceProgress(), kill_edges=True, kill_duplicates=True):
    """
    Characterizes the peaks in an image.

        Parameters:
        ----------

        peak_width : int (optional)
                expected peak width.  Affects characteristic fitting window.
                Too big, and you'll include other peaks in the measurement.  
                Too small, and you'll get spurious peaks around your peaks.
                Default is None (attempts to auto-detect)

        target_locations : numpy array (n x 2)
                array of n target locations.  If left as None, will create 
                target locations by locating peaks on the average image of the stack.
                default is None (peaks detected from average image)

        medfilt_radius : int (optional)
                median filter window to apply to smooth the data
                (see scipy.signal.medfilt)
                if 0, no filter will be applied.
                default is set to 5

        Returns:
        -------

        2D numpy array:
        - One row per peak
        - 7 columns:
          0,1 - location
          2 - height
          3,4 - long and short axis length
          5 - orientation
          6 - eccentricity
          7,8 - skew

    """
    try:
        import cv
    except:
        try:
            import cv2.cv as cv
        except:
            print 'Module %s:' % sys.modules[__name__]
            print 'OpenCV is not available, the peak characterization functions will not work.'
            return None
    if target_locations is None:
        # target locations should be a list of arrays.  Each element in the 
        #    list corresponds to a recursion level in peak finding.  The peak 
        #    width can change with each recursion.
        target_locations=two_dim_findpeaks(image, peak_width=peak_width, medfilt_radius=medfilt_radius,
                                           xc_filter=xc_filter, kill_edges=kill_edges, 
                                           kill_duplicates=kill_duplicates)
    imsize=image.shape[0]
    
    total_peaks = target_locations.shape[0]
    rlt=np.zeros((total_peaks,9))
    
    if progress_object is not None:
        progress_object.initialize("Characterizing peaks", total_peaks)
    
    rlt_offset=0
    
    if peak_width is None:
        peak_width=estimate_peak_width(image)

    r=int(np.ceil(peak_width/2))
    roi=np.zeros((r*2,r*2))
    mask = draw_mask((r*2,r*2), r, [(r,r)])
    for c in target_locations:
        peak_left=int(c[0]-r)
        peak_top=int(c[1]-r)
        peak_right=int(c[0]+r)
        peak_bottom=int(c[1]+r)
        #if bxmin<0: bxmin=0; bxmax=peak_width
        #if bymin<0: bymin=0; bymax=peak_width
        #if bxmax>imsize: bxmax=imsize; bxmin=imsize-peak_width
        #if bymax>imsize: bymax=imsize; bymin=imsize-peak_width            
        # skip peaks that are too close to edges.
        if (peak_right)>image.shape[1]+r/4 or (peak_bottom)>image.shape[0]+r/4:
            if progress_object is not None:
                progress_object.increment()
            continue
        # set the neighborhood of the peak to zero so we go look elsewhere
                    #  for other peaks            
        x = np.array(np.arange(peak_left, peak_right),dtype=np.integer)
        y = np.array(np.arange(peak_top, peak_bottom),dtype=np.integer)
        xv,yv = np.meshgrid(x,y)
        roi[:,:] = image[xv.clip(0,image.shape[0]-1),
                         yv.clip(0,image.shape[1]-1)] * mask
        #roi[0:,:]=image[bymin:bymax,bxmin:bxmax]
        # skip frames with significant dead pixels (corners of
        #    rotated images, perhaps
        #if np.average(roi)< 0.5*np.average(image):
        #rlt[loc,:2] = (c[1], c[0])
        #continue
        ms=cv.Moments(cv.fromarray(roi))
        # output from get_characteristics is:
        # x, y, height, long_axis, short_axis, orientation, eccentricity, skew_x, skew_y
        rlt[rlt_offset] = get_characteristics(ms)
        
        # order for these is:
        # amp, xShift, yShift, xWidth, height, yWidth, Rotation
        #  WTF???  Why is this different from return order!?
        # I'm a control freak...
        limit_min = [True, True, True, True, True, True, True]
        limit_max = [True, True, True, True, True, True, True]
        
        # 30 pixels seems like a hell of a lot for a peak...
        max_width=30
        max_height = 1.2*np.max(roi)
        ctr = np.array(roi.shape)/2
        min_height = np.mean(image)/1.5
        
        x = rlt[rlt_offset][0]
        y = rlt[rlt_offset][1]
        amp = image[int(peak_left+x),int(peak_top+y)]
        long_axis = rlt[rlt_offset][3]
        short_axis = rlt[rlt_offset][4] 
        orientation = rlt[rlt_offset][5]
        height = 0
        params = [amp, x, y, long_axis, height, short_axis, orientation]
        
        minpars = [min_height, x-2, y-2, 0, 0, 0, 0]
        maxpars = [max_height, x+2, y+2, max_width, 0, max_width, 360]
        
        # TODO: could use existing moments or parameters to speed up...
        
        amp, fit_x, fit_y, width_x, height, width_y, rot = gaussfit(roi,
                                    limitedmin=limit_min, 
                                    limitedmax=limit_max,
                                    maxpars=maxpars,
                                    minpars=minpars,
                                    params=params
                                    )
        # x and y are the locations within the ROI.  Add the coordinates of 
        #    the top-left corner of our ROI on the global image.
        rlt[rlt_offset,:2] = (np.array([peak_left,peak_top]) + np.array([fit_x,fit_y]))
        #rlt[loc,:2] = np.array([y,x])
        # insert the height
        rlt[rlt_offset,2]=amp+height
        # TODO: compare this with OpenCV moment calculation above:
        #  (this is using the gaussfitter value)
        rlt[rlt_offset,5]=rot
        rlt_offset+=1
        if progress_object is not None:
            progress_object.increment()
    # chuck outliers based on median of height
    d = np.abs(rlt[:,2] - np.median(rlt[:,2]))
    mdev = np.median(d)
    s = d/mdev if mdev else 0
    # kill outliers based on height
    rlt=rlt[np.logical_and(s<10, rlt[:,2]<image.max()*1.3)]
    # kill peaks that are too close to other peaks
    # the minimum width is 1.5 times the smallest peak width.
    #min_width = 1.5*target_locations[-1][0][3]
    #rlt = kill_duplicates(rlt,min_width)
    return rlt
    r = pyregion.open(rpath('W51_22_emission.reg'))
    r_subset = pyregion.ShapeList([x for x in r if 'text' in x.attr[1] and
                                   x.attr[1]['text']=='e8mol_ext'])
    scube = cube.subcube_from_ds9region(r_subset)
    pixscale = scube.wcs.pixel_scale_matrix[1,1]*u.deg
    pixscale_as = pixscale.to(u.arcsec)

    cubemax = scube.max() # normalize everything
    noise = scube.spectral_slab(30*u.km/u.s, 50*u.km/u.s).std(axis=0)
    noise[np.isnan(noise.value)] = 1e10*noise.unit
    noise_norm = (noise / cubemax).value

    dv = cube.spectral_axis.diff().mean()
    vr = [57,64]
    gaussfits = [gaussfitter.gaussfit(
        np.nan_to_num((scube[scube.closest_spectral_channel(v*u.km/u.s),:,:]/cubemax).value),
        err=noise_norm,
        return_error=True)
        for v in ProgressBar(np.arange(vr[0],vr[1]+dv.value,dv.value))]

    integrated_image = (scube.spectral_slab(vr[0]*u.km/u.s,
                                            vr[1]*u.km/u.s).moment0(axis=0))
    gaussfit_total,fitimage = gaussfitter.gaussfit(np.nan_to_num(integrated_image.value),
                                                   err=noise.value,
                                                   returnfitimage=True
                                         )
    # sanity check: make sure the fit is OK (it is)
    pl.figure(2).clf()
    pl.imshow(integrated_image.value, cmap=pl.cm.bone_r)
    cb = pl.colorbar()
    pl.contour(fitimage, cmap=pl.cm.spectral)
    pl.savefig(fpath('sanitycheck_w51e8_core_gaussfits_{0}.png'.format(name)))
    x,y = cube.wcs.sub([wcs.WCSSUB_CELESTIAL]).wcs_world2pix([region.coord_list], 0)[0]

    # not sure why, but it looks like all the images are offset by 1 pix
    x = x+1
    y = y+1

    cutout = cube[channel, y-dy:y+dy, x-dx:x+dx].value

    rms = cube[channel, :20, :20].std()

    #params: (height, amplitude, x, y, width_x, width_y, rota)
    (pars, errors), fitimage = gaussfit(cutout, err=rms,
                                        params=(0, cutout[dy,dx], dx, dy, 1, 1, 0),
                                        limitedmin=[False,False,True,True,True,True,True],
                                        limitedmax=[False,False,True,True,False,False,True],
                                        minpars=[0,0,dx-1,dy-1,0,0,0],
                                        maxpars=[0,0,dx+1,dy+1,2.5,2.5,180],
                                        returnfitimage=True,
                                        return_error=True)
    #print(dict(zip(parnames, zip(pars, errors))))

    row = dict(zip(parnames, pars))
    row.update(dict(zip(['e'+p for p in parnames], errors)))
    row.update({'velocity': vcube.spectral_axis[channel]})
    tbl.add_row(row)
    tbl.pprint()

    pl.figure(ii / 16)
    ax = pl.subplot(4, 4, ii % 16 + 1)
    ax.imshow(cutout, cmap=pl.cm.gray_r)
    ax.contour(fitimage)
import numpy as np
import gaussfitter
from astropy.io import fits
from astropy import units as u
from astropy import wcs

cutout_size = 5

im = fits.getdata('gc450.fits.gz')[462 - cutout_size:462 + cutout_size,
                                   1392 - cutout_size:1392 + cutout_size]
pars = gaussfitter.gaussfit(im,
                            params=[1, 20, cutout_size, cutout_size, 2, 2, 0])
w = wcs.WCS(fits.getheader('gc450.fits.gz'))

fwhm = (wcs.utils.proj_plane_pixel_scales(w) * u.deg).to(
    u.arcsec) * pars[4:6] * np.sqrt(8 * np.log(2))

print("FWHM of Sgr B2 N = {0}".format(fwhm))

im = fits.getdata('gc450.fits.gz')[454 - cutout_size:454 + cutout_size,
                                   1405 - cutout_size:1405 + cutout_size]
pars = gaussfitter.gaussfit(im,
                            params=[1, 20, cutout_size, cutout_size, 2, 2, 0])
w = wcs.WCS(fits.getheader('gc450.fits.gz'))

fwhm = (wcs.utils.proj_plane_pixel_scales(w) * u.deg).to(
    u.arcsec) * pars[4:6] * np.sqrt(8 * np.log(2))

print("FWHM of Sgr B2 M = {0}".format(fwhm))

im = fits.getdata('gc450.fits.gz')[456 - cutout_size:456 + cutout_size,
Exemple #36
0
    if (ima[0].header['filter'] == obs_par['FILTER']):
        print "filename", files[i[0]]
        ima[1].data = ima[1].data * ima[0].header[
            'exptime']  #multiply by exptime to convert data to counts
        LTV1 = ima[1].header['LTV1']  #X offset to get into physical pixels
        LTV2 = ima[1].header['LTV2']  #Y offset to get to physical pixels

        nrow = len(ima[1].data[:, 0])
        ncol = len(ima[1].data[0, :])
        t = ima[0].header['expstart']

        dat = ima[1].data[rmin:rmax,
                          cmin:cmax]  #cuts out stamp around the target star
        err = ima[2].data[rmin:rmax, cmin:cmax]

        results = gaussfitter.gaussfit(dat, err)

        if diagnostics == True:
            plt.title("Direct image")
            plt.imshow(dat * ima[0].header['exptime'],
                       origin='lower',
                       vmin=0,
                       vmax=1000)
            plt.plot(results[2],
                     results[3],
                     marker='x',
                     color='orange',
                     markeredgewidth=3.,
                     ms=10,
                     label='centroid',
                     linestyle="none")
Exemple #37
0
def align_stars(indir, outdir=None, ncpu=1, keepfrac=0.7):
    if outdir == None:
        outdir = indir
    pxhalf = 100  #px right and left of image. final size 2*pxhalf+1
    filetable = ascii.read(indir + 'filetable_bkgrnd.csv', delimiter=',')
    nimages = len(filetable)
    #read in the images
    PAs = []
    images = []
    for ii, fn in enumerate(filetable['fninterm']):
        data, head = fits.getdata(fn, header=True)
        if not len(data.shape) == 2:
            raise ValueError('Unknown data fromat at file %s' % fn)
        starx = int(filetable[ii]['roughx'])
        stary = int(filetable[ii]['roughy'])
        #        nims = data.shape[0]
        datacut = np.full([2 * pxhalf + 1, 2 * pxhalf + 1], np.nan)
        data = data[max(0, stary - pxhalf):min(stary + pxhalf +
                                               1, data.shape[1]),
                    max(0, starx - pxhalf):min(starx + pxhalf +
                                               1, data.shape[0]), ]
        datacut[0:data.shape[0], 0:data.shape[1]] = data
        PAs.append(head[hpa])
        images.append(datacut)
#        for hh in range(nims):
#            images.append( data[hh,:,:])

#/////////////////////////////////////////////////////////
#median combine and first xreg
#/////////////////////////////////////////////////////////

    print('register number getting medianed: ', len(images))
    print(images[0].size)
    print(images[0].shape)
    print(images[0].dtype)
    first_median = np.median(images, axis=0)

    pool = Pool(ncpu)
    get_shifts = subreg(first_median)
    shifts = pool.map(get_shifts, images)
    first_shifts = shifts
    pool.close()

    for hh in range(len(images)):
        images[hh] = shift(images[hh], shifts[hh])

    #/////////////////////////////////////////////////////////
    #keep only the best of images
    #/////////////////////////////////////////////////////////
    cross_reg = []
    for im in images:
        cross_reg.append(np.sum((im - first_median)**2.))

    sorted_cross_reg = np.argsort(cross_reg)
    selected_cross_reg = sorted_cross_reg[0:int(keepfrac * len(images))]
    n_selected = len(selected_cross_reg)

    #/////////////////////////////////////////////////////////
    #median combine and second xreg
    #/////////////////////////////////////////////////////////

    images = np.array(images)[selected_cross_reg, :, :]
    second_median = np.median(images, axis=0)

    print 'second subreg'
    pool = Pool(ncpu)
    get_shifts = subreg(second_median)
    shifts = pool.map(get_shifts, images)
    second_shifts = shifts
    pool.close()

    #get center for images. Move to pxhalf
    xycen = []
    for h in range(n_selected):
        #        xycen.append( get_center(images[h,:,:]) )
        xycen.append( gaussfit(images[h,pxhalf-5:pxhalf+5,
                                        pxhalf-5:pxhalf+5],\
                               params=(0.,-200.,6.,6.,3.,3.,0.))[2:4] - [5.,5.])
    yxcenshift = np.median(xycen, axis=0)[::-1]  #- [pxhalf,pxhalf]
    print('General offset of images: %s' % yxcenshift)
    for h in range(n_selected):
        shifts[h] += yxcenshift
        images[h, :, :] = shift(images[h, :, :], shifts[h])

    #/////////////////////////////////////////////////////////
    #save
    #/////////////////////////////////////////////////////////
    images = np.stack(images, axis=0)
    PAs = np.array(PAs)
    PAs_sel = PAs[selected_cross_reg]
    filet_sel = filetable[selected_cross_reg]
    filet_sel['orig_nr'] = selected_cross_reg
    fits.writeto(outdir + 'center_im_sat.fits', images, clobber=True)
    fits.writeto(outdir + 'rotnth.fits', PAs, clobber=True)

    print(
        'DONE WITH ALL. SAVED DEROTATED IMAGES AND ANGLES IN %s. Their shape is %s'
        % (outdir, images.shape))
Exemple #38
0
def gaborfit(
        data,
        err=None,
        params=(),
        fixed=np.repeat(False, 9),
        limitedmin=[False, False, False, False, True, True, True, True, True],
        limitedmax=[
            False, False, False, False, False, False, True, False, True
        ],
        minpars=[0.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.0, 0.01, 0.0],
        maxpars=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 360.0, 0.0, 360.0],
        quiet=True,
        returnmp=False,
        return_all=False,
        returnfitimage=False,
        **kwargs):
    """ gabor params = (height,amplitude,center_x,center_y,width_x,width_y,theta,lambda,phi) """
    """ gaussian params=(height, amplitude, center_x, center_y, width_x, width_y, theta) """
    if len(params) == 0:
        params,_errors = gaussfit(data,limitedmin=limitedmin[:7],limitedmax=limitedmax[:7],\
                                  minpars=minpars[:7],maxpars=maxpars[:7],return_all=True)
        spatial_freq = math.sqrt(params[2]**2 + params[3]**2)
        phase = 0.0

        params = np.append(params,
                           np.array([spatial_freq, phase], dtype='float'))

    def mpfitfun(data, err):
        if err is None:

            def f(p, fjac=None):                return [0,np.ravel(data-twodgabor(p)\
    (*np.indices(data.shape)))]
        else:

            def f(p, fjac=None):                return [0,np.ravel((data-twodgabor(p)\
    (*np.indices(data.shape)))/err)]

        return f

    parinfo = [{
        'n': 0,
        'value': params[0],
        'limits': [minpars[0], maxpars[0]],
        'limited': [limitedmin[0], limitedmax[0]],
        'fixed': fixed[0],
        'parname': "HEIGHT",
        'error': 0
    }, {
        'n': 1,
        'value': params[1],
        'limits': [minpars[1], maxpars[1]],
        'limited': [limitedmin[1], limitedmax[1]],
        'fixed': fixed[1],
        'parname': "AMPLITUDE",
        'error': 0
    }, {
        'n': 2,
        'value': params[2],
        'limits': [minpars[2], maxpars[2]],
        'limited': [limitedmin[2], limitedmax[2]],
        'fixed': fixed[2],
        'parname': "XSHIFT",
        'error': 0
    }, {
        'n': 3,
        'value': params[3],
        'limits': [minpars[3], maxpars[3]],
        'limited': [limitedmin[3], limitedmax[3]],
        'fixed': fixed[3],
        'parname': "YSHIFT",
        'error': 0
    }, {
        'n': 4,
        'value': params[4],
        'limits': [minpars[4], maxpars[4]],
        'limited': [limitedmin[4], limitedmax[4]],
        'fixed': fixed[4],
        'parname': "XWIDTH",
        'error': 0
    }, {
        'n': 5,
        'value': params[5],
        'limits': [minpars[5], maxpars[5]],
        'limited': [limitedmin[5], limitedmax[5]],
        'fixed': fixed[5],
        'parname': "YWIDTH",
        'error': 0
    }, {
        'n': 6,
        'value': params[6],
        'limits': [minpars[6], maxpars[6]],
        'limited': [limitedmin[6], limitedmax[6]],
        'fixed': fixed[6],
        'parname': "ROTATION",
        'error': 0
    }, {
        'n': 7,
        'value': params[7],
        'limits': [minpars[7], maxpars[7]],
        'limited': [limitedmin[7], limitedmax[7]],
        'fixed': fixed[7],
        'parname': "SPATIALFREQ",
        'error': 0
    }, {
        'n': 8,
        'value': params[8],
        'limits': [minpars[8], maxpars[8]],
        'limited': [limitedmin[8], limitedmax[8]],
        'fixed': fixed[8],
        'parname': "PHASE",
        'error': 0
    }]

    mp = mpfit(mpfitfun(data, err), parinfo=parinfo, quiet=quiet)

    if returnmp:
        returns = (mp)
    elif return_all == 0:
        returns = mp.params
    elif return_all == 1:
        returns = mp.params, mp.perror
    if returnfitimage:
        fitimage = twodgabor(mp.params)(*np.indices(data.shape))
        returns = (returns, fitimage)
    return returns
    nearDeadCutoff=500#100/15 cps for 4000-6000 angstroms
    err[frame<nearDeadCutoff] = np.inf
    entireMask = (err==np.inf)
    maFrame = np.ma.masked_array(frame,entireMask)
    guessAmp = 2000.
    guessHeight = 2000.
    guessWidth=3.
    guessParams = [guessHeight,guessAmp,guessX,guessY,guessWidth]
    limitedmin = 5*[True] 
    limitedmax = 5*[True]
    minpars = [0,0,0,0,.1]
    maxpars = [5000,10000,43,45,10]
    usemoments=[True,True,True,True,True]
    
    
    out = gaussfit(data=maFrame,err=err,params=guessParams,returnfitimage=True,quiet=True,limitedmin=limitedmin,limitedmax=limitedmax,minpars=minpars,maxpars=maxpars,circle=1,usemoments=usemoments,returnmp=True)
    mp = out[0]

    outparams = mp.params
    paramErrors = mp.perror
    chisq = mp.fnorm
    dof = mp.dof
    reducedChisq = chisq/dof
    print reducedChisq
    fitimg = out[1]
    chisqList.append([chisq,dof])


    paramsList.append(outparams)
    errorsList.append(paramErrors)
    print outparams,paramErrors
    ])
    scube = cube.subcube_from_ds9region(r_subset)
    pixscale = scube.wcs.pixel_scale_matrix[1, 1] * u.deg
    pixscale_as = pixscale.to(u.arcsec)

    cubemax = scube.max()  # normalize everything
    noise = scube.spectral_slab(30 * u.km / u.s, 50 * u.km / u.s).std(axis=0)
    noise[np.isnan(noise)] = 1e10 * noise.unit
    noise_norm = (noise / cubemax).value

    dv = cube.spectral_axis.diff().mean()
    vr = [56, 63.5]
    gaussfits = [
        gaussfitter.gaussfit(np.nan_to_num(
            (scube[scube.closest_spectral_channel(v * u.km / u.s), :, :] /
             cubemax).value),
                             err=noise_norm,
                             return_error=True)
        for v in ProgressBar(np.arange(vr[0], vr[1] + dv.value, dv.value))
    ]

    integrated_image = (scube.spectral_slab(vr[0] * u.km / u.s, vr[1] * u.km /
                                            u.s).moment0(axis=0))
    gaussfit_total, fitimage = gaussfitter.gaussfit(np.nan_to_num(
        integrated_image.value),
                                                    err=noise.value,
                                                    returnfitimage=True)
    # sanity check: make sure the fit is OK (it is)
    pl.figure(2).clf()
    pl.imshow(integrated_image.value, cmap=pl.cm.bone_r)
    cb = pl.colorbar()
    peak = [csize-scsize+peak_[0], csize-scsize+peak_[1]]
    
    limitedmin=[F,T,T,T,T,T,T]
    limitedmax=[F,F,T,T,T,T,T]
    minpars=[0, 0, peak[1]-2.0, peak[0]-2.0, 0.5, 0.5, 0]
    maxpars=[0, 0, peak[1]+2.0, peak[0]+2.0, 2, 2, 180]
    params=[0, sp.data.max(), csize, csize, 2, 2, 10]

    if 'h2co' in cubename or 'ch3oh6' in cubename:
        maxpars[4] = 5
        maxpars[5] = 5
        params[4] = 4

    (h,a,x,y,sx,sy,pa),gfim = gaussfitter.gaussfit(cutout.value, returnfitimage=True,
                                                   params=params,
                                                   limitedmin=limitedmin,
                                                   limitedmax=limitedmax,
                                                   minpars=minpars,
                                                   maxpars=maxpars)
    print("Cube {0}: centroid = {1},{2}  width={3},{4}".format(cubename, x, y, sx, sy))
    residual = cutout.value - gfim
    noise = np.std(residual)
    (((h,a,x,y,sx,sy,pa),
      (dh,da,dx,dy,dsx,dsy, dpa)),
     gfim) = gaussfitter.gaussfit(cutout.value, returnfitimage=True,
                                  params=params, limitedmin=limitedmin,
                                  limitedmax=limitedmax, minpars=minpars,
                                  maxpars=maxpars, return_error=True,
                                  err=noise)
    print("Cube {0}: centroid = {1}+/-{dx}, {2}+/-{dy}  width={3},{4} PA={5}".format(cubename, x, y, sx, sy, pa, dx=dx, dy=dy))
  
    hdu = cutout.hdu
   out = image

   p0 = 100.
   p1 = 256.
   p2 = 256.
   p3 = 15.
   p4 = 15.0
   p5 = 2.0
   p6 = 0.  
   
   pFit, error = fit2dlibrary.fit_function(  p0, image, fit2dlibrary.fitdict['Gaussian2Dphi'].function ) 

   print pFit

   pstart = [patchcounts/box/box,braggsig, row, col, 5., 5., 0.] 
   p, fitimg =  gaussfitter.gaussfit( out, params=pstart, returnfitimage=True)
   # p = [height, amplitude, x, y, width_x, width_y, rotation]

   row = p[2]
   col = p[3] 

   print "Sum of counts = %f" % image.sum() 
 
   pngprefix =  sys.argv[FITSFILE] + '_fits'
   falsecolor.inspecpng( [image, fitimg], row, col, out.min(), out.max(), \
                         falsecolor.my_grayscale, pngprefix, 100, origin = 'upper' , step=False, scale=10)