#=============================================================================

if not os.path.exists(Msym_file) and not os.path.exists(Mdir_file):

    print '\tCalculating peaks'
    csamodel = shm.CsaOdfModel(gtab, 6)
    csapeaks = peaks.peaks_from_model(model=csamodel,
                                      data=dwi_data,
                                      sphere=peaks.default_sphere,
                                      relative_peak_threshold=.8,
                                      min_separation_angle=45,
                                      mask=wm_data_bin)

    print '\tTracking'
    seeds = utils.seeds_from_mask(parcellation_wm_data, density=2)
    condition_seeds = condition_seeds(seeds, np.eye(4),
                                      csapeaks.peak_values.shape[:3])
    streamline_generator = EuDX(csapeaks.peak_values,
                                csapeaks.peak_indices,
                                odf_vertices=peaks.default_sphere.vertices,
                                a_low=.05,
                                step_sz=.5,
                                seeds=condition_seeds)
    affine = streamline_generator.affine
    streamlines = list(streamline_generator)

else:
    print '\tTracking already complete'

#=============================================================================
# Create two connectivity matrices - symmetric and directional
#=============================================================================
#=============================================================================

if not os.path.exists(Msym_file) and not os.path.exists(Mdir_file):

    print '\tCalculating peaks'
    csamodel = shm.CsaOdfModel(gtab, 6)
    csapeaks = peaks.peaks_from_model(model=csamodel,
                                      data=dwi_data,
                                      sphere=peaks.default_sphere,
                                      relative_peak_threshold=.8,
                                      min_separation_angle=45,
                                      mask=wm_data_bin)
                                      
    print '\tTracking'
    seeds = utils.seeds_from_mask(parcellation_wm_data, density=2)
    condition_seeds = condition_seeds(seeds, np.eye(4), csapeaks.peak_values.shape[:3])
    streamline_generator = EuDX(csapeaks.peak_values, csapeaks.peak_indices,
                                odf_vertices=peaks.default_sphere.vertices,
                                a_low=.05, step_sz=.5, seeds=condition_seeds)
    affine = streamline_generator.affine
    streamlines = list(streamline_generator)
    
else:
    print '\tTracking already complete'

#=============================================================================
# Create two connectivity matrices - symmetric and directional
#=============================================================================
if not os.path.exists(Msym_file) and not os.path.exists(Mdir_file):
 
    print '\tCreating Connectivity Matrix'
	tensor_fit = tensor_model.fit(data, mask)

	FA = fractional_anisotropy(tensor_fit.evals)
	stopping_values = np.zeros(csapeaks.peak_values.shape)
	stopping_values[:] = FA[..., None]

	'''
	Now we can use EuDX to track all of the white matter. To keep things reasonably
	fast we use `density=2` which will result in 8 seeds per voxel. We'll set
	`a_low` (the parameter which determines the threshold of FA/QA under which
	tracking stops) to be very low because we've already applied a white matter
	mask.
	'''
	print '\tTracking'
	seeds = utils.seeds_from_mask(mask, density=2)
	condition_seeds = condition_seeds(seeds, np.eye(4),
	csapeaks.peak_values.shape[:3], verbose=1)   # Here's the line for 
	streamline_generator = EuDX(stopping_values, csapeaks.peak_indices,
	                            odf_vertices=peaks.default_sphere.vertices,
	                            a_low=.05, step_sz=.5, seeds=condition_seeds)
	affine = streamline_generator.affine
	streamlines = list(streamline_generator)

	'''
	Streamlines are a path though the 3d space of an image represented by a
	set of points. For these points to have a meaningful interpretation, these
	points must be given in a known coordinate system. The ``affine`` attribute of
	the ``streamline_generator`` object specifies the coordinate system of the
	points with respect to the voxel indices of the input data.

	Next we want to create a matrix of streamline connections. To do this we can use
	the `connectivity_matrix` function. This function takes a set of streamlines