def main(): patient_number = "N011618" noddi_data = noddistudy.NoddiData(patient_number) # data_full = noddi_data.get_full() data_odi = noddi_data.get_odi() # data_fiso = noddi_data.get_fiso() data_odi = data_odi.transpose(2,0,1)[:,:,:,None] data_odi = np.concatenate((data_odi,data_odi,data_odi), axis=3) data_odi = tf.convert_to_tensor(data_odi) perceptual_loss(data_odi,data_odi)
directions = [128, 64, 32, 24, 16, 8] ### MAIN LOOP ### for num_directions in directions: print("Generating 1D data for %i directions" % num_directions) subsampling_indices = subsampling.gensamples(num_directions) num_cases = len(patient_database) print("\n") mm = 0 for patient_number in sorted(patient_database.keys()): noddi_data = noddistudy.NoddiData(patient_number) if (noddi_data.get_type() == "test" or noddi_data.get_type() == "duplicate"): continue print("Currently reading: %s as %s data" % (patient_number, noddi_data.get_type())) data_full = noddi_data.get_full()[:, :, 2:(-1 - 3), :] data_odi = noddi_data.get_odi()[:, :, 2:(-1 - 3)] data_fiso = noddi_data.get_fiso()[:, :, 2:(-1 - 3)] data_ficvf = noddi_data.get_ficvf()[:, :, 2:(-1 - 3)] data_gfa = noddi_data.get_gfa()[:, :, 2:(-1 - 3)] data_subsampled = data_full[:, :, :, subsampling_indices]
def gensamples(num_samples, patient_number="N011618", shuffle=False, verbose=False, plot=False, random_seed=500): """Sampling pattern generator for the directions in DSI study. Parameters ---------- num_samples: int Number of directions to be sampled patient_number: str String of the patient number used in the path shuffle: Bool Whether or not to suffle the sampling list verbose: Bool If the sampling is printed out when called plot: Bool If a normalized test example is printed Returns ------- subsampling: array_like 1D array of the sampling indices """ path_to_bvals = ("/v/raid1b/khodgson/MRIdata/DTI/CNC_Imris/" "Stroke_patients/Stroke_DSI_Processing/Data/Bvals") patient_number = "N011618" filename_bvals = "%s/%s_bvals.txt" % (path_to_bvals, patient_number) with open(filename_bvals) as bvalue_file: line = [] for bvalue in bvalue_file.readlines()[0].split(): line.append(int(float(bvalue))) bvalues = np.array(line) path_to_bvecs = ("/v/raid1b/khodgson/MRIdata/DTI/CNC_Imris/" "Stroke_patients/Stroke_DSI_Processing/Data/Bvecs") patient_number = "N011618" filename_bvecs = "%s/%s_original_bvecs.txt" % (path_to_bvecs, patient_number) directions = np.zeros((bvalues.shape[0], 3)) with open(filename_bvecs) as bvecs_file: lines = bvecs_file.readlines() for ii in range(len(lines)): for jj in range(bvalues.shape[0]): directions[jj, ii] = bvalues[jj] * float(lines[ii].split()[jj]) # now divide the directions into shells shell1_divide = 1000 shell2_divide = 2000 bvalue_dict = {} bvalue_dict["b0"] = [] bvalue_dict["shell1"] = [] bvalue_dict["shell2"] = [] bvalue_dict["shell3"] = [] for ii in range(bvalues.shape[0]): if bvalues[ii] == 0: bvalue_dict["b0"].append(ii) elif (0 < bvalues[ii]) and (bvalues[ii] <= shell1_divide): bvalue_dict["shell1"].append(ii) elif (shell1_divide < bvalues[ii]) and (bvalues[ii] <= shell2_divide): bvalue_dict["shell2"].append(ii) elif (shell2_divide < bvalues[ii]): bvalue_dict["shell3"].append(ii) if random_seed < 500: num_b0 = round(num_samples * len(bvalue_dict["b0"]) / bvalues.shape[0]) num_shell1 = round(num_samples * len(bvalue_dict["shell1"]) / bvalues.shape[0]) num_shell2 = round(num_samples * len(bvalue_dict["shell2"]) / bvalues.shape[0]) num_shell3 = num_samples - (num_b0 + num_shell1 + num_shell2) np.random.seed(seed=random_seed) b0 = np.random.choice(bvalue_dict["b0"], num_b0, replace=False) shell1 = np.random.choice(bvalue_dict["shell1"], num_shell1, replace=False) shell2 = np.random.choice(bvalue_dict["shell2"], num_shell2, replace=False) shell3 = np.random.choice(bvalue_dict["shell3"], num_shell3, replace=False) subsampling = np.concatenate((b0, shell1, shell2, shell3), axis=0) else: num_b0 = round(num_samples * len(bvalue_dict["b0"]) / bvalues.shape[0]) num_shell1 = round(num_samples * len(bvalue_dict["shell1"]) / bvalues.shape[0]) num_shell2 = num_samples - (num_b0 + num_shell1) np.random.seed(seed=random_seed) b0 = np.random.choice(bvalue_dict["b0"], num_b0, replace=False) shell1 = np.random.choice(bvalue_dict["shell1"], num_shell1, replace=False) shell2 = np.random.choice(bvalue_dict["shell2"], num_shell2, replace=False) subsampling = np.concatenate((b0, shell1, shell2), axis=0) directions_used = np.zeros((num_samples, 3)) directions_total = np.zeros((directions.shape[0] - num_samples, 3)) jj = 0 kk = 0 for ii in range(directions.shape[0]): if ii in subsampling: directions_used[jj, :] = directions[ii, :] jj += 1 else: directions_total[kk, :] = directions[ii, :] kk += 1 assert jj == num_samples if shuffle is True: np.random.shuffle(subsampling) if verbose is True: print("b0 sampling:") print(b0) print("shell1 sampling:") print(shell1) print("shell2 sampling:") print(shell2) print("shell3 sampling:") print(shell3) print("num_samples") print(subsampling) if plot is True: study = noddistudy.NoddiData(patient_number) data_full = study.get_full() data = data_full[:, :, :, subsampling] maxs = np.amax(data.reshape(-1, num_samples), axis=0).squeeze() print(maxs) data /= maxs[None, None, None, :] plt.figure() display.Render(data[:, :, 25, :]) plt.show() return directions_used, directions_total
from matplotlib import pyplot as plt import json import os import sys import time from utils import display from utils import readhdf5 sys.path.append("/home/mirl/egibbons/noddi") from noddi_utils import noddistudy metadata_name = "/home/mirl/egibbons/noddi/noddi_utils/noddi_metadata.json" with open(metadata_name) as metadata: database = json.load(metadata) print(database.keys()) for patient_name in database.keys(): study = noddistudy.NoddiData(patient_name) data = study.get_full() plt.figure() display.Render(data[:, :, 25, :]) plt.show() # list_names = noddistudy.NoddiData()