L_temp_n.append(imgtck[i]) elif imgtck[i][j][0] == 0: if (j - 20) in range(len(imgtck[i])) \ and (j + 20) in range(len(imgtck[i])) \ and imgtck[i][j - 20][0] * imgtck[i][j + 20][0] < 0: L_temp_need.append(imgtck[i]) else: L_temp_n.append(imgtck[i]) return L_temp_need, L_temp_n if __name__ == '__main__': from pyfat.io.load import load_tck from pyfat.io.save import save_tck # load data file = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100206/Diffusion/100k_sift_1M45006_dynamic250.tck' imgtck = load_tck(file) # extract CC L_temp = extract_multi_node(imgtck)[1] # print L_temp # save data out_path = '/home/brain/workingdir/data/dwi/hcp/' \ 'preprocessed/response_dhollander/100206/result/CC_multi_node_fib.tck' save_tck(L_temp, imgtck.header, imgtck.tractogram.data_per_streamline, imgtck.tractogram.data_per_point, imgtck.tractogram.affine_to_rasmm, out_path)
import matplotlib.pyplot as plt # load data data_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100408/Structure/T1w_acpc_dc_restore_brain.nii.gz' img = nib.load(data_path) img_data = img.get_data() # tck_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ # 'response_dhollander/100408/result/45006old/CC_fib_ncut_set0-1_1.tck' # tck_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ # 'response_dhollander/100408/result/result20vs45/cc_20fib_step20_new_sample5000.tck' tck_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100408/result/result20vs45/cc_20fib_step20_new_correct.tck' imgtck = load_tck(tck_path) # streamstck = imgtck.streamlines # print streamstck # extract cc fib # imgtck_fib = extract_cc(imgtck) # remove multi-node fib # imgtck_fib = extract_multi_node(imgtck_fib)[0] # step > 20 # imgtck_fib = extract_cc_step(imgtck_fib)[0] # extract node according to x-value Ls_temp = xmin_extract(imgtck) # print len(Ls_temp) # show node or density # show_2d_node(img, Ls_temp)
# fiber density of volume def fib_density_map(volume, fiber, output): ''' fiber density map :param volume: structure image T1w :param fiber: tck file :param output: density map file :return: ''' shape = volume.shape affine = volume.affine streamstck = fiber.streamlines image_volume = ditu.density_map(streamstck, vol_dims=shape, voxel_size=0.625, affine=affine) dm_img = nib.Nifti1Image(image_volume.astype("int16"), affine) dm_img.to_filename(output) if __name__ == '__main__': from pyfat.io.load import load_tck img = nib.load("/home/brain/workingdir/data/dwi/hcp/preprocessed/" "response_dhollander/100206/Structure/T1w_short.nii.gz") img_cc = load_tck('/home/brain/workingdir/data/dwi/hcp/preprocessed/' 'response_dhollander/100206/result/CC_fib.tck') output = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100206/result/CC_fib_density_map.nii.gz' fib_density_map(img, img_cc, output)
# !/usr/bin/python # -*- coding: utf-8 -*- from pyfat.io.load import load_tck from pyfat.io.save import save_tck from pyfat.algorithm.remove_non_cc import extract_up_z data_path = '/home/brain/workingdir/data/dwi/hcp/' \ 'preprocessed/response_dhollander/100206/result/CC_fib.tck' # load tck data img_cc = load_tck(data_path) L_temp = extract_up_z(img_cc) out_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100206/result/CC_fib_remove_non_cc_z-10_x-min.tck' save_tck(L_temp, img_cc.header, img_cc.tractogram.data_per_streamline, img_cc.tractogram.data_per_point, img_cc.tractogram.affine_to_rasmm, out_path)
# !/usr/bin/python # -*- coding: utf-8 -*- from pyfat.io.load import load_tck from pyfat.io.save import save_tck from pyfat.algorithm.node_extract import xmin_extract from pyfat.algorithm.hierarchical_clustering import hierarchical_clust import nibabel.streamlines.array_sequence as nibAS from sklearn.neighbors import kneighbors_graph input_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100206/result/CC_fib.tck' img_cc = load_tck(input_path) Ls_temp = xmin_extract(img_cc) # connectivity = kneighbors_graph(Ls_temp, n_neighbors=10, mode='connectivity', include_self=True) # connectivity = kneighbors_graph(Ls_temp, n_neighbors=10, include_self=False) labels = hierarchical_clust(Ls_temp, 4, linkage='complete') print len(labels) d = zip(labels, Ls_temp) L_temp_0 = nibAS.ArraySequence() L_temp_1 = nibAS.ArraySequence() L_temp_2 = nibAS.ArraySequence() L_temp_3 = nibAS.ArraySequence() for k in range(len(d)): if d[k][0] == 0: L_temp_0.append(img_cc.streamlines[k]) if d[k][0] == 1: L_temp_1.append(img_cc.streamlines[k])
ren.SetBackground(1, 1, 1) fvtk.add(ren, fvtk.streamtube(imgtck.streamlines, colormap_full)) fvtk.record(ren, n_frames=1, out_path=out_path, size=(600, 600)) fvtk.show(ren) if __name__ == '__mian__': from pyfat.io.load import load_tck from dipy.io.pickles import save_pickle from dipy.segment.clustering import QuickBundles from dipy.segment.metric import SumPointwiseEuclideanMetric # load fiber data data_path = '/home/brain/workingdir/data/dwi/hcp/' \ 'preprocessed/response_dhollander/101006/result/CC_fib.tck' imgtck = load_tck(data_path) world_coords = True if not world_coords: from dipy.tracking.streamline import transform_streamlines streamlines = transform_streamlines(imgtck.streamlines, np.linalg.inv(imgtck.affine)) metric = SumPointwiseEuclideanMetric(feature=ArcLengthFeature()) qb = QuickBundles(threshold=2., metric=metric) clusters = qb.cluster(streamlines) # extract > 100 # print len(clusters) # 89 for c in clusters: if len(c) < 100:
# !/usr/bin/python # -*- coding: utf-8 -*- from pyfat.io.load import load_tck from pyfat.algorithm.node_extract import xmin_extract from pyfat.algorithm.node_clustering import hiera_single_clust source_path = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100408/result/result20vs45/cc_20fib_lr1.5_new_correct_sample1000.tck' data = load_tck(source_path) Ls_temp = xmin_extract(data) c = hiera_single_clust(Ls_temp) print c
alpha = 1 prefix = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/100408/result/463_clusters/clusters' # prefix = input('Input the prefix of images:') files = glob.glob(prefix + '_*') num = len(files) filename_lens = [len(x) for x in files] # length of the files min_len = min(filename_lens) # minimal length of filenames max_len = max(filename_lens) # maximal length of filenames if min_len == max_len: # the last number of each filename has the same length files = sorted(files) # sort the files in ascending order else: # maybe the filenames are:x_0.png ... x_10.png ... x_100.png index = [0 for x in range(num)] for i in range(num): filename = files[i] start = filename.rfind('_') + 1 end = filename.rfind('.') file_no = int(filename[start:end]) index[i] = file_no index = sorted(index) files = [prefix + '_' + str(x) + '.tck' for x in index] for f in files[23:]: streamlines = load_tck(f).streamlines vertex = terminus2surface_map(streamlines, geo_path) surface_roi_contour(subjects_dir, subject_id, hemi, surf, alpha, vertex, lr_label) # surface_streamlines_map(subjects_dir, subject_id, hemi, surf, alpha, vertex)
plt.figure('Fiber statistics') plt.subplot(111) plt.title('Length histogram') plt.hist(lengths, color='burlywood') plt.xlabel('Length') plt.ylabel('Count') # save length histogram # plt.legend() # plt.savefig('lr250_sift12_hcp_FFA_projabs-2_length_histogram.png') plt.show() if __name__ == '__main__': from pyfat.io.load import load_tck, load_trk # tck fname_tck = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/lr250_sift12_hcp_FFA_projabs-2.tck' imgtck = load_tck(fname_tck) stream = imgtck.streamlines lengths = list(fib_lengths_count(stream)) show(lengths) # trk fname_trk = '/home/brain/workingdir/data/dwi/hcp/preprocessed/' \ 'response_dhollander/lr250_sift12_hcp_FFA_projabs-2.trk' imgtrk, hdr = load_trk(fname_trk) stream = [s[0] for s in imgtrk] lengths = list(fib_lengths_count(stream)) show(lengths)