def init_od_s2s(state_dict: Dict, seq2seq: tf.keras.Model) -> OutlierSeq2Seq: """ Initialize OutlierSeq2Seq. Parameters ---------- state_dict Dictionary containing the parameter values. seq2seq Loaded seq2seq model. Returns ------- Initialized OutlierSeq2Seq instance. """ seq_len, n_features = state_dict['shape'][1:] od = OutlierSeq2Seq(n_features, seq_len, threshold=state_dict['threshold'], seq2seq=seq2seq, latent_dim=state_dict['latent_dim'], output_activation=state_dict['output_activation']) return od
def test_seq2seq(seq2seq_params): # OutlierSeq2Seq parameters n_features, seq_len, threshold, threshold_perc, return_instance_score, \ return_feature_score, outlier_perc, outlier_type = seq2seq_params # create artificial sine time series X = np.sin(np.linspace(-50, 50, 10000)).astype(np.float32).reshape((-1, n_features)) # create outliers for threshold and detection X_threshold = inject_outlier_ts(X, perc_outlier=100 - threshold_perc, perc_window=10, n_std=10., min_std=9.).data X_outlier = inject_outlier_ts(X, perc_outlier=100 - threshold_perc, perc_window=10, n_std=10., min_std=9.).data # define architecture od = OutlierSeq2Seq(n_features, seq_len, threshold=threshold, latent_dim=latent_dim) if threshold is None: assert od.threshold == 0. else: assert od.threshold == threshold assert od.meta == {'name': 'OutlierSeq2Seq', 'detector_type': 'offline', 'data_type': 'time-series', 'version': __version__} # fit OutlierSeq2Seq od.fit(X, epochs=2, verbose=False) # create some outliers and infer threshold od.infer_threshold(X_threshold, threshold_perc=threshold_perc) # compute scores and check ranges fscore, iscore = od.score(X_threshold, outlier_perc=outlier_perc) if isinstance(od.threshold, np.ndarray): perc_score = 100 * (fscore < 0).astype(int).sum() / iscore.shape[0] / n_features assert threshold_perc + 5 > perc_score > threshold_perc - 5 # create outliers and make predictions od_preds = od.predict(X_outlier, return_instance_score=return_instance_score, return_feature_score=return_feature_score, outlier_type=outlier_type, outlier_perc=outlier_perc) assert od_preds['meta'] == od.meta if outlier_type == 'instance': assert od_preds['data']['is_outlier'].shape == (X.shape[0],) if return_instance_score: assert od_preds['data']['is_outlier'].sum() == (od_preds['data']['instance_score'] > 0).astype(int).sum() elif outlier_type == 'feature': assert od_preds['data']['is_outlier'].shape == X.shape if return_feature_score: assert od_preds['data']['is_outlier'].sum() == (od_preds['data']['feature_score'] > 0).astype(int).sum() if return_feature_score: assert od_preds['data']['feature_score'].shape == X.shape else: assert od_preds['data']['feature_score'] is None if return_instance_score: assert od_preds['data']['instance_score'].shape == (X.shape[0],) else: assert od_preds['data']['instance_score'] is None
def seq2seq(data, df): X_train = np.array(data).astype(np.float32) # initialize outlier detector n_features = 5 seq_len = 50 od = OutlierSeq2Seq(n_features, seq_len, threshold=None, latent_dim=20) # train od.fit(X_train[:7200], epochs=10, verbose=False) od.infer_threshold(X_train[:7200], threshold_perc=95) od_preds = od.predict( X_train[:7200], outlier_type='instance', # use 'feature' or 'instance' level return_feature_score=True, # scores used to determine outliers return_instance_score=True) pred = (od_preds['data']['is_outlier'] == 1) a = (df.iloc[:7200]).loc[pred] return a
OutlierVAE(threshold=threshold, latent_dim=latent_dim, samples=samples, **kwargs), OutlierAE(threshold=threshold, **kwargs), OutlierVAEGMM(threshold=threshold, gmm_density_net=gmm_density_net, n_gmm=n_gmm, latent_dim=latent_dim, samples=samples, **kwargs), OutlierProphet(threshold=.7, growth='logistic'), SpectralResidual(threshold=threshold, window_amp=10, window_local=10), OutlierSeq2Seq(input_dim, seq_len, threshold=threshold, threshold_net=threshold_net, latent_dim=latent_dim) ] n_tests = len(detector) @pytest.fixture def select_detector(request): return detector[request.param] @pytest.mark.parametrize('select_detector', list(range(n_tests)), indirect=True) def test_save_load(select_detector):