Esempio n. 1
0
def _findAllInstancesFromSeedLoc(X, Xblur, seedStartIdx, seedEndIdx, Lmin, Lmax, Lfilt, p0, p0blur, logs_0, bsfScore=0):

    windowLen = seedEndIdx - seedStartIdx  # assume end idx not inclusive

    # ================================ candidate location generation

    x0 = Xblur[:, seedStartIdx:seedEndIdx]
    dotProds = _dotProdsWithAllWindows(x0, X)

    # compute best locations to try and then sort them in decreasing order
    bestIdxs = ar.nonOverlappingMaxima(dotProds, Lmin)
    sortIdxs = np.argsort(dotProds[bestIdxs])[::-1]
    idxs = bestIdxs[sortIdxs]

    # print("{} idxs: {}".format(seedStartIdx, idxs)) # TODO remove
    # print "dotProds", dotProds[::5]
    # print "sum of dotProds", np.sum(dotProds)

    # print "sum of Phi, PhiBlur", np.sum(X), np.sum(Xblur)

    # ================================ now figure out which idxs should be instances

    # initialize counts; we add a tiny const so we don't take log(0)
    idx = idxs[0]
    counts = np.copy(X[:, idx : idx + windowLen])
    countsBlur = np.copy(Xblur[:, idx : idx + windowLen]) + 0.0001

    bestOdds = -np.inf
    bestFilt = None
    bestLocs = None
    for i, idx in enumerate(idxs[1:]):
        k = i + 2.0

        # update counts
        window = X[:, idx : idx + windowLen]
        windowBlur = Xblur[:, idx : idx + windowLen]
        counts += window
        countsBlur += windowBlur

        # pattern odds
        theta_1 = countsBlur / k
        logs_1 = np.log(theta_1)
        # logs_1[np.isneginf(logs_1)] = -999 # any non-inf number--will be masked by counts
        logDiffs = logs_1 - logs_0
        gains = counts * logDiffs  # *must* use this so -999 is masked
        threshMask = gains > 0
        threshMask *= theta_1 > 0.5
        filt = logDiffs * threshMask
        logOdds = np.sum(counts * filt)

        # nearest enemy odds
        nextWindowOdds = -np.inf
        if k < len(idxs):
            idx = idxs[k]
            nextWindow = X[:, idx : idx + windowLen]
            nextWindowOdds = np.sum(filt * nextWindow) * k

            # subtract nearest enemy or "expected" enemy from noise
        randomOdds = np.sum(filt) * p0blur * k
        penalty = max(randomOdds, nextWindowOdds)
        logOdds -= penalty

        if logOdds > bestOdds:
            bestOdds = logOdds
            bestFilt = np.copy(filt)
            bestLocs = idxs[:k]

    return bestOdds, bestLocs, bestFilt
Esempio n. 2
0
File: ff.py Progetto: dblalock/flock
def _findAllInstancesFromSeedLoc(X,
                                 Xblur,
                                 seedStartIdx,
                                 seedEndIdx,
                                 Lmin,
                                 Lmax,
                                 Lfilt,
                                 p0,
                                 p0blur,
                                 logs_0,
                                 bsfScore=0):

    windowLen = seedEndIdx - seedStartIdx  # assume end idx not inclusive

    # ================================ candidate location generation

    x0 = Xblur[:, seedStartIdx:seedEndIdx]
    dotProds = _dotProdsWithAllWindows(x0, X)

    # compute best locations to try and then sort them in decreasing order
    bestIdxs = ar.nonOverlappingMaxima(dotProds, Lmin)
    sortIdxs = np.argsort(dotProds[bestIdxs])[::-1]
    idxs = bestIdxs[sortIdxs]

    # ================================ now figure out which idxs should be instances

    # initialize counts
    idx = idxs[0]
    counts = np.copy(X[:, idx:idx + windowLen])
    countsBlur = np.copy(Xblur[:, idx:idx + windowLen])

    bestOdds = -np.inf
    bestFilt = None
    bestLocs = None
    for i, idx in enumerate(idxs[1:]):
        k = i + 2.

        # update counts
        window = X[:, idx:idx + windowLen]
        windowBlur = Xblur[:, idx:idx + windowLen]
        counts += window
        countsBlur += windowBlur

        # pattern odds
        theta_1 = countsBlur / k
        logs_1 = np.log(theta_1)
        logs_1[np.isneginf(
            logs_1)] = -999  # any non-inf number--will be masked by counts
        logDiffs = (logs_1 - logs_0)
        gains = counts * logDiffs  # *must* use this so -999 is masked
        threshMask = gains > 0
        threshMask *= theta_1 > .5
        filt = logDiffs * threshMask
        logOdds = np.sum(counts * filt)

        # nearest enemy odds
        nextWindowOdds = -np.inf
        if k < len(idxs):
            idx = idxs[k]
            nextWindow = X[:, idx:idx + windowLen]
            nextWindowOdds = np.sum(filt * nextWindow) * k

        # subtract nearest enemy or "expected" enemy from noise
        randomOdds = np.sum(filt) * p0blur * k
        penalty = max(randomOdds, nextWindowOdds)
        logOdds -= penalty

        if logOdds > bestOdds:
            bestOdds = logOdds
            bestFilt = np.copy(filt)
            bestLocs = idxs[:k]

    return bestOdds, bestLocs, bestFilt
Esempio n. 3
0
def candidatesFromDotProds(dotProds, Lmin):
    bestIdxs = ar.nonOverlappingMaxima(dotProds, Lmin)
    sortIdxs = np.argsort(dotProds[bestIdxs])[::-1]
    return bestIdxs[sortIdxs]