def combineWindows(self): """Take raw combined labels and compress them to combinedWindows.""" combinedWindows = {} for relativePath, labels in self.combinedLabels.iteritems(): delta = labels["timestamp"][1] - labels["timestamp"][0] labels = labels[labels["label"] == 1] dataSetWindows = [] if labels.shape[0] != 0: t1 = None t0 = None for _, row in labels.iterrows(): t1 = row["timestamp"] if t0 is None: window = [strf(t1)] elif t1 - t0 != delta: window.append(strf(t0)) dataSetWindows.append(window) window = [strf(t1)] t0 = t1 window.append(strf(t1)) dataSetWindows.append(window) combinedWindows[relativePath] = dataSetWindows self.combinedWindows = combinedWindows
def relaxWindows(self): """ This takes all windows and relaxes them by a certain percentage of the data. A length (relaxWindowLength) is picked before hand and each window is lengthened on both its left and right side by that length. This length is chosen as a certain percetange of the dataset. """ allRelaxedWindows = {} for relativePath, limits in self.combinedWindows.iteritems(): data = self.corpus.dataSets[relativePath].data length = len(data["timestamp"]) percentOfDataSet = 0.025 numWindows = len(limits) relaxWindowLength = int(percentOfDataSet*length) relaxedWindows = [] for i, limit in enumerate(limits): t1, t2 = limit indices = map(( lambda t: data["timestamp"][data["timestamp"] == t].index[0]), limit) t1Index = max(indices[0] - relaxWindowLength, 0) t2Index = min(indices[0] + relaxWindowLength, length-1) relaxedLimit = [strf(data["timestamp"][t1Index]), strf(data["timestamp"][t2Index])] relaxedWindows.append(relaxedLimit) allRelaxedWindows[relativePath] = relaxedWindows self.combinedRelaxedWindows = allRelaxedWindows
def relaxWindows(self, percentOfDataSet = 0.1): """ This takes all windows and relaxes them (expands them) by a certain percentage of the data. A length (relaxWindowLength) is picked beforehand and each window is lengthened on both its left and right side by that length. This length is chosen as a certain percentage of the datafile. """ allRelaxedWindows = {} for relativePath, limits in self.combinedWindows.iteritems(): data = self.corpus.dataFiles[relativePath].data length = len(data["timestamp"]) relaxWindowLength = int(percentOfDataSet*length) relaxedWindows = [] # print "\n\n========================" # print "file=",relativePath, "relaxation amount=",relaxWindowLength for limit in limits: leftIndex = data[data["timestamp"] == limit[0]]["timestamp"].index[0] rightIndex = data[data["timestamp"] == limit[1]]["timestamp"].index[0] newLeftIndex = max(leftIndex - relaxWindowLength/2, 0) newRightIndex = min(rightIndex + relaxWindowLength/2, length-1) relaxedLimit = [strf(data["timestamp"][newLeftIndex]), strf(data["timestamp"][newRightIndex])] # print "original window indices=",leftIndex,rightIndex # print "relaxed indices=",newLeftIndex,newRightIndex # print "original timestamps=",limit # print "relaxed timestamps=",relaxedLimit relaxedWindows.append(relaxedLimit) allRelaxedWindows[relativePath] = relaxedWindows self.combinedRelaxedWindows = allRelaxedWindows
def applyWindows(self): """ This takes all the true anomalies, as calculated by combineLabels(), and adds a standard window. The window length is the class variable windowSize, and the location is centered on the anomaly timestamp. If verbosity = 2, the window metrics are printed to the console. """ allWindows = {} for relativePath, anomalies in self.labelIndices.iteritems(): data = self.corpus.dataFiles[relativePath].data length = len(data) num = len(anomalies) if num: windowLength = int(self.windowSize * length / len(anomalies)) else: windowLength = int(self.windowSize * length) if self.verbosity == 2: print "----" print "Window metrics for file", relativePath print "file length =", length, ";" \ "number of windows =", num, ";" \ "window length =", windowLength windows = [] for a in anomalies: front = max(a - windowLength / 2, 0) back = min(a + windowLength / 2, length - 1) windowLimit = [ strf(data["timestamp"][front]), strf(data["timestamp"][back]) ] windows.append(windowLimit) allWindows[relativePath] = windows self.combinedWindows = allWindows
def applyWindows(self): """ This takes all the true anomalies, as calculated by combineLabels(), and adds a standard window. The window length is the class variable windowSize, and the location is centered on the anomaly timestamp. If verbosity=2, the window metrics are printed to the console. """ allWindows = {} for relativePath, anomalies in self.labelIndices.iteritems(): data = self.corpus.dataFiles[relativePath].data length = len(data) num = len(anomalies) if num: windowLength = int(self.windowSize * length / len(anomalies)) else: windowLength = int(self.windowSize * length) if self.verbosity==2: print "----" print "Window metrics for file", relativePath print "file length =", length, ";" \ "number of windows =", num, ";" \ "window length =", windowLength windows = [] for a in anomalies: front = max(a - windowLength/2, 0) back = min(a + windowLength/2, length-1) windowLimit = [strf(data["timestamp"][front]), strf(data["timestamp"][back])] windows.append(windowLimit) allWindows[relativePath] = windows self.combinedWindows = allWindows
def combineWindows(self): """Take raw combined Labels and compress them to combinedWindows.""" allWindows = {} for relativePath, labels in self.combinedLabels.iteritems(): delta = labels["timestamp"][1] - labels["timestamp"][0] labels = labels[labels["label"] == 1] dataSetWindows = [] if labels.shape[0] == 0: dataSetWindows = [] else: curr = None prev = None for _, row in labels.iterrows(): curr = row["timestamp"] if not prev: currentWindow = [strf(curr)] elif curr - prev != delta: currentWindow.append(strf(prev)) dataSetWindows.append(currentWindow) currentWindow = [strf(curr)] prev = curr currentWindow.append(strf(curr)) dataSetWindows.append(currentWindow) allWindows[relativePath] = dataSetWindows self.combinedWindows = allWindows