def test_odf_sh_to_sharp(): SNR = None S0 = 1 _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, _ = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = default_sphere with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:, :, :] = odf_gt[:] with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z with warnings.catch_warnings(): warnings.filterwarnings("ignore", message=descoteaux07_legacy_msg, category=PendingDeprecationWarning) fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=0.1) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
def test_odf_sh_to_sharp(): SNR = 100 S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, sticks = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric724') qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:, :, :] = odf_gt[:] odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=1.) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
def test_odf_sh_to_sharp(): SNR = None S0 = 1 _, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) gtab = gradient_table(bvals, bvecs) mevals = np.array(([0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003])) S, sticks = multi_tensor(gtab, mevals, S0, angles=[(10, 0), (100, 0)], fractions=[50, 50], snr=SNR) sphere = get_sphere('symmetric724') qb = QballModel(gtab, sh_order=8, assume_normed=True) qbfit = qb.fit(S) odf_gt = qbfit.odf(sphere) Z = np.linalg.norm(odf_gt) odfs_gt = np.zeros((3, 1, 1, odf_gt.shape[0])) odfs_gt[:,:,:] = odf_gt[:] odfs_sh = sf_to_sh(odfs_gt, sphere, sh_order=8, basis_type=None) odfs_sh /= Z fodf_sh = odf_sh_to_sharp(odfs_sh, sphere, basis=None, ratio=3 / 15., sh_order=8, lambda_=1., tau=0.1) fodf = sh_to_sf(fodf_sh, sphere, sh_order=8, basis_type=None) directions2, _, _ = peak_directions(fodf[0, 0, 0], sphere) assert_equal(directions2.shape[0], 2)
def qball(gtab, data, name, sh_order=4): qballmodel = QballModel(gtab, sh_order) data_small = data[:, :, 39:40] qball_fit = qballmodel.fit(data_small) qball_odf = qball_fit.odf(default_sphere) odf_spheres = actor.odf_slicer(qball_odf, sphere=default_sphere, scale=0.9, norm=False, colormap='plasma') ren = window.Scene() ren.add(odf_spheres) print('Saving illustration as qball_odfs_{}.png'.format( name)) #data.shape[-1] - 1)) window.record(ren, out_path='results/qball_odfs_{}.png'.format(name), size=(600, 600)) return qball_odf, qball_fit.shm_coeff
def main(): parser = _build_arg_parser() args = parser.parse_args() if not args.not_all: args.gfa = args.gfa or 'gfa.nii.gz' args.peaks = args.peaks or 'peaks.nii.gz' args.peak_indices = args.peak_indices or 'peaks_indices.nii.gz' args.sh = args.sh or 'sh.nii.gz' args.nufo = args.nufo or 'nufo.nii.gz' args.a_power = args.a_power or 'anisotropic_power.nii.gz' arglist = [ args.gfa, args.peaks, args.peak_indices, args.sh, args.nufo, args.a_power ] if args.not_all and not any(arglist): parser.error('When using --not_all, you need to specify at least ' + 'one file to output.') assert_inputs_exist(parser, [args.input, args.bvals, args.bvecs]) assert_outputs_exists(parser, args, arglist) nbr_processes = args.nbr_processes parallel = True if nbr_processes <= 0: nbr_processes = None elif nbr_processes == 1: parallel = False # Load data img = nib.load(args.input) data = img.get_data() affine = img.get_affine() bvals, bvecs = read_bvals_bvecs(args.bvals, args.bvecs) if not is_normalized_bvecs(bvecs): logging.warning('Your b-vectors do not seem normalized...') bvecs = normalize_bvecs(bvecs) if bvals.min() != 0: if bvals.min() > 20: raise ValueError( 'The minimal bvalue is greater than 20. This is highly ' 'suspicious. Please check your data to ensure everything is ' 'correct.\nValue found: {0}'.format(bvals.min())) else: logging.warning( 'Warning: no b=0 image. Setting b0_threshold to ' 'bvals.min() = %s', bvals.min()) gtab = gradient_table(bvals, bvecs, b0_threshold=bvals.min()) else: gtab = gradient_table(bvals, bvecs) sphere = get_sphere('symmetric724') if args.mask is None: mask = None else: mask = nib.load(args.mask).get_data().astype(np.bool) if args.use_qball: model = QballModel(gtab, sh_order=int(args.sh_order), smooth=0.006) else: model = CsaOdfModel(gtab, sh_order=int(args.sh_order), smooth=0.006) odfpeaks = peaks_from_model(model=model, data=data, sphere=sphere, relative_peak_threshold=.5, min_separation_angle=25, mask=mask, return_odf=False, normalize_peaks=True, return_sh=True, sh_order=int(args.sh_order), sh_basis_type=args.basis, npeaks=5, parallel=parallel, nbr_processes=nbr_processes) if args.gfa: nib.save(nib.Nifti1Image(odfpeaks.gfa.astype(np.float32), affine), args.gfa) if args.peaks: nib.save( nib.Nifti1Image(reshape_peaks_for_visualization(odfpeaks), affine), args.peaks) if args.peak_indices: nib.save(nib.Nifti1Image(odfpeaks.peak_indices, affine), args.peak_indices) if args.sh: nib.save( nib.Nifti1Image(odfpeaks.shm_coeff.astype(np.float32), affine), args.sh) if args.nufo: peaks_count = (odfpeaks.peak_indices > -1).sum(3) nib.save(nib.Nifti1Image(peaks_count.astype(np.int32), affine), args.nufo) if args.a_power: odf_a_power = anisotropic_power(odfpeaks.shm_coeff) nib.save(nib.Nifti1Image(odf_a_power.astype(np.float32), affine), args.a_power)
data_small = data[20:50,55:85, 38:40] csamodel = CsaOdfModel(gtab, 4, smooth=0.006) csa_fit = csamodel.fit(data_small) sphere = get_sphere('symmetric724') csa_odf = csa_fit.odf(sphere) gfa_csa = gfa(csa_odf) odfs = csa_odf.clip(0) gfa_csa_wo_zeros = gfa(odfs) csa_mm = minmax_normalize(odfs) gfa_csa_mm = gfa(csa_mm) qballmodel = QballModel(gtab, 6, smooth=0.006) qball_fit = qballmodel.fit(data_small) qball_odf = qball_fit.odf(sphere) gfa_qball = gfa(qball_odf) gfa_qball_mm = gfa(minmax_normalize(qball_odf)) print 'Saving GFAs...' nib.save(nib.Nifti1Image(gfa_qball.astype('float32'), affine), 'gfa.nii.gz') nib.save(nib.Nifti1Image(gfa_qball_mm.astype('float32'), affine), 'gfa_mm.nii.gz') nib.save(nib.Nifti1Image(gfa_csa.astype('float32'), affine), 'gfa_csa.nii.gz') nib.save(nib.Nifti1Image(gfa_csa_wo_zeros.astype('float32'), affine), 'gfa_csa_wo_neg.nii.gz') nib.save(nib.Nifti1Image(gfa_csa_mm.astype('float32'), affine), 'gfa_csa_mm.nii.gz')
def main(): parser = _build_arg_parser() args = parser.parse_args() if not args.not_all: args.gfa = args.gfa or 'gfa.nii.gz' args.peaks = args.peaks or 'peaks.nii.gz' args.peak_indices = args.peak_indices or 'peaks_indices.nii.gz' args.sh = args.sh or 'sh.nii.gz' args.nufo = args.nufo or 'nufo.nii.gz' args.a_power = args.a_power or 'anisotropic_power.nii.gz' arglist = [ args.gfa, args.peaks, args.peak_indices, args.sh, args.nufo, args.a_power ] if args.not_all and not any(arglist): parser.error('When using --not_all, you need to specify at least ' + 'one file to output.') assert_inputs_exist(parser, [args.in_dwi, args.in_bval, args.in_bvec]) assert_outputs_exist(parser, args, arglist) validate_nbr_processes(parser, args) nbr_processes = args.nbr_processes parallel = nbr_processes > 1 # Load data img = nib.load(args.in_dwi) data = img.get_fdata(dtype=np.float32) bvals, bvecs = read_bvals_bvecs(args.in_bval, args.in_bvec) if not is_normalized_bvecs(bvecs): logging.warning('Your b-vectors do not seem normalized...') bvecs = normalize_bvecs(bvecs) check_b0_threshold(args, bvals.min()) gtab = gradient_table(bvals, bvecs, b0_threshold=bvals.min()) sphere = get_sphere('symmetric724') mask = None if args.mask: mask = get_data_as_mask(nib.load(args.mask)) # Sanity check on shape of mask if mask.shape != data.shape[:-1]: raise ValueError('Mask shape does not match data shape.') if args.use_qball: model = QballModel(gtab, sh_order=args.sh_order, smooth=DEFAULT_SMOOTH) else: model = CsaOdfModel(gtab, sh_order=args.sh_order, smooth=DEFAULT_SMOOTH) odfpeaks = peaks_from_model(model=model, data=data, sphere=sphere, relative_peak_threshold=.5, min_separation_angle=25, mask=mask, return_odf=False, normalize_peaks=True, return_sh=True, sh_order=int(args.sh_order), sh_basis_type=args.sh_basis, npeaks=5, parallel=parallel, nbr_processes=nbr_processes) if args.gfa: nib.save(nib.Nifti1Image(odfpeaks.gfa.astype(np.float32), img.affine), args.gfa) if args.peaks: nib.save( nib.Nifti1Image(reshape_peaks_for_visualization(odfpeaks), img.affine), args.peaks) if args.peak_indices: nib.save(nib.Nifti1Image(odfpeaks.peak_indices, img.affine), args.peak_indices) if args.sh: nib.save( nib.Nifti1Image(odfpeaks.shm_coeff.astype(np.float32), img.affine), args.sh) if args.nufo: peaks_count = (odfpeaks.peak_indices > -1).sum(3) nib.save(nib.Nifti1Image(peaks_count.astype(np.int32), img.affine), args.nufo) if args.a_power: odf_a_power = anisotropic_power(odfpeaks.shm_coeff) nib.save(nib.Nifti1Image(odf_a_power.astype(np.float32), img.affine), args.a_power)
csamodel = CsaOdfModel(gtab, 4, smooth=0.006) print 'Computing the CSA odf...' coeff = csamodel._get_shm_coef(data) print coeff.shape print 'Computing GFA...' print np.min(coeff[:,:,:,0]),np.max(coeff[:,:,:,0]) gfa_sh = np.sqrt(1.0 - (coeff[:,:,:,0] ** 2 / ( np.sum(np.square(coeff), axis=3) ) ) ) gfa_sh[np.isnan(gfa_sh)] = 0 print 'Saving nifti...' nib.save(nib.Nifti1Image(gfa_sh.astype('float32'), affine), 'gfa_full_brain.nii.gz') nib.save(nib.Nifti1Image(coeff.astype('float32'), affine), 'csa_odf_sh.nii.gz') qballmodel = QballModel(gtab, 4, smooth=0.006) print 'Computing the QBALL odf...' coeff = qballmodel._get_shm_coef(data) print coeff.shape print 'Computing GFA...' print np.min(coeff[:,:,:,0]),np.max(coeff[:,:,:,0]) gfa_sh = np.sqrt(1.0 - (coeff[:,:,:,0] ** 2 / ( np.sum(np.square(coeff), axis=3) ) ) ) gfa_sh[np.isnan(gfa_sh)] = 0 print 'Saving nifti...' nib.save(nib.Nifti1Image(gfa_sh.astype('float32'), affine), 'gfa_qball_full_brain.nii.gz') nib.save(nib.Nifti1Image(coeff.astype('float32'), affine), 'qball_odf_sh.nii.gz') opdtmodel = OpdtModel(gtab, 4, smooth=0.006)
#GFA = csafit.gfa #nib.save(nib.Nifti1Image(GFA.astype('float32'), affine), 'gfa_csa.nii.gz') nib.save(nib.Nifti1Image(coeff.astype('float32'), affine), 'csa_odf_sh.nii.gz') sphere = get_sphere('symmetric724') odfs = sh_to_sf(coeff[20:50,55:85, 38:39], sphere, order) if vizu : from dipy.viz import fvtk r = fvtk.ren() fvtk.add(r, fvtk.sphere_funcs(odfs, sphere, colormap='jet')) fvtk.show(r) fvtk.clear(r) qballmodel = QballModel(gtab, order, smooth=0.006) print 'Computing the QBALL odf...' qballfit = qballmodel.fit(data) coeff = qballfit._shm_coef #GFA = qballfit.gfa # dipy 0.6 compatible GFA = np.sqrt(1.0 - (coeff[:,:,:,0] ** 2 / ( np.sum(np.square(coeff), axis=3) ) ) ) GFA[np.isnan(GFA)] = 0 nib.save(nib.Nifti1Image(GFA.astype('float32'), affine), 'gfa_qball.nii.gz') nib.save(nib.Nifti1Image(coeff.astype('float32'), affine), 'qball_odf_sh.nii.gz') if vizu : odfs = sh_to_sf(coeff[20:50,55:85, 38:39], sphere, order) fvtk.add(r, fvtk.sphere_funcs(odfs, sphere, colormap='jet'))