def single_lap(moving_tractogram, static_tractogram, kdt, prototypes, example, k=500, distance_func = bundles_distances_mam): """Code for LAP from a single example. """ print("Retrieving affine from dictionary.") table_filename = 'affine_dictionary.pickle' table = pickle.load(open(table_filename)) moving_tractogram_basename = ntpath.basename(moving_tractogram) static_tractogram_basename = ntpath.basename(static_tractogram) affine = table[moving_tractogram_basename, static_tractogram_basename].items()[0][1] print("Applying the affine to the example bundle.") example_bundle = nib.streamlines.load(example) example_bundle = example_bundle.streamlines example_bundle_aligned = np.array([apply_affine(affine, s) for s in example_bundle]) 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 Rectangular linear Assignment Problem (RLAP).") static_tractogram = nib.streamlines.load(static_tractogram) static_tractogram = static_tractogram.streamlines estimated_bundle_idx, min_cost_values = RLAP(kdt, k, dm_example_bundle_aligned, example_bundle_aligned, static_tractogram, distance_func) estimated_bundle = static_tractogram[estimated_bundle_idx] return estimated_bundle_idx, min_cost_values, len(example_bundle)
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 lap_single_example(moving_tractogram, static_tractogram, example): """Code for LAP from a single example. """ k = 500 distance_func = bundles_distances_mam print("Computing the affine slr transformation.") affine = tractograms_slr(moving_tractogram, static_tractogram) print("Applying the affine to the example bundle.") example_bundle = nib.streamlines.load(example) example_bundle = example_bundle.streamlines example_bundle_aligned = np.array( [apply_affine(affine, s) for s in example_bundle]) 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 kdt, prototypes = compute_kdtree_and_dr_tractogram(static_tractogram) 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 Rectangular linear Assignment Problem (RLAP).") estimated_bundle_idx, min_cost_values = RLAP(kdt, k, dm_example_bundle_aligned, example_bundle_aligned, static_tractogram, distance_func) estimated_bundle = static_tractogram[estimated_bundle_idx] return estimated_bundle_idx, min_cost_values, len(example_bundle)
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)