# Modified Stylesheet for notebook. from IPython.core.display import HTML def css_styling(): styles = open("custom.css", "r").read() return HTML(styles) css_styling() import matplotlib.pyplot as plt import numpy as np from collections import defaultdict np.random.seed(seed=111111) np.set_printoptions(precision=2, suppress=True) import sys; sys.path.append('../src/') from change_detector import ChangeDetector # Initialize change detector detector = ChangeDetector() print detector print "Signal size:", detector.signal_size # Add one point to the change_detector next(detector.step(10)) # (The step() function is written as a python generator) print detector print "Signal size:", detector.signal_size from change_detector import OnlineSimulator signal = np.random.randint(10, size=20) print "Signal: {}".format(signal) # reset sim = OnlineSimulator(detector, signal) # pass the signal the online_simulator sim.run(plot=True) (Note that the stopping rule will not be triggered, because we haven't created any stopping rules yet.) print detector Fine, okay, that works, but it doesn't do much. We'll now write a new Class that extends ChangeDetector. We'll add a residual. class MeanDetector(ChangeDetector): """ This is a simple detector that extends the base ChangeDetector. This serves as a useful "skeleton" on how to override the methods of ChangeDetector. We're adding a "residual" -- we'll keep track of the mean of the signal. """ def __init__(self): super(MeanDetector, self).__init__() self.total_val = 0 # Mean is a pretty simple and useful residual self.mean_ = np.nan def update_residuals(self, new_signal_value): self._update_base_residuals(new_signal_value) self.total_val += new_signal_value self.mean_ = float(self.total_val) / self.signal_size def check_stopping_rules(self, new_signal_value): # Implement Stopping rules here if np.abs(new_signal_value - self.mean_) > 100: self.rules_triggered = True detector = MeanDetector() OnlineSimulator(detector, signal).run() signal[17] = 200 plt.plot(signal) # Let's run our detector on this signal OnlineSimulator(detector, signal).run()