def test_orient2rgb(): e2 = [0.70710678, 0, 0, 0, 0.70710678, 0] v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) v2 = np.array([1, 0, 0, 0, 1, 0]) npt.assert_equal(colormap.orient2rgb(v), v) npt.assert_almost_equal(colormap.orient2rgb(v2), e2) npt.assert_raises(IOError, colormap.orient2rgb, np.array(1))
def _orientation_colors(points, cmap='rgb_standard'): """ Parameters ---------- points : (N, 3) array or ndarray points coordinates array. cmap : string ('rgb_standard', 'boys_standard'), optional colormap. Returns ------- colors_list : ndarray list of Kx3 colors. Where K is the number of lines. """ if cmap.lower() == 'rgb_standard': col_list = [ orient2rgb(points[i + 1] - points[i]) for i in range(0, len(points), 2) ] elif cmap.lower() == 'boys_standard': col_list = [ boys2rgb(points[i + 1] - points[i]) for i in range(0, len(points), 2) ] else: raise ValueError("Invalid colormap. The only available options are " "'rgb_standard' and 'boys_standard'.") return np.asarray(col_list)
def main(): parser = _build_arg_parser() args = parser.parse_args() # Make sure the colors are consistent between executions if args.random_coloring is not None: random.seed(int(args.random_coloring)) # Handle bundle filenames. 3 cases are possible: # A list of files was passed as arguments # A directory was passed as argument # A single-file or path containing wildcard was specified bundle_filenames = args.in_bundles if len(args.in_bundles) == 1: # If only one file is specified, it may be a whole folder or # a single file, or a wildcard usage in_path = args.in_bundles[0] if os.path.isdir(in_path): # Load the folder bundle_filenames = [ os.path.join(in_path, f) for f in os.listdir(in_path) ] else: # Load the file/wildcard bundle_filenames = glob.glob(in_path) assert_inputs_exist(parser, bundle_filenames, args.color_dict) scene = window.Scene() scene.background(tuple(map(int, args.background))) def subsample(list_obj): """ Lazily subsample a list """ return list(itertools.islice(list_obj, 0, None, args.subsample)) # Load each bundle, subsample and downsample it if needed for filename in bundle_filenames: try: # Lazy-load streamlines to minimize ram usage tractogram_gen = nib.streamlines.load(filename, lazy_load=True).tractogram streamlines_gen = tractogram_gen.streamlines except ValueError: # Not a file loadable by nibabel's streamline API print('Skipping {}'.format(filename)) continue # Actually load streamlines according to the subsample argument streamlines = subsample(streamlines_gen) if args.downsample: streamlines = set_number_of_points(streamlines, args.downsample) # Handle bundle colors. Either assign a random bright color to each # bundle, or load a color specific to each bundle, or let the bundles # be colored according to their local orientation if args.random_coloring: color = random_rgb() elif args.color_dict: with open(args.color_dict) as json_file: # Color dictionary color_dict = json.load(json_file) # Extract filenames to compare against the color dictionary basename = os.path.splitext(os.path.basename(filename))[0] # Load colors color = color_dict[basename] \ if basename in color_dict.keys() \ else color_dict['default'] elif args.color_from_streamlines: color = subsample(tractogram_gen.data_per_streamline[ args.color_from_streamlines]) elif args.color_from_points: color = subsample( tractogram_gen.data_per_point[args.color_from_points]) elif args.uniform_coloring: # Assign uniform coloring to streamlines color = tuple(map(int, args.uniform_coloring)) elif args.local_coloring: # Compute coloring from local orientations # Compute segment orientation diff = [np.diff(list(s), axis=0) for s in streamlines] # Repeat first segment so that the number of segments matches # the number of points diff = [[d[0]] + list(d) for d in diff] # Flatten the list of segments orientations = np.asarray([o for d in diff for o in d]) # Turn the segments into colors color = colormap.orient2rgb(orientations) else: # Streamline color will depend on the streamlines' endpoints. color = None # TODO: Coloring from a volume of local orientations line_actor = streamline_actor[args.shape](streamlines, colors=color, linewidth=args.width) scene.add(line_actor) # If there's actually streamlines to display if len(bundle_filenames): # Showtime ! showm = window.ShowManager(scene, reset_camera=True) showm.initialize() showm.start()