def __init__(self, coords, symbol='o', size=10, edge_width=1, edge_width_rel=None, edge_color='black', face_color='white', scaling=True): visual = MarkersNode() super().__init__(visual) # Save the marker coordinates self._coords = coords # Save the marker style params self._symbol = symbol self._size = size self._edge_width = edge_width self._edge_width_rel = edge_width_rel self._edge_color = edge_color self._face_color = face_color self._scaling = scaling self._marker_types = marker_types self._colors = get_color_names() # update flags self._need_display_update = False self._need_visual_update = False self.name = 'markers' self._qt = QtMarkersLayer(self) self._selected_markers = None
def get_color_namelist(): """Gets all the color names supported by napari. Returns ------- list[str] All the color names supported by napari. """ return get_color_names()
def __init__( self, vectors, *, edge_width=1, edge_color='red', length=1, opacity=1, blending='translucent', visible=True, name=None, ): super().__init__( Mesh(), name=name, opacity=opacity, blending=blending, visible=visible, ) # events for non-napari calculations self.events.add(length=Event, edge_width=Event) # Save the vector style params self._edge_width = edge_width self._edge_color = edge_color self._colors = get_color_names() self._mesh_vertices = np.empty((0, 2)) self._mesh_triangles = np.empty((0, 3), dtype=np.uint32) # Data containing vectors in the currently viewed slice self._data_view = np.empty((0, 2, 2)) # length attribute self._length = length # update flags self._need_display_update = False self._need_visual_update = False # assign vector data and establish default behavior with self.freeze_refresh(): self.data = vectors # Re intitialize indices depending on image dims self._indices = (0,) * (self.ndim - 2) + ( slice(None, None, None), slice(None, None, None), ) # Trigger generation of view slice and thumbnail self._set_view_slice()
def __init__( self, data, *, edge_width=1, edge_color='red', length=1, name=None, metadata=None, scale=None, translate=None, opacity=0.7, blending='translucent', visible=True, ): super().__init__( data, 2, name=name, metadata=metadata, scale=scale, translate=translate, opacity=opacity, blending=blending, visible=visible, ) # events for non-napari calculations self.events.add(length=Event, edge_width=Event, edge_color=Event) # Save the vector style params self._edge_width = edge_width self._edge_color = edge_color self._colors = get_color_names() self._mesh_vertices = np.empty((0, 2)) self._mesh_triangles = np.empty((0, 3), dtype=np.uint32) # Data containing vectors in the currently viewed slice self._data_view = np.empty((0, 2, 2)) self._displayed_stored = [] self._view_vertices = [] self._view_faces = [] # length attribute self._length = length # assign vector data and establish default behavior self.data = data
def __init__(self, vectors, width=1, color='red', averaging=1, length=1, name=None): visual = Mesh() super().__init__(visual) # events for non-napari calculations self.events.add(length=Event, width=Event, averaging=Event) # Store underlying data model self._data_types = ('image', 'coords') self._data_type = None # Save the line style params self._width = width self._color = color self._colors = get_color_names() # averaging and length attributes self._averaging = averaging self._length = length # update flags self._need_display_update = False self._need_visual_update = False # assign vector data and establish default behavior self._raw_data = None self._original_data = vectors self._current_data = vectors self._vectors = self._convert_to_vector_type(vectors) vertices, triangles = self._generate_meshes(self._vectors, self.width) self._mesh_vertices = vertices self._mesh_triangles = triangles if name is None: self.name = 'vectors' else: self.name = name self._qt_properties = QtVectorsLayer(self)
def get_color_namelist(): """A wrapper around vispy's get_color_names designed to add a "transparent" (alpha = 0) color to it. Once https://github.com/vispy/vispy/pull/1794 is merged this function is no longer necessary. Returns ------- color_dict : list A list of all valid vispy color names plus "transparent". """ names = get_color_names() names.append('transparent') return names
def __init__(self, coords, symbol='o', size=10, edge_width=1, edge_width_rel=None, edge_color='black', face_color='white', scaling=True, n_dimensional=False, *, name=None): super().__init__(MarkersNode(), name) self.events.add(mode=Event, size=Event, face_color=Event, edge_color=Event, symbol=Event, n_dimensional=Event) # Freeze refreshes with self.freeze_refresh(): # Save the marker coordinates self._coords = coords # Save the marker style params self.symbol = symbol self.size = size self.edge_width = edge_width self.edge_width_rel = edge_width_rel self.edge_color = edge_color self.face_color = face_color self.scaling = scaling self.n_dimensional = n_dimensional self._marker_types = marker_types self._colors = get_color_names() self._selected_markers = None self._mode = 'pan/zoom' self._mode_history = self._mode self._status = self._mode # update flags self._need_display_update = False self._need_visual_update = False self._qt_properties = QtMarkersLayer(self) self._qt_controls = QtMarkersControls(self)
def test_color_interpretation(): """Test basic color interpretation API""" # test useful ways of single color init r = ColorArray('r') print(r) # test repr r2 = ColorArray(r) assert_equal(r, r2) r2.rgb = 0, 0, 0 assert_equal(r2, ColorArray('black')) assert_equal(r, ColorArray('r')) # modifying new one preserves old assert_equal(r, r.copy()) assert_equal(r, ColorArray('#ff0000')) assert_equal(r, ColorArray('#FF0000FF')) assert_equal(r, ColorArray('red')) assert_equal(r, ColorArray('red', alpha=1.0)) assert_equal(ColorArray((1, 0, 0, 0.1)), ColorArray('red', alpha=0.1)) assert_array_equal(r.rgb.ravel(), (1., 0., 0.)) assert_array_equal(r.rgba.ravel(), (1., 0., 0., 1.)) assert_array_equal(r.RGBA.ravel(), (255, 0, 0, 255)) # handling multiple colors rgb = ColorArray(list('rgb')) print(rgb) # multi repr assert_array_equal(rgb, ColorArray(np.eye(3))) # complex/annoying case rgb = ColorArray(['r', (0, 1, 0), '#0000ffff']) assert_array_equal(rgb, ColorArray(np.eye(3))) assert_raises(RuntimeError, ColorArray, ['r', np.eye(3)]) # can't nest # getting/setting properties r = ColorArray('#ffff') assert_equal(r, ColorArray('white')) r = ColorArray('#ff000000') assert_true('turquoise' in get_color_names()) # make sure our JSON loaded assert_equal(r.alpha, 0) r.alpha = 1.0 assert_equal(r, ColorArray('r')) r.alpha = 0 r.rgb = (1, 0, 0) assert_equal(r.alpha, 0) assert_equal(r.hex, ['#ff0000']) r.alpha = 1 r.hex = '00ff00' assert_equal(r, ColorArray('g')) assert_array_equal(r.rgb.ravel(), (0., 1., 0.)) r.RGB = 255, 0, 0 assert_equal(r, ColorArray('r')) assert_array_equal(r.RGB.ravel(), (255, 0, 0)) r.RGBA = 255, 0, 0, 0 assert_equal(r, ColorArray('r', alpha=0)) w = ColorArray() w.rgb = ColorArray('r').rgb + ColorArray('g').rgb + ColorArray('b').rgb assert_equal(w, ColorArray('white')) w = ColorArray('white') assert_equal(w, w.darker().lighter()) assert_equal(w, w.darker(0.1).darker(-0.1)) w2 = w.darker() assert_true(w != w2) w.darker(copy=False) assert_equal(w, w2) with use_log_level('warning', record=True, print_msg=False) as w: w = ColorArray('white') w.value = 2 assert_equal(len(w), 1) assert_equal(w, ColorArray('white')) # warnings and errors assert_raises(ValueError, ColorArray, '#ffii00') # non-hex assert_raises(ValueError, ColorArray, '#ff000') # too short assert_raises(ValueError, ColorArray, [0, 0]) # not enough vals assert_raises(ValueError, ColorArray, [2, 0, 0]) # val > 1 assert_raises(ValueError, ColorArray, [-1, 0, 0]) # val < 0 c = ColorArray([2., 0., 0.], clip=True) # val > 1 assert_true(np.all(c.rgb <= 1)) c = ColorArray([-1., 0., 0.], clip=True) # val < 0 assert_true(np.all(c.rgb >= 0)) # make sure our color dict works for key in get_color_names(): assert_true(ColorArray(key)) assert_raises(ValueError, ColorArray, 'foo') # unknown color error _color_dict = get_color_dict() assert isinstance(_color_dict, dict) assert set(_color_dict.keys()) == set(get_color_names())
def __init__( self, data=None, *, symbol='o', size=10, edge_width=1, edge_color='black', face_color='white', n_dimensional=False, name=None, metadata=None, scale=None, translate=None, opacity=1, blending='translucent', visible=True, ): if data is None: data = np.empty((0, 2)) ndim = data.shape[1] super().__init__( ndim, name=name, metadata=metadata, scale=scale, translate=translate, opacity=opacity, blending=blending, visible=visible, ) self.events.add( mode=Event, size=Event, edge_width=Event, face_color=Event, edge_color=Event, symbol=Event, n_dimensional=Event, highlight=Event, ) self._colors = get_color_names() # Save the point coordinates self._data = data self.dims.clip = False # Save the point style params self.symbol = symbol self._n_dimensional = n_dimensional self.edge_width = edge_width # The following point properties are for the new points that will # be added. For any given property, if a list is passed to the # constructor so each point gets its own value then the default # value is used when adding new points if np.isscalar(size): self._size = size else: self._size = 10 if type(edge_color) is str: self._edge_color = edge_color else: self._edge_color = 'black' if type(face_color) is str: self._face_color = face_color else: self._face_color = 'white' # Indices of selected points self._selected_data = [] self._selected_data_stored = [] self._selected_data_history = [] # Indices of selected points within the currently viewed slice self._selected_view = [] # Index of hovered point self._value = None self._value_stored = None self._selected_box = None self._mode = Mode.PAN_ZOOM self._mode_history = self._mode self._status = self.mode self._drag_start = None # Nx2 array of points in the currently viewed slice self._data_view = np.empty((0, 2)) # Sizes of points in the currently viewed slice self._sizes_view = 0 # Full data indices of points located in the currently viewed slice self._indices_view = [] self._drag_box = None self._drag_box_stored = None self._is_selecting = False self._clipboard = {} self.edge_colors = list( itertools.islice( ensure_iterable(edge_color, color=True), 0, len(self.data) ) ) self.face_colors = list( itertools.islice( ensure_iterable(face_color, color=True), 0, len(self.data) ) ) self.sizes = size # Trigger generation of view slice and thumbnail self._update_dims()
def __init__( self, coords, *, symbol='o', size=10, edge_width=1, edge_color='black', face_color='white', n_dimensional=False, name=None, ): # Create a compound visual with the following four subvisuals: # Lines: The lines of the interaction box used for highlights. # Markers: The the outlines for each point used for highlights. # Markers: The actual markers of each point. visual = Compound([Line(), Markers(), Markers()]) super().__init__(visual, name) self.events.add( mode=Event, size=Event, face_color=Event, edge_color=Event, symbol=Event, n_dimensional=Event, ) self._colors = get_color_names() # Freeze refreshes with self.freeze_refresh(): # Save the point coordinates self._data = coords # Save the point style params self.symbol = symbol self.n_dimensional = n_dimensional self.edge_width = edge_width self.sizes = size self.edge_colors = list( itertools.islice(ensure_iterable(edge_color, color=True), 0, len(self.data))) self.face_colors = list( itertools.islice(ensure_iterable(face_color, color=True), 0, len(self.data))) # The following point properties are for the new points that will # be added. For any given property, if a list is passed to the # constructor so each point gets its own value then the default # value is used when adding new points if np.isscalar(size): self._size = size else: self._size = 10 if type(edge_color) is str: self._edge_color = edge_color else: self._edge_color = 'black' if type(face_color) is str: self._face_color = face_color else: self._face_color = 'white' # Indices of selected points self._selected_data = [] self._selected_data_stored = [] self._selected_data_history = [] # Indices of selected points within the currently viewed slice self._selected_view = [] # Index of hovered point self._hover_point = None self._hover_point_stored = None self._selected_box = None self._mode = Mode.PAN_ZOOM self._mode_history = self._mode self._status = self.mode self._drag_start = None # Nx2 array of points in the currently viewed slice self._data_view = np.empty((0, 2)) # Sizes of points in the currently viewed slice self._sizes_view = 0 # Full data indices of points located in the currently viewed slice self._indices_view = [] self._drag_box = None self._drag_box_stored = None self._is_selecting = False self._clipboard = {} # update flags self._need_display_update = False self._need_visual_update = False # Re intitialize indices depending on image dims self._indices = (0, ) * (self.ndim - 2) + ( slice(None, None, None), slice(None, None, None), ) # Trigger generation of view slice and thumbnail self._set_view_slice()
def test_color_interpretation(): """Test basic color interpretation API""" # test useful ways of single color init r = ColorArray('r') print(r) # test repr r2 = ColorArray(r) assert_equal(r, r2) r2.rgb = 0, 0, 0 assert_equal(r2, ColorArray('black')) assert_equal(r, ColorArray('r')) # modifying new one preserves old assert_equal(r, r.copy()) assert_equal(r, ColorArray('#ff0000')) assert_equal(r, ColorArray('#FF0000FF')) assert_equal(r, ColorArray('red')) assert_equal(r, ColorArray('red', alpha=1.0)) assert_equal(ColorArray((1, 0, 0, 0.1)), ColorArray('red', alpha=0.1)) assert_array_equal(r.rgb.ravel(), (1., 0., 0.)) assert_array_equal(r.rgba.ravel(), (1., 0., 0., 1.)) assert_array_equal(r.RGBA.ravel(), (255, 0, 0, 255)) # handling multiple colors rgb = ColorArray(list('rgb')) print(rgb) # multi repr assert_array_equal(rgb, ColorArray(np.eye(3))) # complex/annoying case rgb = ColorArray(['r', (0, 1, 0), '#0000ffff']) assert_array_equal(rgb, ColorArray(np.eye(3))) assert_raises(RuntimeError, ColorArray, ['r', np.eye(3)]) # can't nest # getting/setting properties r = ColorArray('#ffff') assert_equal(r, ColorArray('white')) r = ColorArray('#ff000000') assert_true('turquoise' in get_color_names()) # make sure our JSON loaded assert_equal(r.alpha, 0) r.alpha = 1.0 assert_equal(r, ColorArray('r')) r.alpha = 0 r.rgb = (1, 0, 0) assert_equal(r.alpha, 0) assert_equal(r.hex, ['#ff0000']) r.alpha = 1 r.hex = '00ff00' assert_equal(r, ColorArray('g')) assert_array_equal(r.rgb.ravel(), (0., 1., 0.)) r.RGB = 255, 0, 0 assert_equal(r, ColorArray('r')) assert_array_equal(r.RGB.ravel(), (255, 0, 0)) r.RGBA = 255, 0, 0, 0 assert_equal(r, ColorArray('r', alpha=0)) w = ColorArray() w.rgb = ColorArray('r').rgb + ColorArray('g').rgb + ColorArray('b').rgb assert_equal(w, ColorArray('white')) w = ColorArray('white') assert_equal(w, w.darker().lighter()) assert_equal(w, w.darker(0.1).darker(-0.1)) w2 = w.darker() assert_true(w != w2) w.darker(copy=False) assert_equal(w, w2) with use_log_level('warning', record=True, print_msg=False) as w: w = ColorArray('white') w.value = 2 assert_equal(len(w), 1) assert_equal(w, ColorArray('white')) # warnings and errors assert_raises(ValueError, ColorArray, '#ffii00') # non-hex assert_raises(ValueError, ColorArray, '#ff000') # too short assert_raises(ValueError, ColorArray, [0, 0]) # not enough vals assert_raises(ValueError, ColorArray, [2, 0, 0]) # val > 1 assert_raises(ValueError, ColorArray, [-1, 0, 0]) # val < 0 c = ColorArray([2., 0., 0.], clip=True) # val > 1 assert_true(np.all(c.rgb <= 1)) c = ColorArray([-1., 0., 0.], clip=True) # val < 0 assert_true(np.all(c.rgb >= 0)) # make sure our color dict works for key in get_color_names(): assert_true(ColorArray(key)) assert_raises(ValueError, ColorArray, 'foo') # unknown color error
parent=vb2.scene) curve2b = scene.visuals.Isocurve(myfunc, levels=levels2, color_lev='cubehelix', parent=vb4.scene) # set viewport vb3.camera.set_range((0, 100), (0, 100)) vb4.camera.set_range((0, 100), (0, 100)) # setup update parameters up = 1 index = 1 clip = np.linspace(myfunc.min(), myfunc.max(), num=51) cmap = cycle(get_colormaps()) color = cycle(get_color_names()) def update(ev): global myfunc, index, up, levels2, noise, cmap, color if index > 0 and index < 25: # update left panes rolling upwards noise = np.roll(noise, 1, axis=0) image1.set_data(noise) curve1a.set_data(noise) curve1b.set_data(noise) # update colors/colormap if (index % 5) == 0: curve1b.color = next(color)
curve2a = scene.visuals.Isocurve( myfunc, levels=levels2[::4], color_lev='k', parent=vb2.scene) curve2b = scene.visuals.Isocurve( myfunc, levels=levels2, color_lev='cubehelix', parent=vb4.scene) # set viewport vb3.camera.set_range((0, 100), (0, 100)) vb4.camera.set_range((0, 100), (0, 100)) # setup update parameters up = 1 index = 1 clip = np.linspace(myfunc.min(), myfunc.max(), num=51) cmap = cycle(get_colormaps()) color = cycle(get_color_names()) def update(ev): global myfunc, index, up, levels2, noise, cmap, color if index > 0 and index < 25: # update left panes rolling upwards noise = np.roll(noise, 1, axis=0) image1.set_data(noise) curve1a.set_data(noise) curve1b.set_data(noise) # update colors/colormap if (index % 5) == 0: curve1b.color = next(color)