def test_estimate_radius_with_rtap(radius_gt=5e-3): gtab = get_gtab_taiwan_dsi() tau = 1 / (4 * np.pi**2) # we estimate the infinite diffusion time case for a perfectly reflecting # cylinder using the Callaghan model E = cylinders_and_ball_soderman(gtab, tau, radii=[radius_gt], snr=None, angles=[(0, 90)], fractions=[100])[0] # estimate radius using anisotropic MAP-MRI. mapmod = mapmri.MapmriModel(gtab, radial_order=6, laplacian_regularization=True, laplacian_weighting=0.01) mapfit = mapmod.fit(E) radius_estimated = np.sqrt(1 / (np.pi * mapfit.rtap())) assert_almost_equal(radius_estimated, radius_gt, 5) # estimate radius using isotropic MAP-MRI. # note that the radial order is higher and the precision is lower due to # less accurate signal extrapolation. mapmod = mapmri.MapmriModel(gtab, radial_order=8, laplacian_regularization=True, laplacian_weighting=0.01, anisotropic_scaling=False) mapfit = mapmod.fit(E) radius_estimated = np.sqrt(1 / (np.pi * mapfit.rtap())) assert_almost_equal(radius_estimated, radius_gt, 4)
def compute_RTOP(dwi_, mask_, bvals_, bvecs_): if path.exists(dwi_) and path.exists(mask_) and path.exists( bvals_) and path.exists(bvecs_): dwi = nib.load(dwi_) mask = nib.load(mask_) bvals = np.loadtxt(bvals_) * units.s / units.mm**2 bvecs = np.loadtxt(bvecs_) else: print("One of the files does not exist, exit.") exit() dwi_data = dwi.get_data() mask_data = mask.get_data() nodif = dwi.slicer[:, :, :, 0] gtab = gradients.gradient_table_from_bvals_bvecs(bvals, bvecs, b0_threshold=5) map_model = mapmri.MapmriModel(gtab, laplacian_regularization=True, laplacian_weighting=0.2) map_fit = map_model.fit(dwi_data, mask=mask_data) rtop = map_fit.rtop() rtop_cortex_norm = rtop / rtop[mask_data != 0].mean() rtop_nii = nib.Nifti1Image(rtop, affine=dwi.affine) rtop_cortex_norm_nii = nib.Nifti1Image(rtop_cortex_norm, affine=dwi.affine) rtop_nii.to_filename('RTOP_cortex.nii.gz') rtop_cortex_norm_nii.to_filename('RTOP_cortex_norm.nii.gz') return
def make_predictions(gtab, data, anisotropic_scaling=True): map_model = mapmri.MapmriModel(gtab['train'], positivity_constraint=True, laplacian_weighting='GCV', radial_order=8, anisotropic_scaling=anisotropic_scaling) mapfit = map_model.fit(data['train']) fitted_data = mapfit.fitted_signal() pred = mapfit.predict(gtab['test']) return fitted_data, pred
smoothness over the entire signal, including the extrapolation beyond the measured signal. In practice this results in, but does not guarantee positive solutions of the diffusion propagator. The positivity constraint guarantees a positive solution in a set of discrete points, which in general results in smooth solutions, but does not guarantee it. A suggested strategy is to use a low Laplacian weight together with the positivity constraint. In this way both desired properties are guaranteed in the final solution. For now we will generate the anisotropic models for all combinations. """ radial_order = 6 map_model_laplacian_aniso = mapmri.MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=.2) map_model_positivity_aniso = mapmri.MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=False, positivity_constraint=True) map_model_both_aniso = mapmri.MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=.05, positivity_constraint=True) """ Note that when we use only Laplacian regularization, the "GCV" option may select very low regularization weights in very anisotropic white matter such
def _run_interface(self, runtime): gtab = self._get_gtab() dwi_img = nb.load(self.inputs.dwi_file) data = dwi_img.get_fdata(dtype=np.float32) mask_img, mask_array = self._get_mask(dwi_img, gtab) weighting = "GCV" if self.inputs.laplacian_weighting == "GCV" else \ self.inputs.laplacian_weighting if self.inputs.laplacian_regularization and \ self.inputs.positivity_constraint: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=self.inputs.radial_order, laplacian_regularization=True, laplacian_weighting=weighting, positivity_constraint=True, bval_threshold=self.inputs.b0_threshold, anisotropic_scaling=self.inputs.anisotropic_scaling) elif self.inputs.positivity_constraint: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=self.inputs.radial_order, laplacian_regularization=False, positivity_constraint=True, bval_threshold=self.inputs.b0_threshold, anisotropic_scaling=self.inputs.anisotropic_scaling) elif self.inputs.laplacian_regularization: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=self.inputs.radial_order, laplacian_regularization=True, laplacian_weighting=weighting, bval_threshold=self.inputs.b0_threshold, anisotropic_scaling=self.inputs.anisotropic_scaling) else: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=self.inputs.radial_order, laplacian_regularization=False, positivity_constraint=False, bval_threshold=self.inputs.b0_threshold, anisotropic_scaling=self.inputs.anisotropic_scaling) LOGGER.info("Fitting MAPMRI Model.") mapfit_aniso = map_model_aniso.fit(data, mask=mask_array) rtop = mapfit_aniso.rtop() self._results['rtop'] = self._save_scalar(rtop, "_rtop", runtime, dwi_img) ll = mapfit_aniso.norm_of_laplacian_signal() self._results['lapnorm'] = self._save_scalar(ll, "_lapnorm", runtime, dwi_img) m = mapfit_aniso.msd() self._results['msd'] = self._save_scalar(m, "_msd", runtime, dwi_img) q = mapfit_aniso.qiv() self._results['qiv'] = self._save_scalar(q, "_qiv", runtime, dwi_img) rtap = mapfit_aniso.rtap() self._results['rtap'] = self._save_scalar(rtap, "_rtap", runtime, dwi_img) rtpp = mapfit_aniso.rtpp() self._results['rtpp'] = self._save_scalar(rtpp, "_rtpp", runtime, dwi_img) coeffs = mapfit_aniso.mapmri_coeff self._results['mapmri_coeffs'] = self._save_scalar( coeffs, "_mapcoeffs", runtime, dwi_img) # Write DSI Studio or MRtrix self._write_external_formats(runtime, mapfit_aniso, mask_img, "_MAPMRI") return runtime
def run(self, data_files, bvals_files, bvecs_files, small_delta, big_delta, b0_threshold=50.0, laplacian=True, positivity=True, bval_threshold=2000, save_metrics=[], laplacian_weighting=0.05, radial_order=6, out_dir='', out_rtop='rtop.nii.gz', out_lapnorm='lapnorm.nii.gz', out_msd='msd.nii.gz', out_qiv='qiv.nii.gz', out_rtap='rtap.nii.gz', out_rtpp='rtpp.nii.gz', out_ng='ng.nii.gz', out_perng='perng.nii.gz', out_parng='parng.nii.gz'): """Workflow for fitting the MAPMRI model (with optional Laplacian regularization). Generates rtop, lapnorm, msd, qiv, rtap, rtpp, non-gaussian (ng), parallel ng, perpendicular ng saved in a nifti format in input files provided by `data_files` and saves the nifti files to an output directory specified by `out_dir`. In order for the MAPMRI workflow to work in the way intended either the Laplacian or positivity or both must be set to True. Parameters ---------- data_files : string Path to the input volume. bvals_files : string Path to the bval files. bvecs_files : string Path to the bvec files. small_delta : float Small delta value used in generation of gradient table of provided bval and bvec. big_delta : float Big delta value used in generation of gradient table of provided bval and bvec. b0_threshold : float, optional Threshold used to find b0 volumes. laplacian : bool, optional Regularize using the Laplacian of the MAP-MRI basis. positivity : bool, optional Constrain the propagator to be positive. bval_threshold : float, optional Sets the b-value threshold to be used in the scale factor estimation. In order for the estimated non-Gaussianity to have meaning this value should set to a lower value (b<2000 s/mm^2) such that the scale factors are estimated on signal points that reasonably represent the spins at Gaussian diffusion. save_metrics : variable string, optional List of metrics to save. Possible values: rtop, laplacian_signal, msd, qiv, rtap, rtpp, ng, perng, parng laplacian_weighting : float, optional Weighting value used in fitting the MAPMRI model in the Laplacian and both model types. radial_order : unsigned int, optional Even value used to set the order of the basis. out_dir : string, optional Output directory. (default: current directory) out_rtop : string, optional Name of the rtop to be saved. out_lapnorm : string, optional Name of the norm of Laplacian signal to be saved. out_msd : string, optional Name of the msd to be saved. out_qiv : string, optional Name of the qiv to be saved. out_rtap : string, optional Name of the rtap to be saved. out_rtpp : string, optional Name of the rtpp to be saved. out_ng : string, optional Name of the Non-Gaussianity to be saved. out_perng : string, optional Name of the Non-Gaussianity perpendicular to be saved. out_parng : string, optional Name of the Non-Gaussianity parallel to be saved. """ io_it = self.get_io_iterator() for (dwi, bval, bvec, out_rtop, out_lapnorm, out_msd, out_qiv, out_rtap, out_rtpp, out_ng, out_perng, out_parng) in io_it: logging.info('Computing MAPMRI metrics for {0}'.format(dwi)) data, affine = load_nifti(dwi) bvals, bvecs = read_bvals_bvecs(bval, bvec) if b0_threshold < bvals.min(): warn("b0_threshold (value: {0}) is too low, increase your " "b0_threshold. It should be higher than the first b0 " "value({1}).".format(b0_threshold, bvals.min())) gtab = gradient_table(bvals=bvals, bvecs=bvecs, small_delta=small_delta, big_delta=big_delta, b0_threshold=b0_threshold) if not save_metrics: save_metrics = [ 'rtop', 'laplacian_signal', 'msd', 'qiv', 'rtap', 'rtpp', 'ng', 'perng', 'parng' ] if laplacian and positivity: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=laplacian_weighting, positivity_constraint=True, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) elif positivity: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=False, positivity_constraint=True, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) elif laplacian: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=laplacian_weighting, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) else: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=False, positivity_constraint=False, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) # for name, fname, func in [('rtop', out_rtop, mapfit_aniso.rtop), # ]: # if name in save_metrics: # r = func() # save_nifti(fname, r.astype(np.float32), affine) if 'rtop' in save_metrics: r = mapfit_aniso.rtop() save_nifti(out_rtop, r.astype(np.float32), affine) if 'laplacian_signal' in save_metrics: ll = mapfit_aniso.norm_of_laplacian_signal() save_nifti(out_lapnorm, ll.astype(np.float32), affine) if 'msd' in save_metrics: m = mapfit_aniso.msd() save_nifti(out_msd, m.astype(np.float32), affine) if 'qiv' in save_metrics: q = mapfit_aniso.qiv() save_nifti(out_qiv, q.astype(np.float32), affine) if 'rtap' in save_metrics: r = mapfit_aniso.rtap() save_nifti(out_rtap, r.astype(np.float32), affine) if 'rtpp' in save_metrics: r = mapfit_aniso.rtpp() save_nifti(out_rtpp, r.astype(np.float32), affine) if 'ng' in save_metrics: n = mapfit_aniso.ng() save_nifti(out_ng, n.astype(np.float32), affine) if 'perng' in save_metrics: n = mapfit_aniso.ng_perpendicular() save_nifti(out_perng, n.astype(np.float32), affine) if 'parng' in save_metrics: n = mapfit_aniso.ng_parallel() save_nifti(out_parng, n.astype(np.float32), affine) logging.info('MAPMRI saved in {0}'.format( os.path.dirname(out_dir)))
def run(self, data_file, data_bvals, data_bvecs, small_delta, big_delta, b0_threshold=0.0, laplacian=True, positivity=True, bval_threshold=2000, save_metrics=[], laplacian_weighting=0.05, radial_order=6, out_dir='', out_rtop='rtop.nii.gz', out_lapnorm='lapnorm.nii.gz', out_msd='msd.nii.gz', out_qiv='qiv.nii.gz', out_rtap='rtap.nii.gz', out_rtpp='rtpp.nii.gz', out_ng='ng.nii.gz', out_perng='perng.nii.gz', out_parng='parng.nii.gz'): """ Workflow for fitting the MAPMRI model (with optional Laplacian regularization). Generates rtop, lapnorm, msd, qiv, rtap, rtpp, non-gaussian (ng), parallel ng, perpendicular ng saved in a nifti format in input files provided by `data_file` and saves the nifti files to an output directory specified by `out_dir`. In order for the MAPMRI workflow to work in the way intended either the laplacian or positivity or both must be set to True. Parameters ---------- data_file : string Path to the input volume. data_bvals : string Path to the bval files. data_bvecs : string Path to the bvec files. small_delta : float Small delta value used in generation of gradient table of provided bval and bvec. big_delta : float Big delta value used in generation of gradient table of provided bval and bvec. b0_threshold : float, optional Threshold used to find b=0 directions (default 0.0) laplacian : bool Regularize using the Laplacian of the MAP-MRI basis (default True) positivity : bool Constrain the propagator to be positive. (default True) bval_threshold : float Sets the b-value threshold to be used in the scale factor estimation. In order for the estimated non-Gaussianity to have meaning this value should set to a lower value (b<2000 s/mm^2) such that the scale factors are estimated on signal points that reasonably represent the spins at Gaussian diffusion. (default: 2000) save_metrics : list of strings List of metrics to save. Possible values: rtop, laplacian_signal, msd, qiv, rtap, rtpp, ng, perng, parng (default: [] (all)) laplacian_weighting : float Weighting value used in fitting the MAPMRI model in the laplacian and both model types. (default: 0.05) radial_order : unsigned int Even value used to set the order of the basis (default: 6) out_dir : string, optional Output directory (default: input file directory) out_rtop : string, optional Name of the rtop to be saved out_lapnorm : string, optional Name of the norm of laplacian signal to be saved out_msd : string, optional Name of the msd to be saved out_qiv : string, optional Name of the qiv to be saved out_rtap : string, optional Name of the rtap to be saved out_rtpp : string, optional Name of the rtpp to be saved out_ng : string, optional Name of the Non-Gaussianity to be saved out_perng : string, optional Name of the Non-Gaussianity perpendicular to be saved out_parng : string, optional Name of the Non-Gaussianity parallel to be saved """ io_it = self.get_io_iterator() for (dwi, bval, bvec, out_rtop, out_lapnorm, out_msd, out_qiv, out_rtap, out_rtpp, out_ng, out_perng, out_parng) in io_it: logging.info('Computing MAPMRI metrics for {0}'.format(dwi)) img = nib.load(dwi) data = img.get_data() affine = img.affine bvals, bvecs = read_bvals_bvecs(bval, bvec) gtab = gradient_table(bvals=bvals, bvecs=bvecs, small_delta=small_delta, big_delta=big_delta, b0_threshold=b0_threshold) if not save_metrics: save_metrics = ['rtop', 'laplacian_signal', 'msd', 'qiv', 'rtap', 'rtpp', 'ng', 'perng', 'parng'] if laplacian and positivity: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=laplacian_weighting, positivity_constraint=True, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) elif positivity: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=False, positivity_constraint=True, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) elif laplacian: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting=laplacian_weighting, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) else: map_model_aniso = mapmri.MapmriModel( gtab, radial_order=radial_order, laplacian_regularization=False, positivity_constraint=False, bval_threshold=bval_threshold) mapfit_aniso = map_model_aniso.fit(data) if 'rtop' in save_metrics: r = mapfit_aniso.rtop() rtop = nib.nifti1.Nifti1Image(r.astype(np.float32), affine) nib.save(rtop, out_rtop) if 'laplacian_signal' in save_metrics: ll = mapfit_aniso.norm_of_laplacian_signal() lap = nib.nifti1.Nifti1Image(ll.astype(np.float32), affine) nib.save(lap, out_lapnorm) if 'msd' in save_metrics: m = mapfit_aniso.msd() msd = nib.nifti1.Nifti1Image(m.astype(np.float32), affine) nib.save(msd, out_msd) if 'qiv' in save_metrics: q = mapfit_aniso.qiv() qiv = nib.nifti1.Nifti1Image(q.astype(np.float32), affine) nib.save(qiv, out_qiv) if 'rtap' in save_metrics: r = mapfit_aniso.rtap() rtap = nib.nifti1.Nifti1Image(r.astype(np.float32), affine) nib.save(rtap, out_rtap) if 'rtpp' in save_metrics: r = mapfit_aniso.rtpp() rtpp = nib.nifti1.Nifti1Image(r.astype(np.float32), affine) nib.save(rtpp, out_rtpp) if 'ng' in save_metrics: n = mapfit_aniso.ng() ng = nib.nifti1.Nifti1Image(n.astype(np.float32), affine) nib.save(ng, out_ng) if 'perng' in save_metrics: n = mapfit_aniso.ng_perpendicular() ng = nib.nifti1.Nifti1Image(n.astype(np.float32), affine) nib.save(ng, out_perng) if 'parng' in save_metrics: n = mapfit_aniso.ng_parallel() ng = nib.nifti1.Nifti1Image(n.astype(np.float32), affine) nib.save(ng, out_parng) logging.info('MAPMRI saved in {0}'. format(os.path.dirname(out_dir)))
data_path = os.path.normpath(data_path) bval_path = os.path.normpath(bval_path) bvec_path = os.path.normpath(bvec_path) small_delta = 0.008 #in seconds big_delta = 0.02 #in seconds bvals, bvecs = read_bvals_bvecs(bval_path, bvec_path) gtab = gradient_table(bvals, bvecs, big_delta=big_delta, small_delta=small_delta) print('Gradient Table Loaded and Ready for use \n') data = loadmat(data_path) actual_data = data['ms_data'] print('Data all loaded and ready for use \n') print('data.shape (%d, %d)' % actual_data.shape) radial_order = 6 map_model_both_aniso = mapmri.MapmriModel(gtab, radial_order=radial_order, laplacian_regularization=True, laplacian_weighting='GCV', positivity_constraint=True) mapfit_both_aniso = map_model_both_aniso.fit(actual_data) print('MapMri signal coefficients estimated')