def run_sfa(dynamic_copies=2, expansion_order=1, cut_off=55): """ Batch Slow Feature Analysis Runs the batch slow feature analysis algorithm on the Tennessee Eastman process data set Parameters ---------- dynamic_copies: int The number of lagged copies per data point expansion_order: int The order of nonlinear expansion performed on the data cut_off: int The index to split the features into the slow and fast groups """ """ Import data """ X, T0, T4, T5, T10 = imp.import_tep_sets() """ Create plotter object """ figure_text = (f"Lagged Copies= {dynamic_copies} | " f"Expansion Order= {expansion_order} | " f"$M_d$= {cut_off}") plotter = SFAPlotter(show=False, save=True, figure_text=figure_text) """ Train model """ SlowFeature = SFA(data=X, dynamic_copies=dynamic_copies, expansion_order=expansion_order) SlowFeature.delta = 3 Y = SlowFeature.train() SlowFeature.partition_manual(cut_off) # Calculate speed indices for features eta = np.around( (X.shape[1] / (2 * np.pi)) * np.sqrt(SlowFeature.features_speed), 2) # Plot features plotter.plot_features("SFA", Y, eta, num_features=5) """ Test model """ T_dc, T_ec, S_dc, S_ec = SlowFeature.calculate_crit_values(alpha=SIG) data_iterable = [("SFA_IDV(0)", T0), ("SFA_IDV(4)", T4), ("SFA_IDV(5)", T5), ("SFA_IDV(10)", T10)] for name, test in data_iterable: print("Evaluating test: " + name) stats = SlowFeature.calculate_monitors(test).T # Create critical value lines for plots threshold = np.ones((test.shape[1], 1)) Tdc = threshold * T_dc Tec = threshold * T_ec Sdc = threshold * S_dc Sec = threshold * S_ec stats_crit = np.concatenate((Tdc, Tec, Sdc, Sec), axis=1).T # Plot stats plotter.plot_monitors(name, stats, stats_crit) plt.show()
def sfa_significance(dynamic_copies=2, expansion_order=1, cut_off=55, normal_data_included=0.99): """ Import data """ X, T0, _, _, _ = imp.import_tep_sets() """ Train model """ SlowFeature = SFA(data=X, dynamic_copies=dynamic_copies, expansion_order=expansion_order) SlowFeature.delta = 3 Y = SlowFeature.train() SlowFeature.partition_manual(cut_off) """ Test model """ data_points = T0.shape[1] stats = np.zeros((4, data_points)) stats_crit = np.zeros((4, data_points)) ub = 1 lb = 0 all_significance = [0, 0, 0, 0] for j in range(4): while True: # Smaller significance includes more data significance = (ub + lb) / 2 print(f"Checking alpha = {significance}") crits = SlowFeature.calculate_crit_values(alpha=significance) stats = SlowFeature.calculate_monitors(T0).T normal_fraction = np.sum(stats[j, :] < crits[j]) / data_points if normal_fraction < normal_data_included: ub = significance else: ub = 1 all_significance[j] = significance break return (all_significance)
def plot_incsfa(significance): X, T0, T4, T5, T10 = imp.import_tep_sets() num_vars, train_data_points = X.shape """ Train model """ # Create IncSFA object SlowFeature = IncSFA(33, 99, 99, 2, 1, 2, 0) SlowFeature.delta = 3 SlowFeature.Md = 55 SlowFeature.Me = 99 - 55 # Train model for i in range(X.shape[1]): _ = SlowFeature.add_data(X[:, i], use_svd_whitening=True) SlowFeature.converged = True # """ Test model """ test_data = [("IncSFA_IDV(0)", T0), ("IncSFA_IDV(4)", T4), ("IncSFA_IDV(5)", T5), ("IncSFA_IDV(10)", T10)] for name, test in test_data: test_obj = copy.deepcopy(SlowFeature) data_points = test.shape[1] stats = np.zeros((4, data_points)) for i in range(data_points): run = test_obj.add_data(test[:, i], alpha=0.05) stats[:, i] = run[1] crit_vals = [] for alpha in significance: p = 1 - alpha n = train_data_points Md = 55 Me = 99 - 55 gd = (Md * (n**2 - 2 * n)) / ((n - 1) * (n - Md - 1)) ge = (Me * (n**2 - 2 * n)) / ((n - 1) * (n - Me - 1)) T_d_crit = ST.chi2.ppf(p, Md) T_e_crit = ST.chi2.ppf(p, Me) S_d_crit = gd * ST.f.ppf(p, Md, n - Md - 1) S_e_crit = ge * ST.f.ppf(p, Me, n - Me - 1) crit_vals.append([T_d_crit, T_e_crit, S_d_crit, S_e_crit]) _fig = plt.figure(name) plt.subplots_adjust(wspace=0.4) figure_text = ("Lagged Copies= 2 | Expansion Order= 1 | $M_d$= 55 | " "Epochs= 1 | K= 99 | J= 99 | L= 2 ") plt.figtext(0.05, 0.02, figure_text) titles = ["$T^2$", "$T_e^2$", "$S^2$", "$S_e^2$"] for i in range(4): plt.subplot(4, 1, i + 1) plt.ylabel(titles[i]) plt.xlabel("Sample") plt.plot(stats[i, 3:]) for j in range(len(significance)): c_vals = np.ones((data_points)) * crit_vals[j][i] line_label = f"alpha = {significance[j]}" plt.plot(c_vals, linestyle='--', label=line_label) if i == 0: plt.legend(loc='upper left') _fig.set_size_inches(21, 9) plt.savefig(name, dpi=350) plt.close(fig=_fig)
def incsfa_significance(num_whitened_signals=99, num_features=99, sample_weight_parameter=2, expansion_order=1, dynamic_copies=2, conv_tol=0, cut_off=55, epochs=1, use_SVD=True, normal_data_included=0.99): """ Import data """ X, T0, _, _, _ = imp.import_tep_sets() num_vars, data_points = X.shape """ Train model """ # Create IncSFA object SlowFeature = IncSFA(input_variables=num_vars, num_features=num_features, num_components=num_whitened_signals, L=sample_weight_parameter, expansion_order=expansion_order, dynamic_copies=dynamic_copies, conv_tol=conv_tol) SlowFeature.delta = 3 SlowFeature.Md = cut_off SlowFeature.Me = num_features - cut_off # Create empty arrays to store outputs total_data_points = data_points * epochs Y = np.zeros((num_features, total_data_points)) stats = np.zeros((4, total_data_points)) stats_crit = np.zeros((4, total_data_points)) # Train model for j in range(epochs): print("Running epoch " + str(j + 1) + "/" + str(epochs)) for i in range(X.shape[1]): pos = j * X.shape[1] + i run = SlowFeature.add_data(X[:, i], use_svd_whitening=use_SVD) # Store data Y[:, pos] = run[0].flat stats[:, pos] = run[1] stats_crit[:, pos] = run[2] SlowFeature.converged = True """ Test model """ data_points = T0.shape[1] stats = np.zeros((4, data_points)) stats_crit = np.zeros((4, data_points)) ub = 1 lb = 0 all_significance = [0, 0, 0, 0] for j in range(4): while True: # Smaller significance includes more data significance = (ub + lb) / 2 print(f"Checking alpha = {significance}") test_obj = copy.deepcopy(SlowFeature) for i in range(data_points): output = test_obj.add_data(T0[:, i], alpha=significance) _, stats[:, i], stats_crit[:, i] = output normal_fraction = (np.sum(stats[j, :] < stats_crit[j, :]) / data_points) if normal_fraction < normal_data_included: ub = significance else: ub = 1 all_significance[j] = significance break return (all_significance)
from incsfa import IncSFA from rsfa import RSFA if __name__ == "__main__": # Define parameters m = 33 k = 1 d = 2 K = 99 J = 99 M_d = 55 alpha = 0.01 L = 2 # Import data set X, T0, T4, T5, T10 = imp.import_tep_sets() # Create models ModelSFA = SlowFeature = SFA(data=X, dynamic_copies=d, expansion_order=k) ModelRSFA = RSFA(input_variables=m, num_features=J, num_components=K, L=L, expansion_order=k, dynamic_copies=d) ModelIncSFA = IncSFA(input_variables=m, num_features=J, num_components=K, L=L, expansion_order=k, dynamic_copies=d)
def run_incsfa(dynamic_copies=2, expansion_order=1, cut_off=55, num_whitened_signals=99, num_features=99, sample_weight_parameter=2, conv_tol=0, epochs=1, plot_last_epoch=True, use_SVD=True): """ Incremental Slow Feature Analysis Runs the incremental slow feature analysis algorithm on the Tennessee Eastman process data set Parameters ---------- dynamic_copies: int The number of lagged copies per data point expansion_order: int The order of nonlinear expansion performed on the data cut_off: int The index to split the features into the slow and fast groups num_whitened_signals: int The number of principal components to calculate in the whitening step num_features: int The number of features to calculate sample_weight_parameter: float The sample weighting parameter used for setting the learning rate conv_tol: float The tolerance for convergance epochs: int The number of times to pass the training data to the model plot_last_epoch: boolean Only plot the last epoch of data for the features plot """ """ Import data """ X, T0, T4, T5, T10 = imp.import_tep_sets() num_vars, data_points = X.shape """ Create plotter object """ figure_text = (f"Lagged Copies= {dynamic_copies} | " f"Expansion Order= {expansion_order} | " f"$M_d$= {cut_off} | " f"Epochs: {epochs} | " f"K= {num_whitened_signals} | " f"J= {num_features} | " f"L= {sample_weight_parameter} | " f"Tolerance= {conv_tol}") plotter = SFAPlotter(show=False, save=True, figure_text=figure_text) """ Train model """ # Create IncSFA object SlowFeature = IncSFA(input_variables=num_vars, num_features=num_features, num_components=num_whitened_signals, L=sample_weight_parameter, expansion_order=expansion_order, dynamic_copies=dynamic_copies, conv_tol=conv_tol) SlowFeature.delta = 3 SlowFeature.Md = cut_off SlowFeature.Me = num_features - cut_off # Create empty arrays to store outputs total_data_points = data_points * epochs Y = np.zeros((num_features, total_data_points)) stats = np.zeros((4, total_data_points)) stats_crit = np.zeros((4, total_data_points)) # Train model for j in range(epochs): print("Running epoch " + str(j + 1) + "/" + str(epochs)) for i in range(X.shape[1]): pos = j * X.shape[1] + i run = SlowFeature.add_data(X[:, i], use_svd_whitening=use_SVD) # Store data Y[:, pos] = run[0].flat stats[:, pos] = run[1] stats_crit[:, pos] = run[2] SlowFeature.converged = True if plot_last_epoch: Y = Y[:, -data_points:] stats = stats[:, -data_points:] stats_crit = stats_crit[:, -data_points:] # Calculate speed indices for features eta = np.around( Y.shape[1] / (2 * np.pi) * np.sqrt(SlowFeature.features_speed), 2) # Plot features plotter.plot_features("IncSFA", Y, eta, num_features=5) # """ Test model """ test_data = [("IncSFA_IDV(0)", T0), ("IncSFA_IDV(4)", T4), ("IncSFA_IDV(5)", T5), ("IncSFA_IDV(10)", T10)] for name, test in test_data: print("Evaluating test: " + name) test_obj = copy.deepcopy(SlowFeature) data_points = test.shape[1] stats = np.zeros((4, data_points)) stats_crit = np.zeros((4, data_points)) for i in range(data_points): _, stats[:, i], stats_crit[:, i] = test_obj.add_data(test[:, i], alpha=SIG) # Plot stats plotter.plot_monitors(name, stats, stats_crit)