Exemple #1
0
def plotSeqAndFeatures(seq, X, model, createModelAx=False, padBothSides=False, capYLim=1000):
	"""plots the time series above the associated feature matrix"""

	plt.figure(figsize=(10, 8))
	if createModelAx:
		nRows = 4
		nCols = 7
		axSeq = plt.subplot2grid((nRows,nCols), (0,0), colspan=(nCols-1))
		axSim = plt.subplot2grid((nRows,nCols), (1,0), colspan=(nCols-1), rowspan=(nRows-1))
		axPattern = plt.subplot2grid((nRows,nCols), (1,nCols-1), rowspan=(nRows-1))
		axes = (axSeq, axSim, axPattern)
	else:
		nRows = 4
		nCols = 1
		axSeq = plt.subplot2grid((nRows,nCols), (0,0))
		axSim = plt.subplot2grid((nRows,nCols), (1,0), rowspan=(nRows-1))
		axes = (axSeq, axSim)

	for ax in axes:
		ax.autoscale(tight=True)

	axSeq.plot(seq)
	axSeq.set_ylim([seq.min(), min(capYLim, seq.max())])

	if padBothSides:
		padLen = (len(seq) - X.shape[1]) // 2
		Xpad = ar.addZeroCols(X, padLen, prepend=True)
		Xpad = ar.addZeroCols(Xpad, padLen, prepend=False)
	else:
		padLen = len(seq) - X.shape[1]
		Xpad = ar.addZeroCols(Xpad, padLen, prepend=False)
	axSim.imshow(Xpad, interpolation='nearest', aspect='auto')

	axSeq.set_title("Time Series", fontsize=28, y=1.04)
	axSim.set_title("Feature Matrix", fontsize=28, y=1.01)

	# plot the learned pattern model
	if createModelAx:
		axPattern.set_title("Learned\nPattern", fontsize=24, y=1.02)

		if model is not None:
			axPattern.imshow(model, interpolation='nearest', aspect='auto')
			tickLocations = plt.FixedLocator([0, model.shape[1]])
			axPattern.xaxis.set_major_locator(tickLocations)
			axPattern.yaxis.tick_right() # y ticks on right side
		else:
			print("WARNING: attempted to plot null feature weights!")

	for ax in axes:
		for tick in ax.get_xticklabels() + ax.get_yticklabels():
			tick.set_fontsize(20)
	removeEveryOtherYTick(axSeq)

	return axes
Exemple #2
0
def _learnFF(seq, X, Xblur, Lmin, Lmax, Lfilt, generateSeedsStep=0.1):

    padLen = (len(seq) - X.shape[1]) // 2
    X = ar.addZeroCols(X, padLen, prepend=True)
    X = ar.addZeroCols(X, padLen, prepend=False)
    Xblur = ar.addZeroCols(Xblur, padLen, prepend=True)
    Xblur = ar.addZeroCols(Xblur, padLen, prepend=False)

    timeStartSeed = time.clock()

    # find seeds; i.e., candidate instance indices from which to generalize
    numShifts = int(1.0 / generateSeedsStep) + 1
    stepLen = int(Lmax * generateSeedsStep)
    windowLen = Lmax + stepLen

    # score all subseqs based on how much they don't look like random walks
    # when examined using different sliding window lengths
    # scores = np.zeros(len(seq))
    # for dim in range(seq.shape[1]):
    # 	# compute these just once, not once per length
    # 	dimData = seq[:, dim].flatten()
    # 	std = np.std(dimData[1:] - dimData[:-1])
    # 	for divideBy in [1, 2, 4, 8]:
    # 		partialScores = feat.windowScoresRandWalk(dimData, Lmin // divideBy, std)
    # 		scores[:len(partialScores)] += partialScores
    scores = _seedScores(seq, Lmin)

    # figure out optimal pair based on scores of all subseqs
    bestIdx = np.argmax(scores)
    start = max(0, bestIdx - Lmin)
    end = min(len(scores), start + Lmin)
    scores[start:end] = -1  # disqualify idxs within Lmin of bestIdx
    secondBestIdx = np.argmax(scores)

    # compute all seed idxs from this pair
    seedIdxs = [bestIdx, secondBestIdx]
    maxIdx = X.shape[1] - windowLen - 1
    seedStartIdxs = _computeAllSeedIdxsFromPair(seedIdxs, numShifts, stepLen, maxIdx)
    seedEndIdxs = seedStartIdxs + windowLen

    timeEndSeed = time.clock()

    bsfScore, bsfLocs, bsfFilt = _findInstancesUsingSeedLocs(
        X, Xblur, seedStartIdxs, seedEndIdxs, Lmin, Lmax, Lfilt, windowLen=windowLen
    )

    startIdxs, endIdxs = _extractTrueLocs(X, Xblur, bsfLocs, bsfFilt, windowLen, Lmin, Lmax)

    timeEndFF = time.clock()
    print "learnFF(): seconds to find seeds, regions, total =\n\t{:.3f}\t{:.3f}\t{:.3f}".format(
        timeEndSeed - timeStartSeed, timeEndFF - timeEndSeed, timeEndFF - timeStartSeed
    )

    return startIdxs, endIdxs, bsfFilt
Exemple #3
0
def _learnFF(seq, X, Xblur, Lmin, Lmax, Lfilt, generateSeedsStep=.1):

    padLen = (len(seq) - X.shape[1]) // 2
    X = ar.addZeroCols(X, padLen, prepend=True)
    X = ar.addZeroCols(X, padLen, prepend=False)
    Xblur = ar.addZeroCols(Xblur, padLen, prepend=True)
    Xblur = ar.addZeroCols(Xblur, padLen, prepend=False)

    timeStartSeed = time.clock()

    # find seeds; i.e., candidate instance indices from which to generalize
    numShifts = int(1. / generateSeedsStep) + 1
    stepLen = int(Lmax * generateSeedsStep)
    windowLen = Lmax + stepLen

    # score all subseqs based on how much they don't look like random walks
    # when examined using different sliding window lengths
    scores = np.zeros(len(seq))
    for dim in range(seq.shape[1]):
        # compute these just once, not once per length
        dimData = seq[:, dim].flatten()
        std = np.std(dimData[1:] - dimData[:-1])
        for divideBy in [1, 2, 4, 8]:
            partialScores = feat.windowScoresRandWalk(dimData,
                                                      Lmin // divideBy, std)
            scores[:len(partialScores)] += partialScores

    # figure out optimal pair based on scores of all subseqs
    bestIdx = np.argmax(scores)
    start = max(0, bestIdx - Lmin)
    end = min(len(scores), start + Lmin)
    scores[start:end] = -1  # disqualify idxs within Lmin of bestIdx
    secondBestIdx = np.argmax(scores)

    # compute all seed idxs from this pair
    seedIdxs = [bestIdx, secondBestIdx]
    maxIdx = X.shape[1] - windowLen - 1
    seedStartIdxs = _computeAllSeedIdxsFromPair(seedIdxs, numShifts, stepLen,
                                                maxIdx)
    seedEndIdxs = seedStartIdxs + windowLen

    timeEndSeed = time.clock()

    bsfScore, bsfLocs, bsfFilt = _findInstancesUsingSeedLocs(
        X,
        Xblur,
        seedStartIdxs,
        seedEndIdxs,
        Lmin,
        Lmax,
        Lfilt,
        windowLen=windowLen)

    startIdxs, endIdxs = _extractTrueLocs(X, Xblur, bsfLocs, bsfFilt,
                                          windowLen, Lmin, Lmax)

    timeEndFF = time.clock()
    print "learnFF(): seconds to find seeds, regions, total =\n\t{:.3f}\t{:.3f}\t{:.3f}".format(
        timeEndSeed - timeStartSeed, timeEndFF - timeEndSeed,
        timeEndFF - timeStartSeed)

    return startIdxs, endIdxs, bsfFilt
Exemple #4
0
def plotSeqAndFeatures(seq,
                       X,
                       model,
                       createModelAx=False,
                       padBothSides=False,
                       capYLim=1000):
    """plots the time series above the associated feature matrix"""

    plt.figure(figsize=(10, 8))
    if createModelAx:
        nRows = 4
        nCols = 7
        axSeq = plt.subplot2grid((nRows, nCols), (0, 0), colspan=(nCols - 1))
        axSim = plt.subplot2grid((nRows, nCols), (1, 0),
                                 colspan=(nCols - 1),
                                 rowspan=(nRows - 1))
        axPattern = plt.subplot2grid((nRows, nCols), (1, nCols - 1),
                                     rowspan=(nRows - 1))
        axes = (axSeq, axSim, axPattern)
    else:
        nRows = 4
        nCols = 1
        axSeq = plt.subplot2grid((nRows, nCols), (0, 0))
        axSim = plt.subplot2grid((nRows, nCols), (1, 0), rowspan=(nRows - 1))
        axes = (axSeq, axSim)

    for ax in axes:
        ax.autoscale(tight=True)

    axSeq.plot(seq)
    axSeq.set_ylim([seq.min(), min(capYLim, seq.max())])

    if padBothSides:
        padLen = (len(seq) - X.shape[1]) // 2
        Xpad = ar.addZeroCols(X, padLen, prepend=True)
        Xpad = ar.addZeroCols(Xpad, padLen, prepend=False)
    else:
        padLen = len(seq) - X.shape[1]
        Xpad = ar.addZeroCols(Xpad, padLen, prepend=False)
    axSim.imshow(Xpad, interpolation='nearest', aspect='auto')

    axSeq.set_title("Time Series", fontsize=28, y=1.04)
    axSim.set_title("Feature Matrix", fontsize=28, y=1.01)

    # plot the learned pattern model
    if createModelAx:
        axPattern.set_title("Learned\nPattern", fontsize=24, y=1.02)

        if model is not None:
            axPattern.imshow(model, interpolation='nearest', aspect='auto')
            tickLocations = plt.FixedLocator([0, model.shape[1]])
            axPattern.xaxis.set_major_locator(tickLocations)
            axPattern.yaxis.tick_right()  # y ticks on right side
        else:
            print("WARNING: attempted to plot null feature weights!")

    for ax in axes:
        for tick in ax.get_xticklabels() + ax.get_yticklabels():
            tick.set_fontsize(20)
    removeEveryOtherYTick(axSeq)

    return axes