# HG changeset patch # User greg # Date 1512054660 18000 # Node ID 883327c429700652f442b9db8560da712f0baec1 # Parent 7b98048e19042562d1b20acfb64fe7d81b774e88 Uploaded diff -r 7b98048e1904 -r 883327c42970 create_streamlines.py --- a/create_streamlines.py Thu Nov 30 10:10:54 2017 -0500 +++ b/create_streamlines.py Thu Nov 30 10:11:00 2017 -0500 @@ -1,8 +1,8 @@ #!/usr/bin/env python import argparse +import os import shutil -from dipy.data import fetch_stanford_t1, read_stanford_labels, read_stanford_t1 from dipy.reconst import peaks, shm from dipy.tracking import utils from dipy.tracking.eudx import EuDX @@ -16,20 +16,40 @@ import numpy as np parser = argparse.ArgumentParser() -parser.add_argument('--drmi_dataset', dest='drmi_dataset', help='Input dataset') +parser.add_argument('--input', dest='input', help='Input dataset') +parser.add_argument('--input_extra_files_path', dest='input_extra_files_path', help='Input dataset extra files paths') parser.add_argument('--output_superiorfrontal_nifti', dest='output_superiorfrontal_nifti', help='Output superiorfrontal nifti1 dataset') +parser.add_argument('--output_superiorfrontal_nifti_files_path', dest='output_superiorfrontal_nifti_files_path', help='Output superiorfrontal nifti1 extra files path') parser.add_argument('--output_trackvis_header', dest='output_trackvis_header', help='Output superiorfrontal track visualization header dataset') args = parser.parse_args() -hardi_img, gtab, labels_img = read_stanford_labels() +def move_directory_files(source_dir, destination_dir, copy=False, remove_source_dir=False): + source_directory = os.path.abspath(source_dir) + destination_directory = os.path.abspath(destination_dir) + if not os.path.isdir(destination_directory): + os.makedirs(destination_directory) + for dir_entry in os.listdir(source_directory): + source_entry = os.path.join(source_directory, dir_entry) + if copy: + shutil.copy(source_entry, destination_directory) + else: + shutil.move(source_entry, destination_directory) + if remove_source_dir: + os.rmdir(source_directory) + +# Get input data. +# TODO: do not hard-code 'stanford_hardi' +input_dir = 'stanford_hardi' +os.mkdir(input_dir) +for f in os.listdir(args.input_extra_files_path): + shutil.copy(os.path.join(args.input_extra_files_path, f), input_dir) +hard_img, gtab = read_stanford_hardi() data = hardi_img.get_data() +labels_file = os.path.join(input_dir, "aparc-reduced.nii.gz") +labels_img = nib.load(labels_file) labels = labels_img.get_data() -# For possible later use: if args.drmi_dataset == 'stanford_t1': -#fetch_stanford_t1() -#t1 = read_stanford_t1() -#t1_data = t1.get_data() white_matter = (labels == 1) | (labels == 2) csamodel = shm.CsaOdfModel(gtab, 6) csapeaks = peaks.peaks_from_model(model=csamodel, data=data, sphere=peaks.default_sphere, relative_peak_threshold=.8, min_separation_angle=45, mask=white_matter) @@ -40,35 +60,7 @@ cc_slice = labels == 2 cc_streamlines = utils.target(streamlines, cc_slice, affine=affine) cc_streamlines = list(cc_streamlines) -#other_streamlines = utils.target(streamlines, cc_slice, affine=affine, include=False) -#other_streamlines = list(other_streamlines) -#assert len(other_streamlines) + len(cc_streamlines) == len(streamlines) -## Make display objects -#color = line_colors(cc_streamlines) -#cc_streamlines_actor = fvtk.line(cc_streamlines, line_colors(cc_streamlines)) -#cc_ROI_actor = fvtk.contour(cc_slice, levels=[1], colors=[(1., 1., 0.)], opacities=[1.]) -#vol_actor = fvtk.slicer(t1_data) -#vol_actor.display(40, None, None) -#vol_actor2 = vol_actor.copy() -#vol_actor2.display(None, None, 35) -## Add display objects to canvas -#r = fvtk.ren() -#fvtk.add(r, vol_actor) -#fvtk.add(r, vol_actor2) -#fvtk.add(r, cc_streamlines_actor) -#fvtk.add(r, cc_ROI_actor) -## Save figures -#fvtk.record(r, n_frames=1, out_path="corpuscallosum_axial.png", size=(800, 800)) -#shutil.move("corpuscallosum_axial.png", args.output_corpuscallosum_axial) -#fvtk.camera(r, [-1, 0, 0], [0, 0, 0], viewup=[0, 0, 1]) -#fvtk.record(r, n_frames=1, out_path="corpuscallosum_sagittal.png", size=(800, 800)) -#shutil.move("corpuscallosum_sagittal.png", args.output_corpuscallosum_sagittal) -M, grouping = utils.connectivity_matrix(cc_streamlines, labels, affine=affine, return_mapping=True, mapping_as_streamlines=True) -#M[:3, :] = 0 -#M[:, :3] = 0 -#plt.imshow(np.log1p(M), interpolation='nearest') -#plt.savefig("connectivity.png") -#shutil.move("connectivity.png", args.output_connectivity) + lr_superiorfrontal_track = grouping[11, 54] shape = labels.shape dm = utils.density_map(lr_superiorfrontal_track, shape, affine=affine) @@ -76,6 +68,8 @@ dm_img = nib.Nifti1Image(dm.astype("int16"), hardi_img.affine) dm_img.to_filename("lr-superiorfrontal-dm.nii") shutil.move('lr-superiorfrontal-dm.nii', args.output_superiorfrontal_nifti) +move_directory_files(input_dir, args.output_superiorfrontal_nifti_files_path) + # Make a trackvis header so we can save streamlines voxel_size = labels_img.header.get_zooms() trackvis_header = nib.trackvis.empty_header()