def fg_from_trk(trk_file, affine=None): """ Read data from a trackvis .trk file and create a FiberGroup object according to the information in it. """ # Generate right away, since we're going to do it anyway: read_trk = tv.read(trk_file, as_generator=False) fibers_trk = read_trk[0] # Per default read from the affine from the file header: if affine is not None: aff = affine else: hdr = read_trk[1] aff = tv.aff_from_hdr(hdr) # If the header contains a bogus affine, we revert to np.eye(4), so we # don't get into trouble later: try: np.matrix(aff).getI() except np.linalg.LinAlgError: e_s = "trk file contains bogus header, reverting to np.eye(4)" warnings.warn(e_s) aff = np.eye(4) fibers = [] for f in fibers_trk: fibers.append(ozf.Fiber(np.array(f[0]).T, affine=aff)) return ozf.FiberGroup(fibers, affine=aff)
def fg_from_trk(trk_file, affine=None): """ Read data from a trackvis .trk file and create a FiberGroup object according to the information in it. """ # Generate right away, since we're going to do it anyway: read_trk = tv.read(trk_file, as_generator=False) fibers_trk = read_trk[0] # Per default read from the affine from the file header: if affine is not None: aff = affine else: hdr = read_trk[1] aff= tv.aff_from_hdr(hdr) # If the header contains a bogus affine, we revert to np.eye(4), so we # don't get into trouble later: try: np.matrix(aff).getI() except np.linalg.LinAlgError: e_s = "trk file contains bogus header, reverting to np.eye(4)" warnings.warn(e_s) aff = np.eye(4) fibers = [] for f in fibers_trk: fibers.append(ozf.Fiber(np.array(f[0]).T,affine=aff)) return ozf.FiberGroup(fibers, affine=aff)
def _run_interface(self, runtime): # Loading the ROI file import nibabel as nib import numpy as np from dipy.tracking import utils img = nib.load(self.inputs.ROI_file) data = img.get_data() affine = img.get_affine() # Getting the FA file img = nib.load(self.inputs.FA_file) FA_data = img.get_data() FA_affine = img.get_affine() # Loading the streamlines from nibabel import trackvis streams, hdr = trackvis.read(self.inputs.trackfile,points_space='rasmm') streamlines = [s[0] for s in streams] streamlines_affine = trackvis.aff_from_hdr(hdr,atleast_v2=True) # Checking for negative values from dipy.tracking._utils import _mapping_to_voxel, _to_voxel_coordinates endpoints = [sl[0::len(sl)-1] for sl in streamlines] lin_T, offset = _mapping_to_voxel(affine, (1.,1.,1.)) inds = np.dot(endpoints, lin_T) inds += offset negative_values = np.where(inds <0)[0] for negative_value in sorted(negative_values, reverse=True): del streamlines[negative_value] # Constructing the streamlines matrix matrix,mapping = utils.connectivity_matrix(streamlines=streamlines,label_volume=data,affine=streamlines_affine,symmetric=True,return_mapping=True,mapping_as_streamlines=True) matrix[matrix < 10] = 0 # Constructing the FA matrix dimensions = matrix.shape FA_matrix = np.empty(shape=dimensions) for i in range(0,dimensions[0]): for j in range(0,dimensions[1]): if matrix[i,j]: dm = utils.density_map(mapping[i,j], FA_data.shape, affine=streamlines_affine) FA_matrix[i,j] = np.mean(FA_data[dm>0]) else: FA_matrix[i,j] = 0 FA_matrix[np.tril_indices(n=len(FA_matrix))] = 0 FA_matrix = FA_matrix.T + FA_matrix - np.diagonal(FA_matrix) from nipype.utils.filemanip import split_filename _, base, _ = split_filename(self.inputs.trackfile) np.savetxt(base + '_FA_matrix.txt',FA_matrix,delimiter='\t') return runtime
def _run_interface(self, runtime): # Loading the ROI file from dipy.tracking import utils import nibabel as nib import numpy as np import os img = nib.load(self.inputs.ROI_file) data = img.get_data() affine = img.get_affine() # Getting ROI volumes if they haven't been generated if not os.path.isfile('/imaging/jb07/CALM/DWI/FA_connectome/Atlas_volumes.csv'): import nibabel as nib import numpy as np import os import pandas as pd import subprocess atlas_file = ROI_file img = nib.load(atlas_file) data = img.get_data() affine = img.get_affine() volumes = pd.DataFrame() atlas_labels = np.unique(data) for atlas_label in atlas_labels: data = nib.load(atlas_file).get_data() data[data != atlas_label] = 0 data[data == atlas_label] = 1 nib.save(nib.Nifti1Image(data, affine), 'temp.nii.gz') volumes.set_value(atlas_label, 'volume', subprocess.check_output(os.environ['FSLDIR'] + '/bin/fslstats temp.nii.gz -V', shell=True).split(' ')[0]) os.remove('temp.nii.gz') volumes.to_csv('/imaging/jb07/CALM/DWI/FA_connectome/Atlas_volumes.csv') ROI_volumes = pd.read_csv('/home/jb07/CALM/DWI/FA_connectome/Atlas_volumes.csv') # Getting the FA file img = nib.load(self.inputs.FA_file) FA_data = img.get_data() FA_affine = img.get_affine() # Loading the streamlines from nibabel import trackvis streams, hdr = trackvis.read(self.inputs.trackfile,points_space='rasmm') streamlines = [s[0] for s in streams] streamlines_affine = trackvis.aff_from_hdr(hdr,atleast_v2=True) # Checking for negative values from dipy.tracking._utils import _mapping_to_voxel, _to_voxel_coordinates endpoints = [sl[0::len(sl)-1] for sl in streamlines] lin_T, offset = _mapping_to_voxel(affine, (1.,1.,1.)) inds = np.dot(endpoints, lin_T) inds += offset negative_values = np.where(inds <0)[0] for negative_value in sorted(negative_values, reverse=True): del streamlines[negative_value] # Constructing the streamlines matrix matrix,mapping = utils.connectivity_matrix(streamlines=streamlines,label_volume=data,affine=streamlines_affine,symmetric=True,return_mapping=True,mapping_as_streamlines=True) matrix[matrix < 10] = 0 # Constructing the FA matrix dimensions = matrix.shape FA_matrix = np.empty(shape=dimensions) density_matrix = np.empty(shape=dimensions) density_corrected_matrix = np.empty(shape=dimensions) for i in range(0,dimensions[0]): for j in range(0,dimensions[1]): if matrix[i,j]: dm = utils.density_map(mapping[i,j], FA_data.shape, affine=streamlines_affine) FA_matrix[i,j] = np.mean(FA_data[dm>0]) if np.sum(dm > 0) > 0: density_matrix[i,j] = np.sum(dm[dm > 0]) density_corrected_matrix[i,j] = np.sum(dm[dm > 0])/np.sum([ROI_volumes.iloc[i].values.astype('int'), ROI_volumes.iloc[j].values.astype('int')]) else: density_matrix[i,j] = 0 density_corrected_matrix[i,j] = 0 else: FA_matrix[i,j] = 0 density_matrix[i,j] = 0 density_corrected_matrix[i,j] = 0 FA_matrix[np.tril_indices(n=len(FA_matrix))] = 0 FA_matrix = FA_matrix.T + FA_matrix - np.diagonal(FA_matrix) density_matrix[np.tril_indices(n=len(density_matrix))] = 0 density_matrix = density_matrix.T + density_matrix - np.diagonal(density_matrix) density_corrected_matrix[np.tril_indices(n=len(density_corrected_matrix))] = 0 density_corrected_matrix = density_corrected_matrix.T + density_corrected_matrix - np.diagonal(density_corrected_matrix) from nipype.utils.filemanip import split_filename _, base, _ = split_filename(self.inputs.trackfile) np.savetxt(base + '_FA_matrix.txt',FA_matrix,delimiter='\t') np.savetxt(base + '_density_matrix.txt',density_matrix,delimiter='\t') np.savetxt(base + '_volume_corrected_density_matrix.txt',density_corrected_matrix,delimiter='\t')