def test_dki_predict(): dkiM = dki.DiffusionKurtosisModel(gtab_2s) pred = dkiM.predict(crossing_ref, S0=100) assert_array_almost_equal(pred, signal_cross) # just to check that it works with more than one voxel: pred_multi = dkiM.predict(multi_params, S0=100) assert_array_almost_equal(pred_multi, DWI) # Check that it works with more than one voxel, and with a different S0 # in each voxel: pred_multi = dkiM.predict(multi_params, S0=100*np.ones(pred_multi.shape[:3])) assert_array_almost_equal(pred_multi, DWI) # check the function predict of the DiffusionKurtosisFit object dkiF = dkiM.fit(DWI) pred_multi = dkiF.predict(gtab_2s, S0=100) assert_array_almost_equal(pred_multi, DWI) dkiF = dkiM.fit(pred_multi) pred_from_fit = dkiF.predict(dkiM.gtab, S0=100) assert_array_almost_equal(pred_from_fit, DWI) # Test the module function: pred = dki.dki_prediction(crossing_ref, gtab_2s, S0=100) assert_array_almost_equal(pred, signal_cross) # Test the module function with S0 volume: pred = dki.dki_prediction(multi_params, gtab_2s, S0=100 * np.ones(multi_params.shape[:3])) assert_array_almost_equal(pred, DWI)
def predict(params_file, gtab, S0_file=None, out_dir=None): """ Create a signal prediction from DKI params params_file : str Full path to a file with parameters saved from a DKI fit gtab : GradientTable object The gradient table to predict for S0_file : str Full path to a nifti file that contains S0 measurements to incorporate into the prediction. If the file contains 4D data, the volumes that contain the S0 data must be the same as the gtab.b0s_mask. """ if out_dir is None: out_dir = op.join(op.split(params_file)[0]) if S0_file is None: S0 = 100 else: S0 = nib.load(S0_file).get_fdata() # If the S0 data is 4D, we assume it comes from an acquisition that had # B0 measurements in the same volumes described in the gtab: if len(S0.shape) == 4: S0 = np.mean(S0[..., gtab.b0s_mask], -1) # Otherwise, we assume that it's already a 3D volume, and do nothing img = nib.load(params_file) params = img.get_fdata() pred = dki.dki_prediction(params, gtab, S0=S0) fname = op.join(out_dir, 'dki_prediction.nii.gz') nib.save(nib.Nifti1Image(pred, img.affine), fname) return fname
def predict(params_file, gtab, S0_file=None, out_dir=None): """ Create a signal prediction from DKI params params_file : str Full path to a file with parameters saved from a DKI fit gtab : GradientTable object The gradient table to predict for S0_file : str Full path to a nifti file that contains S0 measurements to incorporate into the prediction. If the file contains 4D data, the volumes that contain the S0 data must be the same as the gtab.b0s_mask. """ if out_dir is None: out_dir = op.join(op.split(params_file)[0]) if S0_file is None: S0 = 100 else: S0 = nib.load(S0_file).get_data() # If the S0 data is 4D, we assume it comes from an acquisition that had # B0 measurements in the same volumes described in the gtab: if len(S0.shape) == 4: S0 = np.mean(S0[..., gtab.b0s_mask], -1) # Otherwise, we assume that it's already a 3D volume, and do nothing img = nib.load(params_file) params = img.get_data() pred = dki.dki_prediction(params, gtab, S0=S0) fname = op.join(out_dir, 'dki_prediction.nii.gz') nib.save(nib.Nifti1Image(pred, img.affine), fname) return fname