def __init__(self, feature_path, spectrograms_path, BC_datapath, Kfold_path, type, annotation="Is Eeg Usable For Clinical Purposes"): """" annotation="Quality Of Eeg" or "Is Eeg Usable For Clinical Purposes" """ self.get_data = preprossingPipeline(BC_datapath=BC_datapath) self.LDA = None #LDA model self.PCA = None #PCA model self.TSNE = None #TSNE model self.max_windows = 30 self.max_files = 10 self.le = preprocessing.LabelEncoder() with open(os.path.join(os.getcwd(), Kfold_path), "r") as read_file: Kfold = json.load(read_file) trainNames = Kfold[f"train_0"] testNames = Kfold[f"test_0"] self.type = type if type == "Feture_test": self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_vectors( trainNames, feature_path, annotation) self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_vectors( testNames, feature_path, annotation) self.le.fit(np.append(self.filenames_train, self.filenames_test)) self.X_train = scale_data(self.X_train) self.X_test = scale_data(self.X_test) if type == "Spectrograms_test": self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_vectors( trainNames, spectrograms_path, annotation) self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_vectors( testNames, spectrograms_path, annotation) self.le.fit(np.append(self.filenames_train, self.filenames_test)) self.X_train = scale_data(self.X_train) self.X_test = scale_data(self.X_test) if type == "Spectrograms_balance": self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_balance( 5, spectrograms_path, annotation) self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_balance( 26, spectrograms_path, annotation) self.le.fit(self.filenames_test) if type == "Feature_balance": self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_balance( 5, feature_path, annotation) self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_balance( 26, feature_path, annotation) self.le.fit(self.filenames_test)
def make_pca(windows, make_spectograms=False): if make_spectograms: path_pca = os.path.join(os.getcwd(), 'Villads', 'PCA_spectograms.sav') else: path_pca = os.path.join(os.getcwd(), 'Villads', 'PCA_feature_vectors_1.sav') pca = pickle.load(open(path_pca, 'rb')) windows = scale_data(windows) windows = pca.transform(windows) return windows
def plot_comand(feature_path, path_pca, BC_datapath, newplot=True): pca = pickle.load(open(path_pca, 'rb')) get_data = preprossingPipeline(BC_datapath=BC_datapath) feature_vectors, labels, filenames, window_id = get_data.make_label( quality=None, is_usable=None, max_files=10, path=feature_path) while True: print("What you want to plot, options pca or") command = input() if command == "pca": feature_vectors, labels, filenames, window_id = get_data.make_label( quality=None, is_usable=None, max_files=10, path=feature_path) scaled_feature_vectors = scale_data(feature_vectors) pca_vectors = pca.transform(scaled_feature_vectors) print("what components ex 0 1") components = input() components = components.split(" ") plot_pca_interactiv(pca_vectors, labels, window_id, components[0], components[1]) if command == "break": break if command == "spec" or command == "EEG": print("Insert index") idx = int(input()) if newplot == False: get_data.plot_window(window_id[idx][0], window_id[idx][1], type=command) else: data, time, chanels = get_data.plot_window(window_id[idx][0], window_id[idx][1], type=command, plot=True) data = np.array(data).T df = pd.DataFrame(data, columns=chanels, index=time) fig = make_subplots(rows=len(chanels), cols=1) for i, ch in enumerate(chanels): fig.add_trace(px.line(df, y=ch, x=df.index), row=(i + 1), col=1) fig = px.line(df, y=chanels[0], x=df.index) fig.show()
feature_vectors_9_10,labels_9_10,filenames_1,idx_1= C.make_label(max_files=15,quality=[9],is_usable=None,path = path, max_windows=60) feature_vectors_5,labels_5,filenames_2,idx_2= C.make_label(max_files=15,quality=[5],is_usable=None,path = path, max_windows=60) fv1, l1, f1, i1 = C.make_label(max_files=70,quality=None,is_usable='Yes',path = path, max_windows=2) fv2, l2, f2, i2 = C.make_label(max_files=70,quality=None,is_usable='No',path = path, max_windows=2) fv=np.vstack((feature_vectors_1,feature_vectors_5)) feature_vectors=np.vstack((fv,feature_vectors_9_10)) fv_new = np.vstack((fv1,fv2)) filenames=filenames+filenames_1+filenames_2 labels=labels_1+labels_9_10+labels_5 index = idx+idx_1+idx_2 l = l1+l2 scaled_feature_vector=scale_data(feature_vectors) scaled_new = scale_data(fv_new) pca = PCA() pca.fit(scaled_feature_vector) pca_fv=pca.transform(scaled_feature_vector) pca_fv1 = pca.transform(scaled_new) neigh = KNeighborsClassifier(n_neighbors=3) neigh.fit(pca_fv, labels) J = [] for i in range(126): J.append(np.argmax(neigh.predict_proba(pca_fv1[i:i+1]))) J2 = []
from Villads.PCA_TSNE_classes import scale_data from Visualization.PCA_TSNE_plot import plot_pca import numpy as np from sklearn.discriminant_analysis import LinearDiscriminantAnalysis path='/Users/villadsstokbro/Dokumenter/DTU/KID/3. semester/Fagprojekt/spectograms_all_ch/' C=preprossingPipeline(BC_datapath='/Users/villadsstokbro/Dokumenter/DTU/KID/3. semester/Fagprojekt/BrainCapture/dataEEG',mac=True) feature_vectors_1,labels_1,filenames,idx= C.make_label(max_files=5,quality=None,is_usable='No',path = path,max_windows=10) feature_vectors_9_10,labels_9_10,filenames_1,idx_1= C.make_label(max_files=5,quality=None,is_usable='Yes',path = path) feature_vectors=np.vstack((feature_vectors_1,feature_vectors_9_10)) filenames=filenames+filenames_1 labels=labels_1+labels_9_10 scaled_feature_vectors=scale_data(feature_vectors) pca=PCA() tsne=TSNE() pca.fit(scaled_feature_vectors) pca_vectors=pca.transform(scaled_feature_vectors) tsne_vectors=tsne.fit_transform(scaled_feature_vectors) labels_new=[a[0] for a in idx+idx_1] clf = LinearDiscriminantAnalysis() ld = clf.fit(scaled_feature_vectors,labels) plot_pca(pca_vectors,labels,np.unique(labels)) plot_pca(tsne_vectors,labels,np.unique(labels),model='TSNE') plot_pca(pca_vectors,labels_new,np.unique(labels_new)) plot_pca(tsne_vectors,labels_new,np.unique(labels_new),model='TSNE') plot_pca(ld.transform(scaled_feature_vectors),)
def fit_pca(fv): scaled_new_fv = scale_data(fv) pca = PCA() pca.fit(fv) pca_fv = pca.transform(fv) return pca_fv