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_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_data() npt.assert_almost_equal(prediction, nib.load(fdata).get_data()) # 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_data() npt.assert_almost_equal(prediction[mask], nib.load(fdata).get_data()[mask])
def test_cli(): 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) cmd = " ".join([ "pyAFQ_dti", "-d", op.join(tmpdir, "dti.nii.gz"), "-l", op.join(tmpdir, "dti.bval"), "-c", op.join(tmpdir, "dti.bvec") ]) out = os.system(cmd) assert out == 0 assert op.exists(op.join(tmpdir, 'dti', 'dti_params.nii.gz'))
def test_fit_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) cmd = ["pyAFQ_dti", "-d", fdata, "-l", fbval, "-c", fbvec, "-o", tmpdir, '-b', '0'] out = runner.run_command(cmd) npt.assert_equal(out[0], 0) # Get expected values names = ['FA', 'MD', 'AD', 'RD'] for n in names: fname = op.join(tmpdir, "dti_%s.nii.gz" % n) img = nib.load(fdata) affine = img.get_affine() shape = img.shape[:-1] assert_image_shape_affine(fname, shape, affine)
def test_fit_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) cmd = ["pyAFQ_dti", "-d", fdata, "-l", fbval, "-c", fbvec, "-o", tmpdir, '-b', '0'] out = runner.run_command(cmd) npt.assert_equal(out[0], 0) # Get expected values names = ['FA', 'MD', 'AD', 'RD'] for n in names: fname = op.join(tmpdir, "dti_%s.nii.gz" % n) img = nib.load(fdata) affine = img.affine shape = img.shape[:-1] assert_image_shape_affine(fname, shape, affine)
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) cmd1 = ["pyAFQ_dti", "-d", fdata, "-l", fbval, "-c", fbvec, "-o", tmpdir] out = runner.run_command(cmd1) npt.assert_equal(out[0], 0) # Get expected values fparams = op.join(tmpdir, "dti_params.nii.gz") cmd2 = ["pyAFQ_dti_predict", "-p", fparams, "-l", fbval, "-c", fbvec, "-o", tmpdir, '-b', '0'] out = runner.run_command(cmd2) npt.assert_equal(out[0], 0) pred = nib.load(op.join(tmpdir, "dti_prediction.nii.gz")).get_data() data = nib.load(op.join(tmpdir, "dti.nii.gz")).get_data() npt.assert_array_almost_equal(pred, data)
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) cmd1 = ["pyAFQ_dti", "-d", fdata, "-l", fbval, "-c", fbvec, "-o", tmpdir] out = runner.run_command(cmd1) npt.assert_equal(out[0], 0) # Get expected values fparams = op.join(tmpdir, "dti_params.nii.gz") cmd2 = ["pyAFQ_dti_predict", "-p", fparams, "-l", fbval, "-c", fbvec, "-o", tmpdir, '-b', '0'] out = runner.run_command(cmd2) npt.assert_equal(out[0], 0) pred = nib.load(op.join(tmpdir, "dti_prediction.nii.gz")).get_fdata() data = nib.load(op.join(tmpdir, "dti.nii.gz")).get_fdata() npt.assert_array_almost_equal(pred, data)
def test_noise_from_b0(): out_shape = (5, 6, 7) with nbtmp.InTemporaryDirectory() as tmpdir: # make artificial dti data 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, out_shape=out_shape) # make artifical mask mask = np.ones(out_shape) mask[0, 0, :] = 0 # load data and mask bvals, bvecs = read_bvals_bvecs(fbval, fbvec) img, data, gtab, mask = ut.prepare_data(fdata, fbval, fbvec, mask=mask, b0_threshold=50) # test noise_from_b0 noise = dti.noise_from_b0(data, gtab, bvals, mask=mask) npt.assert_almost_equal(noise, 0)