def track(dname, fdwi, fbval, fbvec, fmask=None, seed_density=1, show=False):

    data, affine = load_nifti(fdwi)
    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    gtab = gradient_table(bvals, bvecs, b0_threshold=50)

    if fmask is None:
        from dipy.segment.mask import median_otsu
        b0_mask, mask = median_otsu(
            data)  # TODO: check parameters to improve the mask
    else:
        mask, mask_affine = load_nifti(fmask)
        mask = np.squeeze(mask)  #fix mask dimensions

    # compute DTI model
    from dipy.reconst.dti import TensorModel
    tenmodel = TensorModel(gtab)  #, fit_method='OLS') #, min_signal=5000)

    # fit the dti model
    tenfit = tenmodel.fit(data, mask=mask)

    # save fa
    ffa = dname + 'tensor_fa.nii.gz'

    fa_img = nib.Nifti1Image(tenfit.fa.astype(np.float32), affine)
    nib.save(fa_img, ffa)

    sh_order = 8  #TODO: check what that does
    if data.shape[-1] < 15:
        raise ValueError('You need at least 15 unique DWI volumes to '
                         'compute fiber ODFs. You currently have: {0}'
                         ' DWI volumes.'.format(data.shape[-1]))
    elif data.shape[-1] < 30:
        sh_order = 6

    # compute the response equation ?
    from dipy.reconst.csdeconv import auto_response
    response, ratio = auto_response(gtab, data)
    response = list(response)

    peaks_sphere = get_sphere('symmetric362')

    #TODO: check what that does
    peaks_csd = peaks_from_model(
        model=tenmodel,
        data=data,
        sphere=peaks_sphere,
        relative_peak_threshold=.5,  #.5
        min_separation_angle=25,
        mask=mask,
        return_sh=True,
        sh_order=sh_order,
        normalize_peaks=True,
        parallel=False)

    peaks_csd.affine = affine
    fpeaks = dname + 'peaks.npz'
    save_peaks(fpeaks, peaks_csd)

    from dipy.io.trackvis import save_trk
    from dipy.tracking import utils
    from dipy.tracking.local import (ThresholdTissueClassifier, LocalTracking)

    stopping_thr = 0.25  #0.25

    pam = load_peaks(fpeaks)

    #ffa = dname + 'tensor_fa_nomask.nii.gz'
    fa, fa_affine = load_nifti(ffa)

    classifier = ThresholdTissueClassifier(fa, stopping_thr)

    # seeds

    seed_mask = fa > 0.4  #0.4 #TODO: check this parameter

    seeds = utils.seeds_from_mask(seed_mask,
                                  density=seed_density,
                                  affine=affine)

    # tractography, if affine then in world coordinates
    streamlines = LocalTracking(pam,
                                classifier,
                                seeds,
                                affine=affine,
                                step_size=.5)

    # Compute streamlines and store as a list.
    streamlines = list(streamlines)

    ftractogram = dname + 'tractogram.trk'

    #save .trk
    save_trk_old_style(ftractogram, streamlines, affine, fa.shape)

    if show:
        #render
        show_results(data, streamlines, fa, fa_affine)
def simple_viewer(streamlines, vol, affine):

    from dipy.viz import actor, window

    renderer = window.Renderer()
    renderer.add(actor.line(streamlines))
    renderer.add(actor.slicer(vol, affine))
    window.show(renderer)


dname = '/Users/ghfc/Desktop/Celine/brainhack/data/'

fdwi = dname + '2dseq_conv_32.nii.gz'

data, affine = load_nifti(fdwi)

fbval = dname + 'P64_F01.bval'
fbvec = dname + 'P64_F01.bvec'

bvals, bvecs = read_bvals_bvecs(fbval, fbvec)

gtab = gradient_table(bvals, bvecs, b0_threshold=50)

fmask = dname + 'mask_extern.nii'

mask_vol, mask_affine = load_nifti(fmask)

sh_order = 8
if data.shape[-1] < 15:
    raise ValueError('You need at least 15 unique DWI volumes to '
def simple_viewer(streamlines, vol, affine):

    from dipy.viz import actor, window

    renderer = window.Renderer()
    renderer.add(actor.line(streamlines))
    renderer.add(actor.slicer(vol, affine))
    window.show(renderer)


dname = "/Users/ghfc/Desktop/Celine/brainhack/data/"

fdwi = dname + "2dseq_conv_32.nii.gz"

data, affine = load_nifti(fdwi)

fbval = dname + "P64_F01.bval"
fbvec = dname + "P64_F01.bvec"

bvals, bvecs = read_bvals_bvecs(fbval, fbvec)

gtab = gradient_table(bvals, bvecs, b0_threshold=50)

fmask = dname + "mask_extern.nii"

mask_vol, mask_affine = load_nifti(fmask)

sh_order = 8
if data.shape[-1] < 15:
    raise ValueError(
def track(dname, fdwi, fbval, fbvec, fmask=None, seed_density = 1, show=False):
    
    data, affine = load_nifti(fdwi)
    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
    gtab = gradient_table(bvals, bvecs, b0_threshold=50)
    
    if fmask is None: 
        from dipy.segment.mask import median_otsu
        b0_mask, mask = median_otsu(data)  # TODO: check parameters to improve the mask
    else: 
        mask, mask_affine = load_nifti(fmask)
        mask = np.squeeze(mask) #fix mask dimensions 
        
    # compute DTI model
    from dipy.reconst.dti import TensorModel
    tenmodel = TensorModel(gtab)#, fit_method='OLS') #, min_signal=5000)
    
    # fit the dti model
    tenfit = tenmodel.fit(data, mask=mask)
    
    # save fa
    ffa = dname + 'tensor_fa.nii.gz'

    fa_img = nib.Nifti1Image(tenfit.fa.astype(np.float32), affine)
    nib.save(fa_img, ffa)
    
    sh_order = 8 #TODO: check what that does
    if data.shape[-1] < 15:
        raise ValueError('You need at least 15 unique DWI volumes to '
                         'compute fiber ODFs. You currently have: {0}'
                         ' DWI volumes.'.format(data.shape[-1]))
    elif data.shape[-1] < 30:
        sh_order = 6
        
    # compute the response equation ?
    from dipy.reconst.csdeconv import auto_response
    response, ratio = auto_response(gtab, data)
    response = list(response)
    
    peaks_sphere = get_sphere('symmetric362')

    #TODO: check what that does
    peaks_csd = peaks_from_model(model=tenmodel,
                                 data=data,
                                 sphere=peaks_sphere,
                                 relative_peak_threshold=.5, #.5
                                 min_separation_angle=25,
                                 mask=mask,
                                 return_sh=True,
                                 sh_order=sh_order,
                                 normalize_peaks=True,
                                 parallel=False)
    
    peaks_csd.affine = affine
    fpeaks = dname + 'peaks.npz'
    save_peaks(fpeaks, peaks_csd)
    
    from dipy.io.trackvis import save_trk
    from dipy.tracking import utils
    from dipy.tracking.local import (ThresholdTissueClassifier,
                                     LocalTracking)

    stopping_thr = 0.25   #0.25

    pam = load_peaks(fpeaks)

    #ffa = dname + 'tensor_fa_nomask.nii.gz'
    fa, fa_affine = load_nifti(ffa)

    classifier = ThresholdTissueClassifier(fa,
                                           stopping_thr)
    
    # seeds 
    

    seed_mask = fa > 0.4 #0.4 #TODO: check this parameter

    seeds = utils.seeds_from_mask(
        seed_mask,
        density=seed_density,
        affine=affine)
    
    # tractography, if affine then in world coordinates
    streamlines = LocalTracking(pam, classifier,
                                seeds, affine=affine, step_size=.5)

    # Compute streamlines and store as a list.
    streamlines = list(streamlines)

    ftractogram = dname + 'tractogram.trk'

    #save .trk
    save_trk_old_style(ftractogram, streamlines, affine, fa.shape)

    if show:
        #render
        show_results(data,streamlines, fa, fa_affine)