Skip to content

Commit

Permalink
Small changes for napari 0.4.17 (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
brisvag authored Dec 7, 2022
1 parent d3f42f6 commit 064f59e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
7 changes: 3 additions & 4 deletions blik/_tests/test_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions blik/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions blik/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down Expand Up @@ -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',
Expand Down
74 changes: 42 additions & 32 deletions blik/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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']
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 064f59e

Please sign in to comment.