def main(argv=None): parser = get_parser() arguments = parser.parse_args(argv) verbose = arguments.v set_loglevel(verbose=verbose) fname_bval_list = arguments.i # Build fname_out if arguments.o is not None: fname_out = arguments.o else: path_in, file_in, ext_in = extract_fname(fname_bval_list[0]) fname_out = path_in + 'bvals_concat' + ext_in # Open bval files and concatenate bvals_concat = '' # for file_i in fname_bval_list: # f = open(file_i, 'r') # for line in f: # bvals_concat += line # f.close() from dipy.data.fetcher import read_bvals_bvecs for i_fname in fname_bval_list: bval_i, bvec_i = read_bvals_bvecs(i_fname, None) bvals_concat += ' '.join(str(v) for v in bval_i) bvals_concat += ' ' # Write new bval new_f = open(fname_out, 'w') new_f.write(bvals_concat) new_f.close()
def main(argv=None): """ Main function :param argv: :return: """ parser = get_parser() arguments = parser.parse_args(argv) verbose = arguments.v set_global_loglevel(verbose=verbose) # check number of input args if not len(arguments.i) == len(arguments.order): raise Exception( "Number of items between flags '-i' and '-order' should be the same." ) if not len(arguments.bval) == len(arguments.bvec): raise Exception( "Number of files for bval and bvec should be the same.") # Concatenate NIFTI files im_list = [Image(fname) for fname in arguments.i] im_concat = concat_data(im_list, dim=3, squeeze_data=False) im_concat.save(arguments.o) printv("Generated file: {}".format(arguments.o)) # Concatenate bvals and bvecs bvals_concat = '' bvecs_concat = ['', '', ''] i_dwi = 0 # counter for DWI files, to read in bvec/bval files for i_item in range(len(arguments.order)): if arguments.order[i_item] == 'b0': # count number of b=0 n_b0 = Image(arguments.i[i_item]).dim[3] bval = np.array([0.0] * n_b0) bvec = np.array([[0.0, 0.0, 0.0]] * n_b0) elif arguments.order[i_item] == 'dwi': # read bval/bvec files bval, bvec = read_bvals_bvecs(arguments.bval[i_dwi], arguments.bvec[i_dwi]) i_dwi += 1 # Concatenate bvals bvals_concat += ' '.join(str(v) for v in bval) bvals_concat += ' ' # Concatenate bvecs for i in (0, 1, 2): bvecs_concat[i] += ' '.join( str(v) for v in map(lambda n: '%.16f' % n, bvec[:, i])) bvecs_concat[i] += ' ' bvecs_concat = '\n'.join( str(v) for v in bvecs_concat) # transform list into lines of strings # Write files new_f = open(arguments.obval, 'w') new_f.write(bvals_concat) new_f.close() printv("Generated file: {}".format(arguments.obval)) new_f = open(arguments.obvec, 'w') new_f.write(bvecs_concat) new_f.close() printv("Generated file: {}".format(arguments.obvec))
def main(): # Get parser info parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_bval_list = arguments["-i"] # Build fname_out if "-o" in arguments: fname_out = arguments["-o"] else: path_in, file_in, ext_in = extract_fname(fname_bval_list[0]) fname_out = path_in + 'bvals_concat' + ext_in # Open bval files and concatenate bvals_concat = '' # for file_i in fname_bval_list: # f = open(file_i, 'r') # for line in f: # bvals_concat += line # f.close() for i_fname in fname_bval_list: bval_i, bvec_i = read_bvals_bvecs(i_fname, None) bvals_concat += ' '.join(str(v) for v in bval_i) bvals_concat += ' ' # Write new bval new_f = open(fname_out, 'w') new_f.write(bvals_concat) new_f.close()
def main(): # Get parser info parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_bval_list = arguments["-i"] # Build fname_out if "-o" in arguments: fname_out = arguments["-o"] else: path_in, file_in, ext_in = extract_fname(fname_bval_list[0]) fname_out = path_in+'bvals_concat'+ext_in # Open bval files and concatenate bvals_concat = '' # for file_i in fname_bval_list: # f = open(file_i, 'r') # for line in f: # bvals_concat += line # f.close() for i_fname in fname_bval_list: bval_i, bvec_i = read_bvals_bvecs(i_fname, None) bvals_concat += ' '.join(str(v) for v in bval_i) bvals_concat += ' ' # Write new bval new_f = open(fname_out, 'w') new_f.write(bvals_concat) new_f.close()
def dmri_single_volumes(tmp_path): """ Create .nii.gz, bvals, and bvecs files for individual dMRI volumes. This prep is necessary because `sct_testing_data` lacks individual bvals and bvecs files for the `dmri_T000#.nii.gz` files, and sct_dmri_separate_b0_and_dwi won't generate new bvals/bvecs file either. """ # Get all bvals/bvecs corresponding to individual dMRI volumes bvals, bvecs = read_bvals_bvecs(sct_test_path('dmri', 'bvals.txt'), sct_test_path('dmri', 'bvecs.txt')) fname_dmri_imgs, fname_bvals, fname_bvecs = [], [], [] for i, (bval, bvec) in enumerate(zip(bvals, bvecs)): # 1. Use existing single-volume dMRI files from sct_testing_data fname_dmri_imgs.append(sct_test_path('dmri', f'dmri_T000{i}.nii.gz')) # 2. Copy bval into single-volume bvals file fname_bval = str(tmp_path / f'bvals_T000{i}.txt') fname_bvals.append(fname_bval) with open(fname_bval, 'w') as f: f.write(str(bval)) # 3. Copy bvec into single-volume bvecs file fname_bvec = str(tmp_path / f'bvecs_T000{i}.txt') fname_bvecs.append(fname_bvec) with open(fname_bvec, 'w') as f: f.write(' '.join(map(str, bvec))) return fname_dmri_imgs, fname_bvals, fname_bvecs
def main(argv=None): parser = get_parser() arguments = parser.parse_args(argv if argv else ['--help']) verbose = arguments.v set_global_loglevel(verbose=verbose) fname_bvecs_list = arguments.i # Build fname_out if arguments.o is not None: fname_out = arguments.o else: path_in, file_in, ext_in = extract_fname(fname_bvecs_list[0]) fname_out = path_in + 'bvecs_concat' + ext_in # # Open bvec files and collect values # nb_files = len(fname_bvecs_list) # bvecs_all = [] # for i_fname in fname_bvecs_list: # bvecs = [] # with open(i_fname) as f: # for line in f: # bvec_line = map(float, line.split()) # bvecs.append(bvec_line) # bvecs_all.append(bvecs) # f.close() # # Concatenate # bvecs_concat = '' # for i in range(0, 3): # for j in range(0, nb_files): # bvecs_concat += ' '.join(str(v) for v in bvecs_all[j][i]) # bvecs_concat += ' ' # bvecs_concat += '\n' # # Open bvec files and collect values bvecs_all = ['', '', ''] for i_fname in fname_bvecs_list: from dipy.data.fetcher import read_bvals_bvecs bval_i, bvec_i = read_bvals_bvecs(None, i_fname) for i in range(0, 3): bvecs_all[i] += ' '.join( str(v) for v in map(lambda n: '%.16f' % n, bvec_i[:, i])) bvecs_all[i] += ' ' # Concatenate bvecs_concat = '\n'.join(str(v) for v in bvecs_all) # Write new bvec new_f = open(fname_out, 'w') new_f.write(bvecs_concat) new_f.close()
def main(): # Get parser info parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_bvecs_list = arguments["-i"] # Build fname_out if "-o" in arguments: fname_out = arguments["-o"] else: path_in, file_in, ext_in = extract_fname(fname_bvecs_list[0]) fname_out = path_in + 'bvecs_concat' + ext_in # # Open bvec files and collect values # nb_files = len(fname_bvecs_list) # bvecs_all = [] # for i_fname in fname_bvecs_list: # bvecs = [] # with open(i_fname) as f: # for line in f: # bvec_line = map(float, line.split()) # bvecs.append(bvec_line) # bvecs_all.append(bvecs) # f.close() # # Concatenate # bvecs_concat = '' # for i in range(0, 3): # for j in range(0, nb_files): # bvecs_concat += ' '.join(str(v) for v in bvecs_all[j][i]) # bvecs_concat += ' ' # bvecs_concat += '\n' # # Open bvec files and collect values bvecs_all = ['', '', ''] for i_fname in fname_bvecs_list: bval_i, bvec_i = read_bvals_bvecs(None, i_fname) for i in range(0, 3): bvecs_all[i] += ' '.join( str(v) for v in map(lambda n: '%.16f' % n, bvec_i[:, i])) bvecs_all[i] += ' ' # Concatenate bvecs_concat = '\n'.join(str(v) for v in bvecs_all) # Write new bvec new_f = open(fname_out, 'w') new_f.write(bvecs_concat) new_f.close()
def main(): # Get parser info parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_bvecs_list = arguments["-i"] # Build fname_out if "-o" in arguments: fname_out = arguments["-o"] else: path_in, file_in, ext_in = extract_fname(fname_bvecs_list[0]) fname_out = path_in + 'bvecs_concat' + ext_in # # Open bvec files and collect values # nb_files = len(fname_bvecs_list) # bvecs_all = [] # for i_fname in fname_bvecs_list: # bvecs = [] # with open(i_fname) as f: # for line in f: # bvec_line = map(float, line.split()) # bvecs.append(bvec_line) # bvecs_all.append(bvecs) # f.close() # # Concatenate # bvecs_concat = '' # for i in range(0, 3): # for j in range(0, nb_files): # bvecs_concat += ' '.join(str(v) for v in bvecs_all[j][i]) # bvecs_concat += ' ' # bvecs_concat += '\n' # # Open bvec files and collect values bvecs_all = ['', '', ''] for i_fname in fname_bvecs_list: bval_i, bvec_i = read_bvals_bvecs(None, i_fname) for i in range(0, 3): bvecs_all[i] += ' '.join(str(v) for v in map(lambda n: '%.16f'%n, bvec_i[:, i])) bvecs_all[i] += ' ' # Concatenate bvecs_concat = '\n'.join(str(v) for v in bvecs_all) # Write new bvec new_f = open(fname_out, 'w') new_f.write(bvecs_concat) new_f.close()
def main(): # Get parser info parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_bvecs = arguments['-bvec'] # Read bvecs bvecs = read_bvals_bvecs(fname_bvecs, None) bvecs = bvecs[0] # if first dimension is not equal to 3 (x,y,z), transpose bvecs file if not bvecs.shape[0] == 3: bvecs = bvecs.transpose() x, y, z = bvecs[0], bvecs[1], bvecs[2] # Get total number of directions n_dir = len(x) # Get effective number of directions bvecs_eff = [] n_b0 = 0 for i in range(0, n_dir): add_direction = True # check if b=0 if abs(x[i]) < bzero and abs(x[i]) < bzero and abs(x[i]) < bzero: n_b0 += 1 add_direction = False else: # loop across bvecs_eff for j in range(0, len(bvecs_eff)): # if bvalue already present, then do not add to bvecs_eff if bvecs_eff[j] == [x[i], y[i], z[i]]: add_direction = False if add_direction: bvecs_eff.append([x[i], y[i], z[i]]) n_dir_eff = len(bvecs_eff) # Display scatter plot fig = plt.figure(facecolor='white', figsize=(9, 8)) fig.suptitle('Number of b=0: ' + str(n_b0) + ', Number of b!=0: ' + str(n_dir - n_b0) + ', Number of effective directions (without duplicates): ' + str(n_dir_eff)) # plt.ion() # Display three views plot_2dscatter(fig_handle=fig, subplot=221, x=bvecs[0][:], y=bvecs[1][:], xlabel='X', ylabel='Y') plot_2dscatter(fig_handle=fig, subplot=222, x=bvecs[0][:], y=bvecs[2][:], xlabel='X', ylabel='Z') plot_2dscatter(fig_handle=fig, subplot=223, x=bvecs[1][:], y=bvecs[2][:], xlabel='Y', ylabel='Z') # 3D ax = fig.add_subplot(224, projection='3d') # ax.auto_scale_xyz([-1, 1], [-1, 1], [-1, 1]) for i in range(0, n_dir): # x, y, z = bvecs[0], bvecs[1], bvecs[2] # if b=0, do not plot if not (abs(x[i]) < bzero and abs(x[i]) < bzero and abs(x[i]) < bzero): ax.scatter(x[i], y[i], z[i]) ax.set_xlim3d(-1, 1) ax.set_ylim3d(-1, 1) ax.set_zlim3d(-1, 1) plt.title('3D view (use mouse to rotate)') plt.axis('off') # plt.draw() # Save image sct.printv("Saving figure: bvecs.png\n") plt.savefig('bvecs.png') plt.show()
def main(): # Get parser info parser = get_parser() arguments = parser.parse(sys.argv[1:]) fname_bvecs = arguments['-bvec'] # Read bvecs bvecs = read_bvals_bvecs(fname_bvecs, None) bvecs = bvecs[0] # if first dimension is not equal to 3 (x,y,z), transpose bvecs file if not bvecs.shape[0] == 3: bvecs = bvecs.transpose() x, y, z = bvecs[0], bvecs[1], bvecs[2] # Get total number of directions n_dir = len(x) # Get effective number of directions bvecs_eff = [] n_b0 = 0 for i in range(0, n_dir): add_direction = True # check if b=0 if abs(x[i]) < bzero and abs(x[i]) < bzero and abs(x[i]) < bzero: n_b0 += 1 add_direction = False else: # loop across bvecs_eff for j in range(0, len(bvecs_eff)): # if bvalue already present, then do not add to bvecs_eff if bvecs_eff[j] == [x[i], y[i], z[i]]: add_direction = False if add_direction: bvecs_eff.append([x[i], y[i], z[i]]) n_dir_eff = len(bvecs_eff) # Display scatter plot fig = plt.figure(facecolor='white', figsize=(9, 8)) fig.suptitle('Number of b=0: '+str(n_b0)+', Number of b!=0: '+str(n_dir-n_b0)+', Number of effective directions (without duplicates): '+str(n_dir_eff)) # plt.ion() # Display three views plot_2dscatter(fig_handle=fig, subplot=221, x=bvecs[0][:], y=bvecs[1][:], xlabel='X', ylabel='Y') plot_2dscatter(fig_handle=fig, subplot=222, x=bvecs[0][:], y=bvecs[2][:], xlabel='X', ylabel='Z') plot_2dscatter(fig_handle=fig, subplot=223, x=bvecs[1][:], y=bvecs[2][:], xlabel='Y', ylabel='Z') # 3D ax = fig.add_subplot(224, projection='3d') # ax.auto_scale_xyz([-1, 1], [-1, 1], [-1, 1]) for i in range(0, n_dir): # x, y, z = bvecs[0], bvecs[1], bvecs[2] # if b=0, do not plot if not(abs(x[i]) < bzero and abs(x[i]) < bzero and abs(x[i]) < bzero): ax.scatter(x[i], y[i], z[i]) ax.set_xlim3d(-1, 1) ax.set_ylim3d(-1, 1) ax.set_zlim3d(-1, 1) plt.title('3D view (use mouse to rotate)') plt.axis('off') # plt.draw() # Save image sct.printv("Saving figure: bvecs.png\n") plt.savefig('bvecs.png') plt.show()
def main(argv=None): parser = get_parser() arguments = parser.parse_args(argv) verbose = arguments.v set_loglevel(verbose=verbose) fname_bvecs = arguments.bvec fname_bvals = arguments.bval # Read bvals and bvecs files (if arguments.bval is not passed, bvals will be None) bvals, bvecs = read_bvals_bvecs(fname_bvals, fname_bvecs) # if first dimension is not equal to 3 (x,y,z), transpose bvecs file if bvecs.shape[0] != 3: bvecs = bvecs.transpose() # if bvals file was not passed, create dummy unit bvals array (necessary fot scatter plots) if bvals is None: bvals = np.repeat(1, bvecs.shape[1]) # multiply unit b-vectors by b-values x, y, z = bvecs[0] * bvals, bvecs[1] * bvals, bvecs[2] * bvals # Set different color for each shell (bval) shell_colors = {} # Create iterator with different colors from brg palette colors = iter(cm.nipy_spectral(np.linspace(0, 1, len(np.unique(bvals))))) for unique_bval in np.unique(bvals): # skip b=0 if unique_bval < BZERO_THRESH: continue shell_colors[unique_bval] = next(colors) # Get total number of directions n_dir = len(x) # Get effective number of directions bvecs_eff = [] n_b0 = 0 for i in range(0, n_dir): add_direction = True # check if b=0 if abs(x[i]) < BZERO_THRESH and abs(x[i]) < BZERO_THRESH and abs( x[i]) < BZERO_THRESH: n_b0 += 1 add_direction = False else: # loop across bvecs_eff for j in range(0, len(bvecs_eff)): # if bvalue already present, then do not add to bvecs_eff if bvecs_eff[j] == [x[i], y[i], z[i]]: add_direction = False if add_direction: bvecs_eff.append([x[i], y[i], z[i]]) n_dir_eff = len(bvecs_eff) # Display scatter plot fig = plt.figure(facecolor='white', figsize=(9, 8)) fig.suptitle('Number of b=0: ' + str(n_b0) + ', Number of b!=0: ' + str(n_dir - n_b0) + ', Number of effective directions (without duplicates): ' + str(n_dir_eff)) # Display three views plot_2dscatter(fig_handle=fig, subplot=221, x=x, y=y, xlabel='X', ylabel='Y', bvals=bvals, colors=shell_colors) plot_2dscatter(fig_handle=fig, subplot=222, x=x, y=z, xlabel='X', ylabel='Z', bvals=bvals, colors=shell_colors) plot_2dscatter(fig_handle=fig, subplot=223, x=y, y=z, xlabel='Y', ylabel='Z', bvals=bvals, colors=shell_colors) # 3D ax = fig.add_subplot(224, projection='3d') # ax.auto_scale_xyz([-1, 1], [-1, 1], [-1, 1]) for i in range(0, n_dir): # x, y, z = bvecs[0], bvecs[1], bvecs[2] # if b=0, do not plot if not (abs(x[i]) < BZERO_THRESH and abs(x[i]) < BZERO_THRESH and abs(x[i]) < BZERO_THRESH): ax.scatter(x[i], y[i], z[i], color=shell_colors[bvals[i]], alpha=0.7) ax.set_xlim3d(-max(bvals), max(bvals)) ax.set_ylim3d(-max(bvals), max(bvals)) ax.set_zlim3d(-max(bvals), max(bvals)) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.title('3D view (use mouse to rotate)') plt.axis('on') # plt.draw() plt.tight_layout() # add legend with b-values if bvals file was passed if arguments.bval is not None: create_custom_legend(fig, shell_colors, bvals) # Save image printv("Saving figure: bvecs.png\n") plt.savefig('bvecs.png')