def test_gaussian_weights(): # Some bogus x,y,z coordinates x = np.arange(10) y = np.arange(10) z = np.arange(10) # This has the wrong shape (2, 3, 10): bundle = np.array([[x, y, z], [x, y, z]]) pytest.raises(ValueError, seg.gaussian_weights, bundle) # Reallocate with the right shape. This time, we're going to create a # distribution for which we can predict the weights we would expect # to get: bundle = np.array([np.array([x, y, z]).T + 1, np.array([x, y, z]).T - 1]) # In this case, all nodes receives an equal weight of 0.5: w = seg.gaussian_weights(bundle) npt.assert_equal(w, np.ones(bundle.shape[:-1]) * 0.5) # Here, some nodes are twice as far from the mean as others bundle = np.array([np.array([x, y, z]).T + 2, np.array([x, y, z]).T + 1, np.array([x, y, z]).T - 1, np.array([x, y, z]).T - 2]) w = seg.gaussian_weights(bundle) # And their weights should be halved # XXX Need to check how to transform this through the # Gaussian distribution! npt.assert_almost_equal(w[0], w[1] / 2)
def test_gaussian_weights(): # Some bogus x,y,z coordinates x = np.arange(10) y = np.arange(10) z = np.arange(10) # Create a distribution for which we can predict the weights we would # expect to get: bundle = np.array([np.array([x, y, z]).T + 1, np.array([x, y, z]).T - 1]) # In this case, all nodes receives an equal weight of 0.5: w = seg.gaussian_weights(bundle) npt.assert_equal(w, np.ones(bundle.shape[:-1]) * 0.5) # Here, some nodes are twice as far from the mean as others bundle = np.array([ np.array([x, y, z]).T + 2, np.array([x, y, z]).T + 1, np.array([x, y, z]).T - 1, np.array([x, y, z]).T - 2 ]) w = seg.gaussian_weights(bundle) # And their weights should be halved # XXX Need to check how to transform this through the # Gaussian distribution! npt.assert_almost_equal(w[0], w[1] / 2)
print("Segmenting fiber groups...") fiber_groups = seg.segment(hardi_fdata, hardi_fbval, hardi_fbvec, streamlines, bundles, reg_template=MNI_T2_img, mapping=mapping, as_generator=False, affine=img.affine) print("Cleaning fiber groups...") for bundle in bundles: fiber_groups[bundle] = seg.clean_fiber_group(fiber_groups[bundle]) FA_img = nib.load(dti_params['FA']) FA_data = FA_img.get_data() print("Extracting tract profiles...") for bundle in bundles: fig, ax = plt.subplots(1) weights = seg.gaussian_weights(fiber_groups[bundle]) profile = seg.calculate_tract_profile(FA_data, fiber_groups[bundle], weights=weights) ax.plot(profile) ax.set_title(bundle) plt.show()
prob_threshold=5) print("Getting tract profiles") n_points = 100 dfs = [] for bundle in fiber_groups: print("Getting profile for: %s" % bundle) if len(fiber_groups[bundle]) > 0: bundle_df = pd.DataFrame(data={ 'tractID': [bundle] * n_points, 'nodeID': np.arange(1, n_points + 1)}) for stat_key in dti_params.keys(): if stat_key == 'params': pass else: stat_data = nib.load(dti_params[stat_key]).get_data() fgarray = seg._resample_bundle(fiber_groups[bundle], n_points) weights = seg.gaussian_weights(fgarray) profile = seg.calculate_tract_profile(stat_data, fgarray, weights=weights) bundle_df[stat_key] = profile dfs.append(bundle_df) else: print("There are no fibers in %s" % bundle) result = pd.concat(dfs)
MNI_T2_img = dpd.read_mni_template() if not op.exists('mapping.nii.gz'): import dipy.core.gradients as dpg gtab = dpg.gradient_table(hardi_fbval, hardi_fbvec) mapping = reg.syn_register_dwi(hardi_fdata, gtab) reg.write_mapping(mapping, './mapping.nii.gz') else: mapping = reg.read_mapping('./mapping.nii.gz', img, MNI_T2_img) print("Segmenting fiber groups...") fiber_groups = seg.segment(hardi_fdata, hardi_fbval, hardi_fbvec, streamlines, bundles, reg_template=MNI_T2_img, mapping=mapping, as_generator=False) FA_img = nib.load(dti_params['FA']) FA_data = FA_img.get_data() print("Extracting tract profiles...") for bundle in bundles: fig, ax = plt.subplots(1) w = seg.gaussian_weights(fiber_groups[bundle]) profile = seg.calculate_tract_profile(FA_data, fiber_groups[bundle], weights=w) ax.plot(profile) ax.set_title(bundle)