def add_grid_sources(grid_sources, well_positions, well_shape, well_spacing, site_spacing, add_source, source_settings=None): if source_settings is None: source_settings = {} for channel_name, well_sources in grid_sources.items(): shape = None settings = source_settings.get(channel_name, {}) channel_layers = [] for well_name, sources in well_sources.items(): well_x, well_y = well_positions[well_name] for pos, source in enumerate(sources): layer = add_source(source, name=f"{channel_name}_{well_name}_{pos}", **settings) if shape is None: shape = source.shape if "scale" in settings: scale = settings["scale"] assert len(scale) == len(shape) shape = tuple(sc * sh for sc, sh in zip(shape, scale)) else: assert source.shape == shape, f"{source.shape}, {shape}" world_pos = get_world_position(well_x, well_y, pos, well_shape, well_spacing, site_spacing, shape) layer.translate = world_pos channel_layers.append(layer) link_layers(channel_layers) return shape
def _on_mda_finished(self, sequence: useq.MDASequence): """Save layer and add increment to save name.""" meta = _mda.SEQUENCE_META.get(sequence) or _mda.SequenceMeta() seq_uid = sequence.uid if meta.mode == "explorer": layergroups = defaultdict(set) for lay in self.viewer.layers: if lay.metadata.get("uid") == seq_uid: key = f"{lay.metadata['ch_name']}_idx{lay.metadata['ch_id']}" layergroups[key].add(lay) for group in layergroups.values(): link_layers(group) meta = _mda.SEQUENCE_META.pop(sequence, self._mda_meta) save_sequence(sequence, self.viewer.layers, meta) # reactivate gui when mda finishes. self._set_enabled(True)
def _on_mda_finished(self, sequence: useq.MDASequence): meta = self.SEQUENCE_META.get(sequence) or SequenceMeta() seq_uid = sequence.uid if meta.mode == "explorer": layergroups = defaultdict(set) for lay in self.viewer.layers: if lay.metadata.get("uid") == seq_uid: key = _channel_key(lay.metadata["ch_name"], lay.metadata["ch_id"]) layergroups[key].add(lay) for group in layergroups.values(): link_layers(group) meta = self.SEQUENCE_META.pop(sequence, SequenceMeta()) save_sequence(sequence, self.viewer.layers, meta) self._set_enabled(True)
def add_positional_sources(positional_sources, positions, add_source, source_settings=None): if source_settings is None: source_settings = {} for channel_name, sources in positional_sources.items(): settings = source_settings.get(channel_name, {}) channel_layers = [] for sample, source in sources.items(): layer = add_source(source, name=f"{channel_name}_{sample}", **settings) position = positions[sample] if len(source.shape) > len(position): ndim_non_spatial = len(source.shape) - len(position) position = ndim_non_spatial * [0] + list(position) layer.translate = list(position) channel_layers.append(layer) shape = source.shape link_layers(channel_layers) return shape
'show_when': 'active_is_rgb', }, 'napari:merge_stack': { 'description': trans._('Merge to Stack'), 'action': _merge_stack, 'enable_when': ('selection_count > 1 and only_images_selected and same_shape'), 'show_when': 'True', }, 'sep1': _SEPARATOR, 'napari:link_selected_layers': { 'description': trans._('Link Layers'), 'action': lambda ll: link_layers(ll.selection), 'enable_when': 'selection_count > 1 and not all_layers_linked', 'show_when': 'not all_layers_linked', }, 'napari:unlink_selected_layers': { 'description': trans._('Unlink Layers'), 'action': lambda ll: unlink_layers(ll.selection), 'enable_when': 'all_layers_linked', 'show_when': 'all_layers_linked', }, 'napari:select_linked_layers': { 'description': trans._('Select Linked Layers'), 'action': _select_linked_layers, 'enable_when': 'linked_layers_unselected', 'show_when': 'True', },
"""Demonstrates the `link_layers` function. This function takes a list of layers and an optional list of attributes, and links them such that when one of the linked attributes changes on any of the linked layers, all of the other layers follow. """ import napari from napari.experimental import link_layers import numpy as np viewer = napari.view_image(np.random.rand(3, 64, 64), channel_axis=0) # link contrast_limits and gamma between all layers in viewer # NOTE: you may also omit the second argument to link ALL valid, common # attributes for the set of layers provided link_layers(viewer.layers, ('contrast_limits', 'gamma')) # unlinking may be done with napari.experimental.unlink_layers # this may also be done in a context manager: # with napari.experimental.layers_linked([layers]): # ... napari.run()