MASK = nib.load(MASK_PATH).get_data() TRACT_MASKS = nib.load(TRACT_MASKS_PTH).get_data() TRACT_MASKS = TRACT_MASKS[:, :, :, 1:] # remove background class SAVE_PATH = join(DATA_DIR, SUBJ_ID, "pca_stats_unnorm.npz") ncs = list(range(1, 30)) stats = { "n_components": [], "scores": [] } for nc in ncs: print("n_components = {}".format(nc).center(100, "-")) features, _ = dsutils.make_pca_volume(DATA, MASK, n_components=nc, normalize=False) train_slices = [('sagittal', 72), ('coronal', 87), ('axial', 72)] train_masks = dsutils.create_data_masks(TRACT_MASKS, train_slices, LABELS) X_train, y_train, train_coords = dsutils.create_dataset_from_data_mask(features, train_masks, labels=LABELS, multi_label=True) X_test, y_test, test_coords = dsutils.create_dataset_from_data_mask(features, TRACT_MASKS, labels=LABELS, multi_label=True) clf = RandomForestClassifier(n_estimators=100, bootstrap=True, oob_score=True, random_state=0, n_jobs=-1, max_features='auto', class_weight='balanced',
# transpose features if orient == 'coronal': orient_features = orient_features.transpose(1, 0, 2, 3) if orient == 'axial': orient_features = orient_features.transpose(1, 2, 0, 3) TRACT_MASKS = nib.load(TRACT_MASKS_PTH).get_data() TRACT_MASKS = TRACT_MASKS[:, :, :, 1:] # remove background class FEATURES = orient_features # ---------------------------------------------Train Set---------------------------------------------- train_slices = [('sagittal', 72), ('coronal', 87), ('axial', 72)] train_masks = dsutils.create_data_masks(TRACT_MASKS, train_slices, LABELS, verbose=False) X_train, y_train, train_coords = dsutils.create_dataset_from_data_mask( FEATURES, train_masks, labels=LABELS, multi_label=True) if ADD_COORDS: X_train = np.concatenate((X_train, train_coords), axis=1) # --------------------------------------Random Forest Classifier-------------------------------------- clf = RandomForestClassifier(n_estimators=100, bootstrap=True, oob_score=True, random_state=0, n_jobs=-1, max_features='auto', class_weight='balanced', max_depth=None,
x = data['data'].to(device) feature = encoder(x) orient_feature = feature.detach().cpu().squeeze().permute(1, 2, 0) idx = int(data['file_name'][0][:-4][-3:]) features[idx] = orient_feature features = features.numpy() # transpose features features = features.transpose(1, 0, 2, 3) print("Features from epoch={} were created".format(epoch)) # -----------------------------------------Prepare train set------------------------------------------ print('Prepare train set'.center(100, '-')) train_slices = [('sagittal', 72), ('coronal', 87), ('axial', 72)] train_masks = dsutils.create_data_masks(ml_masks, train_slices, labels) X_train, y_train, train_coords = dsutils.create_dataset_from_data_mask(features, train_masks, multi_label=True) print("Trainset shape: ", X_train.shape) # ------------------------------------------Prepare test set------------------------------------------ print('Prepare test set'.center(100, '-')) print("Test set is the whole brain volume.") # test_masks = ml_masks test_slices = [('sagittal', 71), ('coronal', 86), ('axial', 71)] test_masks = dsutils.create_data_masks(ml_masks, test_slices, labels) X_test, y_test, test_coords = dsutils.create_dataset_from_data_mask(features, test_masks, multi_label=True) print("Testset shape: ", X_test.shape)
dmri = nib.load(dmri_path) print("dMRI shape: ", dmri.shape) dmri_data = dmri.get_fdata() # load masks masks_path = os.path.join(data_dir, subj_id, 'tract_masks') ml_masks = np.load(os.path.join(masks_path, 'multi_label_mask.npz'))['data'] ml_masks = ml_masks[:, :, :, 1:] # remove background class print("Mask shape: ", ml_masks.shape) labels = ['Other', 'CG', 'CST', 'FX', 'CC'] training_slices = [('sagittal', 72), ('coronal', 87), ('axial', 72)] # make training data train_masks = dsutils.create_data_masks(ml_masks, training_slices, labels) X_train, y_train, train_coords = dsutils.create_dataset_from_data_mask( dmri_data, train_masks, labels=labels, multi_label=True) # make test data test_masks = ml_masks X_test, y_test, test_coords = dsutils.create_dataset_from_data_mask( dmri_data, test_masks, labels=labels, multi_label=True) X_train = np.hstack((train_coords, X_train)) X_test = np.hstack((test_coords, X_test)) print("X_train: ", X_train.shape) print("X_test: ", X_test.shape) num_features = list(range(1, 289)) best_f1 = 0 best_n = None
print("Preparing test sets.".center(100, "-")) test_sets = {} results = {} orients_num_slices = {"axial": 145, "coronal": 174, "sagittal": 145} orient_ranges = { "axial": range(52, 93), "coronal": range(67, 108), "sagittal": range(52, 93) } for orient, num_slices in orients_num_slices.items(): for i in orient_ranges[orient]: mask_name = (orient, i) tmsk = dsutils.create_data_masks(TRACT_MASKS, [mask_name], LABELS, verbose=False) X_test, y_test, test_coords = dsutils.create_dataset_from_data_mask( FEATURES, tmsk, labels=LABELS, multi_label=True) if X_test.shape[0] == 0: continue if ADD_COORDS: X_test = np.concatenate((X_test, test_coords), axis=1) test_sets[mask_name] = {} test_sets[mask_name]["x"] = X_test test_sets[mask_name]["y"] = y_test test_sets[mask_name]["coords"] = test_coords results[mask_name] = 1