In [1]:
import os.path as op
import time
import numpy as np
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import tools
from imp import reload
reload(tools)
from tools import syn_registration, affine_registration
%matplotlib inline


import dipy.tracking.streamline as dts
import dipy.tracking.utils as dtu
import dipy.data as dpd

In [2]:
import nibabel as nib
import dipy.data as dpd
import os

In [3]:
afqpath = '/Users/arokem/source/AFQ/'
# afqpath = '/home/jyeatman/git/afq'

In [4]:
ni, gtab = dpd.read_stanford_hardi()


Dataset is already in place. If you want to fetch it again please first remove the folder /Users/arokem/.dipy/stanford_hardi 

In [5]:
hardi_data = ni.get_data()
hardi_affine = ni.get_affine()

In [6]:
b0 = hardi_data[..., gtab.b0s_mask]

In [7]:
mean_b0 = np.mean(b0, -1)

In [8]:
ni_b0 = nib.Nifti1Image(mean_b0, hardi_affine)

In [9]:
ni_b0.to_filename('mean_b0.nii')

In [10]:
plt.matshow(mean_b0[:,:,mean_b0.shape[-1]//2], cmap=cm.bone)


Out[10]:
<matplotlib.image.AxesImage at 0x1197c3e50>

In [11]:
dpd.fetch_mni_template()


Data size is approximately 35MB
Dataset is already in place. If you want to fetch it again please first remove the folder /Users/arokem/.dipy/mni_template 
Out[11]:
({'COPYING': ('https://digital.lib.washington.edu/researchworks/bitstream/handle/1773/33312/COPYING',
   '6e2168072e80aa4c0c20f1e6e52ec0c8'),
  'mni_icbm152_t1_tal_nlin_asym_09a.nii': ('https://digital.lib.washington.edu/researchworks/bitstream/handle/1773/33312/mni_icbm152_t1_tal_nlin_asym_09a.nii',
   '1ea8f4f1e41bc17a94602e48141fdbc8'),
  'mni_icbm152_t2_tal_nlin_asym_09a.nii': ('https://digital.lib.washington.edu/researchworks/bitstream/handle/1773/33312/mni_icbm152_t2_tal_nlin_asym_09a.nii',
   'f41f2e1516d880547fbf7d6a83884f0d')},
 '/Users/arokem/.dipy/mni_template')

In [12]:
MNI_T2 = dpd.read_mni_template()


Data size is approximately 35MB
Dataset is already in place. If you want to fetch it again please first remove the folder /Users/arokem/.dipy/mni_template 

In [13]:
MNI_T2_data = MNI_T2.get_data()

In [14]:
MNI_T2_affine = MNI_T2.get_affine()

In [15]:
#xformed_b0, affine = affine_registration(mean_b0, MNI_T2_data, 
#                                         moving_grid2world=hardi_affine, 
#                                         static_grid2world=MNI_T2_affine)

In [16]:
#plt.matshow(xformed_b0[:, :, xformed_b0.shape[-1]//2], cmap=cm.bone)
#plt.matshow(MNI_T2_data[:, :, MNI_T2_data.shape[-1]//2], cmap=cm.bone)

In [17]:
warped_b0, mapping = syn_registration(mean_b0, MNI_T2_data,
                                      moving_grid2world=hardi_affine, 
                                      static_grid2world=MNI_T2_affine, 
                                      #step_length=0.1,
                                      #sigma_diff=2.0,
                                      metric='CC', dim=3, level_iters = [10, 10, 5], 
                                      #prealign=affine.affine)
                                      prealign=None)


Creating scale space from the moving image. Levels: 3. Sigma factor: 0.200000.
Creating scale space from the static image. Levels: 3. Sigma factor: 0.200000.
Optimizing level 2
Optimizing level 1
Optimizing level 0

In [18]:
plt.matshow(warped_b0[:,:,warped_b0.shape[-1]//2], cmap=cm.bone)
plt.matshow(MNI_T2_data[:, :, MNI_T2_data.shape[-1]//2], cmap=cm.bone)


Out[18]:
<matplotlib.image.AxesImage at 0x1177d2710>

In [19]:
new_ni = nib.Nifti1Image(warped_b0, MNI_T2_affine)

In [20]:
new_ni.to_filename('./warped_b0.nii.gz')

In [21]:
LOCC_ni = nib.load(os.path.join(afqpath,'templates/callosum2/L_Occipital.nii.gz'))
ROCC_ni = nib.load(os.path.join(afqpath,'templates/callosum2/R_Occipital.nii.gz'))
midsag_ni = nib.load(os.path.join(afqpath,'templates/callosum2/Callosum_midsag.nii.gz'))

In [22]:
LOCC_data = LOCC_ni.get_data()
ROCC_data = ROCC_ni.get_data()
midsag_data = midsag_ni.get_data()

In [23]:
import scipy.ndimage as ndim

In [24]:
dilated_midsag = ndim.binary_dilation(midsag_data)

In [25]:
midsag_affine = midsag_ni.get_affine()

In [26]:
#warped_midsag = mapping.transform_inverse(midsag_data)

In [27]:
dilated_midsag.astype(int)


Out[27]:
array([[[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ..., 
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ..., 
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ..., 
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

       ..., 
       [[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ..., 
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ..., 
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]],

       [[0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        ..., 
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0],
        [0, 0, 0, ..., 0, 0, 0]]])

In [28]:
warped_midsag = mapping.transform_inverse(ndim.binary_dilation(midsag_data).astype(int), interpolation='nearest')
warped_LOCC = mapping.transform_inverse(ndim.binary_dilation(LOCC_data).astype(int), interpolation='nearest')
warped_ROCC = mapping.transform_inverse(ndim.binary_dilation(ROCC_data).astype(int), interpolation='nearest')

In [29]:
#bin_warped_LOCC = np.ceil(warped_LOCC)
bin_warped_midsag = np.ceil(warped_midsag)

In [30]:
im = np.sum(np.ceil(bin_warped_midsag), axis=0)

In [31]:
plt.matshow(im)


Out[31]:
<matplotlib.image.AxesImage at 0x117829bd0>

In [32]:
import scipy.ndimage as ndim

In [33]:
#filled_warped_LOCC= ndim.binary_dilation(bin_warped_LOCC, iterations=1).astype(int)
filled_warped_midsag= ndim.binary_fill_holes(bin_warped_midsag.astype(int)).astype(int)

In [34]:
np.unique(filled_warped_midsag)


Out[34]:
array([0, 1])

In [35]:
filled_warped_midsag.shape


Out[35]:
(81, 106, 76)

In [36]:
filled_warped_midsag_ni = nib.Nifti1Image(filled_warped_midsag, hardi_affine)

In [37]:
filled_warped_midsag_ni.to_filename('./warped_midsag.nii.gz')

In [38]:
im = np.sum(np.ceil(ndim.binary_fill_holes(filled_warped_midsag)), axis=0)

In [39]:
plt.matshow(im, cmap=cm.gray)


Out[39]:
<matplotlib.image.AxesImage at 0x101c01450>

In [40]:
sl_from_file = nib.trackvis.read('./csa_white_matter.trk', points_space='rasmm')

In [41]:
all_sl = [s[0] for s in sl_from_file[0]]

In [42]:
xform_sl = [s for s in dtu.move_streamlines(all_sl, np.linalg.inv(hardi_affine)) if s.shape[0]>10]

In [43]:
len(xform_sl)


Out[43]:
499293

In [44]:
select_sl = xform_sl
ROIs = [warped_midsag, warped_LOCC, warped_ROCC]
t0 = time.time()
for r in ROIs:
    select_sl = dts.select_by_rois(select_sl, [r.astype(bool)], [True])

select_sl = list(select_sl)
t1 = time.time()
print(t1 - t0)


162.786913872

In [45]:
import dipy.tracking.streamline as dts

In [46]:
select_sl = dts.orient_by_rois(select_sl, ROIs[0], ROIs[1])

In [47]:
t0 = time.time()
select_sl = [s for s in select_sl]
t1 = time.time()
print(t1 - t0)


0.000786066055298

In [48]:
len(select_sl)


Out[48]:
3831

In [49]:
from dipy.viz import fvtk
from dipy.viz.colormap import line_colors

In [50]:
ren = fvtk.ren()
fvtk.add(ren, fvtk.line(select_sl, line_colors(select_sl)))
fvtk.record(ren, n_frames=1, out_path='callosal_tracks.png', size=(600, 600))
LOCC_ROI_actor = fvtk.contour(warped_LOCC, levels=[1], colors=[(1., 1., 0.)],
                            opacities=[1.])

#ROCC_ROI_actor = fvtk.contour(warped_ROCC, levels=[1], colors=[(1., 1., 0.)],
#                            opacities=[1.])

cc_ROI_actor = fvtk.contour(warped_midsag, levels=[1], colors=[(1., 1., 0.)],
                            opacities=[1.])


for act in [cc_ROI_actor, LOCC_ROI_actor]:#, ROCC_ROI_actor]:
    fvtk.add(ren, act)

fvtk.show(ren)

In [51]:
import IPython.display as display

In [52]:
display.Image(filename='callosal_tracks.png')


Out[52]:

In [53]:
len(select_sl)


Out[53]:
3831

In [79]:
import dipy.tracking.streamlinespeed as speed

fgarray = np.array(speed.set_number_of_points(select_sl, 100))

fgarray.shape

#fcore = np.median(fgarray,axis=0)
#fcore.shape


Out[79]:
(3831, 100, 3)

In [74]:
import dipy.tracking.streamline as dts

In [75]:
reload(dts)


Out[75]:
<module 'dipy.tracking.streamline' from '/Users/arokem/source/dipy/dipy/tracking/streamline.py'>

In [76]:
from dipy.tracking.streamline import values_from_volume

In [ ]:
values_from_volume(b0, fgarray, affine=)

In [56]:
fgarray.shape


Out[56]:
(3831, 100, 3)

In [57]:
fvtk.line?

In [58]:
fgarray[0]


Out[58]:
array([[ 46.28879166,  21.84094238,  38.19010544],
       [ 47.15712863,  22.56239918,  37.9429851 ],
       [ 47.91000543,  23.41079025,  37.72555409],
       [ 48.59187019,  24.31617824,  37.49560429],
       [ 49.25463904,  25.23393412,  37.25914574],
       [ 49.94175591,  26.15055486,  37.10285271],
       [ 50.58691759,  27.10745634,  37.03668639],
       [ 50.98179481,  28.17637754,  37.17255091],
       [ 51.16559246,  29.30403515,  37.34824425],
       [ 51.29448906,  30.44977343,  37.41850663],
       [ 51.29267669,  31.6000877 ,  37.30991591],
       [ 51.21774042,  32.72943419,  37.07990401],
       [ 50.98668064,  33.80691456,  36.7316317 ],
       [ 50.65366896,  34.86047829,  36.39210853],
       [ 50.18277256,  35.87777507,  36.11390485],
       [ 49.56652564,  36.81600494,  35.83733652],
       [ 48.85356627,  37.67839853,  35.5479733 ],
       [ 48.02515344,  38.4383559 ,  35.28492118],
       [ 47.04728276,  38.95344509,  34.95876447],
       [ 45.9795339 ,  39.31894369,  34.71099366],
       [ 44.90754864,  39.72786905,  34.56637454],
       [ 43.8213442 ,  40.09834602,  34.43113778],
       [ 42.69555162,  40.32730808,  34.30173332],
       [ 41.55279399,  40.4694165 ,  34.20066139],
       [ 40.40025309,  40.45657272,  34.12416861],
       [ 39.25393821,  40.30541248,  34.09961085],
       [ 38.11252416,  40.12837874,  34.14005033],
       [ 36.99238508,  39.86081213,  34.23981951],
       [ 35.8955416 ,  39.54566576,  34.42109571],
       [ 34.80928574,  39.21448278,  34.63777613],
       [ 33.75628086,  38.75468363,  34.76476468],
       [ 32.70436058,  38.27627744,  34.79767889],
       [ 31.67940297,  37.74385981,  34.74830527],
       [ 30.67568641,  37.21240992,  34.94601825],
       [ 29.72569716,  36.68983448,  35.34133934],
       [ 28.95783823,  35.98707785,  35.84104648],
       [ 28.39931119,  35.08249099,  36.28611286],
       [ 27.99473734,  34.03776054,  36.56531492],
       [ 27.71974735,  32.92524682,  36.71230141],
       [ 27.44562022,  31.8117526 ,  36.86006357],
       [ 27.31469231,  30.68160288,  37.05569976],
       [ 27.38124661,  29.57094398,  37.36798549],
       [ 27.51214838,  28.44606804,  37.59893791],
       [ 27.57663735,  27.29821037,  37.71996314],
       [ 27.65032281,  26.14445715,  37.73898614],
       [ 27.83031298,  25.00369781,  37.77359636],
       [ 28.09693045,  23.8832825 ,  37.87748485],
       [ 28.46088245,  22.79809608,  38.03752567],
       [ 28.93611301,  21.77155005,  38.27227276],
       [ 29.64033532,  20.91144713,  38.5820968 ],
       [ 30.40745088,  20.10234445,  38.8332332 ],
       [ 30.89487095,  19.17087599,  38.38972654],
       [ 31.32919008,  18.34575699,  37.70816939],
       [ 31.59166902,  17.61904439,  36.85132564],
       [ 31.74407234,  17.05036772,  35.85857913],
       [ 31.82005809,  16.71566322,  34.75613721],
       [ 31.86471409,  16.75394688,  33.6099626 ],
       [ 31.93818029,  17.17492231,  32.53759394],
       [ 31.99499517,  17.75347764,  31.5404434 ],
       [ 32.24269859,  18.42530947,  30.63355267],
       [ 32.2885704 ,  19.27782711,  29.85587601],
       [ 31.96121705,  20.26694956,  29.4975798 ],
       [ 31.30125252,  21.21300983,  29.47648412],
       [ 30.59331565,  22.12495109,  29.42283068],
       [ 29.88664255,  23.04019379,  29.41821782],
       [ 29.15422809,  23.92825987,  29.44573426],
       [ 28.32126408,  24.71702339,  29.5879765 ],
       [ 27.53860156,  25.53847055,  29.80996304],
       [ 26.79936523,  26.36803066,  30.12829378],
       [ 26.09696032,  27.23650407,  30.4280489 ],
       [ 25.41226623,  28.14195401,  30.64672527],
       [ 24.93427306,  29.18187951,  30.77673434],
       [ 24.45381191,  30.23152509,  30.83750119],
       [ 24.05011872,  31.31122648,  30.91999428],
       [ 23.67940882,  32.39497719,  31.07834735],
       [ 23.41806625,  33.51250859,  31.20830261],
       [ 23.23923645,  34.63922306,  31.08031819],
       [ 22.96958658,  35.67585067,  30.64791921],
       [ 22.63450131,  36.71607163,  30.26998226],
       [ 22.36344863,  37.78954107,  29.9364448 ],
       [ 22.04158999,  38.80945224,  29.50379137],
       [ 21.67608988,  39.82505117,  29.09465423],
       [ 21.38335736,  40.87878938,  28.72038171],
       [ 21.12494843,  41.91199569,  28.26952087],
       [ 20.85229989,  42.91563395,  27.7658234 ],
       [ 20.5561939 ,  43.89136013,  27.22012764],
       [ 20.27311141,  44.89754108,  26.72611436],
       [ 20.02297382,  45.97612633,  26.40099475],
       [ 19.99407546,  47.09189489,  26.11508709],
       [ 20.12520607,  48.20536373,  25.83653027],
       [ 20.40832914,  49.31372307,  25.67150188],
       [ 20.7862232 ,  50.388013  ,  25.47290788],
       [ 21.10661519,  51.45999715,  25.19180114],
       [ 21.47198037,  52.52745022,  24.95822981],
       [ 22.26667966,  53.28498776,  25.19859742],
       [ 23.11862342,  53.94501197,  25.61764139],
       [ 24.01559737,  54.58314857,  25.97068993],
       [ 24.93535363,  55.20800069,  26.28844867],
       [ 25.83692275,  55.70898902,  26.79545308],
       [ 26.65026474,  55.93307114,  27.57834435]])

In [59]:
ren = fvtk.ren()
fvtk.add(ren, fvtk.line([s for s in fgarray], [0, .5, 1],opacity=.1))
fvtk.add(ren, fvtk.line(fcore, [1, 1, 0],opacity=1, linewidth = 5))
LOCC_ROI_actor = fvtk.contour(warped_LOCC, levels=[1], colors=[(1., 1., 0.)],
                            opacities=[1.])

#ROCC_ROI_actor = fvtk.contour(warped_ROCC, levels=[1], colors=[(1., 1., 0.)],
#                            opacities=[1.])

cc_ROI_actor = fvtk.contour(warped_midsag, levels=[1], colors=[(1., 1., 0.)],
                            opacities=[1.])


for act in [cc_ROI_actor, LOCC_ROI_actor]:#, ROCC_ROI_actor]:
    fvtk.add(ren, act)

    fvtk.record(ren, n_frames=1, out_path='callosal_core.png', size=(600, 600))

fvtk.show(ren)

In [60]:
display.Image(filename='callosal_core.png')


Out[60]:

In [61]:
if not os.path.exists('fa.nii.gz'):
    import dipy.reconst.dti as dti
    dtmodel = dti.TensorModel(gtab)
    dtfit = dtmodel.fit(hardi_data)
    FAdata = dtfit.fa
    FAimg = nib.Nifti1Image(FAdata, hardi_affine)
    nib.save(FAimg, 'fa.nii.gz')
else:
    FAimg = nib.load('fa.nii.gz')
    FAdata = FAimg.get_data()

In [62]:
FAimg.affine


Out[62]:
array([[   2.,    0.,    0.,  -80.],
       [   0.,    2.,    0., -120.],
       [   0.,    0.,    2.,  -60.],
       [   0.,    0.,    0.,    1.]])

In [63]:
from scipy.interpolate import RegularGridInterpolator

In [64]:
from dipy.align.vector_fields import interpolate_scalar_3d

In [65]:
interpolate_scalar_3d?

In [66]:
import nibabel as nib
import numpy as np

In [67]:
img = nib.Nifti1Image(np.arange(2000, dtype=np.float).reshape(20, 10, 10), np.eye(4))

In [68]:
data = img.get_data()

In [69]:
data.dtype


Out[69]:
dtype('float64')

In [71]:
import values_from_volume


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-71-56c1e79ed69d> in <module>()
----> 1 from dipy.tracking.streamline import values_from_volume

ImportError: cannot import name values_from_volume

In [108]:
fgarray = np.array(speed.set_number_of_points(select_sl, 100))
values = values_from_volume(FAdata, fgarray, affine=None)
w = np.ones(values.shape) / values.shape[0]

In [116]:
w.shape


Out[116]:
(3831, 100)

In [117]:
tract_profile = np.sum(w * values, 0)

In [ ]:


In [118]:
plt.plot(tract_profile)


Out[118]:
[<matplotlib.lines.Line2D at 0x1e10d1690>]

In [92]:
%matplotlib inline
plt.plot(np.mean(values, 0))


Out[92]:
[<matplotlib.lines.Line2D at 0x1e11a8790>]

In [119]:
fgarray.shape


Out[119]:
(3831, 100, 3)

In [125]:



Out[125]:
array([[  7.79696493,   2.73080157,   2.7134098 ],
       [  2.73080157,  18.59148405,  -0.16630518],
       [  2.7134098 ,  -0.16630518,  48.61044694]])

In [123]:
import scipy.stats as stat


Out[123]:
array([[ 8487139.64080319,  3793980.19013384,  7344281.52260756],
       [ 3793980.19013384,  1763834.39140575,  3280315.58525439],
       [ 7344281.52260756,  3280315.58525439,  6545899.77212253]])

In [ ]:
#def gaussian_weights(fgarray)

for ii in range(fgarray.shape[1]):
    cov = np.cov(fgarray[:, ii, :].T)

In [134]:
import dipy.tracking.streamlinespeed as speed

def calculate_tract_profile(img, streamlines, affine=None, n_points=100, 
                            weighting=None):
    """ 
    
    Parameters
    ---------- 
    img : 3D volume
    
    streamlines : list of arrays, or array
    
    weighting : 1D array, or str (optional)
    
        'gaussian'
        'distance'
        'uniform' (default)
    
    """
    if isinstance(streamlines, list):
        # Resample each streamline to the same number of points 
        # list => array
        fgarray = np.array(speed.set_number_of_points(streamlines, n_points))

    values = values_from_volume(img, fgarray, affine=affine)
    
    # We assume that weights *always sum to 1 across streamlines*:
    if weighting is None: 
        w = np.ones(values.shape) / values.shape[0]
        
    #elif weighting == 'gaussian':
    #    w = gaussian_weights(fgarray)
    
    #elif weighting == 'distance':

    tract_profile = np.sum(w * values, 0)

    #tract_profile = np.mean(w * values, 0)
    
    if weighting is not None:
        return weights, tract_profile
    else: 
        return tract_profile

In [135]:
tract_profile = calculate_tract_profile(FAdata, select_sl)

In [136]:
plt.plot(tract_profile)


Out[136]:
[<matplotlib.lines.Line2D at 0x1d5990150>]

In [ ]:
affine = np.array([[0.5, 0, 0, 10], [0, 0.5, 0, -10], [0, 0, 0.5, 0], [0, 0, 0, 1]])

In [ ]:
xform_streamlines = dtu.move_streamlines(streamlines, affine)

In [ ]:
streamlines

In [ ]:
list(xform_streamlines)

In [ ]:
vv = vals_from_img(img, np.array(streamlines), sl_affine=affine)

In [ ]:
vv

In [ ]:
debug

In [ ]:
vv[0][0]

In [ ]:
debug

In [ ]:
np.array([[1, 0, 0], [1.5, 0, 0], [2, 0, 0]])

In [ ]:
import dipy.align.vector_fields as vfu

In [ ]:
np.random.seed(3121121)
sz = 64
target_shape = (sz, sz, sz)
image = np.empty(target_shape)
image[...] = np.random.randint(0, 10, np.size(image)).reshape(target_shape)
# Select some coordinates to interpolate at
nsamples = 200
locations =\
    np.random.ranf(3 * nsamples).reshape((nsamples, 3)) * (sz + 2) - 1.0

# Call the implementation under test
interp, inside = vfu.interpolate_scalar_3d(image, locations)

In [ ]:
vfu.interpolate_scalar_nn_3d?

In [ ]:
image.shape

In [ ]:
interp[0]

In [ ]:
data = np.arange(2000).reshape(20, 10, 10)

In [ ]:
data[1.5, 0, 0]

In [ ]: