def test_dti_xval(): """ Test k-fold cross-validation """ data = nib.load(fdata).get_data() gtab = gt.gradient_table(fbval, fbvec) dm = dti.TensorModel(gtab, "LS") # The data has 102 directions, so will not divide neatly into 10 bits npt.assert_raises(ValueError, xval.kfold_xval, dm, data, 10) # But we can do this with 2 folds: kf_xval = xval.kfold_xval(dm, data, 2) # In simulation with no noise, COD should be perfect: 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]])] S = sims.single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None) dm = dti.TensorModel(gtab, "LS") kf_xval = xval.kfold_xval(dm, S, 2) cod = xval.coeff_of_determination(S, kf_xval) npt.assert_array_almost_equal(cod, np.ones(kf_xval.shape[:-1]) * 100) # Test with 2D data for use of a mask S = np.array([[S, S], [S, S]]) mask = np.ones(S.shape[:-1], dtype=bool) mask[1, 1] = 0 kf_xval = xval.kfold_xval(dm, S, 2, mask=mask) cod2d = xval.coeff_of_determination(S, kf_xval) npt.assert_array_almost_equal(np.round(cod2d[0, 0]), cod)
def test_dti_xval(): """ Test k-fold cross-validation """ data = nib.load(fdata).get_data() gtab = gt.gradient_table(fbval, fbvec) dm = dti.TensorModel(gtab, 'LS') # The data has 102 directions, so will not divide neatly into 10 bits npt.assert_raises(ValueError, xval.kfold_xval, dm, data, 10) # But we can do this with 2 folds: kf_xval = xval.kfold_xval(dm, data, 2) # In simulation with no noise, COD should be perfect: 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]]) ] S = sims.single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None) dm = dti.TensorModel(gtab, 'LS') dmfit = dm.fit(S) kf_xval = xval.kfold_xval(dm, S, 2) cod = xval.coeff_of_determination(S, kf_xval) npt.assert_array_almost_equal(cod, np.ones(kf_xval.shape[:-1]) * 100)
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 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 test_dti_xval(): data = load_nifti_data(fdata) gtab = gt.gradient_table(fbval, fbvec) dm = dti.TensorModel(gtab, 'LS') # The data has 102 directions, so will not divide neatly into 10 bits npt.assert_raises(ValueError, xval.kfold_xval, dm, data, 10) # In simulation with no noise, COD should be perfect: 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]]) ] S = sims.single_tensor(gtab, 100, mevals[0], mevecs[0], snr=None) dm = dti.TensorModel(gtab, 'LS') kf_xval = xval.kfold_xval(dm, S, 2) cod = xval.coeff_of_determination(S, kf_xval) npt.assert_array_almost_equal(cod, np.ones(kf_xval.shape[:-1]) * 100) # Test with 2D data for use of a mask S = np.array([[S, S], [S, S]]) mask = np.ones(S.shape[:-1], dtype=bool) mask[1, 1] = 0 kf_xval = xval.kfold_xval(dm, S, 2, mask=mask) cod2d = xval.coeff_of_determination(S, kf_xval) npt.assert_array_almost_equal(np.round(cod2d[0, 0]), cod)
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)
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) """ We plot a scatter plot of the data with the model predictions in each of these voxels, focusing only on the diffusion-weighted measurements (each point corresponds to a different gradient direction). The two models are compared in each sub-plot (blue=DTI, red=CSD). """ fig, ax = plt.subplots(1,2) fig.set_size_inches([12,6])
""" 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) """ We plot a scatter plot of the data with the model predictions in each of these voxels, focusing only on the diffusion-weighted measurements (each point corresponds to a different gradient direction). The two models are compared in each sub-plot (blue=DTI, red=CSD). """ fig, ax = plt.subplots(1, 2) fig.set_size_inches([12, 6]) ax[0].plot(cc_vox[gtab.b0s_mask == 0],
def compare_models(subject): s3 = boto3.resource('s3') boto3.setup_default_session(profile_name='cirrus') bucket = s3.Bucket('hcp-dki') path_dki = '%s/%s_cod_dki.nii.gz' % (subject, subject) path_dti = '%s/%s_cod_dti.nii.gz' % (subject, subject) if not (exists(path_dki, bucket.name) and exists(path_dti, bucket.name)): print("Files don't exist - going ahead") with tempfile.TemporaryDirectory() as tempdir: try: bucket = setup_boto() dwi_file = op.join(tempdir, 'data.nii.gz') bvec_file = op.join(tempdir, 'data.bvec') bval_file = op.join(tempdir, 'data.bval') data_files = {} data_files[dwi_file] = \ 'HCP_900/%s/T1w/Diffusion/data.nii.gz' % subject data_files[bvec_file] = \ 'HCP_900/%s/T1w/Diffusion/bvecs' % subject data_files[bval_file] = \ 'HCP_900/%s/T1w/Diffusion/bvals' % subject for k in data_files.keys(): if not op.exists(k): bucket.download_file(data_files[k], k) wm_file = op.join(tempdir, 'wm.nii.gz') boto3.setup_default_session(profile_name='cirrus') s3 = boto3.resource('s3') s3.meta.client.download_file( 'hcp-dki', '%s/%s_white_matter_mask.nii.gz' % (subject, subject), wm_file) wm_mask = nib.load(wm_file).get_data().astype(bool) dwi_img = nib.load(dwi_file) data = dwi_img.get_data() bvals = np.loadtxt(bval_file) bvecs = np.loadtxt(bvec_file) gtab = dpg.gradient_table(bvals, bvecs, b0_threshold=10) s3 = boto3.resource('s3') boto3.setup_default_session(profile_name='cirrus') bucket = s3.Bucket('hcp-dki') for model_object, method in zip([dti.TensorModel, dki.DiffusionKurtosisModel], ['dti', 'dki']): path_method = '%s/%s_cod_%s.nii.gz' % (subject, subject, method) if not (exists(path_method, bucket.name)): print("No %s file - fitting" % method) print("1") model = model_object(gtab) print("2") pred = xval.kfold_xval(model, data, 5, mask=wm_mask) print("3") cod = xval.coeff_of_determination(pred, data) cod_file = op.join(tempdir, 'cod_%s.nii.gz' % method) print("4") nib.save(nib.Nifti1Image(cod, dwi_img.affine), cod_file) print("5") s3.meta.client.upload_file( cod_file, 'hcp-dki', path_method) return subject, True except Exception as err: return subject, err.args else: return subject, True
def calc_cod1000(subject): s3 = boto3.resource('s3') boto3.setup_default_session(profile_name='cirrus') bucket = s3.Bucket('hcp-dki') path_dti = '%s/%s_cod_dti_1000.nii.gz' % (subject, subject) path_dki = '%s/%s_cod_dki_1000.nii.gz' % (subject, subject) if not (exists(path_dti, bucket.name) and exists(path_dki, bucket.name)): print("File doesn't exist - going ahead") with tempfile.TemporaryDirectory() as tempdir: try: bucket = setup_boto() dwi_file = op.join(tempdir, 'data.nii.gz') bvec_file = op.join(tempdir, 'data.bvec') bval_file = op.join(tempdir, 'data.bval') data_files = {} data_files[dwi_file] = \ 'HCP_900/%s/T1w/Diffusion/data.nii.gz' % subject data_files[bvec_file] = \ 'HCP_900/%s/T1w/Diffusion/bvecs' % subject data_files[bval_file] = \ 'HCP_900/%s/T1w/Diffusion/bvals' % subject for k in data_files.keys(): if not op.exists(k): bucket.download_file(data_files[k], k) wm_file = op.join(tempdir, 'wm.nii.gz') boto3.setup_default_session(profile_name='cirrus') s3 = boto3.resource('s3') bucket = s3.Bucket('hcp-dki') s3.meta.client.download_file( 'hcp-dki', '%s/%s_white_matter_mask.nii.gz' % (subject, subject), wm_file) wm_mask = nib.load(wm_file).get_data().astype(bool) dwi_img = nib.load(dwi_file) data = dwi_img.get_data() bvals = np.loadtxt(bval_file) bvecs = np.loadtxt(bvec_file) idx = bvals < 1985 if not exists(path_dki, bucket.name): gtab = dpg.gradient_table(bvals, bvecs, b0_threshold=10) dki_model = dki.DiffusionKurtosisModel(gtab) # Use all the data to calculate the mode pred = xval.kfold_xval(dki_model, data, 5, mask=wm_mask) # But compare only on the b=1000 shell (same as DTI): cod = xval.coeff_of_determination(pred[..., idx], data[..., idx]) cod_file = op.join(tempdir, 'cod_dki_1000.nii.gz') nib.save(nib.Nifti1Image(cod, dwi_img.affine), cod_file) s3.meta.client.upload_file( cod_file, 'hcp-dki', path_dki) if not exists(path_dti, bucket.name): data = data[..., idx] gtab = dpg.gradient_table(bvals[idx], bvecs[:, idx].squeeze(), b0_threshold=10) model = dti.TensorModel(gtab) pred = xval.kfold_xval(model, data, 5, mask=wm_mask) cod = xval.coeff_of_determination(pred, data) cod_file = op.join(tempdir, 'cod_dti_1000.nii.gz') nib.save(nib.Nifti1Image(cod, dwi_img.affine), cod_file) s3.meta.client.upload_file( cod_file, 'hcp-dki', path_dti) return subject, True except Exception as err: return subject, err.args else: return subject, True
def compare_models(subject): s3 = boto3.resource('s3') boto3.setup_default_session(profile_name='cirrus') bucket = s3.Bucket('hcp-dki') path_dki = '%s/%s_cod_dki.nii.gz' % (subject, subject) path_dti = '%s/%s_cod_dti.nii.gz' % (subject, subject) if not (exists(path_dki, bucket.name) and exists(path_dti, bucket.name)): print("Files don't exist - going ahead") with tempfile.TemporaryDirectory() as tempdir: try: bucket = setup_boto() dwi_file = op.join(tempdir, 'data.nii.gz') bvec_file = op.join(tempdir, 'data.bvec') bval_file = op.join(tempdir, 'data.bval') data_files = {} data_files[dwi_file] = \ 'HCP_900/%s/T1w/Diffusion/data.nii.gz' % subject data_files[bvec_file] = \ 'HCP_900/%s/T1w/Diffusion/bvecs' % subject data_files[bval_file] = \ 'HCP_900/%s/T1w/Diffusion/bvals' % subject for k in data_files.keys(): if not op.exists(k): bucket.download_file(data_files[k], k) wm_file = op.join(tempdir, 'wm.nii.gz') boto3.setup_default_session(profile_name='cirrus') s3 = boto3.resource('s3') s3.meta.client.download_file( 'hcp-dki', '%s/%s_white_matter_mask.nii.gz' % (subject, subject), wm_file) wm_mask = nib.load(wm_file).get_data().astype(bool) dwi_img = nib.load(dwi_file) data = dwi_img.get_data() bvals = np.loadtxt(bval_file) bvecs = np.loadtxt(bvec_file) gtab = dpg.gradient_table(bvals, bvecs, b0_threshold=10) s3 = boto3.resource('s3') boto3.setup_default_session(profile_name='cirrus') bucket = s3.Bucket('hcp-dki') for model_object, method in zip( [dti.TensorModel, dki.DiffusionKurtosisModel], ['dti', 'dki']): path_method = '%s/%s_cod_%s.nii.gz' % (subject, subject, method) if not (exists(path_method, bucket.name)): print("No %s file - fitting" % method) print("1") model = model_object(gtab) print("2") pred = xval.kfold_xval(model, data, 5, mask=wm_mask) print("3") cod = xval.coeff_of_determination(pred, data) cod_file = op.join(tempdir, 'cod_%s.nii.gz' % method) print("4") nib.save(nib.Nifti1Image(cod, dwi_img.affine), cod_file) print("5") s3.meta.client.upload_file(cod_file, 'hcp-dki', path_method) return subject, True except Exception as err: return subject, err.args else: return subject, True
def calc_cod1000(subject): s3 = boto3.resource('s3') boto3.setup_default_session(profile_name='cirrus') bucket = s3.Bucket('hcp-dki') path_dti = '%s/%s_cod_dti_1000.nii.gz' % (subject, subject) path_dki = '%s/%s_cod_dki_1000.nii.gz' % (subject, subject) if not (exists(path_dti, bucket.name) and exists(path_dki, bucket.name)): print("File doesn't exist - going ahead") with tempfile.TemporaryDirectory() as tempdir: try: bucket = setup_boto() dwi_file = op.join(tempdir, 'data.nii.gz') bvec_file = op.join(tempdir, 'data.bvec') bval_file = op.join(tempdir, 'data.bval') data_files = {} data_files[dwi_file] = \ 'HCP_900/%s/T1w/Diffusion/data.nii.gz' % subject data_files[bvec_file] = \ 'HCP_900/%s/T1w/Diffusion/bvecs' % subject data_files[bval_file] = \ 'HCP_900/%s/T1w/Diffusion/bvals' % subject for k in data_files.keys(): if not op.exists(k): bucket.download_file(data_files[k], k) wm_file = op.join(tempdir, 'wm.nii.gz') boto3.setup_default_session(profile_name='cirrus') s3 = boto3.resource('s3') bucket = s3.Bucket('hcp-dki') s3.meta.client.download_file( 'hcp-dki', '%s/%s_white_matter_mask.nii.gz' % (subject, subject), wm_file) wm_mask = nib.load(wm_file).get_data().astype(bool) dwi_img = nib.load(dwi_file) data = dwi_img.get_data() bvals = np.loadtxt(bval_file) bvecs = np.loadtxt(bvec_file) idx = bvals < 1985 if not exists(path_dki, bucket.name): gtab = dpg.gradient_table(bvals, bvecs, b0_threshold=10) dki_model = dki.DiffusionKurtosisModel(gtab) # Use all the data to calculate the mode pred = xval.kfold_xval(dki_model, data, 5, mask=wm_mask) # But compare only on the b=1000 shell (same as DTI): cod = xval.coeff_of_determination(pred[..., idx], data[..., idx]) cod_file = op.join(tempdir, 'cod_dki_1000.nii.gz') nib.save(nib.Nifti1Image(cod, dwi_img.affine), cod_file) s3.meta.client.upload_file(cod_file, 'hcp-dki', path_dki) if not exists(path_dti, bucket.name): data = data[..., idx] gtab = dpg.gradient_table(bvals[idx], bvecs[:, idx].squeeze(), b0_threshold=10) model = dti.TensorModel(gtab) pred = xval.kfold_xval(model, data, 5, mask=wm_mask) cod = xval.coeff_of_determination(pred, data) cod_file = op.join(tempdir, 'cod_dti_1000.nii.gz') nib.save(nib.Nifti1Image(cod, dwi_img.affine), cod_file) s3.meta.client.upload_file(cod_file, 'hcp-dki', path_dti) return subject, True except Exception as err: return subject, err.args else: return subject, True