Ejemplo n.º 1
0
    def scan(self, regfilt, threshold=5.0):

        bins = numpy.arange(self.time.min(),
                            self.time.max() + 1e-4, self.timebin)

        xbins = numpy.arange(self.xmin, self.xmax, 25)
        ybins = numpy.arange(self.ymin, self.ymax, 25)

        region = Likelihood.Region(self.xmin, self.xmax, self.ymin, self.ymax,
                                   40, regfilt, self.expomap, self.eventfile)

        ls = Likelihood.Likelihood(self.X, self.Y, region)

        # Build the likelihood model

        # Bkg model (isotropic)
        iso = Likelihood.Isotropic("bkg", 1.0)

        m = Likelihood.GlobalModel("likeModel") + iso

        knownSources = []

        for i, src in enumerate(self.sources):

            thisSrc = Likelihood.PointSource("src%i" % (i + 1), src[0], src[1],
                                             self.eventfile, self.hwu)

            m += thisSrc

            if i >= 10:
                log.info("Fixing normalization of source %s" % (i + 1))

                thisSrc["src%i_norm" % (i + 1)].fix()

            knownSources.append(thisSrc)

        ls.setModel(m)

        # Minimize the mlogLike to get the background level
        like0 = ls.minimize()

        figIntegrated = ls.plot()

        excessesFound = []

        figs = [figIntegrated]

        for t1, t2 in zip(bins[:-1], bins[1:]):

            sys.stderr.write(".")

            idx = (self.time >= t1) & (self.time < t2)

            # db = DbScanner(self.X[idx],self.Y[idx])

            # db = SAODetect(self.X[idx],self.Y[idx])

            # db = cellDetect.CellDetect(self.X[idx],self.Y[idx],200,3)

            ls = Likelihood.Likelihood(self.X[idx], self.Y[idx], region)

            totCounts = ls.getTotalCounts()

            if totCounts < 3:
                # No need to perform any fit. Too few counts in this
                # interval

                log.debug("Skip interval %s - %s, less than 3 counts here" %
                          (t1, t2))

                continue

            # Build the likelihood model

            # Bkg model (isotropic)
            iso = Likelihood.Isotropic("bkg", 0.1)

            m = Likelihood.GlobalModel("likeModel") + iso

            for i, src in enumerate(knownSources):
                m += src

            ls.setModel(m)

            # Minimize the mlogLike to get the background level
            like0 = ls.minimize(verbose=0)

            # ls.plot()

            bkgLevel = iso['bkg_amp'].value / pow(region.binsize, 2.0)
            error = iso['bkg_amp'].error / pow(region.binsize, 2.0)

            # print("Bkg: %s +/- %s" %(bkgLevel, error))

            db = cellDetect.CellDetect(self.X[idx], self.xmin, self.xmax,
                                       self.Y[idx], self.ymin, self.ymax, 200,
                                       3)

            centroids = db.findExcesses(bkgLevel * pow(200, 2),
                                        error * pow(200, 2))

            newSources = []

            if len(centroids) > 0:
                # Verify if this source is truly significant

                TSs = []

                for (x, y) in centroids:

                    sys.stderr.write("-")

                    # Avoid computing the TS for a source too close to the ones already
                    # known

                    d = map(
                        lambda src: math.sqrt(
                            pow(src.xpos - x, 2) + pow(src.ypos - y, 2)),
                        knownSources)

                    dd = filter(lambda x: x < 5.0 / 0.05, d)

                    if len(dd) > 0:
                        # There is a source close to this centroid. No need
                        # to investigate further
                        continue

                    thisSrc = Likelihood.PointSource("testsrc", x, y,
                                                     self.eventfile, self.hwu,
                                                     knownSources[0].outfile)

                    ls.model += thisSrc

                    like1 = ls.minimize(verbose=0)

                    TSs.append(2 * (like0 - like1))
                    # print("(X,Y) = (%s,%s) -> TS = %s" %(x,y,TSs[-1]))

                    ls.model.removeSource("testsrc")

                    # Using the approximation TS = sqrt( significance )

                    if TSs[-1] >= pow(threshold, 2):
                        newSources.append([x, y, TSs[-1]])

            if len(newSources) > 0:
                db.centroids = numpy.array(newSources)
                fig = db.plot(xbins, ybins)

                figs.append(fig)

                for (x, y, ts) in newSources:
                    excessesFound.append([t1, t2, x, y, math.sqrt(ts)])

        sys.stderr.write("\n")

        return excessesFound, figs