From 6193c53b7617abeb2bc0c7dc135a84eecdaa300c Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Sun, 10 Dec 2023 17:49:32 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- modulo_2/sesion_1/src/exercise-S1-3-01.py | 14 +++-- modulo_2/sesion_1/src/exercise-S1-3-02.py | 19 ++++--- .../sesion_1/src/utils/modules_pbashivan.py | 54 +++++++++---------- modulo_2/sesion_2/src/exercise-S1-3-01.py | 14 +++-- modulo_2/sesion_2/src/exercise-S1-3-02.py | 19 ++++--- modulo_2/sesion_2/src/exercise-S2-2-01.py | 21 ++++---- modulo_2/sesion_2/src/exercise-S2-2-02.py | 26 +++++---- modulo_2/sesion_2/src/models/model_modules.py | 4 +- .../sesion_2/src/utils/modules_pbashivan.py | 54 +++++++++---------- modulo_2/sesion_3/src/exercise-S1-3-01.py | 14 +++-- modulo_2/sesion_3/src/exercise-S1-3-02.py | 19 ++++--- modulo_2/sesion_3/src/exercise-S2-2-01.py | 21 ++++---- modulo_2/sesion_3/src/exercise-S2-2-02.py | 26 +++++---- modulo_2/sesion_3/src/exercise-S2-2-03.py | 17 +++--- modulo_2/sesion_3/src/models/model_modules.py | 4 +- .../sesion_3/src/utils/modules_pbashivan.py | 54 +++++++++---------- modulo_2/sesion_4/src/exercise-S1-3-01.py | 14 +++-- modulo_2/sesion_4/src/exercise-S1-3-02.py | 19 ++++--- modulo_2/sesion_4/src/exercise-S2-2-01.py | 21 ++++---- modulo_2/sesion_4/src/exercise-S2-2-02.py | 26 +++++---- modulo_2/sesion_4/src/exercise-S2-2-03.py | 17 +++--- modulo_2/sesion_4/src/exercise-S3-1-01.py | 23 ++++---- modulo_2/sesion_4/src/exercise-S3-1-02.py | 19 ++++--- modulo_2/sesion_4/src/models/model_modules.py | 4 +- .../sesion_4/src/utils/modules_pbashivan.py | 54 +++++++++---------- modulo_2/sesion_5/src/exercise-S1-3-01.py | 14 +++-- modulo_2/sesion_5/src/exercise-S1-3-02.py | 19 ++++--- modulo_2/sesion_5/src/exercise-S2-2-01.py | 21 ++++---- modulo_2/sesion_5/src/exercise-S2-2-02.py | 26 +++++---- modulo_2/sesion_5/src/exercise-S2-2-03.py | 17 +++--- modulo_2/sesion_5/src/exercise-S3-1-01.py | 23 ++++---- modulo_2/sesion_5/src/exercise-S3-1-02.py | 19 ++++--- modulo_2/sesion_5/src/exercise-S3-1-03.py | 25 +++++---- modulo_2/sesion_5/src/losses/mmd.py | 19 ++++--- modulo_2/sesion_5/src/losses/semantic.py | 14 +---- modulo_2/sesion_5/src/losses/vat.py | 7 +-- modulo_2/sesion_5/src/models/model_modules.py | 4 +- .../sesion_5/src/utils/modules_pbashivan.py | 54 +++++++++---------- modulo_2/sesion_6/src/exercise-S1-3-01.py | 14 +++-- modulo_2/sesion_6/src/exercise-S1-3-02.py | 19 ++++--- modulo_2/sesion_6/src/exercise-S2-2-01.py | 21 ++++---- modulo_2/sesion_6/src/exercise-S2-2-02.py | 26 +++++---- modulo_2/sesion_6/src/exercise-S2-2-03.py | 17 +++--- modulo_2/sesion_6/src/exercise-S3-1-01.py | 23 ++++---- modulo_2/sesion_6/src/exercise-S3-1-02.py | 19 ++++--- modulo_2/sesion_6/src/exercise-S3-1-03.py | 25 +++++---- modulo_2/sesion_6/src/exercise-S4-1-01.py | 23 ++++---- modulo_2/sesion_6/src/losses/mmd.py | 19 ++++--- modulo_2/sesion_6/src/losses/semantic.py | 14 +---- modulo_2/sesion_6/src/losses/vat.py | 7 +-- modulo_2/sesion_6/src/models/model_modules.py | 4 +- .../sesion_6/src/utils/modules_pbashivan.py | 54 +++++++++---------- 52 files changed, 528 insertions(+), 596 deletions(-) diff --git a/modulo_2/sesion_1/src/exercise-S1-3-01.py b/modulo_2/sesion_1/src/exercise-S1-3-01.py index 677714a..0dfe796 100644 --- a/modulo_2/sesion_1/src/exercise-S1-3-01.py +++ b/modulo_2/sesion_1/src/exercise-S1-3-01.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -93,8 +93,7 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = [] - for i, (train_index, test_index) in enumerate(skf.split(x, y)): - + for train_index, test_index in skf.split(x, y): x_train, y_train = x[train_index], y[train_index] x_test, y_test = x[test_index], y[test_index] @@ -139,7 +138,7 @@ def kfcv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -179,10 +178,9 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume + "/kfcv-results.csv" - f = open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/kfcv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_1/src/exercise-S1-3-02.py b/modulo_2/sesion_1/src/exercise-S1-3-02.py index 2063861..8cf7ac7 100644 --- a/modulo_2/sesion_1/src/exercise-S1-3-02.py +++ b/modulo_2/sesion_1/src/exercise-S1-3-02.py @@ -35,7 +35,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,7 +79,7 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) @@ -88,7 +88,7 @@ def losocv(X, Y, subjects, args): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Divide dataset into training, validation and testing sets @@ -130,7 +130,7 @@ def losocv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -164,20 +164,19 @@ def losocv(X, Y, subjects, args): print('Epoch: %d loss: %4f Acc: %.4f F1-score: %.4f AUC: %.4f' % (epoch+1, avg_loss, acc_test, f1_test, auc_test)) print('Saving model...') - torch.save(model.state_dict(), 'trained_model/source' + str(foldNum+1) + '.pt') + torch.save(model.state_dict(), f'trained_model/source{str(foldNum + 1)}.pt') print("\n") # add to list list_metrics_clsf.append([acc_test, f1_test, auc_test, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_1/src/utils/modules_pbashivan.py b/modulo_2/sesion_1/src/utils/modules_pbashivan.py index 10f5901..eefe663 100644 --- a/modulo_2/sesion_1/src/utils/modules_pbashivan.py +++ b/modulo_2/sesion_1/src/utils/modules_pbashivan.py @@ -31,16 +31,16 @@ def load_data(data_file): ------- data: array_like """ - print("Loading data from %s" % (data_file)) + print(f"Loading data from {data_file}") dataMat = scipy.io.loadmat(data_file, mat_dtype=True) all_data = np.array(dataMat['features']) - + data = np.array(all_data[:,:len(all_data[0])-1]) labels = np.array(all_data[:,len(all_data[0])-1]) - + print("Data loading complete. Shape is %r" % (dataMat['features'].shape,)) - + return data, labels.T - 1 # Sequential indices @@ -278,36 +278,34 @@ def get_subject_indices_kfcv(id_subjects): def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, generate_images=False, size_image=32, visualize=False): - + """ Module used to load dataset of bashivan et al. 2014. """ # load data pbashivan - data, labels = load_data(data_dir + "FeatureMat_timeWin.mat") + data, labels = load_data(f"{data_dir}FeatureMat_timeWin.mat") print("Original data:",data.shape, labels.shape) - + if generate_images: # NOTE: Only a 3D projection is proporcionated, then it is not avaliable other # records with positions. - + #Load locations in 3D - locs_orig = scipy.io.loadmat(data_dir+'Neuroscan_locs_orig.mat', mat_dtype=True) + locs_orig = scipy.io.loadmat( + f'{data_dir}Neuroscan_locs_orig.mat', mat_dtype=True + ) locs3D = locs_orig['A'] - - #Convert to 2D - locs2D =[] - for e in locs3D: - locs2D.append(azim_proj(e)) - + + locs2D = [azim_proj(e) for e in locs3D] #save in numpy array locs2D = np.array(locs2D) - + # visualize projection if visualize: print("No. channels:",locs3D.shape) - + # Plot in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -316,7 +314,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') - + # Plot in 2D fig = plt.figure() ax = fig.add_subplot(111) @@ -325,7 +323,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') plt.show() - + # NOTE: database is defined with 7 time windows # FFT power values extracted for three frequency bands (theta, alpha, beta). # Features are arranged in band and electrodes order (theta_1, theta_2..., @@ -347,18 +345,20 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera images = np.array(images) # transpose images = images.transpose((1, 2, 0, 3, 4)) - scipy.io.savemat(data_dir+'images.mat', mdict={'images': images}) - + scipy.io.savemat(f'{data_dir}images.mat', mdict={'images': images}) + else: #Load locations in 3D - files_mat = scipy.io.loadmat(data_dir+'images.mat', mat_dtype=True) + files_mat = scipy.io.loadmat(f'{data_dir}images.mat', mat_dtype=True) images = files_mat['images'] - + #Load info subjects associated with trials. List of patiens for 2670 trials - subjects_trials= scipy.io.loadmat(data_dir+'trials_subNums.mat', mat_dtype=True) + subjects_trials = scipy.io.loadmat( + f'{data_dir}trials_subNums.mat', mat_dtype=True + ) subjNumbers = np.squeeze(subjects_trials['subjectNum']) - - + + print("Shape images", images.shape) - + return images, labels, subjNumbers \ No newline at end of file diff --git a/modulo_2/sesion_2/src/exercise-S1-3-01.py b/modulo_2/sesion_2/src/exercise-S1-3-01.py index 677714a..0dfe796 100644 --- a/modulo_2/sesion_2/src/exercise-S1-3-01.py +++ b/modulo_2/sesion_2/src/exercise-S1-3-01.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -93,8 +93,7 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = [] - for i, (train_index, test_index) in enumerate(skf.split(x, y)): - + for train_index, test_index in skf.split(x, y): x_train, y_train = x[train_index], y[train_index] x_test, y_test = x[test_index], y[test_index] @@ -139,7 +138,7 @@ def kfcv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -179,10 +178,9 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume + "/kfcv-results.csv" - f = open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/kfcv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_2/src/exercise-S1-3-02.py b/modulo_2/sesion_2/src/exercise-S1-3-02.py index 2063861..8cf7ac7 100644 --- a/modulo_2/sesion_2/src/exercise-S1-3-02.py +++ b/modulo_2/sesion_2/src/exercise-S1-3-02.py @@ -35,7 +35,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,7 +79,7 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) @@ -88,7 +88,7 @@ def losocv(X, Y, subjects, args): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Divide dataset into training, validation and testing sets @@ -130,7 +130,7 @@ def losocv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -164,20 +164,19 @@ def losocv(X, Y, subjects, args): print('Epoch: %d loss: %4f Acc: %.4f F1-score: %.4f AUC: %.4f' % (epoch+1, avg_loss, acc_test, f1_test, auc_test)) print('Saving model...') - torch.save(model.state_dict(), 'trained_model/source' + str(foldNum+1) + '.pt') + torch.save(model.state_dict(), f'trained_model/source{str(foldNum + 1)}.pt') print("\n") # add to list list_metrics_clsf.append([acc_test, f1_test, auc_test, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_2/src/exercise-S2-2-01.py b/modulo_2/sesion_2/src/exercise-S2-2-01.py index 24398dd..a2d74eb 100644 --- a/modulo_2/sesion_2/src/exercise-S2-2-01.py +++ b/modulo_2/sesion_2/src/exercise-S2-2-01.py @@ -35,7 +35,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -80,16 +80,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -144,6 +144,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total loss] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() network_h.train() @@ -176,8 +178,6 @@ def losocv(X, Y, subjects, args): # [Classification Loss] classifier_loss = criterion(outputs_source, labels_source) - # [Total loss] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -209,15 +209,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_2/src/exercise-S2-2-02.py b/modulo_2/sesion_2/src/exercise-S2-2-02.py index 992b131..4784b42 100644 --- a/modulo_2/sesion_2/src/exercise-S2-2-02.py +++ b/modulo_2/sesion_2/src/exercise-S2-2-02.py @@ -39,7 +39,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -83,18 +83,17 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: - continue - + if foldNum >= 2:# and foldNum + 1 <= 11: + continue + # Get source and target datasets (x_src, y_src), (x_trg, y_trg), y_classes = split_losocv(X, Y, fold) @@ -148,6 +147,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [PRELIMINAR TOTAL LOSS] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() @@ -180,8 +181,6 @@ def losocv(X, Y, subjects, args): # [CMD Loss] transfer_loss = cmd_loss.forward(features_source, features_target) - # [PRELIMINAR TOTAL LOSS] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -215,15 +214,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_2/src/models/model_modules.py b/modulo_2/sesion_2/src/models/model_modules.py index ec36b50..77923c9 100644 --- a/modulo_2/sesion_2/src/models/model_modules.py +++ b/modulo_2/sesion_2/src/models/model_modules.py @@ -79,8 +79,6 @@ def __init__(self, input_size, n_classes=4): self.fc_out = nn.Linear(input_size, n_classes) def forward(self, x): - out = self.fc_out(x) - - return out + return self.fc_out(x) diff --git a/modulo_2/sesion_2/src/utils/modules_pbashivan.py b/modulo_2/sesion_2/src/utils/modules_pbashivan.py index 10f5901..eefe663 100644 --- a/modulo_2/sesion_2/src/utils/modules_pbashivan.py +++ b/modulo_2/sesion_2/src/utils/modules_pbashivan.py @@ -31,16 +31,16 @@ def load_data(data_file): ------- data: array_like """ - print("Loading data from %s" % (data_file)) + print(f"Loading data from {data_file}") dataMat = scipy.io.loadmat(data_file, mat_dtype=True) all_data = np.array(dataMat['features']) - + data = np.array(all_data[:,:len(all_data[0])-1]) labels = np.array(all_data[:,len(all_data[0])-1]) - + print("Data loading complete. Shape is %r" % (dataMat['features'].shape,)) - + return data, labels.T - 1 # Sequential indices @@ -278,36 +278,34 @@ def get_subject_indices_kfcv(id_subjects): def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, generate_images=False, size_image=32, visualize=False): - + """ Module used to load dataset of bashivan et al. 2014. """ # load data pbashivan - data, labels = load_data(data_dir + "FeatureMat_timeWin.mat") + data, labels = load_data(f"{data_dir}FeatureMat_timeWin.mat") print("Original data:",data.shape, labels.shape) - + if generate_images: # NOTE: Only a 3D projection is proporcionated, then it is not avaliable other # records with positions. - + #Load locations in 3D - locs_orig = scipy.io.loadmat(data_dir+'Neuroscan_locs_orig.mat', mat_dtype=True) + locs_orig = scipy.io.loadmat( + f'{data_dir}Neuroscan_locs_orig.mat', mat_dtype=True + ) locs3D = locs_orig['A'] - - #Convert to 2D - locs2D =[] - for e in locs3D: - locs2D.append(azim_proj(e)) - + + locs2D = [azim_proj(e) for e in locs3D] #save in numpy array locs2D = np.array(locs2D) - + # visualize projection if visualize: print("No. channels:",locs3D.shape) - + # Plot in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -316,7 +314,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') - + # Plot in 2D fig = plt.figure() ax = fig.add_subplot(111) @@ -325,7 +323,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') plt.show() - + # NOTE: database is defined with 7 time windows # FFT power values extracted for three frequency bands (theta, alpha, beta). # Features are arranged in band and electrodes order (theta_1, theta_2..., @@ -347,18 +345,20 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera images = np.array(images) # transpose images = images.transpose((1, 2, 0, 3, 4)) - scipy.io.savemat(data_dir+'images.mat', mdict={'images': images}) - + scipy.io.savemat(f'{data_dir}images.mat', mdict={'images': images}) + else: #Load locations in 3D - files_mat = scipy.io.loadmat(data_dir+'images.mat', mat_dtype=True) + files_mat = scipy.io.loadmat(f'{data_dir}images.mat', mat_dtype=True) images = files_mat['images'] - + #Load info subjects associated with trials. List of patiens for 2670 trials - subjects_trials= scipy.io.loadmat(data_dir+'trials_subNums.mat', mat_dtype=True) + subjects_trials = scipy.io.loadmat( + f'{data_dir}trials_subNums.mat', mat_dtype=True + ) subjNumbers = np.squeeze(subjects_trials['subjectNum']) - - + + print("Shape images", images.shape) - + return images, labels, subjNumbers \ No newline at end of file diff --git a/modulo_2/sesion_3/src/exercise-S1-3-01.py b/modulo_2/sesion_3/src/exercise-S1-3-01.py index 677714a..0dfe796 100644 --- a/modulo_2/sesion_3/src/exercise-S1-3-01.py +++ b/modulo_2/sesion_3/src/exercise-S1-3-01.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -93,8 +93,7 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = [] - for i, (train_index, test_index) in enumerate(skf.split(x, y)): - + for train_index, test_index in skf.split(x, y): x_train, y_train = x[train_index], y[train_index] x_test, y_test = x[test_index], y[test_index] @@ -139,7 +138,7 @@ def kfcv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -179,10 +178,9 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume + "/kfcv-results.csv" - f = open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/kfcv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_3/src/exercise-S1-3-02.py b/modulo_2/sesion_3/src/exercise-S1-3-02.py index 2063861..8cf7ac7 100644 --- a/modulo_2/sesion_3/src/exercise-S1-3-02.py +++ b/modulo_2/sesion_3/src/exercise-S1-3-02.py @@ -35,7 +35,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,7 +79,7 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) @@ -88,7 +88,7 @@ def losocv(X, Y, subjects, args): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Divide dataset into training, validation and testing sets @@ -130,7 +130,7 @@ def losocv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -164,20 +164,19 @@ def losocv(X, Y, subjects, args): print('Epoch: %d loss: %4f Acc: %.4f F1-score: %.4f AUC: %.4f' % (epoch+1, avg_loss, acc_test, f1_test, auc_test)) print('Saving model...') - torch.save(model.state_dict(), 'trained_model/source' + str(foldNum+1) + '.pt') + torch.save(model.state_dict(), f'trained_model/source{str(foldNum + 1)}.pt') print("\n") # add to list list_metrics_clsf.append([acc_test, f1_test, auc_test, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_3/src/exercise-S2-2-01.py b/modulo_2/sesion_3/src/exercise-S2-2-01.py index 24398dd..a2d74eb 100644 --- a/modulo_2/sesion_3/src/exercise-S2-2-01.py +++ b/modulo_2/sesion_3/src/exercise-S2-2-01.py @@ -35,7 +35,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -80,16 +80,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -144,6 +144,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total loss] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() network_h.train() @@ -176,8 +178,6 @@ def losocv(X, Y, subjects, args): # [Classification Loss] classifier_loss = criterion(outputs_source, labels_source) - # [Total loss] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -209,15 +209,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_3/src/exercise-S2-2-02.py b/modulo_2/sesion_3/src/exercise-S2-2-02.py index 992b131..4784b42 100644 --- a/modulo_2/sesion_3/src/exercise-S2-2-02.py +++ b/modulo_2/sesion_3/src/exercise-S2-2-02.py @@ -39,7 +39,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -83,18 +83,17 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: - continue - + if foldNum >= 2:# and foldNum + 1 <= 11: + continue + # Get source and target datasets (x_src, y_src), (x_trg, y_trg), y_classes = split_losocv(X, Y, fold) @@ -148,6 +147,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [PRELIMINAR TOTAL LOSS] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() @@ -180,8 +181,6 @@ def losocv(X, Y, subjects, args): # [CMD Loss] transfer_loss = cmd_loss.forward(features_source, features_target) - # [PRELIMINAR TOTAL LOSS] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -215,15 +214,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_3/src/exercise-S2-2-03.py b/modulo_2/sesion_3/src/exercise-S2-2-03.py index 451e964..92b8f84 100644 --- a/modulo_2/sesion_3/src/exercise-S2-2-03.py +++ b/modulo_2/sesion_3/src/exercise-S2-2-03.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -217,15 +217,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_3/src/models/model_modules.py b/modulo_2/sesion_3/src/models/model_modules.py index ec36b50..77923c9 100644 --- a/modulo_2/sesion_3/src/models/model_modules.py +++ b/modulo_2/sesion_3/src/models/model_modules.py @@ -79,8 +79,6 @@ def __init__(self, input_size, n_classes=4): self.fc_out = nn.Linear(input_size, n_classes) def forward(self, x): - out = self.fc_out(x) - - return out + return self.fc_out(x) diff --git a/modulo_2/sesion_3/src/utils/modules_pbashivan.py b/modulo_2/sesion_3/src/utils/modules_pbashivan.py index 10f5901..eefe663 100644 --- a/modulo_2/sesion_3/src/utils/modules_pbashivan.py +++ b/modulo_2/sesion_3/src/utils/modules_pbashivan.py @@ -31,16 +31,16 @@ def load_data(data_file): ------- data: array_like """ - print("Loading data from %s" % (data_file)) + print(f"Loading data from {data_file}") dataMat = scipy.io.loadmat(data_file, mat_dtype=True) all_data = np.array(dataMat['features']) - + data = np.array(all_data[:,:len(all_data[0])-1]) labels = np.array(all_data[:,len(all_data[0])-1]) - + print("Data loading complete. Shape is %r" % (dataMat['features'].shape,)) - + return data, labels.T - 1 # Sequential indices @@ -278,36 +278,34 @@ def get_subject_indices_kfcv(id_subjects): def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, generate_images=False, size_image=32, visualize=False): - + """ Module used to load dataset of bashivan et al. 2014. """ # load data pbashivan - data, labels = load_data(data_dir + "FeatureMat_timeWin.mat") + data, labels = load_data(f"{data_dir}FeatureMat_timeWin.mat") print("Original data:",data.shape, labels.shape) - + if generate_images: # NOTE: Only a 3D projection is proporcionated, then it is not avaliable other # records with positions. - + #Load locations in 3D - locs_orig = scipy.io.loadmat(data_dir+'Neuroscan_locs_orig.mat', mat_dtype=True) + locs_orig = scipy.io.loadmat( + f'{data_dir}Neuroscan_locs_orig.mat', mat_dtype=True + ) locs3D = locs_orig['A'] - - #Convert to 2D - locs2D =[] - for e in locs3D: - locs2D.append(azim_proj(e)) - + + locs2D = [azim_proj(e) for e in locs3D] #save in numpy array locs2D = np.array(locs2D) - + # visualize projection if visualize: print("No. channels:",locs3D.shape) - + # Plot in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -316,7 +314,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') - + # Plot in 2D fig = plt.figure() ax = fig.add_subplot(111) @@ -325,7 +323,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') plt.show() - + # NOTE: database is defined with 7 time windows # FFT power values extracted for three frequency bands (theta, alpha, beta). # Features are arranged in band and electrodes order (theta_1, theta_2..., @@ -347,18 +345,20 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera images = np.array(images) # transpose images = images.transpose((1, 2, 0, 3, 4)) - scipy.io.savemat(data_dir+'images.mat', mdict={'images': images}) - + scipy.io.savemat(f'{data_dir}images.mat', mdict={'images': images}) + else: #Load locations in 3D - files_mat = scipy.io.loadmat(data_dir+'images.mat', mat_dtype=True) + files_mat = scipy.io.loadmat(f'{data_dir}images.mat', mat_dtype=True) images = files_mat['images'] - + #Load info subjects associated with trials. List of patiens for 2670 trials - subjects_trials= scipy.io.loadmat(data_dir+'trials_subNums.mat', mat_dtype=True) + subjects_trials = scipy.io.loadmat( + f'{data_dir}trials_subNums.mat', mat_dtype=True + ) subjNumbers = np.squeeze(subjects_trials['subjectNum']) - - + + print("Shape images", images.shape) - + return images, labels, subjNumbers \ No newline at end of file diff --git a/modulo_2/sesion_4/src/exercise-S1-3-01.py b/modulo_2/sesion_4/src/exercise-S1-3-01.py index 677714a..0dfe796 100644 --- a/modulo_2/sesion_4/src/exercise-S1-3-01.py +++ b/modulo_2/sesion_4/src/exercise-S1-3-01.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -93,8 +93,7 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = [] - for i, (train_index, test_index) in enumerate(skf.split(x, y)): - + for train_index, test_index in skf.split(x, y): x_train, y_train = x[train_index], y[train_index] x_test, y_test = x[test_index], y[test_index] @@ -139,7 +138,7 @@ def kfcv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -179,10 +178,9 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume + "/kfcv-results.csv" - f = open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/kfcv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/exercise-S1-3-02.py b/modulo_2/sesion_4/src/exercise-S1-3-02.py index 2063861..8cf7ac7 100644 --- a/modulo_2/sesion_4/src/exercise-S1-3-02.py +++ b/modulo_2/sesion_4/src/exercise-S1-3-02.py @@ -35,7 +35,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,7 +79,7 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) @@ -88,7 +88,7 @@ def losocv(X, Y, subjects, args): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Divide dataset into training, validation and testing sets @@ -130,7 +130,7 @@ def losocv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -164,20 +164,19 @@ def losocv(X, Y, subjects, args): print('Epoch: %d loss: %4f Acc: %.4f F1-score: %.4f AUC: %.4f' % (epoch+1, avg_loss, acc_test, f1_test, auc_test)) print('Saving model...') - torch.save(model.state_dict(), 'trained_model/source' + str(foldNum+1) + '.pt') + torch.save(model.state_dict(), f'trained_model/source{str(foldNum + 1)}.pt') print("\n") # add to list list_metrics_clsf.append([acc_test, f1_test, auc_test, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/exercise-S2-2-01.py b/modulo_2/sesion_4/src/exercise-S2-2-01.py index 24398dd..a2d74eb 100644 --- a/modulo_2/sesion_4/src/exercise-S2-2-01.py +++ b/modulo_2/sesion_4/src/exercise-S2-2-01.py @@ -35,7 +35,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -80,16 +80,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -144,6 +144,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total loss] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() network_h.train() @@ -176,8 +178,6 @@ def losocv(X, Y, subjects, args): # [Classification Loss] classifier_loss = criterion(outputs_source, labels_source) - # [Total loss] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -209,15 +209,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/exercise-S2-2-02.py b/modulo_2/sesion_4/src/exercise-S2-2-02.py index 992b131..4784b42 100644 --- a/modulo_2/sesion_4/src/exercise-S2-2-02.py +++ b/modulo_2/sesion_4/src/exercise-S2-2-02.py @@ -39,7 +39,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -83,18 +83,17 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: - continue - + if foldNum >= 2:# and foldNum + 1 <= 11: + continue + # Get source and target datasets (x_src, y_src), (x_trg, y_trg), y_classes = split_losocv(X, Y, fold) @@ -148,6 +147,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [PRELIMINAR TOTAL LOSS] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() @@ -180,8 +181,6 @@ def losocv(X, Y, subjects, args): # [CMD Loss] transfer_loss = cmd_loss.forward(features_source, features_target) - # [PRELIMINAR TOTAL LOSS] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -215,15 +214,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/exercise-S2-2-03.py b/modulo_2/sesion_4/src/exercise-S2-2-03.py index 451e964..92b8f84 100644 --- a/modulo_2/sesion_4/src/exercise-S2-2-03.py +++ b/modulo_2/sesion_4/src/exercise-S2-2-03.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -217,15 +217,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/exercise-S3-1-01.py b/modulo_2/sesion_4/src/exercise-S3-1-01.py index b36bf08..ff2b6cb 100644 --- a/modulo_2/sesion_4/src/exercise-S3-1-01.py +++ b/modulo_2/sesion_4/src/exercise-S3-1-01.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1 and 2 are executed - if foldNum+1 >= 2:# and foldNum+1 <= 11: + if foldNum >= 1:# and foldNum+1 <= 11: continue # Get source and target datasets @@ -149,6 +149,9 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total Loss] + lambda_dis = 1.0 + lambda_cond = 0.1 for iter_num in range(0, args.max_iterations + 1): # print(iter_num) network_f.train() @@ -188,9 +191,6 @@ def losocv(X, Y, subjects, args): # [SEMANTIC TRANSFER LOSS] loss_sm, Cs_memory, Ct_memory = Semantic(features_source, features_target, labels_source, pseu_labels_target, Cs_memory, Ct_memory, decay=0.9) - # [Total Loss] - lambda_dis = 1.0 - lambda_cond = 0.1 total_loss = classifier_loss + lambda_dis * transfer_loss + lambda_cond * loss_sm # Reset gradients @@ -225,15 +225,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/exercise-S3-1-02.py b/modulo_2/sesion_4/src/exercise-S3-1-02.py index b099922..ba5be2b 100644 --- a/modulo_2/sesion_4/src/exercise-S3-1-02.py +++ b/modulo_2/sesion_4/src/exercise-S3-1-02.py @@ -36,7 +36,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -81,15 +81,15 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - if foldNum+1 >= 3:# and foldNum+1 <= 11: + if foldNum >= 2:# and foldNum+1 <= 11: continue # Divide dataset into training, validation and testing sets @@ -122,7 +122,7 @@ def losocv(X, Y, subjects, args): epoch = 1 eval_iter = x_src.shape[0] // args.batch_size - + # [Build Model] hidden_size = 128 network_f = Extractor().cuda() @@ -211,15 +211,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_4/src/models/model_modules.py b/modulo_2/sesion_4/src/models/model_modules.py index ec36b50..77923c9 100644 --- a/modulo_2/sesion_4/src/models/model_modules.py +++ b/modulo_2/sesion_4/src/models/model_modules.py @@ -79,8 +79,6 @@ def __init__(self, input_size, n_classes=4): self.fc_out = nn.Linear(input_size, n_classes) def forward(self, x): - out = self.fc_out(x) - - return out + return self.fc_out(x) diff --git a/modulo_2/sesion_4/src/utils/modules_pbashivan.py b/modulo_2/sesion_4/src/utils/modules_pbashivan.py index 10f5901..eefe663 100644 --- a/modulo_2/sesion_4/src/utils/modules_pbashivan.py +++ b/modulo_2/sesion_4/src/utils/modules_pbashivan.py @@ -31,16 +31,16 @@ def load_data(data_file): ------- data: array_like """ - print("Loading data from %s" % (data_file)) + print(f"Loading data from {data_file}") dataMat = scipy.io.loadmat(data_file, mat_dtype=True) all_data = np.array(dataMat['features']) - + data = np.array(all_data[:,:len(all_data[0])-1]) labels = np.array(all_data[:,len(all_data[0])-1]) - + print("Data loading complete. Shape is %r" % (dataMat['features'].shape,)) - + return data, labels.T - 1 # Sequential indices @@ -278,36 +278,34 @@ def get_subject_indices_kfcv(id_subjects): def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, generate_images=False, size_image=32, visualize=False): - + """ Module used to load dataset of bashivan et al. 2014. """ # load data pbashivan - data, labels = load_data(data_dir + "FeatureMat_timeWin.mat") + data, labels = load_data(f"{data_dir}FeatureMat_timeWin.mat") print("Original data:",data.shape, labels.shape) - + if generate_images: # NOTE: Only a 3D projection is proporcionated, then it is not avaliable other # records with positions. - + #Load locations in 3D - locs_orig = scipy.io.loadmat(data_dir+'Neuroscan_locs_orig.mat', mat_dtype=True) + locs_orig = scipy.io.loadmat( + f'{data_dir}Neuroscan_locs_orig.mat', mat_dtype=True + ) locs3D = locs_orig['A'] - - #Convert to 2D - locs2D =[] - for e in locs3D: - locs2D.append(azim_proj(e)) - + + locs2D = [azim_proj(e) for e in locs3D] #save in numpy array locs2D = np.array(locs2D) - + # visualize projection if visualize: print("No. channels:",locs3D.shape) - + # Plot in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -316,7 +314,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') - + # Plot in 2D fig = plt.figure() ax = fig.add_subplot(111) @@ -325,7 +323,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') plt.show() - + # NOTE: database is defined with 7 time windows # FFT power values extracted for three frequency bands (theta, alpha, beta). # Features are arranged in band and electrodes order (theta_1, theta_2..., @@ -347,18 +345,20 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera images = np.array(images) # transpose images = images.transpose((1, 2, 0, 3, 4)) - scipy.io.savemat(data_dir+'images.mat', mdict={'images': images}) - + scipy.io.savemat(f'{data_dir}images.mat', mdict={'images': images}) + else: #Load locations in 3D - files_mat = scipy.io.loadmat(data_dir+'images.mat', mat_dtype=True) + files_mat = scipy.io.loadmat(f'{data_dir}images.mat', mat_dtype=True) images = files_mat['images'] - + #Load info subjects associated with trials. List of patiens for 2670 trials - subjects_trials= scipy.io.loadmat(data_dir+'trials_subNums.mat', mat_dtype=True) + subjects_trials = scipy.io.loadmat( + f'{data_dir}trials_subNums.mat', mat_dtype=True + ) subjNumbers = np.squeeze(subjects_trials['subjectNum']) - - + + print("Shape images", images.shape) - + return images, labels, subjNumbers \ No newline at end of file diff --git a/modulo_2/sesion_5/src/exercise-S1-3-01.py b/modulo_2/sesion_5/src/exercise-S1-3-01.py index 677714a..0dfe796 100644 --- a/modulo_2/sesion_5/src/exercise-S1-3-01.py +++ b/modulo_2/sesion_5/src/exercise-S1-3-01.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -93,8 +93,7 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = [] - for i, (train_index, test_index) in enumerate(skf.split(x, y)): - + for train_index, test_index in skf.split(x, y): x_train, y_train = x[train_index], y[train_index] x_test, y_test = x[test_index], y[test_index] @@ -139,7 +138,7 @@ def kfcv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -179,10 +178,9 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume + "/kfcv-results.csv" - f = open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/kfcv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S1-3-02.py b/modulo_2/sesion_5/src/exercise-S1-3-02.py index 2063861..8cf7ac7 100644 --- a/modulo_2/sesion_5/src/exercise-S1-3-02.py +++ b/modulo_2/sesion_5/src/exercise-S1-3-02.py @@ -35,7 +35,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,7 +79,7 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) @@ -88,7 +88,7 @@ def losocv(X, Y, subjects, args): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Divide dataset into training, validation and testing sets @@ -130,7 +130,7 @@ def losocv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -164,20 +164,19 @@ def losocv(X, Y, subjects, args): print('Epoch: %d loss: %4f Acc: %.4f F1-score: %.4f AUC: %.4f' % (epoch+1, avg_loss, acc_test, f1_test, auc_test)) print('Saving model...') - torch.save(model.state_dict(), 'trained_model/source' + str(foldNum+1) + '.pt') + torch.save(model.state_dict(), f'trained_model/source{str(foldNum + 1)}.pt') print("\n") # add to list list_metrics_clsf.append([acc_test, f1_test, auc_test, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S2-2-01.py b/modulo_2/sesion_5/src/exercise-S2-2-01.py index 24398dd..a2d74eb 100644 --- a/modulo_2/sesion_5/src/exercise-S2-2-01.py +++ b/modulo_2/sesion_5/src/exercise-S2-2-01.py @@ -35,7 +35,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -80,16 +80,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -144,6 +144,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total loss] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() network_h.train() @@ -176,8 +178,6 @@ def losocv(X, Y, subjects, args): # [Classification Loss] classifier_loss = criterion(outputs_source, labels_source) - # [Total loss] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -209,15 +209,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S2-2-02.py b/modulo_2/sesion_5/src/exercise-S2-2-02.py index 992b131..4784b42 100644 --- a/modulo_2/sesion_5/src/exercise-S2-2-02.py +++ b/modulo_2/sesion_5/src/exercise-S2-2-02.py @@ -39,7 +39,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -83,18 +83,17 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: - continue - + if foldNum >= 2:# and foldNum + 1 <= 11: + continue + # Get source and target datasets (x_src, y_src), (x_trg, y_trg), y_classes = split_losocv(X, Y, fold) @@ -148,6 +147,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [PRELIMINAR TOTAL LOSS] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() @@ -180,8 +181,6 @@ def losocv(X, Y, subjects, args): # [CMD Loss] transfer_loss = cmd_loss.forward(features_source, features_target) - # [PRELIMINAR TOTAL LOSS] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -215,15 +214,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S2-2-03.py b/modulo_2/sesion_5/src/exercise-S2-2-03.py index 451e964..92b8f84 100644 --- a/modulo_2/sesion_5/src/exercise-S2-2-03.py +++ b/modulo_2/sesion_5/src/exercise-S2-2-03.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -217,15 +217,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S3-1-01.py b/modulo_2/sesion_5/src/exercise-S3-1-01.py index b36bf08..ff2b6cb 100644 --- a/modulo_2/sesion_5/src/exercise-S3-1-01.py +++ b/modulo_2/sesion_5/src/exercise-S3-1-01.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1 and 2 are executed - if foldNum+1 >= 2:# and foldNum+1 <= 11: + if foldNum >= 1:# and foldNum+1 <= 11: continue # Get source and target datasets @@ -149,6 +149,9 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total Loss] + lambda_dis = 1.0 + lambda_cond = 0.1 for iter_num in range(0, args.max_iterations + 1): # print(iter_num) network_f.train() @@ -188,9 +191,6 @@ def losocv(X, Y, subjects, args): # [SEMANTIC TRANSFER LOSS] loss_sm, Cs_memory, Ct_memory = Semantic(features_source, features_target, labels_source, pseu_labels_target, Cs_memory, Ct_memory, decay=0.9) - # [Total Loss] - lambda_dis = 1.0 - lambda_cond = 0.1 total_loss = classifier_loss + lambda_dis * transfer_loss + lambda_cond * loss_sm # Reset gradients @@ -225,15 +225,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S3-1-02.py b/modulo_2/sesion_5/src/exercise-S3-1-02.py index b099922..ba5be2b 100644 --- a/modulo_2/sesion_5/src/exercise-S3-1-02.py +++ b/modulo_2/sesion_5/src/exercise-S3-1-02.py @@ -36,7 +36,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -81,15 +81,15 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - if foldNum+1 >= 3:# and foldNum+1 <= 11: + if foldNum >= 2:# and foldNum+1 <= 11: continue # Divide dataset into training, validation and testing sets @@ -122,7 +122,7 @@ def losocv(X, Y, subjects, args): epoch = 1 eval_iter = x_src.shape[0] // args.batch_size - + # [Build Model] hidden_size = 128 network_f = Extractor().cuda() @@ -211,15 +211,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/exercise-S3-1-03.py b/modulo_2/sesion_5/src/exercise-S3-1-03.py index a7be607..cb6f149 100644 --- a/modulo_2/sesion_5/src/exercise-S3-1-03.py +++ b/modulo_2/sesion_5/src/exercise-S3-1-03.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,16 +79,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1 and 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Get source and target datasets @@ -123,13 +123,17 @@ def losocv(X, Y, subjects, args): # [Build Model] source_model = RecResNet(n_classes=4).cuda() - source_model.load_state_dict(torch.load("./trained_model/source" + str(foldNum+1) + ".pt")) + source_model.load_state_dict( + torch.load(f"./trained_model/source{str(foldNum + 1)}.pt") + ) source_model.eval() set_requires_grad(source_model, requires_grad=False) # [Target model] target_model = RecResNet(n_classes=4).cuda() - target_model.load_state_dict(torch.load("./trained_model/source" + str(foldNum+1) + ".pt")) + target_model.load_state_dict( + torch.load(f"./trained_model/source{str(foldNum + 1)}.pt") + ) # Create adversarial discriminator discriminator = nn.Sequential( @@ -235,15 +239,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_5/src/losses/mmd.py b/modulo_2/sesion_5/src/losses/mmd.py index 4df5331..d3fc2ee 100644 --- a/modulo_2/sesion_5/src/losses/mmd.py +++ b/modulo_2/sesion_5/src/losses/mmd.py @@ -47,14 +47,17 @@ def _mmd2(K_XX, K_XY, K_YY, const_diagonal=False, biased=False): Kt_YY_sum = Kt_YY_sums.sum() # e^T * \tilde{K}_YY * e K_XY_sum = K_XY_sums_0.sum() # e^T * K_{XY} * e - if biased: - mmd2 = ((Kt_XX_sum + sum_diag_X) / (m * m) + return ( + ( + (Kt_XX_sum + sum_diag_X) / (m * m) + (Kt_YY_sum + sum_diag_Y) / (m * m) - - 2.0 * K_XY_sum / (m * m)) - else: - mmd2 = (Kt_XX_sum / (m * (m - 1)) + - 2.0 * K_XY_sum / (m * m) + ) + if biased + else ( + Kt_XX_sum / (m * (m - 1)) + Kt_YY_sum / (m * (m - 1)) - - 2.0 * K_XY_sum / (m * m)) - - return mmd2 + - 2.0 * K_XY_sum / (m * m) + ) + ) diff --git a/modulo_2/sesion_5/src/losses/semantic.py b/modulo_2/sesion_5/src/losses/semantic.py index db76625..0ff8e47 100644 --- a/modulo_2/sesion_5/src/losses/semantic.py +++ b/modulo_2/sesion_5/src/losses/semantic.py @@ -21,19 +21,9 @@ def Semantic(Xs, Xt, Ys, Yt, Cs_memory, Ct_memory, decay=0.3): Xt_k = Xt[Yt==k] # target domain # validate if there is zero elements in Source domain - if len(Xs_k)==0: - Cs_k = 0.0 - else: - # get mean from elements of the source domain - Cs_k = torch.mean(Xs_k,dim=0) - + Cs_k = 0.0 if len(Xs_k)==0 else torch.mean(Xs_k,dim=0) # validate if there is zero elements in Target domain - if len(Xt_k) == 0: - Ct_k = 0.0 - else: - # get mean from elements of the target domain - Ct_k = torch.mean(Xt_k,dim=0) - + Ct_k = 0.0 if len(Xt_k) == 0 else torch.mean(Xt_k,dim=0) # Moving average (MA) Cs[k, :] = (1 - decay) * Cs_memory[k, :] + decay * Cs_k Ct[k, :] = (1 - decay) * Ct_memory[k, :] + decay * Ct_k diff --git a/modulo_2/sesion_5/src/losses/vat.py b/modulo_2/sesion_5/src/losses/vat.py index fb9e9b6..420afcf 100644 --- a/modulo_2/sesion_5/src/losses/vat.py +++ b/modulo_2/sesion_5/src/losses/vat.py @@ -22,8 +22,7 @@ def __init__(self, network_f, network_h, radius=3.5, n_power=1): self.epsilon = 3.5 def forward(self, x, logit): - vat_loss = self.virtual_adversarial_loss(x, logit) - return vat_loss + return self.virtual_adversarial_loss(x, logit) def virtual_adversarial_loss(self, x, logit): # generate perturbation based on a radius @@ -33,9 +32,7 @@ def virtual_adversarial_loss(self, x, logit): logit_p = logit.detach() # for backpropagating the gradient # get predictions for perturbated samples logit_m = self.network_h(self.network_f(x + r_vadv)) - # apply Kullback Leibler divergence - loss = self.kl_divergence(logit_p, logit_m) - return loss + return self.kl_divergence(logit_p, logit_m) def generate_virtual_adversarial_perturbation(self, x, logit): # generate a gaussian perturbation diff --git a/modulo_2/sesion_5/src/models/model_modules.py b/modulo_2/sesion_5/src/models/model_modules.py index ec36b50..77923c9 100644 --- a/modulo_2/sesion_5/src/models/model_modules.py +++ b/modulo_2/sesion_5/src/models/model_modules.py @@ -79,8 +79,6 @@ def __init__(self, input_size, n_classes=4): self.fc_out = nn.Linear(input_size, n_classes) def forward(self, x): - out = self.fc_out(x) - - return out + return self.fc_out(x) diff --git a/modulo_2/sesion_5/src/utils/modules_pbashivan.py b/modulo_2/sesion_5/src/utils/modules_pbashivan.py index 10f5901..eefe663 100644 --- a/modulo_2/sesion_5/src/utils/modules_pbashivan.py +++ b/modulo_2/sesion_5/src/utils/modules_pbashivan.py @@ -31,16 +31,16 @@ def load_data(data_file): ------- data: array_like """ - print("Loading data from %s" % (data_file)) + print(f"Loading data from {data_file}") dataMat = scipy.io.loadmat(data_file, mat_dtype=True) all_data = np.array(dataMat['features']) - + data = np.array(all_data[:,:len(all_data[0])-1]) labels = np.array(all_data[:,len(all_data[0])-1]) - + print("Data loading complete. Shape is %r" % (dataMat['features'].shape,)) - + return data, labels.T - 1 # Sequential indices @@ -278,36 +278,34 @@ def get_subject_indices_kfcv(id_subjects): def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, generate_images=False, size_image=32, visualize=False): - + """ Module used to load dataset of bashivan et al. 2014. """ # load data pbashivan - data, labels = load_data(data_dir + "FeatureMat_timeWin.mat") + data, labels = load_data(f"{data_dir}FeatureMat_timeWin.mat") print("Original data:",data.shape, labels.shape) - + if generate_images: # NOTE: Only a 3D projection is proporcionated, then it is not avaliable other # records with positions. - + #Load locations in 3D - locs_orig = scipy.io.loadmat(data_dir+'Neuroscan_locs_orig.mat', mat_dtype=True) + locs_orig = scipy.io.loadmat( + f'{data_dir}Neuroscan_locs_orig.mat', mat_dtype=True + ) locs3D = locs_orig['A'] - - #Convert to 2D - locs2D =[] - for e in locs3D: - locs2D.append(azim_proj(e)) - + + locs2D = [azim_proj(e) for e in locs3D] #save in numpy array locs2D = np.array(locs2D) - + # visualize projection if visualize: print("No. channels:",locs3D.shape) - + # Plot in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -316,7 +314,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') - + # Plot in 2D fig = plt.figure() ax = fig.add_subplot(111) @@ -325,7 +323,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') plt.show() - + # NOTE: database is defined with 7 time windows # FFT power values extracted for three frequency bands (theta, alpha, beta). # Features are arranged in band and electrodes order (theta_1, theta_2..., @@ -347,18 +345,20 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera images = np.array(images) # transpose images = images.transpose((1, 2, 0, 3, 4)) - scipy.io.savemat(data_dir+'images.mat', mdict={'images': images}) - + scipy.io.savemat(f'{data_dir}images.mat', mdict={'images': images}) + else: #Load locations in 3D - files_mat = scipy.io.loadmat(data_dir+'images.mat', mat_dtype=True) + files_mat = scipy.io.loadmat(f'{data_dir}images.mat', mat_dtype=True) images = files_mat['images'] - + #Load info subjects associated with trials. List of patiens for 2670 trials - subjects_trials= scipy.io.loadmat(data_dir+'trials_subNums.mat', mat_dtype=True) + subjects_trials = scipy.io.loadmat( + f'{data_dir}trials_subNums.mat', mat_dtype=True + ) subjNumbers = np.squeeze(subjects_trials['subjectNum']) - - + + print("Shape images", images.shape) - + return images, labels, subjNumbers \ No newline at end of file diff --git a/modulo_2/sesion_6/src/exercise-S1-3-01.py b/modulo_2/sesion_6/src/exercise-S1-3-01.py index 677714a..0dfe796 100644 --- a/modulo_2/sesion_6/src/exercise-S1-3-01.py +++ b/modulo_2/sesion_6/src/exercise-S1-3-01.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -93,8 +93,7 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = [] - for i, (train_index, test_index) in enumerate(skf.split(x, y)): - + for train_index, test_index in skf.split(x, y): x_train, y_train = x[train_index], y[train_index] x_test, y_test = x[test_index], y[test_index] @@ -139,7 +138,7 @@ def kfcv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -179,10 +178,9 @@ def kfcv(X, Y, subjects, args): list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume + "/kfcv-results.csv" - f = open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/kfcv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S1-3-02.py b/modulo_2/sesion_6/src/exercise-S1-3-02.py index 2063861..8cf7ac7 100644 --- a/modulo_2/sesion_6/src/exercise-S1-3-02.py +++ b/modulo_2/sesion_6/src/exercise-S1-3-02.py @@ -35,7 +35,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,7 +79,7 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) @@ -88,7 +88,7 @@ def losocv(X, Y, subjects, args): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Divide dataset into training, validation and testing sets @@ -130,7 +130,7 @@ def losocv(X, Y, subjects, args): iter_train = iter(source_loader) list_loss = [] - for c in range(len(source_loader)): + for _ in range(len(source_loader)): # get batch samples, labels = iter_train.next() samples = samples.float().cuda() @@ -164,20 +164,19 @@ def losocv(X, Y, subjects, args): print('Epoch: %d loss: %4f Acc: %.4f F1-score: %.4f AUC: %.4f' % (epoch+1, avg_loss, acc_test, f1_test, auc_test)) print('Saving model...') - torch.save(model.state_dict(), 'trained_model/source' + str(foldNum+1) + '.pt') + torch.save(model.state_dict(), f'trained_model/source{str(foldNum + 1)}.pt') print("\n") # add to list list_metrics_clsf.append([acc_test, f1_test, auc_test, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S2-2-01.py b/modulo_2/sesion_6/src/exercise-S2-2-01.py index 24398dd..a2d74eb 100644 --- a/modulo_2/sesion_6/src/exercise-S2-2-01.py +++ b/modulo_2/sesion_6/src/exercise-S2-2-01.py @@ -35,7 +35,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -80,16 +80,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -144,6 +144,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total loss] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() network_h.train() @@ -176,8 +178,6 @@ def losocv(X, Y, subjects, args): # [Classification Loss] classifier_loss = criterion(outputs_source, labels_source) - # [Total loss] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -209,15 +209,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S2-2-02.py b/modulo_2/sesion_6/src/exercise-S2-2-02.py index 992b131..4784b42 100644 --- a/modulo_2/sesion_6/src/exercise-S2-2-02.py +++ b/modulo_2/sesion_6/src/exercise-S2-2-02.py @@ -39,7 +39,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -83,18 +83,17 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: - continue - + if foldNum >= 2:# and foldNum + 1 <= 11: + continue + # Get source and target datasets (x_src, y_src), (x_trg, y_trg), y_classes = split_losocv(X, Y, fold) @@ -148,6 +147,8 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [PRELIMINAR TOTAL LOSS] + lambda_dis = 1.0 for iter_num in range(0, args.max_iterations + 1): network_f.train() @@ -180,8 +181,6 @@ def losocv(X, Y, subjects, args): # [CMD Loss] transfer_loss = cmd_loss.forward(features_source, features_target) - # [PRELIMINAR TOTAL LOSS] - lambda_dis = 1.0 total_loss = classifier_loss + lambda_dis * transfer_loss # Reset gradients @@ -215,15 +214,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S2-2-03.py b/modulo_2/sesion_6/src/exercise-S2-2-03.py index 451e964..92b8f84 100644 --- a/modulo_2/sesion_6/src/exercise-S2-2-03.py +++ b/modulo_2/sesion_6/src/exercise-S2-2-03.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1, 2 are executed - if foldNum + 1 >= 3:# and foldNum + 1 <= 11: + if foldNum >= 2:# and foldNum + 1 <= 11: continue # Get source and target datasets @@ -217,15 +217,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S3-1-01.py b/modulo_2/sesion_6/src/exercise-S3-1-01.py index b36bf08..ff2b6cb 100644 --- a/modulo_2/sesion_6/src/exercise-S3-1-01.py +++ b/modulo_2/sesion_6/src/exercise-S3-1-01.py @@ -37,7 +37,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -82,16 +82,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1 and 2 are executed - if foldNum+1 >= 2:# and foldNum+1 <= 11: + if foldNum >= 1:# and foldNum+1 <= 11: continue # Get source and target datasets @@ -149,6 +149,9 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total Loss] + lambda_dis = 1.0 + lambda_cond = 0.1 for iter_num in range(0, args.max_iterations + 1): # print(iter_num) network_f.train() @@ -188,9 +191,6 @@ def losocv(X, Y, subjects, args): # [SEMANTIC TRANSFER LOSS] loss_sm, Cs_memory, Ct_memory = Semantic(features_source, features_target, labels_source, pseu_labels_target, Cs_memory, Ct_memory, decay=0.9) - # [Total Loss] - lambda_dis = 1.0 - lambda_cond = 0.1 total_loss = classifier_loss + lambda_dis * transfer_loss + lambda_cond * loss_sm # Reset gradients @@ -225,15 +225,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S3-1-02.py b/modulo_2/sesion_6/src/exercise-S3-1-02.py index b099922..ba5be2b 100644 --- a/modulo_2/sesion_6/src/exercise-S3-1-02.py +++ b/modulo_2/sesion_6/src/exercise-S3-1-02.py @@ -36,7 +36,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -81,15 +81,15 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) - if foldNum+1 >= 3:# and foldNum+1 <= 11: + if foldNum >= 2:# and foldNum+1 <= 11: continue # Divide dataset into training, validation and testing sets @@ -122,7 +122,7 @@ def losocv(X, Y, subjects, args): epoch = 1 eval_iter = x_src.shape[0] // args.batch_size - + # [Build Model] hidden_size = 128 network_f = Extractor().cuda() @@ -211,15 +211,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S3-1-03.py b/modulo_2/sesion_6/src/exercise-S3-1-03.py index a7be607..cb6f149 100644 --- a/modulo_2/sesion_6/src/exercise-S3-1-03.py +++ b/modulo_2/sesion_6/src/exercise-S3-1-03.py @@ -34,7 +34,7 @@ def test(model, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -79,16 +79,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1 and 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Get source and target datasets @@ -123,13 +123,17 @@ def losocv(X, Y, subjects, args): # [Build Model] source_model = RecResNet(n_classes=4).cuda() - source_model.load_state_dict(torch.load("./trained_model/source" + str(foldNum+1) + ".pt")) + source_model.load_state_dict( + torch.load(f"./trained_model/source{str(foldNum + 1)}.pt") + ) source_model.eval() set_requires_grad(source_model, requires_grad=False) # [Target model] target_model = RecResNet(n_classes=4).cuda() - target_model.load_state_dict(torch.load("./trained_model/source" + str(foldNum+1) + ".pt")) + target_model.load_state_dict( + torch.load(f"./trained_model/source{str(foldNum + 1)}.pt") + ) # Create adversarial discriminator discriminator = nn.Sequential( @@ -235,15 +239,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/exercise-S4-1-01.py b/modulo_2/sesion_6/src/exercise-S4-1-01.py index 715951d..2ff0dca 100644 --- a/modulo_2/sesion_6/src/exercise-S4-1-01.py +++ b/modulo_2/sesion_6/src/exercise-S4-1-01.py @@ -38,7 +38,7 @@ def test(network_f, network_h, test_loader): start_test = True with torch.no_grad(): - for batch_idx, data in enumerate(test_loader): + for data in test_loader: # get batch data samples = data[0].float().cuda() labels = data[1].long().cuda() @@ -83,16 +83,16 @@ def losocv(X, Y, subjects, args): # variable used to save accuracy results list_metrics_clsf = [] - + # Extract pairs between indexes and subjects fold_pairs = get_subject_indices(subjects) - + # Iterate over fold_pairs for foldNum, fold in enumerate(fold_pairs): print('Beginning fold {0} out of {1}'.format(foldNum+1, len(fold_pairs))) # Only Subjects 1 and 2 are executed - if foldNum + 1 >= 3: + if foldNum >= 2: continue # Get source and target datasets @@ -151,6 +151,9 @@ def losocv(X, Y, subjects, args): # Almacenar pérdida list_loss = [] + # [Total loss] + lambda_c = 0.1 + lambda_v = 0.1 for iter_num in range(0, args.max_iterations + 1): # print(iter_num) network_f.train() @@ -197,9 +200,6 @@ def losocv(X, Y, subjects, args): # [virtural adversarial training loss] loss_trg_vat = vat_loss(inputs_source, outputs_target) - # [Total loss] - lambda_c = 0.1 - lambda_v = 0.1 total_loss = classifier_loss + transfer_loss + lambda_c * ce_loss + lambda_v * loss_trg_vat # Reset gradients @@ -234,15 +234,14 @@ def losocv(X, Y, subjects, args): print("\n") # add to list list_metrics_clsf.append([acc, f1, auc, foldNum+1]) - + # To np array list_metrics_clsf = np.array(list_metrics_clsf) # Save Classification Metrics - save_file = args.dir_resume+"/losocv-results.csv" - f=open(save_file, 'ab') - np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') - f.close() + save_file = f"{args.dir_resume}/losocv-results.csv" + with open(save_file, 'ab') as f: + np.savetxt(f, list_metrics_clsf, delimiter=",", fmt='%0.4f') def main(args): diff --git a/modulo_2/sesion_6/src/losses/mmd.py b/modulo_2/sesion_6/src/losses/mmd.py index 4df5331..d3fc2ee 100644 --- a/modulo_2/sesion_6/src/losses/mmd.py +++ b/modulo_2/sesion_6/src/losses/mmd.py @@ -47,14 +47,17 @@ def _mmd2(K_XX, K_XY, K_YY, const_diagonal=False, biased=False): Kt_YY_sum = Kt_YY_sums.sum() # e^T * \tilde{K}_YY * e K_XY_sum = K_XY_sums_0.sum() # e^T * K_{XY} * e - if biased: - mmd2 = ((Kt_XX_sum + sum_diag_X) / (m * m) + return ( + ( + (Kt_XX_sum + sum_diag_X) / (m * m) + (Kt_YY_sum + sum_diag_Y) / (m * m) - - 2.0 * K_XY_sum / (m * m)) - else: - mmd2 = (Kt_XX_sum / (m * (m - 1)) + - 2.0 * K_XY_sum / (m * m) + ) + if biased + else ( + Kt_XX_sum / (m * (m - 1)) + Kt_YY_sum / (m * (m - 1)) - - 2.0 * K_XY_sum / (m * m)) - - return mmd2 + - 2.0 * K_XY_sum / (m * m) + ) + ) diff --git a/modulo_2/sesion_6/src/losses/semantic.py b/modulo_2/sesion_6/src/losses/semantic.py index db76625..0ff8e47 100644 --- a/modulo_2/sesion_6/src/losses/semantic.py +++ b/modulo_2/sesion_6/src/losses/semantic.py @@ -21,19 +21,9 @@ def Semantic(Xs, Xt, Ys, Yt, Cs_memory, Ct_memory, decay=0.3): Xt_k = Xt[Yt==k] # target domain # validate if there is zero elements in Source domain - if len(Xs_k)==0: - Cs_k = 0.0 - else: - # get mean from elements of the source domain - Cs_k = torch.mean(Xs_k,dim=0) - + Cs_k = 0.0 if len(Xs_k)==0 else torch.mean(Xs_k,dim=0) # validate if there is zero elements in Target domain - if len(Xt_k) == 0: - Ct_k = 0.0 - else: - # get mean from elements of the target domain - Ct_k = torch.mean(Xt_k,dim=0) - + Ct_k = 0.0 if len(Xt_k) == 0 else torch.mean(Xt_k,dim=0) # Moving average (MA) Cs[k, :] = (1 - decay) * Cs_memory[k, :] + decay * Cs_k Ct[k, :] = (1 - decay) * Ct_memory[k, :] + decay * Ct_k diff --git a/modulo_2/sesion_6/src/losses/vat.py b/modulo_2/sesion_6/src/losses/vat.py index fb9e9b6..420afcf 100644 --- a/modulo_2/sesion_6/src/losses/vat.py +++ b/modulo_2/sesion_6/src/losses/vat.py @@ -22,8 +22,7 @@ def __init__(self, network_f, network_h, radius=3.5, n_power=1): self.epsilon = 3.5 def forward(self, x, logit): - vat_loss = self.virtual_adversarial_loss(x, logit) - return vat_loss + return self.virtual_adversarial_loss(x, logit) def virtual_adversarial_loss(self, x, logit): # generate perturbation based on a radius @@ -33,9 +32,7 @@ def virtual_adversarial_loss(self, x, logit): logit_p = logit.detach() # for backpropagating the gradient # get predictions for perturbated samples logit_m = self.network_h(self.network_f(x + r_vadv)) - # apply Kullback Leibler divergence - loss = self.kl_divergence(logit_p, logit_m) - return loss + return self.kl_divergence(logit_p, logit_m) def generate_virtual_adversarial_perturbation(self, x, logit): # generate a gaussian perturbation diff --git a/modulo_2/sesion_6/src/models/model_modules.py b/modulo_2/sesion_6/src/models/model_modules.py index ec36b50..77923c9 100644 --- a/modulo_2/sesion_6/src/models/model_modules.py +++ b/modulo_2/sesion_6/src/models/model_modules.py @@ -79,8 +79,6 @@ def __init__(self, input_size, n_classes=4): self.fc_out = nn.Linear(input_size, n_classes) def forward(self, x): - out = self.fc_out(x) - - return out + return self.fc_out(x) diff --git a/modulo_2/sesion_6/src/utils/modules_pbashivan.py b/modulo_2/sesion_6/src/utils/modules_pbashivan.py index 10f5901..eefe663 100644 --- a/modulo_2/sesion_6/src/utils/modules_pbashivan.py +++ b/modulo_2/sesion_6/src/utils/modules_pbashivan.py @@ -31,16 +31,16 @@ def load_data(data_file): ------- data: array_like """ - print("Loading data from %s" % (data_file)) + print(f"Loading data from {data_file}") dataMat = scipy.io.loadmat(data_file, mat_dtype=True) all_data = np.array(dataMat['features']) - + data = np.array(all_data[:,:len(all_data[0])-1]) labels = np.array(all_data[:,len(all_data[0])-1]) - + print("Data loading complete. Shape is %r" % (dataMat['features'].shape,)) - + return data, labels.T - 1 # Sequential indices @@ -278,36 +278,34 @@ def get_subject_indices_kfcv(id_subjects): def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, generate_images=False, size_image=32, visualize=False): - + """ Module used to load dataset of bashivan et al. 2014. """ # load data pbashivan - data, labels = load_data(data_dir + "FeatureMat_timeWin.mat") + data, labels = load_data(f"{data_dir}FeatureMat_timeWin.mat") print("Original data:",data.shape, labels.shape) - + if generate_images: # NOTE: Only a 3D projection is proporcionated, then it is not avaliable other # records with positions. - + #Load locations in 3D - locs_orig = scipy.io.loadmat(data_dir+'Neuroscan_locs_orig.mat', mat_dtype=True) + locs_orig = scipy.io.loadmat( + f'{data_dir}Neuroscan_locs_orig.mat', mat_dtype=True + ) locs3D = locs_orig['A'] - - #Convert to 2D - locs2D =[] - for e in locs3D: - locs2D.append(azim_proj(e)) - + + locs2D = [azim_proj(e) for e in locs3D] #save in numpy array locs2D = np.array(locs2D) - + # visualize projection if visualize: print("No. channels:",locs3D.shape) - + # Plot in 3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') @@ -316,7 +314,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') - + # Plot in 2D fig = plt.figure() ax = fig.add_subplot(111) @@ -325,7 +323,7 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera ax.set_xlabel('X Label') ax.set_ylabel('Y Label') plt.show() - + # NOTE: database is defined with 7 time windows # FFT power values extracted for three frequency bands (theta, alpha, beta). # Features are arranged in band and electrodes order (theta_1, theta_2..., @@ -347,18 +345,20 @@ def load_bashivan_data(data_dir, n_channels = 64, n_windows=7, n_bands=3, genera images = np.array(images) # transpose images = images.transpose((1, 2, 0, 3, 4)) - scipy.io.savemat(data_dir+'images.mat', mdict={'images': images}) - + scipy.io.savemat(f'{data_dir}images.mat', mdict={'images': images}) + else: #Load locations in 3D - files_mat = scipy.io.loadmat(data_dir+'images.mat', mat_dtype=True) + files_mat = scipy.io.loadmat(f'{data_dir}images.mat', mat_dtype=True) images = files_mat['images'] - + #Load info subjects associated with trials. List of patiens for 2670 trials - subjects_trials= scipy.io.loadmat(data_dir+'trials_subNums.mat', mat_dtype=True) + subjects_trials = scipy.io.loadmat( + f'{data_dir}trials_subNums.mat', mat_dtype=True + ) subjNumbers = np.squeeze(subjects_trials['subjectNum']) - - + + print("Shape images", images.shape) - + return images, labels, subjNumbers \ No newline at end of file