def analyze_connectome_lengths(cff_file,track_name, endpointsmm_name, labels_name,make_figures,write_text_files,txt_file_out_dir='N/A', txt_file_prefix='N/A'):
	"""
	load connectome file
	Usage: 
	FLs, EDs, FLsubEDs,fib_labels,c_trk_fibres
	fib_lengths, euclidean_distances, fib_lengths_minus_EuDs,fiber_labels,fibre_arrays = jg_DWI_util.analyze_connectome_lengths(cff_file,track_name, endpointsmm_name, labels_name,make_figures,write_text_files,txt_file_out_dir=<txt_file_name>, txt_file_prefix=<txt_file_prefix>):
	"""
	import cfflib
	from jg_DWI_util import scatter_and_hist, scatter_simple
	from nipype.interfaces.cmtk.cmtk import length as fib_length
	import numpy as np
		
	c = cfflib.load(cff_file)		
	# Print summary of connectome file
	print 'printing cff file summary: ' 
	c.print_summary()
		
	# Get fibers as a numpy array
	c_trk = c.get_by_name(track_name)
	c_trk.load()
	c_trk_fibers = c_trk.get_fibers_as_numpy()
		
	# Get corresponding fiber length and endpoint_mm data arrays
	c_endpointsmm = c.get_by_name(endpointsmm_name)
	c_endpointsmm.load()
	EPs = c_endpointsmm.data
		
	c_labels = c.get_by_name(labels_name)
	c_labels.load()
	fib_labels = c_labels.data
		
	# Calculate Euclidean distances
	EDs = []
	for e in range(0,len(EPs)):
		dist = np.sqrt(np.square(EPs[e,0,0]-EPs[e,1,0])+np.square(EPs[e,0,1]-EPs[e,1,1])+np.square(EPs[e,0,2]-EPs[e,1,2]))
		EDs.append(dist)
		
	# Calculate fiber lengths
	FLs = []
	for t in c_trk_fibers:
		FLs.append(fib_length(t))
	# Fiber length minus Euclidean distance:
	FLsubEDs = np.subtract(FLs,EDs) 
	
	## write to text files
	if write_text_files==1:
		np.savetxt(os.path.join(txt_file_out_dir, txt_file_prefix+'_fibre_lengths.txt'),FLs)
		np.savetxt(os.path.join(txt_file_out_dir, txt_file_prefix+'_Euclidean_distances.txt'),EDs)
		np.savetxt(os.path.join(txt_file_out_dir, txt_file_prefix+'_fibre_labels.txt'),fib_labels)
		
	# (write all to a single excel file ? )
	if make_figures == 1:
		# Plot Euclidean distance vs. Track length for all fibers
		x = FLs#FLs[0:10000]
		y = EDs #EDs[0:10000]
		print 'length of x = ' + str(len(x))
		print 'length of y = ' + str(len(y))
		scatter_and_hist(x,y)
		scatter_simple(x,y)
	return FLs, EDs, FLsubEDs,fib_labels,c_trk_fibers
def rewrite_trk_file_with_ED_vs_FL_scalars(trk_file_orig,trk_file_new, scalar_type):
	"""
	Read in a trackvis file, calculate the Euclidean distance between
	the start and endpoints of each fibre, and write out a new trackvis
	file where each streamline is ooloured according to length, length-ED, 
	or %ED of L
	"""	
	import nibabel as nib
	import numpy as np
	from nipype.interfaces.cmtk.cmtk import length as fib_length
	fibres_orig, hdr_orig = nib.trackvis.read(trk_file_orig, False)
	hdr_new = hdr_orig.copy()
	outstreams = []
	for f in fibres_orig:
		# Calculate fiber lengths	
		FL = fib_length(f[0]) 
		# Calculate Euclidean distance between fibre start and endpoints
		ED = np.sqrt(np.square(f[0][0][0]-f[0][-1][0])+np.square(f[0][0][1]-f[0][-1][1])+np.square(f[0][0][2]-f[0][-1][2]))
		# Fiber length minus Euclidean distance:
		FLsubED = np.subtract(FL, ED)
		ED_as_percent_of_FL = np.divide(100,FL)*ED
		if scalar_type == 'FL':
			scalar_array = np.ones((len(f[0]),1),dtype='float')*FL
			property_array = np.array([FL], dtype='float32')
		if scalar_type == 'ED':
			scalar_array = np.ones((len(f[0]),1),dtype='float')*ED
			property_array = np.array([ED], dtype='float32')
		if scalar_type == 'FLsubED':
			scalar_array = np.ones((len(f[0]),1),dtype='float')*FLsubED
			property_array = np.array([FLsubED], dtype='float32')
		if scalar_type == 'ED_as_percent_of_FL':
			scalar_array = np.ones((len(f[0]),1),dtype='float')*ED_as_percent_of_FL
			property_array = np.array([ED_as_percent_of_FL], dtype='float32')
		new_tuple=tuple([f[0], scalar_array,property_array])				
		outstreams.append(new_tuple)
	n_fib_out = len(outstreams)
	hdr_new['n_count'] = n_fib_out	
	hdr_new['n_scalars'] = np.array(1, dtype='int16')				#hdr_new['scalar_name'] = np.array(['JG_COLOURS', '', '', '', '', '', '', '', '', ''],dtype='|S20')		
	hdr_new['scalar_name'] = np.array([scalar_type, '', '', '', '', '', '', '', '', ''],dtype='|S20')
	hdr_new['n_properties'] = np.array(1, dtype='int16')
#	hdr_new['property_name'] = np.array(['JG_PROPERTY', '', '', '', '', '', '', '', '', ''],dtype='|S20')
	hdr_new['property_name'] = np.array([scalar_type, '', '', '', '', '', '', '', '', ''],dtype='|S20')
	nib.trackvis.write(trk_file_new, outstreams, hdr_new)