def test_csd_xval(): # First, let's see that it works with some data: data = nib.load(fdata).get_data()[1:3, 1:3, 1:3] # Make it *small* gtab = gt.gradient_table(fbval, fbvec) S0 = np.mean(data[..., gtab.b0s_mask]) response = ([0.0015, 0.0003, 0.0001], S0) csdm = csd.ConstrainedSphericalDeconvModel(gtab, response) kf_xval = xval.kfold_xval(csdm, data, 2, response, sh_order=2) # In simulation, it should work rather well (high COD): psphere = dpd.get_sphere('symmetric362') bvecs = np.concatenate(([[0, 0, 0]], psphere.vertices)) bvals = np.zeros(len(bvecs)) + 1000 bvals[0] = 0 gtab = gt.gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0001], [0.0015, 0.0003, 0.0003])) mevecs = [ np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) ] S0 = 100 S = sims.single_tensor(gtab, S0, mevals[0], mevecs[0], snr=None) sm = csd.ConstrainedSphericalDeconvModel(gtab, response) smfit = sm.fit(S) np.random.seed(12345) response = ([0.0015, 0.0003, 0.0001], S0) kf_xval = xval.kfold_xval(sm, S, 2, response, sh_order=2) # Because of the regularization, COD is not going to be perfect here: cod = xval.coeff_of_determination(S, kf_xval) # We'll just test for regressions: csd_cod = 97 # pre-computed by hand for this random seed # We're going to be really lenient here: npt.assert_array_almost_equal(np.round(cod), csd_cod)
def _fit(gtab, data, mask, response=None, sh_order=None, lambda_=1, tau=0.1): """ Helper function that does the core of fitting a model to data. """ if sh_order is None: ndata = np.sum(~gtab.b0s_mask) # See dipy.reconst.shm.calculate_max_order L1 = (-3 + np.sqrt(1 + 8 * ndata)) / 2.0 sh_order = int(L1) if np.mod(sh_order, 2) != 0: sh_order = sh_order - 1 if sh_order > 8: sh_order = 8 if response is None: response, ratio = csd.auto_response(gtab, data, roi_radius=10, fa_thr=0.7) csdmodel = csd.ConstrainedSphericalDeconvModel(gtab, response, sh_order=sh_order) csdfit = csdmodel.fit(data, mask=mask) return csdfit
def fit_csd(data_files, bval_files, bvec_files, mask=None, response=None, sh_order=8, lambda_=1, tau=0.1, out_dir=None): """ Fit the CSD model and save file with SH coefficients. Parameters ---------- data_files : str or list Files containing DWI data. If this is a str, that's the full path to a single file. If it's a list, each entry is a full path. bval_files : str or list Equivalent to `data_files`. bvec_files : str or list Equivalent to `data_files`. mask : ndarray, optional Binary mask, set to True or 1 in voxels to be processed. Default: Process all voxels. out_dir : str, optional A full path to a directory to store the maps that get computed. Default: file with coefficients gets stored in the same directory as the first DWI file in `data_files`. Returns ------- fname : the full path to the file containing the SH coefficients. """ img, data, gtab, mask = ut.prepare_data(data_files, bval_files, bvec_files) if response is None: response, ratio = csd.auto_response(gtab, data, roi_radius=10, fa_thr=0.7) csdmodel = csd.ConstrainedSphericalDeconvModel(gtab, response, sh_order=sh_order) csdfit = csdmodel.fit(data, mask=mask) if out_dir is None: out_dir = op.join(op.split(data_files)[0], 'dki') if not op.exists(out_dir): os.makedirs(out_dir) aff = img.affine fname = op.join(out_dir, 'csd_sh_coeff.nii.gz') nib.save(nib.Nifti1Image(csdfit.shm_coeff, aff), fname) return fname
def test_csd_xval(): # First, let's see that it works with some data: data = load_nifti_data(fdata)[1:3, 1:3, 1:3] # Make it *small* gtab = gt.gradient_table(fbval, fbvec) S0 = np.mean(data[..., gtab.b0s_mask]) response = ([0.0015, 0.0003, 0.0001], S0) # In simulation, it should work rather well (high COD): psphere = dpd.get_sphere('symmetric362') bvecs = np.concatenate(([[0, 0, 0]], psphere.vertices)) bvals = np.zeros(len(bvecs)) + 1000 bvals[0] = 0 gtab = gt.gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0001], [0.0015, 0.0003, 0.0003])) mevecs = [ np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) ] S0 = 100 S = sims.single_tensor(gtab, S0, mevals[0], mevecs[0], snr=None) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) sm = csd.ConstrainedSphericalDeconvModel(gtab, response) np.random.seed(12345) response = ([0.0015, 0.0003, 0.0001], S0) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) kf_xval = xval.kfold_xval(sm, S, 2, response, sh_order=2) # Because of the regularization, COD is not going to be perfect here: cod = xval.coeff_of_determination(S, kf_xval) # We'll just test for regressions: csd_cod = 97 # pre-computed by hand for this random seed # We're going to be really lenient here: npt.assert_array_almost_equal(np.round(cod), csd_cod) # Test for sD data with more than one voxel for use of a mask: S = np.array([[S, S], [S, S]]) mask = np.ones(S.shape[:-1], dtype=bool) mask[1, 1] = 0 with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) kf_xval = xval.kfold_xval(sm, S, 2, response, sh_order=2, mask=mask) cod = xval.coeff_of_determination(S, kf_xval) npt.assert_array_almost_equal(np.round(cod[0]), csd_cod)
def _fit(gtab, data, mask, response=None, sh_order=8, lambda_=1, tau=0.1): """ Helper function that does the core of fitting a model to data. """ if response is None: response, ratio = csd.auto_response(gtab, data, roi_radius=10, fa_thr=0.7) csdmodel = csd.ConstrainedSphericalDeconvModel(gtab, response, sh_order=sh_order) csdfit = csdmodel.fit(data, mask=mask) return csdfit
dpd.fetch_stanford_hardi() img, gtab = dpd.read_stanford_hardi() data = img.get_data() cc_vox = data[40, 70, 38] cso_vox = data[30, 76, 38] """ We initialize each kind of model: """ dti_model = dti.TensorModel(gtab) response, ratio = csd.auto_response(gtab, data, roi_radius=10, fa_thr=0.7) csd_model = csd.ConstrainedSphericalDeconvModel(gtab, response) """ Next, we perform cross-validation for each kind of model, comparing model predictions to the diffusion MRI data in each one of these voxels. Note that we use 2-fold cross-validation, which means that in each iteration, the model will be fit to half of the data, and used to predict the other half. """ dti_cc = xval.kfold_xval(dti_model, cc_vox, 2) csd_cc = xval.kfold_xval(csd_model, cc_vox, 2, response) dti_cso = xval.kfold_xval(dti_model, cso_vox, 2) csd_cso = xval.kfold_xval(csd_model, cso_vox, 2, response) """