def get_reliability(self):


        # finding sources 
        self.source_finder(image=self.imagename, lsmname=self.poslsm, 
                           thresh=self.pos_smooth, **self.opts_pos)

        self.source_finder(image=self.negativeimage, lsmname=self.neglsm,
                           thresh=self.neg_smooth, **self.opts_neg)

        # removing sources within a specified radius
        self.remove_sources_within(catalog=self.poslsm, rel_excl_src=
                                   self.rel_excl_src)
        self.remove_sources_within(catalog=self.neglsm, rel_excl_src=
                                   self.rel_excl_src)

        # add local variance as a parameter
        if self.do_local_var:
            utils.local_variance(self.imagedata, self.header, 
                              catalog=self.poslsm, wcs=self.wcs, 
                              pixelsize=self.pixelsize, local_region=
                              self.local_var_region, savefig=False,
                              highvariance_factor=None, prefix=self.prefix,
                              neg_side=True)

            utils.local_variance(self.imagedata, self.header,
                              catalog=self.neglsm, wcs=self.wcs,
                              pixelsize=self.pixelsize, local_region=
                              self.local_var_region, savefig=False,
                              highvariance_factor=None, prefix=self.prefix, neg_side=True)
        # compute correlation if only do_psf_corr = True 
        #and the psf is provided 
        if self.do_psf_corr and self.psfname:
            utils.psf_image_correlation(
                 catalog=self.poslsm, psfimage=self.psfname,
                 imagedata=self.imagedata, header=self.header,
                 wcs=self.wcs, pixelsize=self.pixelsize,
                 corr_region=self.psf_corr_region, prefix= self.prefix)
            utils.psf_image_correlation(
                 catalog=self.neglsm, psfimage=self.psfname, 
                 imagedata=self.imagedata, header=self.header,
                 wcs=self.wcs, pixelsize=self.pixelsize, 
                 corr_region=self.psf_corr_region, prefix=self.prefix)
      
        ##TODO verbose vs. logging
        pmodel = Tigger.load(self.poslsm, verbose=self.loglevel)
        nmodel = Tigger.load(self.neglsm, verbose=self.loglevel)
        
        posSources = pmodel.sources
        negSources = nmodel.sources

        npsrc = len(posSources)
        nnsrc = len(negSources)      
 
        positive, labels = self.params(posSources, pmodel)
        negative, labels = self.params(negSources, nmodel)

        # setting up a kernel, Gaussian kernel
        bandwidth = []

        for plane in negative.T:
            bandwidth.append(plane.std())



        nplanes = len(labels)
        cov = numpy.zeros([nplanes, nplanes])


        for i in range(nplanes):
            for j in range(nplanes):
                if i == j:
                    cov[i, j] = bandwidth[i]*((4.0/((nplanes+2)*
                                  npsrc))**(1.0/(nplanes+4.0)))

        pcov = utils.gaussian_kde_set_covariance(positive.T, cov)
        ncov = utils.gaussian_kde_set_covariance(negative.T, cov)
    

        # get number densities
        nps = pcov(positive.T) * npsrc
        nns = ncov(positive.T) * nnsrc

        # define reliability of positive catalog
        rel = (nps-nns)/nps

        for src, rf in zip(posSources, rel):
            src.setAttribute("rel", rf)
            out_lsm = self.poslsm
        pmodel.save(out_lsm)

        if self.makeplots:
            savefig = self.prefix + "_planes.png"
            utils.plot(positive, negative, rel=rel, labels=labels,
                        savefig=savefig, prefix=self.prefix)

        return  self.poslsm, self.neglsm      
    def get_reliability(self):


        # finding sources 
        self.log.info(" Extracting the sources on both sides ")

        pfile = self.prefix + self.catalogue_format + ".fits"
        nfile = self.prefix + "_negative" + self.catalogue_format + ".fits"
        # i need to catch mmap.mmap error here

        # running a source finder
        self.source_finder(image=self.negimage,
                           output=nfile, thresh=self.neg_smooth,
                           savemask=self.savemaskneg,
                           prefix=self.prefix, **self.opts_neg)

        self.source_finder(image=self.imagename,
                           output=pfile, thresh=self.pos_smooth, 
                           savemask=self.savemaskpos,
                           prefix=self.prefix, **self.opts_pos)

        self.log.info(" Source Finder completed successfully ")


         
        pmodel, positive, labels = self.params(pfile)
        nmodel, negative, labels = self.params(nfile)
     
        # setting up a kernel, Gaussian kernel
        bandwidth = []

        for plane in negative.T:
            bandwidth.append(plane.std())

        nplanes = len(labels)
        cov = numpy.zeros([nplanes, nplanes])
        nnsrc = len(negative)
        npsrc = len(positive)
        
        self.log.info(" There are %d positive and %d negtive detections "%(npsrc, nnsrc))
 
        if nnsrc == 0 or npsrc ==0:
            self.log.error("The resulting array has length of 0 thus cannot compute"
                    " the reliability. Aborting.")

        self.log.info(" Computing the reliabilities ")
        for i in range(nplanes):
            for j in range(nplanes):
                if i == j:
                    cov[i, j] = bandwidth[i]*((4.0/((nplanes+2)*
                                  nnsrc))**(1.0/(nplanes+4.0)))
        self.log.info("The resulting covariance matrix is %r"%cov)

        pcov = utils.gaussian_kde_set_covariance(positive.T, cov)
        ncov = utils.gaussian_kde_set_covariance(negative.T, cov)
    
        # get number densities
        nps = pcov(positive.T) * npsrc
        nns = ncov(positive.T) * nnsrc

        # define reliability of positive catalog
        rel = (nps-nns)/nps
        for src, rf in zip(pmodel.sources, rel):
            src.setAttribute("rel", rf)
        self.log.info(" Saved the reliabilities values.")

        # remove sources with poor correlation and high reliability,
        # the values are currently arbitrary
        if self.do_psf_corr and self.derel:
            for s in pmodel.sources:
                cf, r = s.correlation_factor, s.rel
                if cf < 0.006 and r > 0.60:
                    s.rel = 0.0    

        if self.makeplots:
            savefig = self.prefix + "_planes.png"
            utils.plot(positive, negative, rel=rel, labels=labels,
                        savefig=savefig, prefix=self.prefix)

        # removes sources in a given radius from the phase center
        if self.radiusrm:
            self.log.info(" Remove sources ra, dec, radius of  %r" 
                          " from the phase center" %self.radiusrm)
            pmodel = self.remove_sources_within(pmodel)


        if not self.savefits:
            self.log.info(" Deleting the negative image.")
            os.system("rm -r %s"%self.negimage)

        # Set field Center
        pmodel.ra0, pmodel.dec0 = map(numpy.deg2rad, self.wcs.getCentreWCSCoords())

        return  pmodel, nmodel, self.locstep
Exemple #3
0
    def get_reliability(self):

        # finding sources
        self.log.info(" Extracting the sources on both sides ")

        pfile = self.prefix + self.catalogue_format + ".fits"
        nfile = self.prefix + "_negative" + self.catalogue_format + ".fits"
        # i need to catch mmap.mmap error here

        # running a source finder
        self.source_finder(image=self.negimage,
                           output=nfile,
                           thresh=self.neg_smooth,
                           savemask=self.savemaskneg,
                           prefix=self.prefix,
                           **self.opts_neg)

        self.source_finder(image=self.imagename,
                           output=pfile,
                           thresh=self.pos_smooth,
                           savemask=self.savemaskpos,
                           prefix=self.prefix,
                           **self.opts_pos)

        self.log.info(" Source Finder completed successfully ")

        pmodel, positive, labels = self.params(pfile)
        nmodel, negative, labels = self.params(nfile)

        # setting up a kernel, Gaussian kernel
        bandwidth = []

        for plane in negative.T:
            bandwidth.append(plane.std())

        nplanes = len(labels)
        cov = numpy.zeros([nplanes, nplanes])
        nnsrc = len(negative)
        npsrc = len(positive)

        self.log.info(" There are %d positive and %d negtive detections " %
                      (npsrc, nnsrc))

        if nnsrc == 0 or npsrc == 0:
            self.log.error(
                "The resulting array has length of 0 thus cannot compute"
                " the reliability. Aborting.")

        self.log.info(" Computing the reliabilities ")
        for i in range(nplanes):
            for j in range(nplanes):
                if i == j:
                    cov[i, j] = bandwidth[i] * ((4.0 / (
                        (nplanes + 2) * nnsrc))**(1.0 / (nplanes + 4.0)))
        self.log.info("The resulting covariance matrix is %r" % cov)

        pcov = utils.gaussian_kde_set_covariance(positive.T, cov)
        ncov = utils.gaussian_kde_set_covariance(negative.T, cov)

        # get number densities
        nps = pcov(positive.T) * npsrc
        nns = ncov(positive.T) * nnsrc

        # define reliability of positive catalog
        rel = (nps - nns) / nps
        for src, rf in zip(pmodel.sources, rel):
            src.setAttribute("rel", rf)
        self.log.info(" Saved the reliabilities values.")

        # remove sources with poor correlation and high reliability,
        # the values are currently arbitrary
        if self.do_psf_corr and self.derel:
            for s in pmodel.sources:
                cf, r = s.correlation_factor, s.rel
                if cf < 0.006 and r > 0.60:
                    s.rel = 0.0

        if self.makeplots:
            savefig = self.prefix + "_planes.png"
            utils.plot(positive,
                       negative,
                       rel=rel,
                       labels=labels,
                       savefig=savefig,
                       prefix=self.prefix)

        # removes sources in a given radius from the phase center
        if self.radiusrm:
            self.log.info(" Remove sources ra, dec, radius of  %r"
                          " from the phase center" % self.radiusrm)
            pmodel = self.remove_sources_within(pmodel)

        if not self.savefits:
            self.log.info(" Deleting the negative image.")
            os.system("rm -r %s" % self.negimage)

        # Set field Center
        pmodel.ra0, pmodel.dec0 = map(numpy.deg2rad,
                                      self.wcs.getCentreWCSCoords())

        return pmodel, nmodel, self.locstep