def fit_specindex(self, freqarr, fluxarr, efluxarr, do_log=False): """ Fits spectral index to data. do_log is True/False implies you fit spectral index in logFlux vs logFreq space or not.""" import functions as func import math from scipy.optimize import leastsq x = freqarr flux = fluxarr eflux = efluxarr f0 = N.median(x) mask = N.zeros(len(fluxarr), dtype=bool) if do_log: x = N.log10(x/f0); y = N.log10(flux); sig = N.abs(eflux/flux)/2.303 funct = func.poly else: x = x/f0; y = flux; sig = eflux funct = func.sp_in spin, espin = func.fit_mask_1d(x, y, sig, mask, funct, do_err=True, order=1) if do_log: spin[0] = math.pow(10.0, spin[0]) espin[0] = spin[0]*math.log(10.0)*espin[0] return spin[0], spin[1], espin[1]
def shapelet_getroot(xfn, yfn, xco, xcen, ycen): """ This finds the root for finding the shapelet centre. If there are multiple roots, takes that which closest to the 'centre', taken as the intensity barycentre. This is the python version of getroot.f of anaamika.""" import functions as func root = None npoint = len(xfn) error = 0 if npoint == 0: error = 1 elif yfn.max() * yfn.min() >= 0.: error = 1 minint = 0 minintold = 0 for i in range(1, npoint): if yfn[i - 1] * yfn[i] < 0.: if minintold == 0: # so take nearest to centre if abs(yfn[i - 1]) < abs(yfn[i]): minint = i - 1 else: minint = i else: dnew = func.dist_2pt([xco, xfn[i]], [xcen, ycen]) dold = func.dist_2pt([xco, xfn[minintold]], [xcen, ycen]) if dnew <= dold: minint = i else: minint = minintold minintold = minint if minint < 1 or minint > npoint: error = 1 if error != 1: low = minint - min(2, minint) #-1) up = minint + min(2, npoint - 1 - minint) # python array indexing rubbish nfit = up - low + 1 xfit = xfn[low:low + nfit] yfit = yfn[low:low + nfit] sig = N.ones(nfit) smask = N.zeros(nfit, dtype=bool) xx = [i for i in range(low, low + nfit)] [c, m], errors = func.fit_mask_1d(xfit, yfit, sig, smask, func.poly, do_err=False, order=1) root = -c / m if root < xfn[low] or root > xfn[up]: error = 1 return root, error
def shapelet_getroot(xfn, yfn, xco, xcen, ycen): """ This finds the root for finding the shapelet centre. If there are multiple roots, takes that which closest to the 'centre', taken as the intensity barycentre. This is the python version of getroot.f of anaamika.""" import functions as func root = None npoint = len(xfn) error = 0 if npoint == 0: error = 1 elif yfn.max() * yfn.min() >= 0.0: error = 1 minint = 0 minintold = 0 for i in range(1, npoint): if yfn[i - 1] * yfn[i] < 0.0: if minintold == 0: # so take nearest to centre if abs(yfn[i - 1]) < abs(yfn[i]): minint = i - 1 else: minint = i else: dnew = func.dist_2pt([xco, xfn[i]], [xcen, ycen]) dold = func.dist_2pt([xco, xfn[minintold]], [xcen, ycen]) if dnew <= dold: minint = i else: minint = minintold minintold = minint if minint < 1 or minint > npoint: error = 1 if error != 1: low = minint - min(2, minint) # -1) up = minint + min(2, npoint - 1 - minint) # python array indexing rubbish nfit = up - low + 1 xfit = xfn[low : low + nfit] yfit = yfn[low : low + nfit] sig = N.ones(nfit) smask = N.zeros(nfit, dtype=bool) xx = [i for i in range(low, low + nfit)] [c, m], errors = func.fit_mask_1d(xfit, yfit, sig, smask, func.poly, do_err=False, order=1) root = -c / m if root < xfn[low] or root > xfn[up]: error = 1 return root, error
def shape_findcen(image, mask, basis, beta, nmax, beam_pix): # + check_cen_shapelet """ Finds the optimal centre for shapelet decomposition. Minimising various combinations of c12 and c21, as in literature doesnt work for all cases. Hence, for the c1 image, we find the zero crossing for every vertical line and for the c2 image, the zero crossing for every horizontal line, and then we find intersection point of these two. This seems to work even for highly non-gaussian cases. """ import functions as func import sys hc = [] hc = shapelet_coeff(nmax, basis) msk = N.zeros(mask.shape, dtype=bool) for i, v in N.ndenumerate(mask): msk[i] = not v n, m = image.shape cf12 = N.zeros(image.shape, dtype=N.float32) cf21 = N.zeros(image.shape, dtype=N.float32) index = [(i, j) for i in range(n) for j in range(m)] for coord in index: if msk[coord]: B12 = shapelet_image(basis, beta, coord, hc, 0, 1, image.shape) cf12[coord] = N.sum(image * B12 * msk) if coord == (27, 51): dumpy = B12 B21 = shapelet_image(basis, beta, coord, hc, 1, 0, image.shape) cf21[coord] = N.sum(image * B21 * msk) else: cf12[coord] = None cf21[coord] = None (xmax, ymax) = N.unravel_index(image.argmax(), image.shape) # FIX with mask if xmax in [1, n] or ymax in [1, m]: (m1, m2, m3) = func.moment(mask) xmax, ymax = N.round(m2) # in high snr area, get zero crossings for each horizontal and vertical line for c1, c2 resp tr_mask = mask.transpose() tr_cf21 = cf21.transpose() try: (x1, y1) = getzeroes_matrix(mask, cf12, ymax, xmax) # y1 is array of zero crossings (y2, x2) = getzeroes_matrix(tr_mask, tr_cf21, xmax, ymax) # x2 is array of zero crossings # find nominal intersection pt as integers xind = N.where(x1 == xmax) yind = N.where(y2 == ymax) xind = xind[0][0] yind = yind[0][0] # now take 2 before and 2 after, fit straight lines, get proper intersection ninter = 5 if xind < 3 or yind < 3 or xind > n - 2 or yind > m - 2: ninter = 3 xft1 = x1[xind - (ninter - 1) / 2 : xind + (ninter - 1) / 2 + 1] yft1 = y1[xind - (ninter - 1) / 2 : xind + (ninter - 1) / 2 + 1] xft2 = x2[yind - (ninter - 1) / 2 : yind + (ninter - 1) / 2 + 1] yft2 = y2[yind - (ninter - 1) / 2 : yind + (ninter - 1) / 2 + 1] sig = N.ones(ninter, dtype=float) smask1 = N.array([r == 0 for r in yft1]) smask2 = N.array([r == 0 for r in xft2]) cen = [0.0] * 2 if sum(smask1) < len(yft1) and sum(smask2) < len(xft2): [c1, m1], errors = func.fit_mask_1d(xft1, yft1, sig, smask1, func.poly, do_err=False, order=1) [c2, m2], errors = func.fit_mask_1d(xft2, yft2, sig, smask2, func.poly, do_err=False, order=1) if m2 - m1 == 0: cen[0] = cen[1] = 0.0 else: cen[0] = (c1 - c2) / (m2 - m1) cen[1] = c1 + m1 * cen[0] else: cen[0] = cen[1] = 0.0 # check if estimated centre makes sense error = shapelet_check_centre(image, mask, cen, beam_pix) except: error = 1 if error > 0: # print 'Error '+str(error)+' in finding centre, will take 1st moment instead.' (m1, m2, m3) = func.moment(image, mask) cen = m2 return cen
def shape_findcen(image, mask, basis, beta, nmax, beam_pix): # + check_cen_shapelet """ Finds the optimal centre for shapelet decomposition. Minimising various combinations of c12 and c21, as in literature doesnt work for all cases. Hence, for the c1 image, we find the zero crossing for every vertical line and for the c2 image, the zero crossing for every horizontal line, and then we find intersection point of these two. This seems to work even for highly non-gaussian cases. """ import functions as func import sys hc = [] hc = shapelet_coeff(nmax, basis) msk = N.zeros(mask.shape, dtype=bool) for i, v in N.ndenumerate(mask): msk[i] = not v n, m = image.shape cf12 = N.zeros(image.shape, dtype=N.float32) cf21 = N.zeros(image.shape, dtype=N.float32) index = [(i, j) for i in range(n) for j in range(m)] for coord in index: if msk[coord]: B12 = shapelet_image(basis, beta, coord, hc, 0, 1, image.shape) cf12[coord] = N.sum(image * B12 * msk) if coord == (27, 51): dumpy = B12 B21 = shapelet_image(basis, beta, coord, hc, 1, 0, image.shape) cf21[coord] = N.sum(image * B21 * msk) else: cf12[coord] = None cf21[coord] = None (xmax, ymax) = N.unravel_index(image.argmax(), image.shape) # FIX with mask if xmax in [1, n] or ymax in [1, m]: (m1, m2, m3) = func.moment(mask) xmax, ymax = N.round(m2) # in high snr area, get zero crossings for each horizontal and vertical line for c1, c2 resp tr_mask = mask.transpose() tr_cf21 = cf21.transpose() try: (x1, y1) = getzeroes_matrix(mask, cf12, ymax, xmax) # y1 is array of zero crossings (y2, x2) = getzeroes_matrix(tr_mask, tr_cf21, xmax, ymax) # x2 is array of zero crossings # find nominal intersection pt as integers xind = N.where(x1 == xmax) yind = N.where(y2 == ymax) xind = xind[0][0] yind = yind[0][0] # now take 2 before and 2 after, fit straight lines, get proper intersection ninter = 5 if xind < 3 or yind < 3 or xind > n - 2 or yind > m - 2: ninter = 3 xft1 = x1[xind - (ninter - 1) / 2:xind + (ninter - 1) / 2 + 1] yft1 = y1[xind - (ninter - 1) / 2:xind + (ninter - 1) / 2 + 1] xft2 = x2[yind - (ninter - 1) / 2:yind + (ninter - 1) / 2 + 1] yft2 = y2[yind - (ninter - 1) / 2:yind + (ninter - 1) / 2 + 1] sig = N.ones(ninter, dtype=float) smask1 = N.array([r == 0 for r in yft1]) smask2 = N.array([r == 0 for r in xft2]) cen = [0.] * 2 if sum(smask1) < len(yft1) and sum(smask2) < len(xft2): [c1, m1], errors = func.fit_mask_1d(xft1, yft1, sig, smask1, func.poly, do_err=False, order=1) [c2, m2], errors = func.fit_mask_1d(xft2, yft2, sig, smask2, func.poly, do_err=False, order=1) if m2 - m1 == 0: cen[0] = cen[1] = 0.0 else: cen[0] = (c1 - c2) / (m2 - m1) cen[1] = c1 + m1 * cen[0] else: cen[0] = cen[1] = 0.0 # check if estimated centre makes sense error = shapelet_check_centre(image, mask, cen, beam_pix) except: error = 1 if error > 0: #print 'Error '+str(error)+' in finding centre, will take 1st moment instead.' (m1, m2, m3) = func.moment(image, mask) cen = m2 return cen