def test_multi_tissue_mc_model(): scheme = wu_minn_hcp_acquisition_scheme() ball = gaussian_models.G1Ball() cyl = cylinder_models.C1Stick() models = [ball, cyl] S0_responses = [1., 2.] mt_mc = modeling_framework.MultiCompartmentModel( models=models, S0_tissue_responses=S0_responses) mt_mc.set_fixed_parameter('C1Stick_1_lambda_par', 1.7e-9) mt_mc.set_fixed_parameter('G1Ball_1_lambda_iso', 3e-9) mt_mc.set_fixed_parameter('C1Stick_1_mu', [0., 0.]) param_dict = {'partial_volume_0': .5, 'partial_volume_1': .5} E = mt_mc.simulate_signal(scheme, param_dict) mt_mc_fit = mt_mc.fit(scheme, E) sig_fracts = mt_mc_fit.fitted_parameters vol_fracts = mt_mc_fit.fitted_multi_tissue_fractions vol_fracts_norm = mt_mc_fit.fitted_multi_tissue_fractions_normalized assert_almost_equal( sig_fracts['partial_volume_0'], vol_fracts['partial_volume_0'], 2) assert_almost_equal( sig_fracts['partial_volume_1'], vol_fracts['partial_volume_1'] * 2., 2) assert_almost_equal( vol_fracts_norm['partial_volume_0'], 2 / 3., 2) assert_almost_equal( vol_fracts_norm['partial_volume_1'], 1 / 3., 2)
def create_noddi_watson_model(lambda_iso_diff=3.e-9, lambda_par_diff=1.7e-9): """Creates NODDI mulit-compartment model with Watson distribution.""" """ Arguments: lambda_iso_diff: float isotropic diffusivity lambda_par_diff: float parallel diffusivity Returns: MultiCompartmentModel instance NODDI Watson multi-compartment model instance """ ball = gaussian_models.G1Ball() stick = cylinder_models.C1Stick() zeppelin = gaussian_models.G2Zeppelin() watson_dispersed_bundle = SD1WatsonDistributed(models=[stick, zeppelin]) watson_dispersed_bundle.set_tortuous_parameter('G2Zeppelin_1_lambda_perp', 'C1Stick_1_lambda_par', 'partial_volume_0') watson_dispersed_bundle.set_equal_parameter('G2Zeppelin_1_lambda_par', 'C1Stick_1_lambda_par') watson_dispersed_bundle.set_fixed_parameter('G2Zeppelin_1_lambda_par', lambda_par_diff) NODDI_mod = MultiCompartmentModel(models=[ball, watson_dispersed_bundle]) NODDI_mod.set_fixed_parameter('G1Ball_1_lambda_iso', lambda_iso_diff) return NODDI_mod
def test_spherical_harmonics_model_raises(odi=0.15, mu=[0., 0.], lambda_par=1.7e-9): stick = cylinder_models.C1Stick() ball = gaussian_models.G1Ball() watsonstick = distribute_models.SD1WatsonDistributed([stick]) params = { 'SD1Watson_1_odi': odi, 'SD1Watson_1_mu': mu, 'C1Stick_1_lambda_par': lambda_par } data = watsonstick(scheme, **params) assert_raises(ValueError, modeling_framework.MultiCompartmentSphericalHarmonicsModel, [ball]) sh_mod = modeling_framework.MultiCompartmentSphericalHarmonicsModel( [stick]) assert_raises(ValueError, sh_mod.fit, scheme, data, solver='csd_cvxpy') sh_mod = modeling_framework.MultiCompartmentSphericalHarmonicsModel( [stick, ball]) sh_mod.set_fixed_parameter('C1Stick_1_lambda_par', lambda_par) sh_mod.set_fixed_parameter('G1Ball_1_lambda_iso', 3e-9) assert_raises(ValueError, sh_mod.fit, scheme, data, solver='csd_tournier07')
def test_all_models_dispersable(): scheme = wu_minn_hcp_acquisition_scheme() dispersable_models = [ [cylinder_models.C1Stick()], [cylinder_models.C2CylinderStejskalTannerApproximation()], [cylinder_models.C3CylinderCallaghanApproximation()], [cylinder_models.C4CylinderGaussianPhaseApproximation()], [gaussian_models.G1Ball(), gaussian_models.G2Zeppelin()], [gaussian_models.G3TemporalZeppelin()], [sphere_models.S1Dot(), gaussian_models.G2Zeppelin()], [ sphere_models.S2SphereStejskalTannerApproximation(), gaussian_models.G2Zeppelin() ] ] spherical_distributions = [ distribute_models.SD1WatsonDistributed, distribute_models.SD2BinghamDistributed ] for model in dispersable_models: for distribution in spherical_distributions: dist_mod = distribution(model) params = {} for param, card in dist_mod.parameter_cardinality.items(): params[param] = np.random.rand( card) * dist_mod.parameter_scales[param] assert_equal(isinstance(dist_mod(scheme, **params), np.ndarray), True)
def test_fitting_without_b0_raises(): bvals = np.atleast_1d(1e9) bvecs = np.atleast_2d([1., 0., 0.]) scheme = acquisition_scheme_from_bvalues(bvals, bvecs) mc = modeling_framework.MultiCompartmentModel([gaussian_models.G1Ball()]) data = np.atleast_1d(1.) assert_raises(ValueError, mc.fit, scheme, data)
def test_simple_ball_and_stick_optimization(): stick = cylinder_models.C1Stick() ball = gaussian_models.G1Ball() ball_and_stick = (modeling_framework.MultiCompartmentModel( models=[ball, stick])) gt_mu = np.clip(np.random.rand(2), .3, np.inf) gt_lambda_par = (np.random.rand() + 1.) * 1e-9 gt_lambda_iso = gt_lambda_par / 2. gt_partial_volume = 0.3 gt_parameter_vector = ball_and_stick.parameters_to_parameter_vector( C1Stick_1_lambda_par=gt_lambda_par, G1Ball_1_lambda_iso=gt_lambda_iso, C1Stick_1_mu=gt_mu, partial_volume_0=gt_partial_volume, partial_volume_1=1 - gt_partial_volume) E = ball_and_stick.simulate_signal(scheme, gt_parameter_vector) vf_rand = np.random.rand() ball_and_stick.set_initial_guess_parameter('C1Stick_1_lambda_par', (np.random.rand() + 1.) * 1e-9) ball_and_stick.set_initial_guess_parameter('G1Ball_1_lambda_iso', gt_lambda_par / 2.) ball_and_stick.set_initial_guess_parameter('C1Stick_1_mu', np.random.rand(2)) ball_and_stick.set_initial_guess_parameter('partial_volume_0', vf_rand) ball_and_stick.set_initial_guess_parameter('partial_volume_1', 1 - vf_rand) res = ball_and_stick.fit(scheme, E).fitted_parameters_vector assert_array_almost_equal(gt_parameter_vector, res.squeeze(), 2)
def test_multi_compartment_fod_with_parametric_model( odi=0.15, mu=[0., 0.], lambda_iso=3e-9, lambda_par=1.7e-9, vf_intra=0.7): stick = cylinder_models.C1Stick() ball = gaussian_models.G1Ball() watsonstick = distribute_models.SD1WatsonDistributed( [stick]) mc_mod = modeling_framework.MultiCompartmentModel([watsonstick, ball]) sh_mod = modeling_framework.MultiCompartmentSphericalHarmonicsModel( [stick, ball]) sh_mod.set_fixed_parameter('G1Ball_1_lambda_iso', lambda_iso) sh_mod.set_fixed_parameter('C1Stick_1_lambda_par', lambda_par) simulation_parameters = mc_mod.parameters_to_parameter_vector( G1Ball_1_lambda_iso=lambda_iso, SD1WatsonDistributed_1_SD1Watson_1_mu=mu, SD1WatsonDistributed_1_C1Stick_1_lambda_par=lambda_par, SD1WatsonDistributed_1_SD1Watson_1_odi=odi, partial_volume_0=vf_intra, partial_volume_1=1 - vf_intra) data = mc_mod.simulate_signal(scheme, simulation_parameters) sh_fit = sh_mod.fit(scheme, data) vf_intra_estimated = sh_fit.fitted_parameters['partial_volume_0'] assert_almost_equal(vf_intra, vf_intra_estimated) predicted_signal = sh_fit.predict() assert_array_almost_equal(data, predicted_signal[0], 4)
def test_set_parameter_optimization_bounds_raises(): ball = gaussian_models.G1Ball() mc = modeling_framework.MultiCompartmentModel([ball]) assert_raises(ValueError, mc.set_parameter_optimization_bounds, 'not a valid name', [1, 2]) assert_raises(ValueError, mc.set_parameter_optimization_bounds, 'G1Ball_1_lambda_iso', 1) assert_raises(ValueError, mc.set_parameter_optimization_bounds, 'G1Ball_1_lambda_iso', [[1, 2], [1, 2]]) assert_raises(ValueError, mc.set_parameter_optimization_bounds, 'G1Ball_1_lambda_iso', [1, 2, 3]) assert_raises(ValueError, mc.set_parameter_optimization_bounds, 'G1Ball_1_lambda_iso', [2, 1])
def test_multi_tissue_tortuosity_no_s0(): stick = cylinder_models.C1Stick() zeppelin = gaussian_models.G2Zeppelin() ball = gaussian_models.G1Ball() model = modeling_framework.MultiCompartmentModel( models=[stick, zeppelin, ball]) model.set_tortuous_parameter('G2Zeppelin_1_lambda_perp', 'C1Stick_1_lambda_par', 'partial_volume_0', 'partial_volume_1', True) tort = model.parameter_links[0][2] s0ic, s0ec = tort.S0_intra, tort.S0_extra assert_(s0ic == 1 and s0ec == 1)
def test_multi_dimensional_x0(): stick = cylinder_models.C1Stick() ball = gaussian_models.G1Ball() ball_and_stick = ( modeling_framework.MultiCompartmentModel( models=[ball, stick],) ) gt_lambda_par = (np.random.rand() + 1.) * 1e-9 gt_lambda_iso = gt_lambda_par / 2. gt_partial_volume = 0.3 gt_mu_array = np.empty((10, 10, 2)) # I'm putting the orientation of the stick all over the sphere. for i, mu1 in enumerate(np.linspace(0, np.pi, 10)): for j, mu2 in enumerate(np.linspace(-np.pi, np.pi, 10)): gt_mu_array[i, j] = np.r_[mu1, mu2] gt_parameter_vector = ( ball_and_stick.parameters_to_parameter_vector( C1Stick_1_lambda_par=gt_lambda_par, G1Ball_1_lambda_iso=gt_lambda_iso, C1Stick_1_mu=gt_mu_array, partial_volume_0=gt_partial_volume, partial_volume_1=1 - gt_partial_volume) ) E_array = ball_and_stick.simulate_signal( scheme, gt_parameter_vector) ball_and_stick.set_initial_guess_parameter( 'C1Stick_1_lambda_par', gt_lambda_par) ball_and_stick.set_initial_guess_parameter( 'G1Ball_1_lambda_iso', gt_lambda_iso) ball_and_stick.set_initial_guess_parameter( 'C1Stick_1_mu', gt_mu_array) ball_and_stick.set_initial_guess_parameter( 'partial_volume_0', gt_partial_volume) ball_and_stick.set_initial_guess_parameter( 'partial_volume_1', 1 - gt_partial_volume) # I'm giving a voxel-dependent initial condition with gt_mu_array res = ball_and_stick.fit(scheme, E_array).fitted_parameters_vector # optimization should stop immediately as I'm giving the ground truth. assert_equal(np.all(np.ravel(res - gt_parameter_vector) == 0.), True) # and the parameter vector dictionaries of the results and x0 should also # be the same. res_parameters = ball_and_stick.parameter_vector_to_parameters(res) x0_parameters = ball_and_stick.parameter_vector_to_parameters( gt_parameter_vector) for key in res_parameters.keys(): assert_array_equal(x0_parameters[key], res_parameters[key])
def test_raises_models_with_no_orientation(): non_dispersable_models = [ gaussian_models.G1Ball(), sphere_models.S1Dot(), sphere_models.S2SphereStejskalTannerApproximation() ] spherical_distributions = [ distribute_models.SD1WatsonDistributed, distribute_models.SD2BinghamDistributed ] for model in non_dispersable_models: for distribution in spherical_distributions: assert_raises(ValueError, distribution, [model])
def test_MIX_fitting_multimodel(): ball = gaussian_models.G1Ball() zeppelin = gaussian_models.G2Zeppelin() ball_and_zeppelin = (modeling_framework.MultiCompartmentModel( models=[ball, zeppelin])) parameter_vector = ball_and_zeppelin.parameters_to_parameter_vector( G1Ball_1_lambda_iso=2.7e-9, partial_volume_0=.2, partial_volume_1=.8, G2Zeppelin_1_lambda_perp=.5e-9, G2Zeppelin_1_mu=(np.pi / 2., np.pi / 2.), G2Zeppelin_1_lambda_par=1.7e-9) E = ball_and_zeppelin.simulate_signal(scheme, parameter_vector) fit = ball_and_zeppelin.fit(scheme, E, solver='mix').fitted_parameters_vector assert_array_almost_equal(abs(fit).squeeze(), parameter_vector, 2)
def test_simple_ball_and_stick_optimization_vf_fixed(): stick = cylinder_models.C1Stick() ball = gaussian_models.G1Ball() ball_and_stick = ( modeling_framework.MultiCompartmentModel( models=[ball, stick]) ) gt_mu = np.clip(np.random.rand(2), .3, np.inf) gt_lambda_par = (np.random.rand() + 1.) * 1e-9 gt_lambda_iso = gt_lambda_par / 2. gt_partial_volume = 0.3 gt_parameter_vector = ball_and_stick.parameters_to_parameter_vector( C1Stick_1_lambda_par=gt_lambda_par, G1Ball_1_lambda_iso=gt_lambda_iso, C1Stick_1_mu=gt_mu, partial_volume_0=gt_partial_volume, partial_volume_1=1 - gt_partial_volume ) E = ball_and_stick.simulate_signal( scheme, gt_parameter_vector) E2d = np.array([E, E]) vf_rand = np.random.rand() ball_and_stick.set_fixed_parameter( 'partial_volume_0', np.r_[vf_rand, vf_rand]) ball_and_stick.set_fixed_parameter( 'partial_volume_1', np.r_[1 - vf_rand, 1 - vf_rand]) ball_and_stick.set_initial_guess_parameter( 'C1Stick_1_lambda_par', (np.random.rand() + 1.) * 1e-9) ball_and_stick.set_initial_guess_parameter( 'G1Ball_1_lambda_iso', gt_lambda_par / 2.) ball_and_stick.set_initial_guess_parameter( 'C1Stick_1_mu', np.random.rand(2)) vf_fitted = ball_and_stick.fit( scheme, E2d).fitted_parameters['partial_volume_0'][0] assert_equal(vf_fitted, vf_rand)
def test_ball(lambda_iso=1.7e-9): ball = gaussian_models.G1Ball(lambda_iso=lambda_iso) E_ball = ball(scheme) E = np.exp(-bvals * lambda_iso) assert_array_equal(E, E_ball)
def test_raise_combination_NRM_and_others(): ball = gaussian_models.G1Ball() plane = plane_models.P3PlaneCallaghanApproximation() assert_raises(ValueError, modeling_framework.MultiCompartmentModel, [ball, plane])
def main(): # Plot Save Path base_plot_path = r'/nfs/masi/nathv/py_src_code_2020/dmipy_model_pictures' base_plot_path = os.path.normpath(base_plot_path) # Method Saving Paths base_save_path = r'/nfs/masi/nathv/miccai_2020/micro_methods_hcp_mini' base_save_path = os.path.normpath(base_save_path) # Create base saving path for Method # TODO The Method name can be made an argument later on method_name = 'VERDICT' # Base HCP Data Path base_data_path = r'/nfs/HCP/data' base_data_path = os.path.normpath(base_data_path) # Subject ID's list subj_ID_List = ['125525', '118225', '116726', '115825', '115017', '114823'] # TODO When needed loop here over the ID list for subj_ID in subj_ID_List: # Subject Save Path subj_save_path = os.path.join(base_save_path, subj_ID) if os.path.exists(subj_save_path) == False: os.mkdir(subj_save_path) # TODO For later the subject data, bval and bvec reading part can be put inside a function subj_data_path = os.path.join(base_data_path, subj_ID_List[0], 'T1w', 'Diffusion') # Read the Nifti file, bvals and bvecs subj_bvals = np.loadtxt(os.path.join(subj_data_path, 'bvals')) subj_bvecs = np.loadtxt(os.path.join(subj_data_path, 'bvecs')) all_bvals = subj_bvals * 1e6 all_bvecs = np.transpose(subj_bvecs) subj_Acq_Scheme = acquisition_scheme_from_bvalues(all_bvals, all_bvecs, delta=10.6 * 1e-3, Delta=43.1 * 1e-3, TE=89.5 * 1e-3) print(subj_Acq_Scheme.print_acquisition_info) print('Loading the Nifti Data ...') data_start_time = time.time() subj_babel_object = nib.load( os.path.join(subj_data_path, 'data.nii.gz')) subj_data = subj_babel_object.get_fdata() axial_slice_data = subj_data[55:60, 65:70, 60:62, :] data_end_time = time.time() data_time = np.int(np.round(data_end_time - data_start_time)) print('Data Loaded ... Time Taken: {}'.format(data_end_time - data_start_time)) print('The Data Dimensions are: {}'.format(subj_data.shape)) #### Verdict Begin #### sphere = sphere_models.S4SphereGaussianPhaseApproximation( diffusion_constant=0.9e-9) ball = gaussian_models.G1Ball() stick = cylinder_models.C1Stick() verdict_mod = MultiCompartmentModel(models=[sphere, ball, stick]) verdict_mod.set_fixed_parameter('G1Ball_1_lambda_iso', 0.9e-9) verdict_mod.set_parameter_optimization_bounds('C1Stick_1_lambda_par', [3.05e-9, 10e-9]) print('Fitting the Verdict Model ...') fit_start_time = time.time() mcdmi_fit = verdict_mod.fit(subj_Acq_Scheme, axial_slice_data, mask=axial_slice_data[..., 0] > 0, solver='mix', use_parallel_processing=False) fit_end_time = time.time() print('Model Fitting Completed ... Time Taken to fit: {}'.format( fit_end_time - fit_start_time)) fit_time = np.int(np.round(fit_end_time - fit_start_time)) fitted_parameters = mcdmi_fit.fitted_parameters # Get List of Estimated Parameter Names para_Names_list = [] for key, value in fitted_parameters.items(): para_Names_list.append(key) ### Nifti Saving Part # Create a directory per subject subj_method_save_path = os.path.join(subj_save_path, method_name) if os.path.exists(subj_method_save_path) == False: os.mkdir(subj_method_save_path) # Retrieve the affine from already Read Nifti file to form the header affine = subj_babel_object.affine # Loop over fitted parameters name list for each_fitted_parameter in para_Names_list: new_img = nib.Nifti1Image(fitted_parameters[each_fitted_parameter], affine) # Form the file path f_name = each_fitted_parameter + '.nii.gz' param_file_path = os.path.join(subj_method_save_path, f_name) nib.save(new_img, param_file_path) return None
def __init__(self): self.ball = gaussian_models.G1Ball() self.stick = cylinder_models.C1Stick() self.model = MultiCompartmentModel(models=[self.stick, self.ball])
def main(): #Argparse Stuff parser = argparse.ArgumentParser(description='subject_id') parser.add_argument('--subject_id', type=str, default='135124') args = parser.parse_args() # Plot Save Path base_plot_path = r'/nfs/masi/nathv/py_src_code_2020/dmipy_model_pictures' base_plot_path = os.path.normpath(base_plot_path) # Method Saving Paths # TODO KARTHIK base_save_path = r'/root/hcp_results' base_save_path = os.path.normpath(base_save_path) if os.path.exists(base_save_path)==False: os.mkdir(base_save_path) # Create base saving path for Method # TODO The Method name can be made an argument later on method_name = 'NODDI_WATSON' # Base HCP Data Path # TODO KARTHIK This is where we hard set HCP's Data Path base_data_path = r'/root/local_mount/data' base_data_path = os.path.normpath(base_data_path) # Subject ID subj_ID = args.subject_id # Subject Save Path subj_save_path = os.path.join(base_save_path, subj_ID) if os.path.exists(subj_save_path)==False: os.mkdir(subj_save_path) # TODO For later the subject data, bval and bvec reading part can be put inside a function subj_data_path = os.path.join(base_data_path, subj_ID, 'T1w', 'Diffusion') # Read the Nifti file, bvals and bvecs subj_bvals = np.loadtxt(os.path.join(subj_data_path, 'bvals')) subj_bvecs = np.loadtxt(os.path.join(subj_data_path, 'bvecs')) all_bvals = subj_bvals * 1e6 all_bvecs = np.transpose(subj_bvecs) subj_Acq_Scheme = acquisition_scheme_from_bvalues(all_bvals, all_bvecs) print(subj_Acq_Scheme.print_acquisition_info) print('Loading the Nifti Data ...') data_start_time = time.time() subj_babel_object = nib.load(os.path.join(subj_data_path, 'data.nii.gz')) subj_data = subj_babel_object.get_fdata() axial_slice_data = subj_data[50:65, 50:65, 60:62, :] data_end_time = time.time() data_time = np.int(np.round(data_end_time - data_start_time)) print('Data Loaded ... Time Taken: {}'.format(data_end_time - data_start_time)) print('The Data Dimensions are: {}'.format(subj_data.shape)) #### NODDI Watson #### ball = gaussian_models.G1Ball() stick = cylinder_models.C1Stick() zeppelin = gaussian_models.G2Zeppelin() watson_dispersed_bundle = SD1WatsonDistributed(models=[stick, zeppelin]) watson_dispersed_bundle.set_tortuous_parameter('G2Zeppelin_1_lambda_perp', 'C1Stick_1_lambda_par', 'partial_volume_0') watson_dispersed_bundle.set_equal_parameter('G2Zeppelin_1_lambda_par', 'C1Stick_1_lambda_par') watson_dispersed_bundle.set_fixed_parameter('G2Zeppelin_1_lambda_par', 1.7e-9) NODDI_mod = MultiCompartmentModel(models=[ball, watson_dispersed_bundle]) NODDI_mod.set_fixed_parameter('G1Ball_1_lambda_iso', 3e-9) print('Fitting the NODDI Model ...') fit_start_time = time.time() NODDI_fit_hcp = NODDI_mod.fit(subj_Acq_Scheme, subj_data, mask=subj_data[..., 0] > 0, use_parallel_processing=True, number_of_processors=32) fit_end_time = time.time() print('Model Fitting Completed ... Time Taken to fit: {}'.format(fit_end_time - fit_start_time)) fit_time = np.int(np.round(fit_end_time - fit_start_time)) fitted_parameters = NODDI_fit_hcp.fitted_parameters para_Names_list = [] for key, value in fitted_parameters.items(): para_Names_list.append(key) ### Nifti Saving Part # Create a directory per subject subj_method_save_path = os.path.join(subj_save_path, method_name) if os.path.exists(subj_method_save_path)==False: os.mkdir(subj_method_save_path) # Retrieve the affine from already Read Nifti file to form the header affine = subj_babel_object.affine # Loop over fitted parameters name list for each_fitted_parameter in para_Names_list: new_img = nib.Nifti1Image(fitted_parameters[each_fitted_parameter], affine) # Form the file path f_name = each_fitted_parameter + '.nii.gz' param_file_path = os.path.join(subj_method_save_path, f_name) nib.save(new_img, param_file_path) return None
gradient_directions_normalized = normalized_vector(gradient_directions.T) gradient_directions_normalized[np.isnan(gradient_directions_normalized)] = 1.0/np.sqrt(3) bvalues = np.loadtxt(allBvalsNames[iMask]) # given in s/mm^2 bvalues_SI = bvalues * 1e6 acq_scheme = acquisition_scheme_from_bvalues(bvalues_SI, gradient_directions_normalized, delta, Delta) # gtab_dipy = gradient_table(bvalues, gradient_directions, big_delta=Delta, small_delta=delta, atol=3e-2) # acq_scheme = gtab_dipy2mipy(gtab_dipy) acq_scheme.print_acquisition_info dwi_nii = nib.load(allDwiNames[iMask]) dwi = dwi_nii.get_data() mask = nib.load(allMaskNames[iMask]).get_data() ball = gaussian_models.G1Ball() cylinder = cylinder_models.C4CylinderGaussianPhaseApproximation() gamma_cylinder = distribute_models.DD1GammaDistributed(models=[cylinder]) axcaliber_gamma = MultiCompartmentModel(models=[ball, gamma_cylinder]) print axcaliber_gamma.parameter_cardinality axcaliber_gamma.set_fixed_parameter('DD1GammaDistributed_1_C4CylinderGaussianPhaseApproximation_1_lambda_par', 1.7e-9) axcaliber_gamma.set_fixed_parameter('DD1GammaDistributed_1_C4CylinderGaussianPhaseApproximation_1_mu', [0, 0]) axcaliber_gamma_fit = axcaliber_gamma.fit(acq_scheme, dwi, mask = mask, solver='mix', maxiter=100) fitted_parameters = axcaliber_gamma_fit.fitted_parameters hdr = dwi_nii.header
def main(): #Argparse Stuff parser = argparse.ArgumentParser(description='subject_id') parser.add_argument('--subject_id', type=str, default='135124') args = parser.parse_args() # Plot Save Path base_plot_path = r'/nfs/masi/nathv/py_src_code_2020/dmipy_model_pictures' base_plot_path = os.path.normpath(base_plot_path) # Method Saving Paths # TODO KARTHIK base_save_path = r'/root/hcp_results' base_save_path = os.path.normpath(base_save_path) if os.path.exists(base_save_path) == False: os.mkdir(base_save_path) # Create base saving path for Method # TODO The Method name can be made an argument later on method_name = 'VERDICT' # Base HCP Data Path # TODO KARTHIK This is where we hard set HCP's Data Path base_data_path = r'/root/local_mount/data' base_data_path = os.path.normpath(base_data_path) # Subject ID subj_ID = args.subject_id # Subject Save Path subj_save_path = os.path.join(base_save_path, subj_ID) if os.path.exists(subj_save_path) == False: os.mkdir(subj_save_path) # TODO For later the subject data, bval and bvec reading part can be put inside a function subj_data_path = os.path.join(base_data_path, subj_ID, 'T1w', 'Diffusion') # Read the Nifti file, bvals and bvecs subj_bvals = np.loadtxt(os.path.join(subj_data_path, 'bvals')) subj_bvecs = np.loadtxt(os.path.join(subj_data_path, 'bvecs')) all_bvals = subj_bvals * 1e6 all_bvecs = np.transpose(subj_bvecs) subj_Acq_Scheme = acquisition_scheme_from_bvalues(all_bvals, all_bvecs, delta=10.6 * 1e-3, Delta=43.1 * 1e-3, TE=89.5 * 1e-3) print(subj_Acq_Scheme.print_acquisition_info) print('Loading the Nifti Data ...') data_start_time = time.time() subj_babel_object = nib.load(os.path.join(subj_data_path, 'data.nii.gz')) subj_data = subj_babel_object.get_fdata() #axial_slice_data = subj_data[55:60, 65:70, 60:62, :] mask_babel_object = nib.load( os.path.join(subj_data_path, 'nodif_brain_mask.nii.gz')) mask_data = mask_babel_object.get_fdata() data_end_time = time.time() data_time = np.int(np.round(data_end_time - data_start_time)) print('Data Loaded ... Time Taken: {}'.format(data_end_time - data_start_time)) print('The Data Dimensions are: {}'.format(subj_data.shape)) #### Verdict Begin #### sphere = sphere_models.S4SphereGaussianPhaseApproximation( diffusion_constant=0.9e-9) ball = gaussian_models.G1Ball() stick = cylinder_models.C1Stick() verdict_mod = MultiCompartmentModel(models=[sphere, ball, stick]) verdict_mod.set_fixed_parameter('G1Ball_1_lambda_iso', 0.9e-9) verdict_mod.set_parameter_optimization_bounds('C1Stick_1_lambda_par', [3.05e-9, 10e-9]) print('Fitting the Verdict Model ...') fit_start_time = time.time() mcdmi_fit = verdict_mod.fit(subj_Acq_Scheme, subj_data, mask=mask_data, solver='mix', use_parallel_processing=True, number_of_processors=64) fit_end_time = time.time() print('Model Fitting Completed ... Time Taken to fit: {}'.format( fit_end_time - fit_start_time)) fit_time = np.int(np.round(fit_end_time - fit_start_time)) fitted_parameters = mcdmi_fit.fitted_parameters # Get List of Estimated Parameter Names para_Names_list = [] for key, value in fitted_parameters.items(): para_Names_list.append(key) ### Nifti Saving Part # Create a directory per subject subj_method_save_path = os.path.join(subj_save_path, method_name) if os.path.exists(subj_method_save_path) == False: os.mkdir(subj_method_save_path) # Retrieve the affine from already Read Nifti file to form the header affine = subj_babel_object.affine # Loop over fitted parameters name list for each_fitted_parameter in para_Names_list: new_img = nib.Nifti1Image(fitted_parameters[each_fitted_parameter], affine) # Form the file path f_name = each_fitted_parameter + '.nii.gz' param_file_path = os.path.join(subj_method_save_path, f_name) nib.save(new_img, param_file_path) return None
def main(): # Plot Save Path base_plot_path = r'/nfs/masi/nathv/py_src_code_2020/dmipy_model_pictures' base_plot_path = os.path.normpath(base_plot_path) # Method Saving Paths base_save_path = r'/nfs/masi/nathv/miccai_2020/micro_methods_hcp_mini' base_save_path = os.path.normpath(base_save_path) # Create base saving path for Method # TODO The Method name can be made an argument later on method_name = 'AXCALIBER' # Base HCP Data Path base_data_path = r'/nfs/HCP/data' base_data_path = os.path.normpath(base_data_path) # Subject ID's list subj_ID_List = ['125525', '118225', '116726', '115825', '115017', '114823'] # TODO When needed loop here over the ID list for subj_ID in subj_ID_List: # Subject Save Path subj_save_path = os.path.join(base_save_path, subj_ID) if os.path.exists(subj_save_path) == False: os.mkdir(subj_save_path) # TODO For later the subject data, bval and bvec reading part can be put inside a function subj_data_path = os.path.join(base_data_path, subj_ID_List[0], 'T1w', 'Diffusion') # Read the Nifti file, bvals and bvecs subj_bvals = np.loadtxt(os.path.join(subj_data_path, 'bvals')) subj_bvecs = np.loadtxt(os.path.join(subj_data_path, 'bvecs')) all_bvals = subj_bvals * 1e6 all_bvecs = np.transpose(subj_bvecs) subj_Acq_Scheme = acquisition_scheme_from_bvalues(all_bvals, all_bvecs, delta=10.6 * 1e-3, Delta=43.1 * 1e-3, TE=89.5 * 1e-3) print(subj_Acq_Scheme.print_acquisition_info) print('Loading the Nifti Data ...') data_start_time = time.time() subj_babel_object = nib.load( os.path.join(subj_data_path, 'data.nii.gz')) subj_data = subj_babel_object.get_fdata() axial_slice_data = subj_data[58:60, 68:70, 60:62, :] mask_babel_object = nib.load( os.path.join(subj_data_path, 'nodif_brain_mask.nii.gz')) mask_data = mask_babel_object.get_fdata() axial_mask_slice_data = mask_data[58:60, 68:70, 60:62] data_end_time = time.time() data_time = np.int(np.round(data_end_time - data_start_time)) print('Data Loaded ... Time Taken: {}'.format(data_end_time - data_start_time)) print('The Data Dimensions are: {}'.format(subj_data.shape)) #### AxCaliber Begin #### ball = gaussian_models.G1Ball() cylinder = cylinder_models.C4CylinderGaussianPhaseApproximation() gamma_cylinder = distribute_models.DD1GammaDistributed( models=[cylinder]) axcaliber_gamma = modeling_framework.MultiCompartmentModel( models=[ball, gamma_cylinder]) axcaliber_gamma.set_fixed_parameter( 'DD1GammaDistributed_1_C4CylinderGaussianPhaseApproximation_1_lambda_par', 1.7e-9) axcaliber_gamma.set_fixed_parameter( 'DD1GammaDistributed_1_C4CylinderGaussianPhaseApproximation_1_mu', [0, 0]) print('Fitting the AxCaliber Model ...') fit_start_time = time.time() mcdmi_fit = axcaliber_gamma.fit(subj_Acq_Scheme, axial_slice_data, mask=axial_mask_slice_data, solver='mix', maxiter=100, use_parallel_processing=True, number_of_processors=32) fit_end_time = time.time() print('Model Fitting Completed ... Time Taken to fit: {}'.format( fit_end_time - fit_start_time)) fit_time = np.int(np.round(fit_end_time - fit_start_time)) fitted_parameters = mcdmi_fit.fitted_parameters # Get List of Estimated Parameter Names para_Names_list = [] for key, value in fitted_parameters.items(): para_Names_list.append(key) ### Nifti Saving Part # Create a directory per subject subj_method_save_path = os.path.join(subj_save_path, method_name) if os.path.exists(subj_method_save_path) == False: os.mkdir(subj_method_save_path) # Retrieve the affine from already Read Nifti file to form the header affine = subj_babel_object.affine # Loop over fitted parameters name list for each_fitted_parameter in para_Names_list: new_img = nib.Nifti1Image(fitted_parameters[each_fitted_parameter], affine) # Form the file path f_name = each_fitted_parameter + '.nii.gz' param_file_path = os.path.join(subj_method_save_path, f_name) nib.save(new_img, param_file_path) print('debug here') return None
def main(): # Plot Save Path base_plot_path = r'/nfs/masi/nathv/py_src_code_2020/dmipy_model_pictures' base_plot_path = os.path.normpath(base_plot_path) # Method Saving Paths base_save_path = r'/nfs/masi/nathv/miccai_2020/micro_methods_hcp_mini' base_save_path = os.path.normpath(base_save_path) # Create base saving path for Method # TODO The Method name can be made an argument later on method_name = 'NODDI_WATSON' # Base HCP Data Path base_data_path = r'/nfs/HCP/data' base_data_path = os.path.normpath(base_data_path) # Subject ID's list #subj_ID_List = ['125525', '118225', '116726'] subj_ID_List = ['115017', '114823', '116726', '118225'] # TODO When needed loop here over the ID list for subj_ID in subj_ID_List: # Subject Save Path subj_save_path = os.path.join(base_save_path, subj_ID) if os.path.exists(subj_save_path) == False: os.mkdir(subj_save_path) # TODO For later the subject data, bval and bvec reading part can be put inside a function subj_data_path = os.path.join(base_data_path, subj_ID, 'T1w', 'Diffusion') # Read the Nifti file, bvals and bvecs subj_bvals = np.loadtxt(os.path.join(subj_data_path, 'bvals')) subj_bvecs = np.loadtxt(os.path.join(subj_data_path, 'bvecs')) all_bvals = subj_bvals * 1e6 all_bvecs = np.transpose(subj_bvecs) subj_Acq_Scheme = acquisition_scheme_from_bvalues(all_bvals, all_bvecs) print(subj_Acq_Scheme.print_acquisition_info) print('Loading the Nifti Data ...') data_start_time = time.time() subj_babel_object = nib.load( os.path.join(subj_data_path, 'data.nii.gz')) subj_data = subj_babel_object.get_fdata() axial_slice_data = subj_data[50:65, 50:65, 60:62, :] data_end_time = time.time() data_time = np.int(np.round(data_end_time - data_start_time)) print('Data Loaded ... Time Taken: {}'.format(data_end_time - data_start_time)) print('The Data Dimensions are: {}'.format(subj_data.shape)) #### NODDI Watson #### ball = gaussian_models.G1Ball() stick = cylinder_models.C1Stick() zeppelin = gaussian_models.G2Zeppelin() watson_dispersed_bundle = SD1WatsonDistributed( models=[stick, zeppelin]) watson_dispersed_bundle.set_tortuous_parameter( 'G2Zeppelin_1_lambda_perp', 'C1Stick_1_lambda_par', 'partial_volume_0') watson_dispersed_bundle.set_equal_parameter('G2Zeppelin_1_lambda_par', 'C1Stick_1_lambda_par') watson_dispersed_bundle.set_fixed_parameter('G2Zeppelin_1_lambda_par', 1.7e-9) NODDI_mod = MultiCompartmentModel( models=[ball, watson_dispersed_bundle]) NODDI_mod.set_fixed_parameter('G1Ball_1_lambda_iso', 3e-9) print('Fitting the NODDI Model ...') fit_start_time = time.time() NODDI_fit_hcp = NODDI_mod.fit(subj_Acq_Scheme, subj_data, mask=subj_data[..., 0] > 0) fit_end_time = time.time() print('Model Fitting Completed ... Time Taken to fit: {}'.format( fit_end_time - fit_start_time)) fit_time = np.int(np.round(fit_end_time - fit_start_time)) fitted_parameters = NODDI_fit_hcp.fitted_parameters para_Names_list = [] for key, value in fitted_parameters.items(): para_Names_list.append(key) ### Nifti Saving Part # Create a directory per subject subj_method_save_path = os.path.join(subj_save_path, method_name) if os.path.exists(subj_method_save_path) == False: os.mkdir(subj_method_save_path) # Retrieve the affine from already Read Nifti file to form the header affine = subj_babel_object.affine # Loop over fitted parameters name list for each_fitted_parameter in para_Names_list: new_img = nib.Nifti1Image(fitted_parameters[each_fitted_parameter], affine) # Form the file path f_name = each_fitted_parameter + '.nii.gz' param_file_path = os.path.join(subj_method_save_path, f_name) nib.save(new_img, param_file_path) return None
def fit_noddi_dmipy(input_dwi, input_bval, input_bvec, input_mask, output_dir, nthreads=1, solver='brute2fine', model_type='WATSON', parallel_diffusivity=1.7e-9, iso_diffusivity=3e-9, bids_fmt=False, bids_id=''): import nibabel as nib from dmipy.signal_models import cylinder_models, gaussian_models from dmipy.distributions.distribute_models import SD1WatsonDistributed, SD2BinghamDistributed from dmipy.core.modeling_framework import MultiCompartmentModel from dmipy.core import modeling_framework from dmipy.core.acquisition_scheme import acquisition_scheme_from_bvalues from dipy.io import read_bvals_bvecs if not os.path.exists(output_dir): os.mkdir(output_dir) #Setup the acquisition scheme bvals, bvecs = read_bvals_bvecs(input_bval, input_bvec) bvals_SI = bvals * 1e6 acq_scheme = acquisition_scheme_from_bvalues(bvals_SI, bvecs) acq_scheme.print_acquisition_info #Load the data img = nib.load(input_dwi) data = img.get_data() #Load the mask img = nib.load(input_mask) mask_data = img.get_data() ball = gaussian_models.G1Ball() #CSF stick = cylinder_models.C1Stick() #Intra-axonal diffusion zeppelin = gaussian_models.G2Zeppelin() #Extra-axonal diffusion if model_type == 'Bingham' or model_type == 'BINGHAM': dispersed_bundle = SD2BinghamDistributed(models=[stick, zeppelin]) else: dispersed_bundle = SD1WatsonDistributed(models=[stick, zeppelin]) dispersed_bundle.set_tortuous_parameter('G2Zeppelin_1_lambda_perp', 'C1Stick_1_lambda_par', 'partial_volume_0') dispersed_bundle.set_equal_parameter('G2Zeppelin_1_lambda_par', 'C1Stick_1_lambda_par') dispersed_bundle.set_fixed_parameter('G2Zeppelin_1_lambda_par', parallel_diffusivity) NODDI_mod = MultiCompartmentModel(models=[ball, dispersed_bundle]) NODDI_mod.set_fixed_parameter('G1Ball_1_lambda_iso', iso_diffusivity) NODDI_fit = NODDI_mod.fit(acq_scheme, data, mask=mask_data, number_of_processors=nthreads, solver=solver) fitted_parameters = NODDI_fit.fitted_parameters if model_type == 'Bingham' or model_type == 'BINGHAM': # get total Stick signal contribution vf_intra = ( fitted_parameters['SD2BinghamDistributed_1_partial_volume_0'] * fitted_parameters['partial_volume_1']) # get total Zeppelin signal contribution vf_extra = ( (1 - fitted_parameters['SD2BinghamDistributed_1_partial_volume_0']) * fitted_parameters['partial_volume_1']) vf_iso = fitted_parameters['partial_volume_0'] odi = fitted_parameters['SD2BinghamDistributed_1_SD2Bingham_1_odi'] else: # get total Stick signal contribution vf_intra = ( fitted_parameters['SD1WatsonDistributed_1_partial_volume_0'] * fitted_parameters['partial_volume_1']) # get total Zeppelin signal contribution vf_extra = ( (1 - fitted_parameters['SD1WatsonDistributed_1_partial_volume_0']) * fitted_parameters['partial_volume_1']) vf_iso = fitted_parameters['partial_volume_0'] odi = fitted_parameters['SD1WatsonDistributed_1_SD1Watson_1_odi'] if bids_fmt: output_odi = output_dir + '/' + bids_id + '_model-NODDI_parameter-ODI.nii.gz' output_vf_intra = output_dir + '/' + bids_id + '_model-NODDI_parameter-ICVF.nii.gz' output_vf_extra = output_dir + '/' + bids_id + '_model-NODDI_parameter-EXVF.nii.gz' output_vf_iso = output_dir + '/' + bids_id + '_model-NODDI_parameter-ISO.nii.gz' else: output_odi = output_dir + '/noddi_ODI.nii.gz' output_vf_intra = output_dir + '/noddi_ICVF.nii.gz' output_vf_extra = output_dir + '/noddi_EXVF.nii.gz' output_vf_iso = output_dir + '/noddi_ISO.nii.gz' #Save the images odi_img = nib.Nifti1Image(odi, img.get_affine(), img.header) odi_img.set_sform(img.get_sform()) odi_img.set_qform(img.get_qform()) nib.save(odi_img, output_odi) icvf_img = nib.Nifti1Image(vf_intra, img.get_affine(), img.header) icvf_img.set_sform(img.get_sform()) icvf_img.set_qform(img.get_qform()) nib.save(icvf_img, output_vf_intra) ecvf_img = nib.Nifti1Image(vf_extra, img.get_affine(), img.header) ecvf_img.set_sform(img.get_sform()) ecvf_img.set_qform(img.get_qform()) nib.save(ecvf_img, output_vf_extra) iso_img = nib.Nifti1Image(vf_iso, img.get_affine(), img.header) iso_img.set_sform(img.get_sform()) iso_img.set_qform(img.get_qform()) nib.save(iso_img, output_vf_iso)
def main(): # Base Path of all given files for All models are wrong base_path = r'/nfs/masi/nathv/memento_2020/all_models_are_wrong/files_project_2927_session_1436090/' base_path = os.path.normpath(base_path) # Just dealing with PGSE for now pgse_acq_params_path = os.path.join(base_path, 'PGSE_AcqParams.txt') pgse_signal_path = os.path.join(base_path, 'PGSE_Simulations.txt') # Read files via Numpy pgse_acq_params = np.loadtxt(pgse_acq_params_path) pgse_signal_data = np.loadtxt(pgse_signal_path) pgse_example_sub_diff = np.loadtxt( '/nfs/masi/nathv/memento_2020/all_models_are_wrong/files_project_2927_session_1436090/2-AllModelsAreWrong-ExampleSubmissions/DIffusivity-ExampleSubmission3/PGSE.txt' ) pgse_example_sub_volfrac = np.loadtxt( '/nfs/masi/nathv/memento_2020/all_models_are_wrong/files_project_2927_session_1436090/2-AllModelsAreWrong-ExampleSubmissions/VolumeFraction-ExampleSubmission3/PGSE.txt' ) # Transpose the Signal data pgse_signal_data = pgse_signal_data.transpose() # Dissect the acquisition parameters to form the Acquisition Table bvecs = pgse_acq_params[:, 1:4] bvals = pgse_acq_params[:, 6] * 1e6 grad_str = pgse_acq_params[:, 0] small_del = pgse_acq_params[:, 4] big_del = pgse_acq_params[:, 5] subj_Acq_Scheme = acquisition_scheme_from_bvalues(bvals, bvecs, delta=small_del, Delta=big_del) print(subj_Acq_Scheme.print_acquisition_info) #### NODDI Watson #### ball = gaussian_models.G1Ball() stick = cylinder_models.C1Stick() zeppelin = gaussian_models.G2Zeppelin() bingham_dispersed_bundle = SD2BinghamDistributed(models=[stick, zeppelin]) bingham_dispersed_bundle.set_tortuous_parameter('G2Zeppelin_1_lambda_perp', 'C1Stick_1_lambda_par', 'partial_volume_0') bingham_dispersed_bundle.set_equal_parameter('G2Zeppelin_1_lambda_par', 'C1Stick_1_lambda_par') bingham_dispersed_bundle.set_fixed_parameter('G2Zeppelin_1_lambda_par', 1.7e-9) NODDI_bingham_mod = MultiCompartmentModel( models=[ball, bingham_dispersed_bundle]) NODDI_bingham_mod.set_fixed_parameter('G1Ball_1_lambda_iso', 3e-9) print('Fitting the NODDI Model ...') fit_start_time = time.time() NODDI_fit_hcp = NODDI_bingham_mod.fit(subj_Acq_Scheme, pgse_signal_data, use_parallel_processing=True, number_of_processors=8) fit_end_time = time.time() print('Model Fitting Completed ... Time Taken to fit: {}'.format( fit_end_time - fit_start_time)) fit_time = np.int(np.round(fit_end_time - fit_start_time)) sub_1_pv0 = NODDI_fit_hcp.fitted_parameters['partial_volume_0'] sub_2_pv1 = NODDI_fit_hcp.fitted_parameters['partial_volume_1'] np.savetxt('noddi_bingham_pv0.txt', sub_1_pv0) np.savetxt('noddi_bingham_pv1.txt', sub_2_pv1) print('Debug here') return None
def __init__(self): self.stick = cylinder_models.C1Stick() self.ball = gaussian_models.G1Ball() self.zeppelin = gaussian_models.G2Zeppelin()