def combine_connectome_node_volumes(ROI_list_xl_file,ROI_img, new_file):
	"""
	Combines all regions listed in the excel file into a single image volume,
	retaining the numbering. Or, can be thought of as sub-sampling the ROI_image_file
	by removing voxels whose intensity value is not listed in the ROI_list_xl_file
	"""
	import os
	from nipype.interfaces import fsl
	fsl.FSLCommand.set_default_output_type('NIFTI')
	temp_img = new_file.split('.nii')[0]+'_temp.nii'
	ROI_list_dict = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_file)
	thresh_val = str(ROI_list_dict.keys()[0])
	fsl.ImageMaths(in_file=ROI_img, op_string=' -thr ' +thresh_val + ' -uthr ' + thresh_val,
		       out_file=new_file).run()
	for k in range(1, len(ROI_list_dict.keys())):
		thresh_val = str(ROI_list_dict.keys()[k])
		print 'node ' + thresh_val
		fsl.ImageMaths(in_file=ROI_img, op_string=' -thr ' +thresh_val + ' -uthr ' + thresh_val,
		out_file=temp_img).run()
		fsl.ImageMaths(in_file=temp_img,in_file2=new_file, op_string = ' -add ', out_file=new_file).run()
	os.remove(temp_img)
def make_trk_file_for_many_connectome_nodes_from_ROI_list(ROI_list_xl_files,cff_file,trk_file_orig, trk_file_new_base, n_fib_thresh=0):
	"""
	Generalization of 'make_trk_file_for_2_connectome_nodes' to 2 sets of nodes. 
	3 Main differences:
	
	1. Node pairs are read from rows in excel file(s), rather than an indices
	2. Only a base outputfile stem, rather than the full output filename,
	   needs to be provided, as because multiple output files are produced
	3. No option to colour fibres with scalars in this function
	
	In order to find connections within one set of nodes, just supply one excel file, 
	in which case the function will only check half the connections
	
	"""	
	import cfflib
	import nibabel as nib
	c = cfflib.load(cff_file)
	c_labels = c.get_connectome_data()[3]
	c_labels.load()
	c_net = c.get_connectome_network()[0]
	c_net.load()
	#c_fibres = c.get_by_name('Tract file 0').load()
	#c_fibres_array = c_fibres. 
	fibres_orig, hdr_orig = nib.trackvis.read(trk_file_orig, False)
	if len(ROI_list_xl_files)==2:
		two_node_lists = 1
		ROI_list_dict1 = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_files[0])
		ROI_list_dict2 = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_files[1])
	else:
		two_node_lists = 0
		ROI_list_dict1 = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_files)
		ROI_list_dict2 = ROI_list_dict1	
	for k in range(0, len(ROI_list_dict1.keys())):
		for kk in range(0,len(ROI_list_dict2.keys())):
			ROI1_name = str(ROI_list_dict1.values()[k])
			ROI1_number = int(ROI_list_dict1.keys()[k])
			ROI2_name = str(ROI_list_dict2.values()[kk])
			ROI2_number = int(ROI_list_dict2.keys()[kk])
			trk_file_new = trk_file_new_base+'_'+str(ROI1_number)+'_'+ROI1_name+'__to__'+str(ROI2_number)+'_'+ROI2_name+'.trk'
			node_indices = [ROI1_number,ROI2_number]
			node_indices_reversed = [ROI2_number, ROI1_number]
			track_indices = []
			a = np.nonzero(c_labels.data==ROI1_number)[0]
			#print 'a = ' + str(a)
			b = np.nonzero(c_labels.data==ROI2_number)[0]
			#print 'b = ' + str(b)
			if ROI1_number in c_net.data.edge[ROI2_number]:
				n_fibs = c_net.data.edge[ROI2_number][ROI1_number]['number_of_fibers']
			elif ROI2_number in c_net.data.edge[ROI1_number]:
				n_fibs = c_net.data.edge[ROI1_number][ROI2_number]['number_of_fibers']
			else: n_fibs = 0
			if n_fibs>=n_fib_thresh:
				#print 'node indices = ' + str(ROI1_number) + ' ' + str(ROI2_number)
				for a_int in a:
					if a_int in b:
						if two_node_lists == 0:
							if kk>k:
								track_indices.append(a_int)
								print 'found track - index ' + str(a_int) + ' , ROIs ' + str(ROI1_number) + ', ' + str(ROI2_number)	
						else: 
							track_indices.append(a_int)
							print 'found track - index ' + str(a_int) + ' , ROIs ' + str(ROI1_number) + ', ' + str(ROI2_number)
			if not track_indices == []:	
				hdr_new = hdr_orig.copy()
				outstreams = []
				for i in track_indices:
					outstreams.append(fibres_orig[i])
				n_fib_out = len(outstreams)
				hdr_new['n_count'] = n_fib_out	
				nib.trackvis.write(trk_file_new, outstreams, hdr_new)