예제 #1
0
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)
예제 #2
0
def test_cylinder_gaussian_phase_profile_narrow_pulse_not_restricted(
        samples=100):
    # the Gaussian Phase model approaches the following equation
    # when delta << tau according to Eq. (13) in VanGelderen et al:
    # np.exp(-2 * (gamma * G * delta * lambda_perp) ** 2)
    mu = [0, 0]
    lambda_par = .1
    diameter = 1e-4
    diffusion_perpendicular = 1.7e-09

    delta = np.tile(1e-3, samples)  # delta towards zero
    Delta = np.tile(1e-3, samples)  # Delta towards infinity
    qvals_perp = np.linspace(0, 3e5, samples)
    n_perp = np.tile(np.r_[1., 0., 0.], (samples, 1))
    scheme = acquisition_scheme_from_qvalues(qvals_perp, n_perp, delta, Delta)

    vangelderen = (cylinder_models.C4CylinderGaussianPhaseApproximation(
        mu=mu, lambda_par=lambda_par, diameter=diameter))
    E_vangelderen = vangelderen(scheme)
    E_free_diffusion = np.exp(-scheme.bvalues * diffusion_perpendicular)
    assert_equal(np.max(np.abs(E_vangelderen - E_free_diffusion)) < 0.01, True)
예제 #3
0
def test_C4_watson_gamma_equals_gamma_watson():
    scheme = wu_minn_hcp_acquisition_scheme()

    cylinder = cylinder_models.C4CylinderGaussianPhaseApproximation()
    watsoncyl = distribute_models.SD1WatsonDistributed([cylinder])

    gammawatsoncyl = distribute_models.DD1GammaDistributed(
        [watsoncyl],
        target_parameter='C4CylinderGaussianPhaseApproximation_1_diameter')

    param = 'SD1WatsonDistributed_1_C4CylinderGaussianPhaseApproximation'
    param += '_1_lambda_par'

    params1 = {
        param: 1.7e-9,
        'DD1Gamma_1_alpha': 2.,
        'DD1Gamma_1_beta': 4e-6,
        'SD1WatsonDistributed_1_SD1Watson_1_odi': 0.4,
        'SD1WatsonDistributed_1_SD1Watson_1_mu': [0., 0.]
    }
    gammacyl = distribute_models.DD1GammaDistributed([cylinder])
    watsongammacyl = distribute_models.SD1WatsonDistributed(
        [gammacyl],
        target_parameter='C4CylinderGaussianPhaseApproximation_1_mu')

    param = 'DD1GammaDistributed_1_C4CylinderGaussianPhaseApproximation'
    param += '_1_lambda_par'

    params2 = {
        param: 1.7e-9,
        'DD1GammaDistributed_1_DD1Gamma_1_alpha': 2.,
        'DD1GammaDistributed_1_DD1Gamma_1_beta': 4e-6,
        'SD1Watson_1_odi': 0.4,
        'SD1Watson_1_mu': [0., 0.]
    }

    assert_array_almost_equal(watsongammacyl(scheme, **params2),
                              gammawatsoncyl(scheme, **params1), 5)
예제 #4
0
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
예제 #5
0
    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

    # create output folder