def run_ica_experiment(_run, root, dataset_dict, n_components, ica_method, fn_name): dataset_data_dict = {} for fold in ("train", "validation", "test"): data = load_subjects_data(root, subjects=dataset_dict[fold], mode="both") dataset_data_dict[fold] = data datasets = dataset_data_dict root = Path(root) gdfs = [datasets["train"][subject]["gdf"] for subject in datasets["train"]] joined_eeg, labels = join_gdfs_to_numpy(gdfs) ica_transform = get_ica_transformers()[ica_method] ica_transform.fit(joined_eeg) results = {} for dataset_name in datasets: dataset_fold = datasets[dataset_name] if not dataset_name in results: results[dataset_name] = {} for subject_id in dataset_fold: gdf_data = dataset_fold[subject_id]["gdf"] data = gdf_data.get_data().T data_arr = ica_transform.transform(data) start = time.time() score = apply_pairwise_parallel(data_arr, SCORING_FN_DICT[fn_name]) duration = time.time() - start results[dataset_name][subject_id] = { "score": score, "duration": duration } with open("./results.json", "w") as jfile: json.dump(results, jfile, indent=4) _run.add_artifact("results.json", content_type="json")
def run_ica_experiment(_run, method_idx): train_epochs = BCI_IV_Comp_Dataset.load_dataset(train_filepaths, as_epochs=True, concatenate=True, drop_bad=True, return_metadata=False, tmin=-1., tmax=3.) preprocess_epochs(train_epochs, inplace=True) test_epochs_list, test_metadata_list = BCI_IV_Comp_Dataset.load_dataset( test_filepaths, as_epochs=True, concatenate=False, drop_bad=True, return_metadata=True, tmin=-1., tmax=3.) all_methods = get_all_methods() methods = all_methods if method_idx is None else [all_methods[method_idx]] name = "" if method_idx is None else "_{}".format(all_methods[method_idx]) print("Using methods", methods) print("Loaded test files:", len(test_epochs_list)) results = dict() for method in methods: print("Running for method", method) results[method] = list() clf = make_pipeline( AverageFrequencyBand(fmin=0.1, fmax=30, picks=PSD_PICKS), StandardScaler(), SVC(C=4)) start = time.time() ICA = get_ica_instance(method, n_components=ICA_N_COMPONENTS) ICA.fit(train_epochs) print("\tICA fitted!") duration = time.time() - start transformed_train_epochs = ICA.get_sources(train_epochs) X_train, Y_train = transformed_train_epochs, transformed_train_epochs.events[:, 2] clf.fit(X_train, Y_train) for i, (epochs, mdata) in enumerate(zip(test_epochs_list, test_metadata_list)): print("\t", i, mdata["id"]) epochs = preprocess_epochs(epochs, inplace=False) transformed_test_epochs = ICA.get_sources(epochs) scores = dict() signal = np.hstack(transformed_test_epochs.get_data()) for fn_name in SCORING_FN_DICT: score = apply_pairwise_parallel(signal, SCORING_FN_DICT[fn_name]) scores[fn_name] = score X_test, Y_test = transformed_test_epochs, transformed_test_epochs.events[:, 2] pred = clf.predict(X_test) bas = balanced_accuracy_score(Y_test, pred) results[method].append({ "id": mdata["id"], "score": scores, "bas": bas, "duration": duration }) results_filepath = f"./results{name}.json" with open(results_filepath, "w") as json_file: json.dump(results, json_file, indent=4) _run.add_artifact(results_filepath, content_type="json")
def run_ica_experiment(_run, method_idx): # filepaths = Path(r"C:\Users\paull\Documents\GIT\BCI_MsC\notebooks\BCI_Comp_IV_2a\BCICIV_2a_gdf/").glob("*T.gdf") dataset, metadata = BCI_IV_Comp_Dataset.load_dataset(filepaths, as_epochs=True, concatenate=False, drop_bad=True, return_metadata=True, tmin=-1., tmax=3.) all_methods = get_all_methods() methods = all_methods if method_idx is None else [all_methods[method_idx]] name = "" if method_idx is None else "_{}".format(all_methods[method_idx]) print("Using methods", methods) results = dict() for method in methods: print("Running for method", method) clf = make_pipeline( CSP(n_components=CSP_N_COMPONENTS), Vectorizer(), MinMaxScaler(), LogisticRegression(penalty='l2', multi_class='auto')) results[method] = list() for i, (epochs, mdata) in enumerate(zip(dataset, metadata)): print("\t", i, mdata["id"]) ICA = get_ica_instance(method, n_components=ICA_N_COMPONENTS) start = time.time() epochs = epochs.copy().load_data().filter(l_freq=None, h_freq=40).resample(90.) transformed_epochs = ICA.fit(epochs).get_sources(epochs) duration = time.time() - start scores = dict() signal = np.hstack(transformed_epochs.get_data()) for fn_name in SCORING_FN_DICT: score = apply_pairwise_parallel(signal, SCORING_FN_DICT[fn_name]) scores[fn_name] = score X, Y = transformed_epochs.get_data(), transformed_epochs.events[:, 2] del epochs, transformed_epochs try: clf.fit(X, Y) except Exception: print("\t\tFailed during fit") results[method].append({ "id": mdata["id"], "score": None, "bas": None, "duration": duration }) continue pred = clf.predict(X) bas = balanced_accuracy_score(Y, pred) results[method].append({ "id": mdata["id"], "score": scores, "bas": bas, "duration": duration }) results_filepath = f"./results{name}.json" with open(results_filepath, "w") as json_file: json.dump(results, json_file, indent=4) _run.add_artifact(results_filepath, content_type="json")
def run_ica_experiment(_run, method_idx): # filepaths = Path(r"C:\Users\paull\Documents\GIT\BCI_MsC\notebooks\BCI_Comp_IV_2a\BCICIV_2a_gdf/").glob("*T.gdf") dataset, metadata = BCI_IV_Comp_Dataset.load_dataset(filepaths, as_epochs=True, concatenate=False, drop_bad=True, return_metadata=True, tmin=-1., tmax=3.) all_methods = get_all_methods() methods = all_methods if method_idx is None else [all_methods[method_idx]] name = "" if method_idx is None else "_{}".format(all_methods[method_idx]) print("Using methods", methods) results = dict() for method in methods: print("Running for method", method) clf = make_pipeline(PSD(fmin=8, fmax=30, picks=PSD_PICKS), Averager(), StandardScaler(), SVC(C=4)) results[method] = list() for i, (epochs, mdata) in enumerate(zip(dataset, metadata)): print("\t", i, mdata["id"]) ICA = get_ica_instance(method, n_components=ICA_N_COMPONENTS, random_state=SEED, max_iter=5000) start = time.time() epochs = preprocess_epochs(epochs) n_epochs = len(epochs) epochs_idx = list(range(n_epochs)) train_epochs_idx, test_epochs_idx = train_test_split( epochs_idx, test_size=TEST_PERCENTAGE, random_state=SEED) train_epochs = epochs.copy().drop(test_epochs_idx) test_epochs = epochs.copy().drop(train_epochs_idx) ICA.fit(train_epochs) transformed_train_epochs = ICA.get_sources(train_epochs) transformed_test_epochs = ICA.get_sources(test_epochs) duration = time.time() - start scores = dict() signal = np.hstack(transformed_test_epochs.get_data()) for fn_name in SCORING_FN_DICT: score = apply_pairwise_parallel(signal, SCORING_FN_DICT[fn_name]) scores[fn_name] = score X, Y = transformed_train_epochs, transformed_train_epochs.events[:, 2] X_test, Y_test = transformed_test_epochs, transformed_test_epochs.events[:, 2] try: clf.fit(X, Y) except Exception as e: print("\t\tFailed during fit:", str(e)) results[method].append({ "id": mdata["id"], "score": None, "bas": None, "duration": duration }) continue pred = clf.predict(X_test) bas = balanced_accuracy_score(Y_test, pred) results[method].append({ "id": mdata["id"], "score": scores, "bas": bas, "duration": duration }) del epochs, transformed_train_epochs, transformed_test_epochs results_filepath = f"./results{name}.json" with open(results_filepath, "w") as json_file: json.dump(results, json_file, indent=4) _run.add_artifact(results_filepath, content_type="json")
def run_ica_experiment(_run, method_idx): train_epochs = BCI_IV_Comp_Dataset.load_dataset( train_filepaths, as_epochs=True, concatenate=True, drop_bad=True, return_metadata=False, tmin=-1., tmax=3. ) train_epochs.load_data().filter(l_freq=None, h_freq=40) test_epochs_list, test_metadata_list = BCI_IV_Comp_Dataset.load_dataset( test_filepaths, as_epochs=True, concatenate=False, drop_bad=True, return_metadata=True, tmin=-1., tmax=3. ) all_methods = get_all_methods() methods = all_methods if method_idx is None else [all_methods[method_idx]] name = "" if method_idx is None else "_{}".format(all_methods[method_idx]) print("Using methods", methods) print("Loaded test files:", len(test_epochs_list)) results = dict() for method in methods: print("Running for method", method) results[method] = list() clf = make_pipeline( CSP(n_components=CSP_N_COMPONENTS), Vectorizer(), MinMaxScaler(), LogisticRegression( penalty='l2', multi_class='auto' ) ) start = time.time() ICA = get_ica_instance(method, n_components=ICA_N_COMPONENTS) ICA.fit(train_epochs) duration = time.time() - start transformed_train_epochs = ICA.get_sources(train_epochs) for i, (epochs, mdata) in enumerate(zip(test_epochs_list, test_metadata_list)): epochs = epochs.copy().load_data() transformed_test_epochs = ICA.get_sources(epochs) scores = dict() signal = np.hstack(transformed_test_epochs.get_data()) for fn_name in SCORING_FN_DICT: score = apply_pairwise_parallel(signal, SCORING_FN_DICT[fn_name]) scores[fn_name] = score X_train, Y_train = transformed_train_epochs.get_data(), transformed_train_epochs.events[:, 2] X_test, Y_test = transformed_test_epochs.get_data(), transformed_test_epochs.events[:, 2] try: clf.fit(X_train, Y_train) except Exception: print("\t\tFailed during fit") results[method].append( { "id": mdata["id"], "score": None, "bas": None, "duration": duration } ) continue pred = clf.predict(X_test) bas = balanced_accuracy_score(Y_test, pred) results[method].append( { "id": mdata["id"], "score": scores, "bas": bas, "duration": duration } ) results_filepath = f"./results{name}.json" with open(results_filepath, "w") as json_file: json.dump(results, json_file, indent=4) _run.add_artifact(results_filepath, content_type="json")