Beispiel #1
0
    def __init__(self, model, model_params):
        """ Initialize a FreeWaterTensorFit class instance.
        Since the free water tensor model is an extension of DTI, class
        instance is defined as subclass of the TensorFit from dti.py

        Parameters
        ----------
        model : FreeWaterTensorModel Class instance
            Class instance containing the free water tensor model for the fit
        model_params : ndarray (x, y, z, 13) or (n, 13)
            All parameters estimated from the free water tensor model.
            Parameters are ordered as follows:
                1) Three diffusion tensor's eigenvalues
                2) Three lines of the eigenvector matrix each containing the
                   first, second and third coordinates of the eigenvector
                3) The volume fraction of the free water compartment

        References
        ----------
        .. [1] Henriques, R.N., Rokem, A., Garyfallidis, E., St-Jean, S.,
               Peterson E.T., Correia, M.M., 2017. [Re] Optimization of a free
               water elimination two-compartment model for diffusion tensor
               imaging. ReScience volume 3, issue 1, article number 2
        """
        TensorFit.__init__(self, model, model_params)
Beispiel #2
0
    def __init__(self, model, model_params):
        """ Initialize a FreeWaterTensorFit class instance.
        Since the free water tensor model is an extension of DTI, class
        instance is defined as subclass of the TensorFit from dti.py

        Parameters
        ----------
        model : FreeWaterTensorModel Class instance
            Class instance containing the free water tensor model for the fit
        model_params : ndarray (x, y, z, 13) or (n, 13)
            All parameters estimated from the free water tensor model.
            Parameters are ordered as follows:
                1) Three diffusion tensor's eigenvalues
                2) Three lines of the eigenvector matrix each containing the
                   first, second and third coordinates of the eigenvector
                3) The volume fraction of the free water compartment
        """
        TensorFit.__init__(self, model, model_params)
Beispiel #3
0
    def __init__(self, model, model_params):
        """ Initialize a FreeWaterTensorFit class instance.
        Since the free water tensor model is an extension of DTI, class
        instance is defined as subclass of the TensorFit from dti.py

        Parameters
        ----------
        model : FreeWaterTensorModel Class instance
            Class instance containing the free water tensor model for the fit
        model_params : ndarray (x, y, z, 13) or (n, 13)
            All parameters estimated from the free water tensor model.
            Parameters are ordered as follows:
                1) Three diffusion tensor's eigenvalues
                2) Three lines of the eigenvector matrix each containing the
                   first, second and third coordinates of the eigenvector
                3) The volume fraction of the free water compartment
        """
        TensorFit.__init__(self, model, model_params)
Beispiel #4
0
    def __init__(self, model, model_params):
        """ Initialize a DiffusionKurtosisFit class instance.

        Since DKI is an extension of DTI, class instance is defined as subclass
        of the TensorFit from dti.py

        Parameters
        ----------
        model : DiffusionKurtosisModel Class instance
            Class instance containing the Diffusion Kurtosis Model for the fit
        model_params : ndarray (x, y, z, 27) or (n, 27)
            All parameters estimated from the diffusion kurtosis model.
            Parameters are ordered as follow:
                1) Three diffusion tensor's eingenvalues
                2) Three lines of the eigenvector matrix each containing the
                   first, second and third coordinates of the eigenvector
                3) Fifteen elements of the kurtosis tensor
        """
        TensorFit.__init__(self, model, model_params)
Beispiel #5
0
    def __init__(self, model, model_params):
        """ Initialize a DiffusionKurtosisFit class instance.

        Since DKI is an extension of DTI, class instance is defined as subclass
        of the TensorFit from dti.py

        Parameters
        ----------
        model : DiffusionKurtosisModel Class instance
            Class instance containing the Diffusion Kurtosis Model for the fit
        model_params : ndarray (x, y, z, 27) or (n, 27)
            All parameters estimated from the diffusion kurtosis model.
            Parameters are ordered as follow:
                1) Three diffusion tensor's eingenvalues
                2) Three lines of the eigenvector matrix each containing the
                   first, second and third coordinates of the eigenvector
                3) Fifteen elements of the kurtosis tensor
        """
        TensorFit.__init__(self, model, model_params)
Beispiel #6
0
    def __init__(self, model, model_params):
        self.model = model
        self.model_params = model_params
        perfusion_params = np.zeros((self.model_params.shape[0], 12))
        diffusion_params = np.zeros((self.model_params.shape[0], 12))
        self.perfusion_fraction = np.zeros(self.model_params.shape[0])
        for vox in range(self.model_params.shape[0]):
            self.perfusion_fraction[vox] = self.model_params[vox, 0]
            tensor_evecs, tensor_evals = _reconstruct_tensor(
                *self.model_params[vox, 1:7])
            diffusion_params[vox] = np.hstack(
                [tensor_evals, tensor_evecs.ravel()])
            perfusion_evecs, perfusion_evals = _reconstruct_tensor(
                *self.model_params[vox, 7:])
            perfusion_params[vox] = np.hstack(
                [perfusion_evals, perfusion_evecs.ravel()])

        self.diffusion_fit = TensorFit(self.model.diffusion_model,
                                       diffusion_params)
        self.perfusion_fit = TensorFit(self.model.perfusion_model,
                                       perfusion_params)
def execution(self, context):

    tensor_coeff = aims.read(self.tensor_coefficients.fullPath())
    tensor_params = np.asarray(tensor_coeff)
    tensor_model = load(self.tensor_model.fullPath())
    gtab = tensor_model.gtab
    #Loading base signal
    S0 = aims.read(self.S0_signal.fullPath())
    S0 = vol_to_array(S0)

    tenfit = TensorFit(tensor_model, tensor_params)
    pred_sign = tenfit.predict(gtab=gtab, S0=S0)
    hdr = tensor_coeff.header()
    pred_vol = array_to_vol(pred_sign, header=hdr)
    aims.write(pred_vol, self.predicted_signal.fullPath())

    #Handling metada
    transformManager = getTransformationManager()
    transformManager.copyReferential(self.predicted_signal,
                                     self.tensor_coefficients)

    context.write("Process finish successfully")
    pass
def execution(self, context):

    #if an existing tensor has already been fitted dont compute a new one .
    if self.tensor_coefficients is not None and self.tensor_model is not None:
        context.write('Fitted Tensor already exists ! Let s use it !')
        tensor_coeff_vol = aims.read(self.tensor_coefficients.fullPath())
        tensor_coeff = np.asarray(tensor_coeff_vol)
        hdr = tensor_coeff_vol.header()
        tensor_model = load(self.tensor_model.fullPath())
        tenfit = TensorFit(tensor_model, tensor_coeff)
        if self.mask is not None:
            mask_vol = aims.read(self.mask.fullPath())
            mask = vol_to_array(mask_vol)
            mask = array_to_mask(mask)
        else:
            context.write(
                'No mask provided ! Estimating impulsionnal response from the whole volume or brain is not really accurate ! A default mask based on Fractionnal Anisotropy is  computed. '
            )
            fa = tenfit.fa
            # just to avoid nan is case of wrong fitting
            fa = np.clip(fa, 0, 1)
            #high FA vale is associated with single fiber direction voxel
            mask = fa > self.fa_threshold
            mask = mask.astype(bool)
        #code extracted from dipy  response_from_mask function
        indices = np.where(mask > 0)
        sub_tenfit = tenfit[indices]
        lambdas = sub_tenfit.evals[:, :2]
        gtab = sub_tenfit.model.gtab
        vol = aims.read(self.diffusion_data.fullPath())
        data = np.asarray(vol)
        S0s = data[indices][:, np.nonzero(gtab.b0s_mask)[0]]
        response, ratio = _get_response(S0s, lambdas)

    else:
        context.write('No Tensor Fitted Yet! Compute a new one')

        gtab = load(self.gradient_table.fullPath())

        if is_multi_shell(gtab):
            context.warning(
                "The DWI scheme for this data is multishell: bvalues",
                shells(gtab),
                ". CSD implementation used in Diffuse currently only handle single shell DWI scheme. By default the higher shell bval",
                max_shell(gtab), " is selected")
            context.warning(
                "Even if only the outer shell is use for deconvolution, the following estimation method will use the full DWI scheme for response estimation. It might be inaccurate  if the deconvolved shell bvalue is too high (b5000)"
            )

        vol = aims.read(self.diffusion_data.fullPath())
        data = np.asarray(vol)

        if self.mask is not None:
            mask_vol = aims.read(self.mask.fullPath())
            mask = vol_to_array(mask_vol)
            mask = array_to_mask(mask)
            response, ratio = response_from_mask(gtab, data, mask)
        else:
            context.warning(
                "No mask provided ! Compute a high-FA based mask:  FA higher than  "
                + str(self.fa_threshold) +
                " are considered as single direction voxels")
            #default tensor model --> we dont store it for now
            tensor = TensorModel(gtab)
            #whole volume fit
            tenfit = tensor.fit(data)
            fa = tenfit.fa
            # just to avoid nan is case of wrong fitting
            fa = np.clip(fa, 0, 1)
            # high FA vale is associated with single fiber direction voxel
            mask = fa > self.fa_threshold
            mask = mask.astype(bool)
            indices = np.where(mask)
            # code extracted from dipy  response_from_mask function
            sub_tenfit = tenfit[indices]
            lambdas = sub_tenfit.evals[:, :2]
            gtab = sub_tenfit.model.gtab
            vol = aims.read(self.diffusion_data.fullPath())
            data = np.asarray(vol)
            S0s = data[indices][:, np.nonzero(gtab.b0s_mask)[0]]
            response, ratio = _get_response(S0s, lambdas)

    #store the response
    dump(response, self.response.fullPath())
def execution(self, context):

    tensor_coeff_vol = aims.read(self.tensor_coefficients.fullPath())
    tensor_coeff = np.asarray(tensor_coeff_vol)
    hdr = tensor_coeff_vol.header()

    tensor_model = load(self.tensor_model.fullPath())

    tenfit = TensorFit(tensor_model, tensor_coeff)
    #Mandatory parameters
    fa = tenfit.fa
    FA = array_to_vol(fa, hdr)
    aims.write(FA, self.fractionnal_anisotropy.fullPath())
    md = tenfit.md
    MD = array_to_vol(md, hdr)
    aims.write(MD, self.mean_diffusivity.fullPath())
    evecs = tenfit.evecs
    vectors = [
        evecs[:, :, :, :, 0], evecs[:, :, :, :, 1], evecs[:, :, :, :, 2]
    ]
    evals = tenfit.evals

    eigen_values = array_to_vol(evals, hdr)
    aims.write(eigen_values, self.evals.fullPath())

    vectors_volume = [array_to_vol(v, hdr) for v in vectors]

    aims.write(vectors_volume[0], self.first_eigen_vector.fullPath())
    aims.write(vectors_volume[1], self.second_eigen_vector.fullPath())
    aims.write(vectors_volume[2], self.third_eigen_vector.fullPath())

    color_fa = tenfit.color_fa
    color_fa_vol = array_to_vol(color_fa)
    aims.write(color_fa_vol, self.colored_fractionnal_anisotropy.fullPath())
    # handling referentials
    transformManager = getTransformationManager()
    transformManager.copyReferential(self.tensor_coefficients,
                                     self.fractionnal_anisotropy)
    transformManager.copyReferential(self.tensor_coefficients,
                                     self.mean_diffusivity)
    # additionnal metadata
    self.mean_diffusivity.setMinf('tensor_coefficients_uuid',
                                  self.tensor_coefficients.uuid())
    self.fractionnal_anisotropy.setMinf('tensor_coefficients_uuid',
                                        self.tensor_coefficients.uuid())

    if self.advanced_indices:
        axial_diffusivity = tenfit.ad
        planarity = tenfit.planarity
        sphericity = tenfit.sphericity
        linearity = tenfit.linearity
        mode = tenfit.mode

        disk_items = [
            self.axial_diffusivity, self.planarity, self.sphericity,
            self.linearity, self.mode
        ]
        arrays = [axial_diffusivity, planarity, sphericity, linearity, mode]

        for ind, a in enumerate(arrays):
            vol = array_to_vol(a, hdr)
            aims.write(vol, disk_items[ind].fullPath())
            transformManager.copyReferential(self.tensor_coefficients,
                                             disk_items[ind])

    pass
Beispiel #10
0
def _dti_fit(row):
    dti_params_file = _dti(row)
    dti_params = nib.load(dti_params_file).get_data()
    tm = TensorModel(row['gtab'])
    tf = TensorFit(tm, dti_params)
    return tf