Esempio n. 1
0
 def normalize(self,area=False):
     """
     Normalize the pulse so that the peak has a value of 1.0
     """
     if area:
         self.data = u.normalize_area(self.data)
     else:
         minimum = np.mean(self.getOffpulse())
         self.data = u.normalize(self.data,minimum=minimum)
Esempio n. 2
0
 def normalize(self, area=False):
     """
     Normalize the pulse so that the peak has a value of 1.0
     """
     if area:
         self.data = u.normalize_area(self.data)
     else:
         minimum = np.mean(self.getOffpulse())
         self.data = u.normalize(self.data, minimum=minimum)
Esempio n. 3
0
def getTemplate(PSR, rcvr):
    if rcvr == "Rcvr1_2" or rcvr == "Rcvr_800":
        dirname = "guppi2"
        be = "GUPPI"
    elif rcvr == "L-wide" or rcvr == "S0wide" or rcvr == "430" or rcvr == "327":
        dirname = "puppi"
        be = "PUPPI"
    tempfilename = "/nanograv/releases/11y/%s/%s/%s.%s.%s.11y.x.sum.sm" % (
        dirname, PSR, PSR, rcvr, be)
    ar = Archive(tempfilename, verbose=False)
    sptemp = SinglePulse(u.normalize(ar.getData(), simple=True),
                         windowsize=256,
                         period=ar.getPeriod())
    return sptemp
Esempio n. 4
0
    def getWeff(self, fourier=False, sumonly=False, timeunits=True):
        """
        Calculate the effective width of the pulse
        """
        if not timeunits or self.period is None:
            return None
        P = self.period
        N = self.nbins
        U = u.normalize(self.data, simple=True)  #remove baseline?

        tot = np.sum(np.power(U[1:] - U[:-1], 2))
        if sumonly:
            return tot
        self.weff = P / np.sqrt(N * tot)
        return self.weff
Esempio n. 5
0
 def getWeff(self,fourier=False,sumonly=False,timeunits=True):
     """
     Calculate the effective width of the pulse
     """
     if not timeunits or self.getPeriod() is None:
         return None
     P=self.getPeriod()
     N=self.getNbins()
     U=u.normalize(self.data,simple=True) #remove baseline?
     
     tot=np.sum(np.power(U[1:]-U[:-1],2))
     if sumonly:
         return tot
     self.weff=P/np.sqrt(N*tot)
     return self.weff
Esempio n. 6
0
    def plotProfiles(self,ax=None,nchan=None,difference=False, labels = False, profiles = None, reset = False, BW = None, F = None):
        """
        Plot the data profiles, useful if on a specific axis
        """

        if nchan is None:
            nchan = self.NCHAN
    
        doshow = False
        if ax is None:
            doshow = True
            fig = figure()
            ax = fig.add_subplot(111)
        if profiles is None:
            profiles = self.getProfiles(nchan=nchan,difference=difference, reset = reset)
        #for profile in profiles:
        #    plt.clf()
        #    plt.plot(profile)
        #    plt.show()
        #raise SystemExit
        
        if BW is None:
            BW = self.getBandwidth()#np.abs(self.getBandwidth())
        if F is None:
            F = self.F #nchan is number of averaged channels
        
        step_size = BW/np.size(F)
        fsize = np.size(F)
        
        scale_diff = np.amax(profiles) - np.amin(profiles)
        for i,prof in enumerate(profiles):
            #gets middle for frequency range 
            index = (i*fsize/nchan + fsize/nchan/2)*step_size + F[0]
            if difference:
                scale_diff = np.amax(prof) - np.amin(prof)
                ax.plot( (prof/scale_diff) * fsize/nchan*abs(step_size) + index, color = 'k')
            else:
                ax.plot(u.normalize(prof,simple=True)*fsize/nchan*abs(step_size) + index, color = 'k')
        ax.set_xlim(0,len(prof))
        if labels:
            ax.axes.set_ylabel('Frequency (MHz)')
            ax.axes.set_xlabel('Phase Bins')
        if doshow:
            show()
        return ax
Esempio n. 7
0
    def spline_smoothing(self, sigma=None, lam=None, **kwargs):
        """ Cubic Spline Interplation
        sigma: phase bin error bars
        lam: (0,1], 1 = maximize fitting through control points (knots), 0 = minimize curvature of spline
        """

        tdata = self.bins
        ydata = u.normalize(self.data, simple=True)
        N = len(ydata)

        noise = self.getOffpulseNoise()
        if lam is None or (lam > 1 or lam <= 0):
            lam = 1 - noise**2
        mu = 2 * float(1 - lam) / (3 * lam)

        ### Define knot locations

        # Rotate the pulse so the peak is at the edges of the spline
        shift = -np.argmax(ydata)
        yshift = np.roll(ydata, shift)

        # Add periodic point
        tdata = np.concatenate((tdata, [tdata[-1] + np.diff(tdata)[0]]))
        yshift = np.concatenate((yshift, [yshift[0]]))

        knots = u.subdivide(tdata, yshift, noise, **kwargs)
        knots = np.array(np.sort(knots), dtype=np.int)
        knots = np.concatenate(([0], knots, [N]))  #Add endpoints

        if sigma is None:
            setsigma = True

        #for passnum in range(2):
        while True:
            Nknots = len(knots)
            Narcs = Nknots - 1
            t = np.array(tdata[knots], dtype=np.float)

            # Determine the knot y-values.
            y = np.zeros_like(t)
            y[0] = yshift[0]
            y[-1] = yshift[-1]
            for i in range(1, len(knots) - 1):
                knotL = knots[i - 1]
                knotR = knots[i + 1]
                dt = tdata[knotL:knotR]
                dy = yshift[knotL:knotR]
                p = np.polyfit(
                    dt, dy, 3
                )  #Fit a preliminary cubic over the data to place the point.
                f = np.poly1d(p)
                y[i] = f(tdata[knots[i]])

            if setsigma:
                sigma = np.ones(len(y), dtype=np.float)
            Sigma = np.diag(sigma[:-1])  #matrix

            # Smoothing with Cubic Splines by D.S.G. Pollock 1999
            h = t[1:] - t[:-1]
            r = 3.0 / h

            f = np.zeros_like(h)
            p = np.zeros_like(h)
            #q = np.zeros_like(h)

            p[0] = 2 * (h[0] + h[-1])
            #q[0] = 3*(y[1] - y[0])/h[0] - 3*(y[-1] - y[-2])/h[-1] #note the indices
            f[0] = -(r[-1] + r[0])
            for i in range(1, Narcs):
                p[i] = 2 * (h[i] + h[i - 1])
                #q[i] = 3*(y[i+1] - y[i])/h[i] - 3*(y[i] - y[i-1])/h[i-1]
                f[i] = -(r[i - 1] + r[i])

            # Build projection matrices
            R = np.zeros((Narcs, Narcs))
            Qp = np.zeros((Narcs, Narcs))

            for i in range(Narcs):
                #for j in range(Narcs):
                #    if i == j:
                R[i, i] = p[i]
                Qp[i, i] = f[i]
                if i != Narcs - 1:
                    R[i + 1, i] = h[i]
                    R[i, i + 1] = h[i]
                    Qp[i + 1, i] = r[i]
                    Qp[i, i + 1] = r[i]
            R[0, -1] = h[-1]
            R[-1, 0] = h[-1]
            Qp[0, -1] = r[-1]
            Qp[-1, 0] = r[-1]
            Q = np.transpose(Qp)

            A = mu * np.dot(np.dot(Qp, Sigma), Q) + R

            b = np.linalg.solve(A, np.dot(Qp, y[:-1]))
            d = y[:-1] - mu * np.dot(np.dot(Sigma, Q), b)
            a = np.zeros(Narcs)
            c = np.zeros(Narcs)

            i = Narcs - 1
            a[i] = (b[0] - b[i]) / (3 * h[i])
            for i in range(Narcs - 1):
                a[i] = (b[i + 1] - b[i]) / (3 * h[i])

            i = Narcs - 1
            c[i] = (d[0] - d[i]) / h[i] - a[i] * h[i]**2 - b[i] * h[i]
            for i in range(Narcs - 1):
                c[i] = (d[i + 1] - d[i]) / h[i] - a[i] * h[i]**2 - b[i] * h[i]

            # Build polynomials
            S = []
            for i in range(Narcs):
                S.append(np.poly1d([a[i], b[i], c[i], d[i]]))

            ytemp = np.zeros_like(yshift)
            for i in range(Narcs):
                ts = np.arange(t[i], t[i + 1])
                hs = ts - t[i]
                yS = S[i](hs)
                ytemp[int(t[i]):int(t[i + 1])] = yS

            ytemp[-1] = ytemp[0]
            resids = yshift - ytemp
            #print resids,noise
            rms_resids = u.RMS(resids)
            #inds = np.where(np.abs(resids)>4*rms_resids)[0]
            inds = np.where(
                np.logical_and(yshift > 3 * noise,
                               np.abs(resids) > 4 * rms_resids)
            )[0]  #require the intensity to be "significant", and the residuals
            if len(inds) == 0:
                break

            #newinds = np.sort(np.abs(resids))
            newinds = np.argsort(np.abs(resids))
            #print np.argmax(np.abs(resids))
            #print newinds
            #raise SystemExit
            #newind = np.argmax(np.abs(resids))

            newind = newinds[-1]
            i = 1
            addknot = True
            while newind in knots and np.any(np.abs(knots - newind) <= 4):
                if i == N:
                    addknot = False
                    break
                i += 1
                newind = newinds[-i]
            '''
            for newind in newinds:
                if newind in knots:
                    continue
                elif np.all(np.abs(newind-knots)<=1):
                    continue
                print newind
                break
            '''
            if addknot:
                #print newind
                knots = np.sort(np.concatenate((knots, [newind])))
            else:
                break

        resids = yshift - ytemp
        rms_resids = u.RMS(resids)

        tdata = tdata[:-1]
        #yshift = yshift[:-1]
        ytemp = ytemp[:-1]

        #yshift = np.roll(yshift,-shift)
        ytemp = np.roll(ytemp, -shift)
        ytemp /= np.max(ytemp)

        return ytemp
Esempio n. 8
0
    def spline_smoothing(self,sigma=None,lam=None,**kwargs):
        """ Cubic Spline Interplation
        sigma: phase bin error bars
        lam: (0,1], 1 = maximize fitting through control points (knots), 0 = minimize curvature of spline
        """

        tdata = self.bins
        ydata = u.normalize(self.data,simple=True)
        N = len(ydata)



        noise = self.getOffpulseNoise()
        if lam is None or (lam > 1 or lam <= 0):
            lam = 1-noise**2
        mu = 2*float(1-lam)/(3*lam)    

        ### Define knot locations

        # Rotate the pulse so the peak is at the edges of the spline
        shift = -np.argmax(ydata)
        yshift = np.roll(ydata,shift)

        # Add periodic point
        tdata = np.concatenate((tdata,[tdata[-1] + np.diff(tdata)[0]]))
        yshift = np.concatenate((yshift,[yshift[0]]))

        knots = u.subdivide(tdata,yshift,noise,**kwargs)
        knots = np.array(np.sort(knots),dtype=np.int)
        knots = np.concatenate(([0],knots,[N])) #Add endpoints

        if sigma is None:
            setsigma = True

        #for passnum in range(2):
        while True:
            Nknots = len(knots)
            Narcs = Nknots-1
            t = np.array(tdata[knots],dtype=np.float)

            # Determine the knot y-values.
            y = np.zeros_like(t)
            y[0] = yshift[0]
            y[-1] = yshift[-1]
            for i in range(1,len(knots)-1):
                knotL = knots[i-1]
                knotR = knots[i+1]
                dt = tdata[knotL:knotR]
                dy = yshift[knotL:knotR]
                p = np.polyfit(dt,dy,3) #Fit a preliminary cubic over the data to place the point.
                f = np.poly1d(p)
                y[i] = f(tdata[knots[i]])
                



            if setsigma:
                sigma = np.ones(len(y),dtype=np.float)
            Sigma = np.diag(sigma[:-1]) #matrix

            # Smoothing with Cubic Splines by D.S.G. Pollock 1999
            h = t[1:]-t[:-1]
            r = 3.0/h

            f = np.zeros_like(h)
            p = np.zeros_like(h)
            #q = np.zeros_like(h)

            p[0] = 2*(h[0] + h[-1])
            #q[0] = 3*(y[1] - y[0])/h[0] - 3*(y[-1] - y[-2])/h[-1] #note the indices
            f[0] = -(r[-1]+r[0])
            for i in range(1,Narcs):
                p[i] = 2*(h[i] + h[i-1])
                #q[i] = 3*(y[i+1] - y[i])/h[i] - 3*(y[i] - y[i-1])/h[i-1]
                f[i] = -(r[i-1]+r[i])

            # Build projection matrices
            R = np.zeros((Narcs,Narcs))
            Qp = np.zeros((Narcs,Narcs))

            for i in range(Narcs):
                #for j in range(Narcs):
                #    if i == j:
                R[i,i] = p[i]
                Qp[i,i] = f[i]
                if i != Narcs -1:
                    R[i+1,i] = h[i]
                    R[i,i+1] = h[i]
                    Qp[i+1,i] = r[i]
                    Qp[i,i+1] = r[i]
            R[0,-1] = h[-1]
            R[-1,0] = h[-1]
            Qp[0,-1] = r[-1]
            Qp[-1,0] = r[-1]
            Q = np.transpose(Qp)


            A = mu*np.dot(np.dot(Qp,Sigma),Q) + R

            b = np.linalg.solve(A,np.dot(Qp,y[:-1]))
            d = y[:-1] - mu*np.dot(np.dot(Sigma,Q),b)
            a = np.zeros(Narcs)
            c = np.zeros(Narcs)

            i = Narcs-1
            a[i] = (b[0] - b[i])/(3*h[i])
            for i in range(Narcs-1):
                a[i] = (b[i+1] - b[i])/(3*h[i])

            i = Narcs-1
            c[i] = (d[0] - d[i])/h[i] - a[i]*h[i]**2 - b[i]*h[i]
            for i in range(Narcs-1):
                c[i] = (d[i+1] - d[i])/h[i] - a[i]*h[i]**2 - b[i]*h[i]

            # Build polynomials
            S = []
            for i in range(Narcs):
                S.append(np.poly1d([a[i],b[i],c[i],d[i]]))


            ytemp = np.zeros_like(yshift)
            for i in range(Narcs):
                ts = np.arange(t[i],t[i+1])
                hs = ts-t[i]
                yS = S[i](hs)
                ytemp[int(t[i]):int(t[i+1])] = yS


            ytemp[-1] = ytemp[0]
            resids = yshift-ytemp
            #print resids,noise
            rms_resids = u.RMS(resids)
            #inds = np.where(np.abs(resids)>4*rms_resids)[0]
            inds = np.where(np.logical_and(yshift>3*noise,np.abs(resids)>4*rms_resids))[0] #require the intensity to be "significant", and the residuals
            if len(inds) == 0:
                break

            #newinds = np.sort(np.abs(resids))
            newinds = np.argsort(np.abs(resids))
            #print np.argmax(np.abs(resids))
            #print newinds
            #raise SystemExit
            #newind = np.argmax(np.abs(resids))


            newind = newinds[-1]
            i = 1
            addknot = True
            while newind in knots and np.any(np.abs(knots-newind)<=4):
                if i == N:
                    addknot = False
                    break
                i+=1
                newind = newinds[-i]

            '''
            for newind in newinds:
                if newind in knots:
                    continue
                elif np.all(np.abs(newind-knots)<=1):
                    continue
                print newind
                break
            '''
            if addknot:
                #print newind
                knots = np.sort(np.concatenate((knots,[newind])))
            else:
                break

        resids = yshift-ytemp
        rms_resids = u.RMS(resids)

        tdata = tdata[:-1]
        #yshift = yshift[:-1]
        ytemp = ytemp[:-1]

        #yshift = np.roll(yshift,-shift)
        ytemp = np.roll(ytemp,-shift)
        ytemp /= np.max(ytemp)

        return ytemp
Esempio n. 9
0
    def __init__(self,filename,NCHAN = NCHAN,templatefilename=None,windowsize=256,prepare = True,**kwargs):
        name , file_extension = os.path.splitext(filename)
        path , self.name = os.path.split(name)
        self.fullname = "%s%s" % (self.name,file_extension)
        if file_extension == '.npz':
            self.npz = np.load(filename)
        #if file_extension == '.fits' or file_extension == '.zap':
        else:
            self.npz = None
        #else:
        #    raise RuntimeError('File has improper extension for Quicklook, and this code will not work')

        self.NCHAN = NCHAN
        
        name, file_extension = os.path.splitext(filename)
        self.filename = filename
        self.templatefilename = templatefilename
        self.windowsize = windowsize
        
        self.ds = None #is all this necessary?
        self.ss = None
        self.acf2d = None
        self.profiles = None
        self.difference_profiles = None
        self.SS_xaxis = None
        self.SS_yaxis = None
        self.ACF2D_xaxis = None
        self.ACF2D_yaxis = None
        self.peak_DM = None
        self.DM_arr = None
        self.calculated_DM = None
        
        
        if self.templatefilename is not None:
                artemp = Archive(self.templatefilename, prepare=False,lowmem=True) #force lowmem?
                artemp.pscrunch()
                temp = u.normalize(u.center_max(artemp.getData()),simple=True)
                self.sptemp = SinglePulse(temp,windowsize=windowsize)
        
        if self.npz is None:
            self.ar = Archive(self.filename, prepare=prepare)
            self.F = self.ar.getAxis('F')
            # temporary holdover from archive.py frequency issues
            #if self.F[0] > self.F[-1]:
            #    self.F = self.F[::-1]
            self.T = self.ar.getAxis('T')
            self.data = self.ar.getData()
    
            #self.calculateAverageProfile()
            
            #calculateAverageProfile
            self.ar.tscrunch()
            self.ar.fscrunch()
            avgprof = self.ar.getData()
            imax = np.argmax(avgprof)
            self.average_profile = u.center_max(avgprof) #not normalized!
            
            if self.templatefilename is None:
                self.spavgprof = SinglePulse(self.average_profile,windowsize=windowsize)
                self.sptemp = SinglePulse(u.normalize(self.average_profile),mpw=self.spavgprof.mpw,opw=self.spavgprof.opw)
            else:
                self.spavgprof = SinglePulse(self.average_profile,mpw=self.sptemp.mpw,opw=self.sptemp.opw)
        
            self.spavgprof.remove_baseline()
            
            #Reset archive
            self.ar.reset()
    
            self.nbins = len(self.average_profile)
            alignval = self.nbins/2 - imax
            self.data = np.roll(self.data,alignval)
            self.DM0 = self.ar.getDM()
        
        else:
            self.F = self.npz['frequency']
            self.T = self.npz['time']
            self.average_profile = self.npz['average_profile'][0,:] #is it bad that we need to do this?
            if self.templatefilename is None:
                self.spavgprof = SinglePulse(self.average_profile,windowsize=windowsize)
                self.sptemp = SinglePulse(u.normalize(self.average_profile),mpw=self.spavgprof.mpw,opw=self.spavgprof.opw)
            else:
                self.spavgprof = SinglePulse(self.average_profile,mpw=self.sptemp.mpw,opw=self.sptemp.opw)
            self.spavgprof.remove_baseline()
            self.nbins = len(self.average_profile)
            self.DM0 = self.npz['DM']
            self.peak_DM = self.npz['peak_DM']
            self.DM_arr = self.npz['DM_arr']
        
        if len(self.F) % self.NCHAN != 0:
            str(len(self.F)) + '%' + str(self.NCHAN) + '=' + str( len(self.F) % self.NCHAN )
            raise UserWarning('Number of frequency channels in file is not multiple of your provided NCHAN')
Esempio n. 10
0
 def normalize(self):
     """
     Normalize the pulse so that the peak has a value of 1.0
     """
     minimum=np.mean(self.getOffpulse())
     self.data=u.normalize(self.data,minimum=minimum)