def load_tractogram(filename, reference, to_space=Space.RASMM, shifted_origin=False, bbox_valid_check=True, trk_header_check=True): """ Load the stateful tractogram from any format (trk, tck, fib, dpy) Parameters ---------- filename : string Filename with valid extension reference : Nifti or Trk filename, Nifti1Image or TrkFile, Nifti1Header or trk.header (dict), or 'same' if the input is a trk file. Reference that provides the spatial attribute. Typically a nifti-related object from the native diffusion used for streamlines generation space : string Space in which the streamlines will be transformed after loading (vox, voxmm or rasmm) shifted_origin : bool Information on the position of the origin, False is Trackvis standard, default (center of the voxel) True is NIFTI standard (corner of the voxel) Returns ------- output : StatefulTractogram The tractogram to load (must have been saved properly) """ _, extension = os.path.splitext(filename) if extension not in ['.trk', '.tck', '.vtk', '.fib', '.dpy']: logging.error('Output filename is not one of the supported format') return False if to_space not in Space: logging.error('Space MUST be one of the 3 choices (Enum)') return False if reference == 'same': if extension == '.trk': reference = filename else: logging.error('Reference must be provided, "same" is only ' + 'available for Trk file.') return False if trk_header_check and extension == '.trk': if not is_header_compatible(filename, reference): logging.error('Trk file header does not match the provided ' + 'reference') return False timer = time.time() data_per_point = None data_per_streamline = None if extension in ['.trk', '.tck']: tractogram_obj = nib.streamlines.load(filename).tractogram streamlines = tractogram_obj.streamlines if extension == '.trk': data_per_point = tractogram_obj.data_per_point data_per_streamline = tractogram_obj.data_per_streamline elif extension in ['.vtk', '.fib']: streamlines = load_vtk_streamlines(filename) elif extension in ['.dpy']: dpy_obj = Dpy(filename, mode='r') streamlines = list(dpy_obj.read_tracks()) dpy_obj.close() logging.debug('Load %s with %s streamlines in %s seconds', filename, len(streamlines), round(time.time() - timer, 3)) sft = StatefulTractogram(streamlines, reference, Space.RASMM, shifted_origin=shifted_origin, data_per_point=data_per_point, data_per_streamline=data_per_streamline) if to_space == Space.VOX: sft.to_vox() elif to_space == Space.VOXMM: sft.to_voxmm() if bbox_valid_check and not sft.is_bbox_in_vox_valid(): raise ValueError('Bounding box is not valid in voxel space, cannot ' + 'load a valid file if some coordinates are invalid') return sft
# Save Streamlines save_trk("my_streamlines.trk", streamlines=streamlines, affine=np.eye(4)) """ 2. We also work on our HDF5 based file format which can read/write massive datasets (as big as the size of your free disk space). With `Dpy` we can support * direct indexing from the disk * memory usage always low * extensions to include different arrays in the same file Here is a simple example. """ from dipy.io.dpy import Dpy dpw = Dpy('fornix.dpy', 'w') """ Write many streamlines at once. """ dpw.write_tracks(streamlines) """ Write one track """ dpw.write_track(streamlines[0]) """ or one track each time. """ for t in streamlines:
def warp_displacements_tracks(fdpy, ffa, fmat, finv, fdis, fdisa, fref, fdpyw): """ Warp tracks from native space to the FMRIB58/MNI space We use here the fsl displacements. Have a look at create_displacements to see an example of how to use these displacements. Parameters ------------ fdpy : filename of the .dpy file with the tractography ffa : filename of nifti to be warped fmat : filename of .mat (flirt) fdis : filename of displacements (fnirtfileutils) fdisa : filename of displacements (fnirtfileutils + affine) finv : filename of invwarp displacements (invwarp) fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz) fdpyw : filename of the warped tractography See also ----------- dipy.external.fsl.create_displacements """ # read the tracks from the image space dpr = Dpy(fdpy, 'r') T = dpr.read_tracks() dpr.close() # copy them in a new file dpw = Dpy(fdpyw, 'w', compression=1) dpw.write_tracks(T) dpw.close() # from fa index to ref index res = flirt2aff_files(fmat, ffa, fref) # load the reference img imgref = nib.load(fref) refaff = imgref.get_affine() # load the invwarp displacements imginvw = nib.load(finv) invwdata = imginvw.get_data() invwaff = imginvw.get_affine() # load the forward displacements imgdis = nib.load(fdis) disdata = imgdis.get_data() # load the forward displacements + affine imgdis2 = nib.load(fdisa) disdata2 = imgdis2.get_data() # from their difference create the affine disaff = disdata2 - disdata del disdata del disdata2 shape = nib.load(ffa).get_data().shape # transform the displacements affine back to image space disaff0 = affine_transform(disaff[..., 0], res[:3, :3], res[:3, 3], shape, order=1) disaff1 = affine_transform(disaff[..., 1], res[:3, :3], res[:3, 3], shape, order=1) disaff2 = affine_transform(disaff[..., 2], res[:3, :3], res[:3, 3], shape, order=1) # remove the transformed affine from the invwarp displacements di = invwdata[:, :, :, 0] + disaff0 dj = invwdata[:, :, :, 1] + disaff1 dk = invwdata[:, :, :, 2] + disaff2 dprw = Dpy(fdpyw, 'r+') rows = len(dprw.f.root.streamlines.tracks) blocks = np.round(np.linspace(0, rows, 10)).astype(int) # lets work in blocks # print rows for i in range(len(blocks) - 1): # print blocks[i],blocks[i+1] # copy a lot of tracks together caboodle = dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] mci = mc(di, caboodle.T, order=1) # interpolations for i displacement mcj = mc(dj, caboodle.T, order=1) # interpolations for j displacement mck = mc(dk, caboodle.T, order=1) # interpolations for k displacement D = np.vstack((mci, mcj, mck)).T # go back to mni image space WI2 = np.dot(caboodle, res[:3, :3].T) + res[:3, 3] + D # and then to mni world space caboodlew = np.dot(WI2, refaff[:3, :3].T) + refaff[:3, 3] # write back dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] = caboodlew.astype('f4') dprw.close()
def save_tractogram(sft, filename, bbox_valid_check=True): """ Save the stateful tractogram in any format (trk, tck, vtk, fib, dpy) Parameters ---------- sft : StatefulTractogram The stateful tractogram to save filename : string Filename with valid extension Returns ------- output : bool Did the saving work properly """ _, extension = os.path.splitext(filename) if extension not in ['.trk', '.tck', '.vtk', '.fib', '.dpy']: TypeError('Output filename is not one of the supported format') if bbox_valid_check and not sft.is_bbox_in_vox_valid(): raise ValueError('Bounding box is not valid in voxel space, cannot ' + 'save a valid file if some coordinates are invalid') old_space = deepcopy(sft.space) old_shift = deepcopy(sft.shifted_origin) sft.to_rasmm() sft.to_center() timer = time.time() if extension in ['.trk', '.tck']: tractogram_type = detect_format(filename) header = create_tractogram_header(tractogram_type, *sft.space_attribute) new_tractogram = Tractogram(sft.streamlines, affine_to_rasmm=np.eye(4)) if extension == '.trk': new_tractogram.data_per_point = sft.data_per_point new_tractogram.data_per_streamline = sft.data_per_streamline fileobj = tractogram_type(new_tractogram, header=header) nib.streamlines.save(fileobj, filename) elif extension in ['.vtk', '.fib']: save_vtk_streamlines(sft.streamlines, filename, binary=True) elif extension in ['.dpy']: dpy_obj = Dpy(filename, mode='w') dpy_obj.write_tracks(sft.streamlines) dpy_obj.close() logging.debug('Save %s with %s streamlines in %s seconds', filename, len(sft), round(time.time() - timer, 3)) if old_space == Space.VOX: sft.to_vox() elif old_space == Space.VOXMM: sft.to_voxmm() if old_shift: sft.to_corner() return True
def dpy(workingdir, input, compressed=None, restored=None, tol_error=0.01, force=False, coords_only=False, verbose=False): if not input.endswith('tck') and not input.endswith('trk'): # we need to convert print('Invalid format') return None if not compressed: compressed = input + '_compressed' + str(tol_error) + '.dpy' if not restored: restored = input + '_restored' + str(tol_error) + '.tck' original = os.path.join(workingdir, input) # # compression # c_time = -1 if not os.path.exists(os.path.join(workingdir, compressed)) or force: # compress again! t0 = time.time() loaded_original = nib.streamlines.load(original, lazy_load=False) original_streamlines = loaded_original.streamlines # parameters from Presseau15 are 0.01 and inf c_streamlines = compress_streamlines(original_streamlines, tol_error=tol_error, max_segment_length=np.inf) # write dpy file # set compression to highest but it does have any effect dpw = Dpy(os.path.join(workingdir, compressed), mode='w', compression=9) for c_s in c_streamlines: dpw.write_track(c_s) dpw.close() c_time = time.time() - t0 if verbose: print('compression done.') # # restoring # d_time = -1 if not os.path.exists(os.path.join(workingdir, restored)) or force: # restore again! t0 = time.time() restored_data = Dpy(os.path.join(workingdir, compressed), mode='r') restored_streamlines = restored_data.read_tracks() restored_data.close() d_time = time.time() - t0 with open(os.path.join(workingdir, restored), 'w') as f: f.write('restoredok.') if verbose: print('restoring done.') # # calculate errors # stats = compressed + '_stats' + str(tol_error) + '.p' if not os.path.exists(os.path.join(workingdir, stats)) or force: statsdata = Runner.error_per_streamlines(original_streamlines, restored_streamlines) sizestatsdata = Runner.sizestats( os.path.join(workingdir, original), os.path.join(workingdir, compressed)) statsdata = [ c_time, d_time, sizestatsdata, statsdata[0], statsdata[1] ] with open(os.path.join(workingdir, stats), 'wb') as f: pickle.dump(statsdata, f) else: with open(os.path.join(workingdir, stats), 'rb') as f: statsdata = pickle.load(f) [c_time, d_time, sizestatsdata, (min_e, max_e, mean_e, std_e), (end_min_e, end_max_e, end_mean_e, end_std_e)] = \ statsdata if verbose: print('Times', c_time, d_time) print('Size Stats', sizestatsdata) print('Error', min_e, max_e, mean_e, std_e) print('Endpoint Error', end_min_e, end_max_e, end_mean_e, end_std_e) return statsdata
def load_tractogram(filename, reference, to_space=Space.RASMM, to_origin=Origin.NIFTI, bbox_valid_check=True, trk_header_check=True): """ Load the stateful tractogram from any format (trk, tck, vtk, fib, dpy) Parameters ---------- filename : string Filename with valid extension reference : Nifti or Trk filename, Nifti1Image or TrkFile, Nifti1Header or trk.header (dict), or 'same' if the input is a trk file. Reference that provides the spatial attribute. Typically a nifti-related object from the native diffusion used for streamlines generation to_space : Enum (dipy.io.stateful_tractogram.Space) Space to which the streamlines will be transformed after loading to_origin : Enum (dipy.io.stateful_tractogram.Origin) Origin to which the streamlines will be transformed after loading NIFTI standard, default (center of the voxel) TRACKVIS standard (corner of the voxel) bbox_valid_check : bool Verification for negative voxel coordinates or values above the volume dimensions. Default is True, to enforce valid file. trk_header_check : bool Verification that the reference has the same header as the spatial attributes as the input tractogram when a Trk is loaded Returns ------- output : StatefulTractogram The tractogram to load (must have been saved properly) """ _, extension = os.path.splitext(filename) if extension not in ['.trk', '.tck', '.vtk', '.fib', '.dpy']: logging.error('Output filename is not one of the supported format.') return False if to_space not in Space: logging.error('Space MUST be one of the 3 choices (Enum).') return False if reference == 'same': if extension == '.trk': reference = filename else: logging.error('Reference must be provided, "same" is only ' 'available for Trk file.') return False if trk_header_check and extension == '.trk': if not is_header_compatible(filename, reference): logging.error('Trk file header does not match the provided ' 'reference.') return False timer = time.time() data_per_point = None data_per_streamline = None if extension in ['.trk', '.tck']: tractogram_obj = nib.streamlines.load(filename).tractogram streamlines = tractogram_obj.streamlines if extension == '.trk': data_per_point = tractogram_obj.data_per_point data_per_streamline = tractogram_obj.data_per_streamline elif extension in ['.vtk', '.fib']: streamlines = load_vtk_streamlines(filename) elif extension in ['.dpy']: dpy_obj = Dpy(filename, mode='r') streamlines = list(dpy_obj.read_tracks()) dpy_obj.close() logging.debug('Load %s with %s streamlines in %s seconds.', filename, len(streamlines), round(time.time() - timer, 3)) sft = StatefulTractogram(streamlines, reference, Space.RASMM, origin=Origin.NIFTI, data_per_point=data_per_point, data_per_streamline=data_per_streamline) sft.to_space(to_space) sft.to_origin(to_origin) if bbox_valid_check and not sft.is_bbox_in_vox_valid(): raise ValueError('Bounding box is not valid in voxel space, cannot ' 'load a valid file if some coordinates are invalid.\n' 'Please set bbox_valid_check to False and then use ' 'the function remove_invalid_streamlines to discard ' 'invalid streamlines.') return sft
def save_tractogram(sft, filename, bbox_valid_check=True): """ Save the stateful tractogram in any format (trk, tck, vtk, fib, dpy) Parameters ---------- sft : StatefulTractogram The stateful tractogram to save filename : string Filename with valid extension bbox_valid_check : bool Verification for negative voxel coordinates or values above the volume dimensions. Default is True, to enforce valid file. Returns ------- output : bool True if the saving operation was successful """ _, extension = os.path.splitext(filename) if extension not in ['.trk', '.tck', '.vtk', '.fib', '.dpy']: raise TypeError('Output filename is not one of the supported format.') if bbox_valid_check and not sft.is_bbox_in_vox_valid(): raise ValueError('Bounding box is not valid in voxel space, cannot ' 'load a valid file if some coordinates are invalid.\n' 'Please set bbox_valid_check to False and then use ' 'the function remove_invalid_streamlines to discard ' 'invalid streamlines.') old_space = deepcopy(sft.space) old_origin = deepcopy(sft.origin) sft.to_rasmm() sft.to_center() timer = time.time() if extension in ['.trk', '.tck']: tractogram_type = detect_format(filename) header = create_tractogram_header(tractogram_type, *sft.space_attributes) new_tractogram = Tractogram(sft.streamlines, affine_to_rasmm=np.eye(4)) if extension == '.trk': new_tractogram.data_per_point = sft.data_per_point new_tractogram.data_per_streamline = sft.data_per_streamline fileobj = tractogram_type(new_tractogram, header=header) nib.streamlines.save(fileobj, filename) elif extension in ['.vtk', '.fib']: save_vtk_streamlines(sft.streamlines, filename, binary=True) elif extension in ['.dpy']: dpy_obj = Dpy(filename, mode='w') dpy_obj.write_tracks(sft.streamlines) dpy_obj.close() logging.debug('Save %s with %s streamlines in %s seconds.', filename, len(sft), round(time.time() - timer, 3)) sft.to_space(old_space) sft.to_origin(old_origin) return True
self.b.pack(side=Tkinter.BOTTOM) def ok(self): self.value = self.s.get() self.parent.destroy() if __name__ == '__main__': #load T1 volume registered in MNI space #img = nib.load('data/subj_05/MPRAGE_32/T1_flirt_out.nii.gz') #data = img.get_data() #affine = img.get_affine() #load the tracks registered in MNI space fdpyw = 'data/subj_05/101_32/DTI/tracks_gqi_1M_linear.dpy' dpr = Dpy(fdpyw, 'r') T = dpr.read_tracks() dpr.close() #load initial QuickBundles with threshold 30mm fpkl = 'data/subj_05/101_32/DTI/qb_gqi_1M_linear_30.pkl' #qb=QuickBundles(T,30.,12) #save_pickle(fpkl,qb) qb = load_pickle(fpkl) #create the interaction system for tracks tl = TrackLabeler('Bundle Picker', qb, qb.downsampled_tracks(), vol_shape=(182, 218, 182), tracks_alpha=1) #add a interactive slicing/masking tool
#d101='/home/eg309/Data/TEST_MR10032/subj_10/101/' d101='/home/eg309/Data/PROC_MR10032/subj_10/101/' ffa=d101+'1312211075232351192010092912092080924175865ep2dadvdiffDSI10125x25x25STs005a001_bet_FA.nii.gz' fdis=d101+'1312211075232351192010092912092080924175865ep2dadvdiffDSI10125x25x25STs005a001_nonlin_displacements.nii.gz' ffareg=d101+'1312211075232351192010092912092080924175865ep2dadvdiffDSI10125x25x25STs005a001_bet_FA_reg.nii.gz' flirtaff=d101+'1312211075232351192010092912092080924175865ep2dadvdiffDSI10125x25x25STs005a001_affine_transf.mat' ftrack=d101+'1312211075232351192010092912092080924175865ep2dadvdiffDSI10125x25x25STs005a001_QA_native.dpy' froi='/home/eg309/Data/PROC_MR10032/NIFTI_ROIs/AnatomicalROIs/ROI01_GCC.nii' froi2='/home/eg309/Data/PROC_MR10032/NIFTI_ROIs/AnatomicalROIs/ROI02_BCC.nii' #froi3='/home/eg309/Data/PROC_MR10032/NIFTI_ROIs/AnatomicalROIs/ROI03_SCC.nii' froi3='/home/eg309/Downloads/SCC_analyze.nii' ref_fname = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' dpr=Dpy(ftrack,'r') print dpr.track_no T=dpr.read_indexed([0,1,2,3,2000,1000000]) for t in T: print t.shape dpr.close() track=T[4] im2im = flirt2aff_files(flirtaff, ffa, ref_fname) #ref_name to be replaced by ffareg print im2im from dipy.core.track_metrics import length print len(track) print length(track) #ntrack=np.dot(im2im[:3,:3],track.T)+im2im[:3,[3]]