def test_path_length(): aoi = np.zeros((20, 20, 20), dtype=bool) aoi[0, 0, 0] = 1 # A few tests for basic usage x = np.arange(20) streamlines = [np.array([x, x, x]).T] pl = path_length(streamlines, np.eye(4), aoi) expected = x.copy() * np.sqrt(3) # expected[0] = np.inf npt.assert_array_almost_equal(pl[x, x, x], expected) aoi[19, 19, 19] = 1 pl = path_length(streamlines, np.eye(4), aoi) expected = np.minimum(expected, expected[::-1]) npt.assert_array_almost_equal(pl[x, x, x], expected) aoi[19, 19, 19] = 0 aoi[1, 1, 1] = 1 pl = path_length(streamlines, np.eye(4), aoi) expected = (x - 1) * np.sqrt(3) expected[0] = 0 npt.assert_array_almost_equal(pl[x, x, x], expected) z = np.zeros(x.shape, x.dtype) streamlines.append(np.array([x, z, z]).T) pl = path_length(streamlines, np.eye(4), aoi) npt.assert_array_almost_equal(pl[x, x, x], expected) npt.assert_array_almost_equal(pl[x, 0, 0], x) # Only streamlines that pass through aoi contribute to path length so if # all streamlines are duds, plm will be all inf. aoi[:] = 0 aoi[0, 0, 0] = 1 streamlines = [] for i in range(1000): rando = np.random.random(size=(100, 3)) * 19 + .5 assert (rando > .5).all() assert (rando < 19.5).all() streamlines.append(rando) pl = path_length(streamlines, np.eye(4), aoi) npt.assert_array_almost_equal(pl, -1) pl = path_length(streamlines, np.eye(4), aoi, fill_value=-12.) npt.assert_array_almost_equal(pl, -12.)
def test_path_length(): aoi = np.zeros((20, 20, 20), dtype=bool) aoi[0, 0, 0] = 1 # A few tests for basic usage x = np.arange(20) streamlines = [np.array([x, x, x]).T] pl = path_length(streamlines, aoi, affine=np.eye(4)) expected = x.copy() * np.sqrt(3) # expected[0] = np.inf npt.assert_array_almost_equal(pl[x, x, x], expected) aoi[19, 19, 19] = 1 pl = path_length(streamlines, aoi, affine=np.eye(4)) expected = np.minimum(expected, expected[::-1]) npt.assert_array_almost_equal(pl[x, x, x], expected) aoi[19, 19, 19] = 0 aoi[1, 1, 1] = 1 pl = path_length(streamlines, aoi, affine=np.eye(4)) expected = (x - 1) * np.sqrt(3) expected[0] = 0 npt.assert_array_almost_equal(pl[x, x, x], expected) z = np.zeros(x.shape, x.dtype) streamlines.append(np.array([x, z, z]).T) pl = path_length(streamlines, aoi, affine=np.eye(4)) npt.assert_array_almost_equal(pl[x, x, x], expected) npt.assert_array_almost_equal(pl[x, 0, 0], x) # Only streamlines that pass through aoi contribute to path length so if # all streamlines are duds, plm will be all inf. aoi[:] = 0 aoi[0, 0, 0] = 1 streamlines = [] for i in range(1000): rando = np.random.random(size=(100, 3)) * 19 + .5 assert (rando > .5).all() assert (rando < 19.5).all() streamlines.append(rando) pl = path_length(streamlines, aoi, affine=np.eye(4)) npt.assert_array_almost_equal(pl, -1) pl = path_length(streamlines, aoi, affine=np.eye(4), fill_value=-12.) npt.assert_array_almost_equal(pl, -12.)
""" Now we calculate the Path Length Map using the corpus callosum streamline bundle and corpus callosum ROI. NOTE: the mask used to seed the tracking does not have to be the Path Length Map base ROI, as we do here, but it often makes sense for them to be the same ROI if we want a map of the whole brain's distance back to our ROI. (e.g. we could test a hypothesis about the motor system by making a streamline bundle model of the cortico-spinal track (CST) and input a lesion mask as our Path Length Map base ROI to restrict the analysis to the CST) """ path_length_map_base_roi = seed_mask # calculate the WMPL wmpl = path_length(streamlines, affine, path_length_map_base_roi) # save the WMPL as a nifti save_nifti('example_cc_path_length_map.nii.gz', wmpl.astype(np.float32), affine) # get the T1 to show anatomical context of the WMPL t1_fname = get_fnames('stanford_t1') t1_data = load_nifti_data(t1_fname) fig = mpl.pyplot.figure() fig.subplots_adjust(left=0.05, right=0.95) ax = AxesGrid(fig, 111, nrows_ncols=(1, 3), cbar_location="right",
Now we calculate the Path Length Map using the corpus callosum streamline bundle and corpus callosum ROI. NOTE: the mask used to seed the tracking does not have to be the Path Length Map base ROI, as we do here, but it often makes sense for them to be the same ROI if we want a map of the whole brain's distance back to our ROI. (e.g. we could test a hypothesis about the motor system by making a streamline bundle model of the cortico-spinal track (CST) and input a lesion mask as our Path Length Map base ROI to restrict the analysis to the CST) """ path_length_map_base_roi = seed_mask # calculate the WMPL wmpl = path_length(streamlines, path_length_map_base_roi, affine) # save the WMPL as a nifti path_length_img = nib.Nifti1Image(wmpl.astype(np.float32), affine) nib.save(path_length_img, 'example_cc_path_length_map.nii.gz') # get the T1 to show anatomical context of the WMPL fetch_stanford_t1() t1 = read_stanford_t1() t1_data = t1.get_data() fig = mpl.pyplot.figure() fig.subplots_adjust(left=0.05, right=0.95) ax = AxesGrid(fig, 111, nrows_ncols=(1, 3),