def compute_X_y_train(tract_name, moving_tractogram_fname, example_fname): """Compute X_train and y_train. """ moving_tractogram = nib.streamlines.load(moving_tractogram_fname) moving_tractogram = moving_tractogram.streamlines print("Compute kdt and prototypes of %s" %moving_tractogram_fname) kdt, prototypes = compute_kdtree_and_dr_tractogram(moving_tractogram, num_prototypes=num_prototypes, distance_func=distance_func, nb_points=nb_points) tract = nib.streamlines.load(example_fname) tract = tract.streamlines print("Computing the superset of %s" %example_fname) superset_idx = compute_superset(tract, kdt, prototypes, k=2000, distance_func=distance_func, nb_points=nb_points) superset = moving_tractogram[superset_idx] exID = ntpath.basename(moving_tractogram_fname)[4:10] print("Computing X_train.") X_train = compute_feature_matrix(superset, tract_name, distance_func=distance_func, nb_points=nb_points) print("Computing y_train.") y_train = np.zeros(len(superset)) tract_idx = streamlines_idx(tract, kdt, prototypes, distance_func=distance_func, nb_points=nb_points) correspondent_idx = np.array([np.where(superset_idx==idx) for idx in tract_idx]) y_train[correspondent_idx] = 1 return X_train, y_train
def compute_X_context_hist(superset, k=100, r=40, n_bins=40): """Add features of the k-Nearest-Neighbors. """ kdt, prototypes = compute_kdtree_and_dr_tractogram( superset, num_prototypes=40, distance_func=distance_func, nb_points=nb_points) X_context_hist = np.zeros((len(superset), n_bins)) for i in range(len(superset)): streamline = superset[i:i + 1] #D, I = NN(streamline, kdt, prototypes, k=k, distance_func=distance_func, nb_points=nb_points) I = NN_radius(streamline, kdt, prototypes, r=r, distance_func=distance_func, nb_points=nb_points) I = I[1:] #remove the index corresdponding to itself nn_str = superset[I] #Compute the exact distance if distance_func == bundles_distances_mdf: streamline = set_number_of_points(streamline, nb_points=nb_points) nn_str = set_number_of_points(nn_str, nb_points=nb_points) D = distance_func(streamline, nn_str) hist, bin_edges = np.histogram(D, bins=0.5 * np.arange(n_bins + 1), range=(0, 0.5 * n_bins)) X_context_hist[i, :] = hist if i == 0: print(hist) return X_context_hist
def nn_single_example(moving_tractogram, static_tractogram, example): """Code for NN from a single example. """ np.random.seed(0) with open('config.json') as f: data = json.load(f) step_size = data["step_size"] distance_func = bundles_distances_mam subjID = ntpath.basename(static_tractogram)[0:6] exID = ntpath.basename(example)[0:6] example_bundle = nib.streamlines.load(example) example_bundle = example_bundle.streamlines example_bundle_res = resample_tractogram(example_bundle, step_size) print( "Retrieving the affine slr transformation for example %s and target %s." % (exID, subjID)) affine = np.load('affine_m%s_s%s.npy' % (exID, subjID)) print("Applying the affine to the example bundle.") example_bundle_aligned = np.array( [apply_affine(affine, s) for s in example_bundle_res]) print( "Compute the dissimilarity representation of the target tractogram and build the kd-tree." ) static_tractogram = nib.streamlines.load(static_tractogram) static_tractogram = static_tractogram.streamlines static_tractogram_res = resample_tractogram(static_tractogram, step_size) static_tractogram = static_tractogram_res if isfile('prototypes.npy') & isfile('kdt'): print("Retrieving past results for kdt and prototypes.") kdt_filename = 'kdt' kdt = pickle.load(open(kdt_filename)) prototypes = np.load('prototypes.npy') else: kdt, prototypes = compute_kdtree_and_dr_tractogram(static_tractogram) #Saving files kdt_filename = 'kdt' pickle.dump(kdt, open(kdt_filename, 'w'), protocol=pickle.HIGHEST_PROTOCOL) np.save('prototypes', prototypes) print( "Compute the dissimilarity of the aligned example bundle with the prototypes of target tractogram." ) example_bundle_aligned = np.array(example_bundle_aligned, dtype=np.object) dm_example_bundle_aligned = distance_func(example_bundle_aligned, prototypes) print("Segmentation as Nearest Neighbour (NN).") estimated_bundle_idx, min_cost_values = NN(kdt, 1, dm_example_bundle_aligned) estimated_bundle = static_tractogram[estimated_bundle_idx] return estimated_bundle_idx, min_cost_values, len(example_bundle)
def classifyber(moving_tractograms_dir, static_tractogram_fname, ex_dir_tract): """Code for classification from multiple examples. """ tract_name = ntpath.basename(ex_dir_tract) moving_tractograms = os.listdir(moving_tractograms_dir) moving_tractograms.sort() examples = os.listdir(ex_dir_tract) examples.sort() nt = len(moving_tractograms) ne = len(examples) assert(nt == ne) X_train = np.array([]) y_train = np.array([]) print("Computing training set using %i examples." %ne) for i in range(nt): moving_tractogram_fname = '%s/%s' %(moving_tractograms_dir, moving_tractograms[i]) example_fname = '%s/%s' %(ex_dir_tract, examples[i]) X_tmp, y_tmp = compute_X_y_train(tract_name, moving_tractogram_fname, example_fname) X_train = np.vstack([X_train, X_tmp]) if X_train.size else X_tmp y_train = np.hstack([y_train, y_tmp]) if y_train.size else y_tmp print(X_train.shape) print("Computing X_test.") static_tractogram = nib.streamlines.load(static_tractogram_fname) static_tractogram = static_tractogram.streamlines print("Compute kdt and prototypes of %s" %static_tractogram_fname) kdt, prototypes = compute_kdtree_and_dr_tractogram(static_tractogram, num_prototypes=num_prototypes, distance_func=distance_func, nb_points=nb_points) print("Computing the test superset...") union_superset_idx = compute_union_superset_idx(kdt, prototypes, ex_dir_tract, distance_func=distance_func, nb_points=nb_points) static_superset = static_tractogram[union_superset_idx] X_test = compute_feature_matrix(static_superset, tract_name, distance_func=distance_func, nb_points=nb_points) del kdt, static_superset print("Normalize X_train and X_test.") scaler = StandardScaler() scaler.fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) print("Classification.") clf = LogisticRegression(class_weight=cw, random_state=42, solver='sag', max_iter=max_iter) t0=time.time() clf.fit(X_train, y_train) print("---->Time to fit X_train of size (%s, %s) = %.2f seconds" %(X_train.shape[0], X_train.shape[1], time.time()-t0)) t1=time.time() y_pred = clf.predict(X_test) y_pred_proba = clf.predict_proba(X_test) print("---->Time to predict X_test of size (%s, %s) = %.2f seconds" %(X_test.shape[0], X_test.shape[1], time.time()-t1)) estimated_tract_idx = np.where(y_pred>0)[0] estimated_tract = static_tractogram[union_superset_idx[estimated_tract_idx]] np.save('estimated_idx_%s.npy' %tract_name, union_superset_idx[estimated_tract_idx]) return estimated_tract
def test_multiple_examples(tractogram_fname, src_dir, tract_name_list, out_dir): """Code for testing. """ num_prototypes = 100 distance_func = bundles_distances_mdf nb_points = 20 tractogram = nib.streamlines.load(tractogram_fname) tractogram = tractogram.streamlines print("Compute kdt and prototypes...") kdt, prototypes = compute_kdtree_and_dr_tractogram(tractogram, num_prototypes=num_prototypes, \ distance_func=distance_func, nb_points=nb_points) for tract_name in tract_name_list: t1 = time.time() k = np.int(0.01 * len(tractogram)) print("Computing the test superset with k = %i..." % k) example_fname = 'templates_tracts/%s.trk' % tract_name tract = nib.streamlines.load(example_fname).streamlines superset_idx_test = compute_superset(tract, kdt, prototypes, k=k, distance_func=distance_func, nb_points=nb_points) superset = tractogram[superset_idx_test] y_pred = tract_predict(src_dir, superset, tract_name, distance_func=distance_func, nb_points=nb_points) estimated_tract_idx = np.where(y_pred > 0)[0] estimated_tract = tractogram[superset_idx_test[estimated_tract_idx]] print("Time to compute classification of tract %s = %.2f seconds" % (tract_name, time.time() - t1)) np.save('estimated_idx_%s.npy' % tract_name, superset_idx_test[estimated_tract_idx]) out_fname = '%s/%s.trk' % (out_dir, tract_name) save_trk(estimated_tract, out_fname) print("Tract saved in %s" % out_fname)
def compute_X_context(X_tmp, superset, k=10): """Add a second feature matrix composed of the features of the k-Nearest-Neighbors. """ kdt, prototypes = compute_kdtree_and_dr_tractogram( superset, num_prototypes=40, distance_func=distance_func, nb_points=nb_points) X_context = np.zeros((len(X_tmp), k * len(X_tmp[0]))) for i in range(len(superset)): nn_idx = compute_superset(superset[i:i + 1], kdt, prototypes, k=k + 1, distance_func=distance_func, nb_points=nb_points) nn_idx = nn_idx[1:] #remove the index corresdponding to itself nn_row = np.array([]) for nn in nn_idx: nn_row = np.hstack([nn_row, X_tmp[nn, :] ]) if nn_row.size else X_tmp[nn, :] X_context[i, :] = nn_row return X_context
step_size = data["step_size"] print("Loading data..") candidate_bundle_idx = np.load(args.candidate_idx) min_cost_values = np.load(args.cost) true_tract = nib.streamlines.load(args.true_tract).streamlines print("Resampling with step size = %s mm" % step_size) true_tract_res = resample_tractogram(true_tract, step_size) true_tract = true_tract_res static_tractogram = nib.streamlines.load(args.static).streamlines static_tractogram_res = resample_tractogram(static_tractogram, step_size) static_tractogram = static_tractogram_res print( "Compute the dissimilarity representation of the target tractogram and build the kd-tree." ) kdt, prototypes = compute_kdtree_and_dr_tractogram(static_tractogram) print("Retrieving indeces of the true_tract") true_tract_idx = streamlines_idx(true_tract, kdt, prototypes) fpr, tpr, AUC = compute_roc_curve(candidate_bundle_idx, min_cost_values, true_tract_idx) if args.out: plot_roc_curve(fpr, tpr, AUC, args.out) with open('csv/output_FiberStats.csv', 'a') as csvFile: writer = csv.writer(csvFile) writer.writerow(np.float16(fpr)) writer.writerow(np.float16(tpr)) writer.writerow(AUC * np.ones(1, dtype=np.float16))
def lap_single_example(moving_tractogram, static_tractogram, example, lD, lE, lR): """Code for LAP from a single example. """ np.random.seed(0) with open('config.json') as f: data = json.load(f) k = data["k"] step_size = data["step_size"] tag = data["_inputs"][2]["datatype_tags"][0].encode("utf-8") distance_func = bundles_distances_mam subjID = ntpath.basename(static_tractogram)[0:6] tract_name = ntpath.basename(example)[7:-10] exID = ntpath.basename(example)[0:6] example_bundle = nib.streamlines.load(example) example_bundle = example_bundle.streamlines example_bundle_res = resample_tractogram(example_bundle, step_size) print( "Retrieving the affine slr transformation for example %s and target %s." % (exID, subjID)) affine = np.load('affine_m%s_s%s.npy' % (exID, subjID)) print("Applying the affine to the example bundle.") example_bundle_aligned = np.array( [apply_affine(affine, s) for s in example_bundle_res]) print( "Compute the dissimilarity representation of the target tractogram and build the kd-tree." ) static_tractogram = nib.streamlines.load(static_tractogram) static_tractogram = static_tractogram.streamlines static_tractogram_res = resample_tractogram(static_tractogram, step_size) static_tractogram = static_tractogram_res if isfile('prototypes.npy') & isfile('kdt'): print("Retrieving past results for kdt and prototypes.") kdt_filename = 'kdt' kdt = pickle.load(open(kdt_filename)) prototypes = np.load('prototypes.npy') else: kdt, prototypes = compute_kdtree_and_dr_tractogram(static_tractogram) #Saving files kdt_filename = 'kdt' pickle.dump(kdt, open(kdt_filename, 'w'), protocol=pickle.HIGHEST_PROTOCOL) np.save('prototypes', prototypes) print("Computing superset with k = %s" % k) superset_idx = compute_superset(example_bundle_aligned, kdt, prototypes, k=k) print("Loading the two-waypoint ROIs of the target...") table_filename = 'ROIs_labels_dictionary.pickle' table = pickle.load(open(table_filename)) roi1_lab = table[tract_name].items()[0][1] roi2_lab = table[tract_name].items()[1][1] if tag == 'afq': roi1_filename = 'aligned_ROIs/sub-%s_var-AFQ_lab-%s_roi.nii.gz' % ( subjID, roi1_lab) roi2_filename = 'aligned_ROIs/sub-%s_var-AFQ_lab-%s_roi.nii.gz' % ( subjID, roi2_lab) elif tag == 'wmaSeg': roi1_filename = 'aligned_ROIs/%s.nii.gz' % roi1_lab roi2_filename = 'aligned_ROIs/%s.nii.gz' % roi2_lab roi1 = nib.load(roi1_filename) roi2 = nib.load(roi2_filename) print("Computing matrices for LAP...") distance_matrix, endpoint_matrix, roi_matrix = compute_lap_matrices( superset_idx, example_bundle_aligned, static_tractogram, roi1, roi2, subjID, exID) print("Using lambdaD = %s, lambdaE = %s and lambdaR = %s" % (lD, lE, lR)) estimated_bundle_idx, min_cost_values = RLAP_modified( distance_matrix, endpoint_matrix, roi_matrix, superset_idx, lD, lE, lR) return estimated_bundle_idx, min_cost_values, len(example_bundle)