def test_predict_input_wrong(self): indev = IndividualAnomalyInductive(w_martingale=15, non_conformity="median", k=20, dev_threshold=0.6) indev.fit(np.array([[1,2,3], [4,5,6], [7,8,9]])) with self.assertRaises(InputValidationError): indev.predict(None, []) with self.assertRaises(InputValidationError): indev.predict(None, "foo")
def test_predict_not_fitted(self): indev = IndividualAnomalyInductive(w_martingale=15, non_conformity="median", k=20, dev_threshold=0.6) with self.assertRaises(NotFittedError): indev.predict(None, [1,1,1])
class AnomalyDetection: def __init__(self): pass def deviation_detection(self, data, mu, sigma, l1=4, l2=8, l3=12): z_s = self.zscore(data, mu, sigma) if (len(z_s.shape) > 1): z_s = z_s[:, 0] t = np.linspace(0, len(z_s) - 1, len(z_s)) thres1 = l1 * sigma thres2 = l2 * sigma thres3 = l3 * sigma plt.scatter(t[np.where(z_s <= thres1)], z_s[np.where(z_s <= thres1)], color='y', label='Normal', alpha=0.3, edgecolors='none') plt.scatter(t[np.where((z_s > thres1) & (z_s <= thres2))], z_s[np.where((z_s > thres1) & (z_s <= thres2))], color='b', label='L1 Threshold', alpha=0.3, edgecolors='none') plt.scatter(t[np.where((z_s > thres2) & (z_s <= thres3))], z_s[np.where((z_s > thres2) & (z_s <= thres3))], color='g', label='L2 Threshold', alpha=0.3, edgecolors='none') plt.scatter(t[np.where(z_s > thres3)], z_s[np.where(z_s > thres3)], color='r', label='Anomalous points', alpha=0.3, edgecolors='none') plt.xlabel('Observation Signal (in samples)') plt.ylabel('Anomaly Score') plt.title('Anomaly Score Estimation') plt.legend() return z_s, sigma def train_cosmo(self, data, w_martingale=15, non_conformity="median", k=20): df = data self.model = IndividualAnomalyInductive(w_martingale=w_martingale, non_conformity=non_conformity, k=k) # Fit the model to a fixed subset of the data X_fit = data.to_numpy() self.model.fit(X_fit) def test_cosmo(self, data): cols = ['Strangeness', 'P-Values', 'Deviation'] lst_dict = [] df = data for t, x in zip(df.index, df.values): info = self.model.predict(t, x) lst_dict.append({ 'Strangeness': info.strangeness, 'P-Values': info.pvalue, 'Deviation': info.deviation }) # Plot strangeness and deviation level over time # gr = model.plot_deviations(figsize=(2000,2000)) df1 = pd.DataFrame(lst_dict, columns=cols) return df1['Strangeness'].to_numpy(), df1['P-Values'].to_numpy() def nonstationary_AD_cosmo(self, data, n, w_martingale, k, non_conformity="median", ref_group=["hour-of-day"]): df = self.data cols = ['Strangeness', 'P-Values', 'Deviation'] lst_dict = [] model = IndividualAnomalyTransductive( w_martingale= w_martingale, # Window size for computing the deviation level non_conformity= non_conformity, # Strangeness measure: "median" or "knn" k=k, # Used if non_conformity is "knn" ref_group=ref_group # Criteria for reference group construction ) for t, x in zip(df.index, df.values): info = model.predict(t, x) lst_dict.append({ 'Strangeness': info.strangeness, 'P-Values': info.pvalue, 'Deviation': info.deviation }) # Plot strangeness and deviation level over time gr = model.plot_deviations(figsize=(2000, 2000)) df1 = pd.DataFrame(lst_dict, columns=cols) return df1, gr
def test_predict_median_w3(self): indev = IndividualAnomalyInductive(w_martingale=3, non_conformity="median", k=20, dev_threshold=0.6) indev.fit(np.array([[1,2,3], [4,5,6], [7,8,9]])) res = indev.predict(None, [1,1,1]) expected = DeviationContext((3**2+4**2+5**2)**0.5, 0, 1/3, False) self.assertEqual(res, expected)
from grand import IndividualAnomalyInductive if __name__ == '__main__': # Get data from one unit (vehicle) dataset = load_artificial_toy(0) #load_vehicles() unit1_train = [x for dt, x in dataset.stream_unit(1) ] # we use unit number 1 for training # Create an instance of IndividualAnomalyInductive indev = IndividualAnomalyInductive( w_martingale=15, # Window size for computing the deviation level non_conformity= "median", # Strangeness measure: "median" or "knn" or "lof" k=50, # Used if non_conformity is "knn" dev_threshold=.6) # Threshold on the deviation level # Fit the IndividualAnomalyInductive detector to unit1_train indev.fit(unit1_train) # At each time step dt, a data-point x comes from the stream of unit number 0 for dt, x in dataset.stream_unit(0): devContext = indev.predict(dt, x) st, pv, dev, isdev = devContext.strangeness, devContext.pvalue, devContext.deviation, devContext.is_deviating print("Time: {} ==> strangeness: {}, p-value: {}, deviation: {} ({})". format(dt, st, pv, dev, "high" if isdev else "low")) # Plot p-values and deviation level over time indev.plot_deviations()