for s_id in np.arange(len(source_ids)):
    print '-----------------------------------------------------------------------------------------'
    print 'Source: ', source_ids[s_id]
    source = str(source_ids[s_id])

    flirt_filename = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Code/data/' + source+'/DIFF2DEPI_EKJ_64dirs_14/DTI/flirt.mat'
    fa_filename= '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_fa_brain.nii.gz' 
        
    tracks_filename = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti.trk'
    linear_filename =  '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_linear.dpy'        
    warp_tracks_linearly(flirt_filename,fa_filename, tracks_filename,linear_filename)
    print 'Saved ', linear_filename
    tract_linear = load_whole_tract(linear_filename)
    if save:
        from common_functions import save_tract_trk
        fa_warped = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Code/data/' + source+'/DIFF2DEPI_EKJ_64dirs_14/DTI/fa_warped.nii.gz'     
        fname_out = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_linear.trk'        
                
        save_tract_trk(tract_linear, fa_warped, fname_out )
        print 'Saved ', fname_out
    if vis:
        tract = load_whole_tract(tracks_filename)        
        from dipy.viz import fvtk
        from common_functions import visualize_tract
        ren = fvtk.ren()
        visualize_tract(ren, tract,color=fvtk.red)
        visualize_tract(ren, tract_linear,color=fvtk.blue)
        fvtk.show(ren)
    clearall()
        
  cst_ext_vtk_fname = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + subj + '_cst_L_tvis_ext.vtk'
  '''
  
  #CST_Right
  #saving CST as trk file
  s_file = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/tvis_tractography/' + subj + '_tracks_dti_tvis.trk'
  #cst_ind = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + subj + '_corticospinal_R_tvis.pkl'            
  #cst_tract = load_tract(s_file,cst_ind)
  cst_ext_ind  = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/50_SFF_in_ext/ROI_seg_native/' + subj + '_cst_R_tvis_ext.pkl'    
  cst_tract_ext = load_tract(s_file,cst_ext_ind)    
  
  s_fa = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + subj + '/DTI/dti_fa_brain.nii.gz'
  #cst_trk_fname = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + subj + '_corticospinal_R_tvis.trk'            
  #save_tract_trk(cst_tract, s_fa, cst_trk_fname)
  cst_ext_trk_fname = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + subj + '_cst_R_tvis_ext.trk'            
  save_tract_trk(cst_tract_ext, s_fa, cst_ext_trk_fname) 
  
  #converting trk file to vtk file
  input_anatomy_ref = '/home/bao/tiensy/Lauren_registration/data_compare_mapping/anatomy/' + subj + '_data_brain.nii.gz'  
  #it is a link to '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + subj + '/DTI/data_brain.nii.gz'
  #cst_vtk_fname = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + subj + '_corticospinal_R_tvis.vtk'
  cst_ext_vtk_fname = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + subj + '_cst_R_tvis_ext.vtk'    
 
      
  in_format = str(".trk")
  out_format = ".vtk"
  #input_file = cst_trk_fname
  #output_file = cst_vtk_fname
  
  input_file = cst_ext_trk_fname
  output_file = cst_ext_vtk_fname