def test_predict_dti(): with nbtmp.InTemporaryDirectory() as tmpdir: fbval = op.join(tmpdir, 'dti.bval') fbvec = op.join(tmpdir, 'dti.bvec') fdata = op.join(tmpdir, 'dti.nii.gz') make_dti_data(fbval, fbvec, fdata) file_dict = dti.fit_dti(fdata, fbval, fbvec, out_dir=tmpdir) params_file = file_dict['params'] gtab = dpg.gradient_table(fbval, fbvec) predict_fname = dti.predict(params_file, gtab, S0_file=fdata, out_dir=tmpdir) prediction = nib.load(predict_fname).get_fdata() npt.assert_almost_equal(prediction, nib.load(fdata).get_fdata()) # If you have a mask into the volume, you will predict only that # part of the volume: mask = np.zeros(prediction.shape[:3], dtype=bool) mask[2:4, 2:4, 2:4] = 1 file_dict = dti.fit_dti(fdata, fbval, fbvec, mask=mask, out_dir=tmpdir) params_file = file_dict['params'] predict_fname = dti.predict(params_file, gtab, S0_file=fdata, out_dir=tmpdir) prediction = nib.load(predict_fname).get_fdata() npt.assert_almost_equal(prediction[mask], nib.load(fdata).get_fdata()[mask])
def test_pft_tracking(): for fname in [fit_dti(fdata, fbval, fbvec)['params'], fit_csd(fdata, fbval, fbvec, response=((0.0015, 0.0003, 0.0003), 100), sh_order=8, lambda_=1, tau=0.1, mask=None, out_dir=tmpdir.name)]: img = nib.load(fdata) data_shape = img.shape data_affine = img.affine pve_wm_data = nib.Nifti1Image(np.ones(data_shape[:3]), img.affine) pve_gm_data = nib.Nifti1Image(np.zeros(data_shape[:3]), img.affine) pve_csf_data = nib.Nifti1Image(np.zeros(data_shape[:3]), img.affine) stop_mask = (pve_wm_data, pve_gm_data, pve_csf_data) for directions in ["det", "prob"]: for stop_threshold in ["ACT", "CMC"]: sl = track( fname, directions, max_angle=30., sphere=None, seed_mask=None, stop_mask=stop_mask, stop_threshold=stop_threshold, n_seeds=1, step_size=step_size, min_length=min_length, tracker="pft").streamlines npt.assert_(len(sl[0]) >= min_length * step_size) # Test error handling: with pytest.raises(RuntimeError): track( fname, directions, max_angle=30., sphere=None, seed_mask=None, stop_mask=0, # Stop mask needs to be a tuple! stop_threshold=stop_threshold, n_seeds=1, step_size=step_size, min_length=min_length, tracker="pft") with pytest.raises(RuntimeError): track( fname, directions, max_angle=30., sphere=None, seed_mask=None, stop_mask=stop_mask, stop_threshold=None, # Stop threshold needs to be a string! n_seeds=1, step_size=step_size, min_length=min_length, tracker="pft")
def test_fit_dti(): # Let's see whether we can pass a list of files for each one: fdata1, fbval1, fbvec1 = dpd.get_fnames('small_101D') fdata2, fbval2, fbvec2 = dpd.get_fnames('small_101D') with nbtmp.InTemporaryDirectory() as tmpdir: file_dict = dti.fit_dti([fdata1, fdata2], [fbval1, fbval2], [fbvec1, fbvec2], out_dir=tmpdir) for f in file_dict.values(): npt.assert_(op.exists(f))
def test_dti_local_tracking(): fdict = fit_dti(fdata, fbval, fbvec) for directions in ["det", "prob"]: sl = track( fdict['params'], directions, max_angle=30., sphere=None, seed_mask=None, n_seeds=1, step_size=step_size, min_length=min_length, tracker="local").streamlines npt.assert_(len(sl[0]) >= min_length * step_size)
dpd.fetch_stanford_hardi() hardi_dir = op.join(fetcher.dipy_home, "stanford_hardi") hardi_fdata = op.join(hardi_dir, "HARDI150.nii.gz") hardi_fbval = op.join(hardi_dir, "HARDI150.bval") hardi_fbvec = op.join(hardi_dir, "HARDI150.bvec") img = nib.load(hardi_fdata) ########################################################################## # Calculate DTI: # ------------------------- print("Calculating DTI...") if not op.exists(op.join(working_dir, 'dti_FA.nii.gz')): dti_params = dti.fit_dti(hardi_fdata, hardi_fbval, hardi_fbvec, out_dir=working_dir) else: dti_params = { 'FA': op.join(working_dir, 'dti_FA.nii.gz'), 'params': op.join(working_dir, 'dti_params.nii.gz') } FA_img = nib.load(dti_params['FA']) FA_data = FA_img.get_fdata() ########################################################################## # Register the individual data to a template: # ------------------------------------------- # For the purpose of bundle segmentation, the individual brain is registered to # the MNI T2 template. The waypoint ROIs used in segmentation are then each