From 064f59e056a528766fb63d971de9a6018aff7112 Mon Sep 17 00:00:00 2001 From: Lorenzo Gaifas Date: Wed, 7 Dec 2022 10:58:38 +0100 Subject: [PATCH] Small changes for napari 0.4.17 (#133) --- README.md | 4 ++ blik/_tests/test_widget.py | 7 ++-- blik/napari.yaml | 2 - blik/reader.py | 6 +-- blik/widgets/main_widget.py | 74 +++++++++++++++++++++---------------- setup.cfg | 4 +- 6 files changed, 54 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index b494b85..c9e28a1 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,7 @@ napari -w blik -- /path/to.star /path/to/mrc/files/* The `-w blik` is important for proper initialization of all the layers. Keep the main widget open to ensure nothing goes wrong! *`blik` is just `napari`*. Particles and images are exposed as simple napari layers, which can be analysed and manipulated with simple python, and most importantly other [napari plugins](https://napari-hub.org/). + +## Widget + +The main widget has a few functions: diff --git a/blik/_tests/test_widget.py b/blik/_tests/test_widget.py index d1b75e9..fbf055e 100644 --- a/blik/_tests/test_widget.py +++ b/blik/_tests/test_widget.py @@ -8,13 +8,12 @@ def test_main_widget(make_napari_viewer): - viewer = make_napari_viewer() + wdg = MainBlikWidget() + assert 'test' not in wdg[0].experiment_id.choices + viewer = make_napari_viewer() with napari.layers._source.layer_source(reader_plugin='blik'): viewer.add_points(metadata={'experiment_id': 'test'}) - - wdg = MainBlikWidget() - assert 'test' not in wdg[0].experiment_id.choices viewer.window.add_dock_widget(wdg) assert 'test' in wdg[0].experiment_id.choices diff --git a/blik/napari.yaml b/blik/napari.yaml index d9745bb..aa4d651 100644 --- a/blik/napari.yaml +++ b/blik/napari.yaml @@ -56,5 +56,3 @@ contributions: display_name: bandpass filter - command: blik.gaussian_filter display_name: gaussian filter - - command: blik.filament_picker - display_name: filament picker diff --git a/blik/reader.py b/blik/reader.py index 70b0d83..f59102b 100644 --- a/blik/reader.py +++ b/blik/reader.py @@ -42,7 +42,7 @@ def read_particles(particles): edge_width=0, scale=scale, shading='spherical', - # antialiasing=0, + antialiasing=0, metadata={'experiment_id': exp_id, 'p_id': p_id}, out_of_slice_display=True, ), @@ -80,8 +80,8 @@ def read_image(image): name=f'{image.experiment_id} - image', scale=[px_size] * image.data.ndim, metadata={'experiment_id': image.experiment_id, 'stack': image.stack}, - interpolation='spline36', - # interpolation3d='linear', + interpolation2d='spline36', + interpolation3d='linear', rendering='average', depiction='plane', blending='translucent', diff --git a/blik/widgets/main_widget.py b/blik/widgets/main_widget.py index 0a209c7..5e6dbe7 100644 --- a/blik/widgets/main_widget.py +++ b/blik/widgets/main_widget.py @@ -33,6 +33,9 @@ def _get_choices(wdg): def _connect_points_to_vectors(p, v): + """ + connect a particle points layer to a vectors layer to keep them in sync. + """ def _update_vectors(): vec_data, vec_color = generate_vectors(p.data, p.features[PSDL.ORIENTATION]) v.data = vec_data @@ -52,44 +55,42 @@ def _update_points_from_features(): p.events.features.connect(_update_vectors) -def _on_init(wdg): +def _attach_callbacks_to_viewer(wdg): """ - hook up widget to update choices wheneven things change in the layerlist - - also sets up a few things on the viewer + attach all callbacks to the napari viewer and enable scale bar """ - @wdg.parent_changed.connect - def _look_for_viewer(): - viewer = find_viewer_ancestor(wdg.native) - if viewer: - viewer.layers.events.inserted.connect(wdg.experiment_id.reset_choices) - viewer.layers.events.removed.connect(wdg.experiment_id.reset_choices) - viewer.layers.events.inserted.connect(lambda e: _connect_layers(viewer, e)) - _connect_layers(viewer, None) + viewer = find_viewer_ancestor(wdg.native) + if viewer: + viewer.layers.events.inserted.connect(lambda e: _connect_layers(viewer, e)) + _connect_layers(viewer, None) - viewer.scale_bar.unit = '0.1nm' # pixels are 1 Angstrom - viewer.scale_bar.visible = True + viewer.scale_bar.unit = '0.1nm' # pixels are 1 Angstrom + viewer.scale_bar.visible = True - def _connect_layers(viewer, e): - points = {} - vectors = {} - for lay in viewer.layers: - p_id = lay.metadata.get('p_id', None) - if p_id is not None: - if isinstance(lay, Points): - points[p_id] = lay - elif isinstance(lay, Vectors): - vectors[p_id] = lay - for p_id, p in points.items(): - v = vectors.get(p_id, None) - if v is not None: - _connect_points_to_vectors(p, v) + +def _connect_layers(viewer, e): + """ + connect all points and vectors layers with the necessary callbacks + """ + points = {} + vectors = {} + for lay in viewer.layers: + p_id = lay.metadata.get('p_id', None) + if p_id is not None: + if isinstance(lay, Points): + points[p_id] = lay + elif isinstance(lay, Vectors): + vectors[p_id] = lay + for p_id, p in points.items(): + v = vectors.get(p_id, None) + if v is not None: + _connect_points_to_vectors(p, v) @magic_factory( auto_call=True, call_button=False, - widget_init=_on_init, + labels=False, experiment_id=dict(widget_type='ComboBox', choices=_get_choices, nullable=True), ) def experiment(viewer: 'napari.Viewer', experiment_id): @@ -136,8 +137,8 @@ def new(l_type) -> 'napari.types.LayerDataTuple': """ create a new layer to add to this experiment """ + layers = getattr(new._main_widget['experiment'], 'current_layers', []) if l_type == 'segmentation': - layers = getattr(new._main_widget['experiment'], 'current_layers', []) for lay in layers: if isinstance(lay, Image): exp_id = lay.metadata['experiment_id'] @@ -154,12 +155,21 @@ def new(l_type) -> 'napari.types.LayerDataTuple': class MainBlikWidget(Container): + """ + Main widget for blik controls. + + Allows to select which layers to view based on the experiment id, to add + existing layer to a certain experiment id, and to create new analysis layers + within. + """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.append(experiment(labels=False)) + exp = experiment() + self.parent_changed.connect(lambda _: _attach_callbacks_to_viewer(exp)) + self.append(exp) self.append(new) - self.append(add_layer) def append(self, item): diff --git a/setup.cfg b/setup.cfg index 6177959..76f34ef 100644 --- a/setup.cfg +++ b/setup.cfg @@ -57,12 +57,12 @@ install_requires = [options.extras_require] all = - napari[all]>=0.4.16 + napari[all]>=0.4.17 napari-properties-plotter napari-properties-viewer napari-label-interpolator testing = - napari[all]>=0.4.16 + napari[all]>=0.4.17 tox pytest pytest-qt