-
-
Notifications
You must be signed in to change notification settings - Fork 410
/
layerlist.py
477 lines (405 loc) · 17.3 KB
/
layerlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import itertools
import warnings
from collections import namedtuple
from functools import cached_property
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union
import numpy as np
from napari.layers import Layer
from napari.layers.image.image import _ImageBase
from napari.utils.events.containers import SelectableEventedList
from napari.utils.naming import inc_name_count
from napari.utils.translations import trans
Extent = namedtuple('Extent', 'data world step')
if TYPE_CHECKING:
from npe2.manifest.io import WriterContribution
class LayerList(SelectableEventedList[Layer]):
"""List-like layer collection with built-in reordering and callback hooks.
Parameters
----------
data : iterable
Iterable of napari.layer.Layer
Events
------
inserting : (index: int)
emitted before an item is inserted at ``index``
inserted : (index: int, value: T)
emitted after ``value`` is inserted at ``index``
removing : (index: int)
emitted before an item is removed at ``index``
removed : (index: int, value: T)
emitted after ``value`` is removed at ``index``
moving : (index: int, new_index: int)
emitted before an item is moved from ``index`` to ``new_index``
moved : (index: int, new_index: int, value: T)
emitted after ``value`` is moved from ``index`` to ``new_index``
changed : (index: int, old_value: T, value: T)
emitted when ``index`` is set from ``old_value`` to ``value``
changed <OVERLOAD> : (index: slice, old_value: List[_T], value: List[_T])
emitted when ``index`` is set from ``old_value`` to ``value``
reordered : (value: self)
emitted when the list is reordered (eg. moved/reversed).
selection.events.changed : (added: Set[_T], removed: Set[_T])
emitted when the set changes, includes item(s) that have been added
and/or removed from the set.
selection.events.active : (value: _T)
emitted when the current item has changed.
selection.events._current : (value: _T)
emitted when the current item has changed. (Private event)
"""
def __init__(self, data=()):
super().__init__(
data=data,
basetype=Layer,
lookup={str: lambda e: e.name},
)
# TODO: figure out how to move this context creation bit.
# Ideally, the app should be aware of the layerlist, but not vice versa.
# This could probably be done by having the layerlist emit events that the app
# connects to, then the `_ctx` object would live on the app, (not here)
from napari._app_model.context import create_context
from napari._app_model.context._layerlist_context import (
LayerListContextKeys,
)
self._ctx = create_context(self)
if self._ctx is not None: # happens during Viewer type creation
self._ctx_keys = LayerListContextKeys(self._ctx)
self.selection.events.changed.connect(self._ctx_keys.update)
# temporary: see note in _on_selection_event
self.selection.events.changed.connect(self._on_selection_changed)
def _on_selection_changed(self, event):
# This method is a temporary workaround to the fact that the Points
# layer needs to know when its selection state changes so that it can
# update the highlight state. This (and the layer._on_selection
# method) can be removed once highlighting logic has been removed from
# the layer model.
for layer in event.added:
layer._on_selection(True)
for layer in event.removed:
layer._on_selection(False)
def _process_delete_item(self, item: Layer):
super()._process_delete_item(item)
item.events.extent.disconnect(self._clean_cache)
self._clean_cache()
def _clean_cache(self):
cached_properties = (
'extent',
'_extent_world',
'_step_size',
'_ranges',
)
[self.__dict__.pop(p, None) for p in cached_properties]
def __newlike__(self, data):
return LayerList(data)
def _coerce_name(self, name, layer=None):
"""Coerce a name into a unique equivalent.
Parameters
----------
name : str
Original name.
layer : napari.layers.Layer, optional
Layer for which name is generated.
Returns
-------
new_name : str
Coerced, unique name.
"""
existing_layers = {x.name for x in self if x is not layer}
for _ in range(len(self)):
if name in existing_layers:
name = inc_name_count(name)
return name
def _update_name(self, event):
"""Coerce name of the layer in `event.layer`."""
layer = event.source
layer.name = self._coerce_name(layer.name, layer)
def _ensure_unique(self, values, allow=()):
bad = set(self._list) - set(allow)
values = tuple(values) if isinstance(values, Iterable) else (values,)
for v in values:
if v in bad:
raise ValueError(
trans._(
"Layer '{v}' is already present in layer list",
deferred=True,
v=v,
)
)
return values
def __setitem__(self, key, value):
old = self._list[key]
if isinstance(key, slice):
value = self._ensure_unique(value, old)
elif isinstance(key, int):
(value,) = self._ensure_unique((value,), (old,))
super().__setitem__(key, value)
def insert(self, index: int, value: Layer):
"""Insert ``value`` before index."""
(value,) = self._ensure_unique((value,))
new_layer = self._type_check(value)
new_layer.name = self._coerce_name(new_layer.name)
self._clean_cache()
new_layer.events.extent.connect(self._clean_cache)
super().insert(index, new_layer)
def toggle_selected_visibility(self):
"""Toggle visibility of selected layers"""
for layer in self.selection:
layer.visible = not layer.visible
@cached_property
def _extent_world(self) -> np.ndarray:
"""Extent of layers in world coordinates.
Default to 2D with (-0.5, 511.5) min/ max values if no data is present.
Corresponds to pixels centered at [0, ..., 511].
Returns
-------
extent_world : array, shape (2, D)
"""
return self._get_extent_world([layer.extent for layer in self])
def _get_min_and_max(self, mins_list, maxes_list):
# Reverse dimensions since it is the last dimensions that are
# displayed.
mins_list = [mins[::-1] for mins in mins_list]
maxes_list = [maxes[::-1] for maxes in maxes_list]
with warnings.catch_warnings():
# Taking the nanmin and nanmax of an axis of all nan
# raises a warning and returns nan for that axis
# as we have do an explicit nan_to_num below this
# behaviour is acceptable and we can filter the
# warning
warnings.filterwarnings(
'ignore',
message=str(
trans._('All-NaN axis encountered', deferred=True)
),
)
min_v = np.nanmin(
list(itertools.zip_longest(*mins_list, fillvalue=np.nan)),
axis=1,
)
max_v = np.nanmax(
list(itertools.zip_longest(*maxes_list, fillvalue=np.nan)),
axis=1,
)
# 512 element default extent as documented in `_get_extent_world`
min_v = np.nan_to_num(min_v, nan=-0.5)
max_v = np.nan_to_num(max_v, nan=511.5)
# switch back to original order
return min_v[::-1], max_v[::-1]
def _get_extent_world(self, layer_extent_list):
"""Extent of layers in world coordinates.
Default to 2D with (-0.5, 511.5) min/ max values if no data is present.
Corresponds to pixels centered at [0, ..., 511].
Returns
-------
extent_world : array, shape (2, D)
"""
if len(self) == 0:
min_v = np.asarray([-0.5] * self.ndim)
max_v = np.asarray([511.5] * self.ndim)
else:
extrema = [extent.world for extent in layer_extent_list]
mins = [e[0] for e in extrema]
maxs = [e[1] for e in extrema]
min_v, max_v = self._get_min_and_max(mins, maxs)
return np.vstack([min_v, max_v])
@cached_property
def _step_size(self) -> np.ndarray:
"""Ideal step size between planes in world coordinates.
Computes the best step size that allows all data planes to be
sampled if moving through the full range of world coordinates.
The current implementation just takes the minimum scale.
Returns
-------
step_size : array, shape (D,)
"""
return self._get_step_size([layer.extent for layer in self])
def _step_size_from_scales(self, scales):
# Reverse order so last axes of scale with different ndim are aligned
scales = [scale[::-1] for scale in scales]
full_scales = list(
np.array(list(itertools.zip_longest(*scales, fillvalue=np.nan)))
)
# restore original order
return np.nanmin(full_scales, axis=1)[::-1]
def _get_step_size(self, layer_extent_list):
if len(self) == 0:
return np.ones(self.ndim)
scales = [extent.step for extent in layer_extent_list]
return self._step_size_from_scales(scales)
def get_extent(self, layers: Iterable[Layer]) -> Extent:
"""
Return extent for a given layer list.
This function is useful for calculating the extent of a subset of layers
when preparing and updating some supplementary layers.
For example see the cross Vectors layer in the `multiple_viewer_widget` example.
Parameters
----------
layers : list of Layer
list of layers for which extent should be calculated
Returns
-------
extent : Extent
extent for selected layers
"""
extent_list = [layer.extent for layer in layers]
return Extent(
data=None,
world=self._get_extent_world(extent_list),
step=self._get_step_size(extent_list),
)
@cached_property
def extent(self) -> Extent:
"""Extent of layers in data and world coordinates."""
return self.get_extent([x for x in self])
@cached_property
def _ranges(self) -> List[Tuple[float, float, float]]:
"""Get ranges for Dims.range in world coordinates.
This shares some code in common with the `extent` property, but
determines Dims.range settings for each dimension such that each
range is aligned to pixel centers at the finest scale.
"""
if len(self) == 0:
return [(0, 1, 1)] * self.ndim
else:
# Determine minimum step size across all layers
layer_extent_list = [layer.extent for layer in self]
scales = [extent.step for extent in layer_extent_list]
min_steps = self._step_size_from_scales(scales)
# Pixel-based layers need to be offset by 0.5 * min_steps to align
# Dims.range with pixel centers in world coordinates
pixel_offsets = [
0.5 * min_steps
if isinstance(layer, _ImageBase)
else [0] * len(min_steps)
for layer in self
]
# Non-pixel layers need an offset of the range stop by min_steps since the upper
# limit of Dims.range is non-inclusive.
point_offsets = [
[0] * len(min_steps)
if isinstance(layer, _ImageBase)
else min_steps
for layer in self
]
# Determine world coordinate extents similarly to
# `_get_extent_world`, but including offsets calculated above.
extrema = [extent.world for extent in layer_extent_list]
mins = [
e[0] + o1[: len(e[0])] for e, o1 in zip(extrema, pixel_offsets)
]
maxs = [
e[1] + o1[: len(e[0])] + o2[: len(e[0])]
for e, o1, o2 in zip(extrema, pixel_offsets, point_offsets)
]
min_v, max_v = self._get_min_and_max(mins, maxs)
# form range tuples, switching back to original dimension order
return [
(start, stop, step)
for start, stop, step in zip(min_v, max_v, min_steps)
]
@property
def ndim(self) -> int:
"""Maximum dimensionality of layers.
Defaults to 2 if no data is present.
Returns
-------
ndim : int
"""
return max((layer.ndim for layer in self), default=2)
def _link_layers(
self,
method: str,
layers: Optional[Iterable[Union[str, Layer]]] = None,
attributes: Iterable[str] = (),
):
# adding this method here allows us to emit an event when
# layers in this group are linked/unlinked. Which is necessary
# for updating context
from napari.layers.utils import _link_layers
if layers is not None:
layers = [self[x] if isinstance(x, str) else x for x in layers] # type: ignore
else:
layers = self
getattr(_link_layers, method)(layers, attributes)
self.selection.events.changed(added={}, removed={})
def link_layers(
self,
layers: Optional[Iterable[Union[str, Layer]]] = None,
attributes: Iterable[str] = (),
):
return self._link_layers('link_layers', layers, attributes)
def unlink_layers(
self,
layers: Optional[Iterable[Union[str, Layer]]] = None,
attributes: Iterable[str] = (),
):
return self._link_layers('unlink_layers', layers, attributes)
def save(
self,
path: str,
*,
selected: bool = False,
plugin: Optional[str] = None,
_writer: Optional['WriterContribution'] = None,
) -> List[str]:
"""Save all or only selected layers to a path using writer plugins.
If ``plugin`` is not provided and only one layer is targeted, then we
directly call the corresponding``napari_write_<layer_type>`` hook (see
:ref:`single layer writer hookspecs <write-single-layer-hookspecs>`)
which will loop through implementations and stop when the first one
returns a non-``None`` result. The order in which implementations are
called can be changed with the Plugin sorter in the GUI or with the
corresponding hook's
:meth:`~napari.plugins._hook_callers._HookCaller.bring_to_front`
method.
If ``plugin`` is not provided and multiple layers are targeted,
then we call
:meth:`~napari.plugins.hook_specifications.napari_get_writer` which
loops through plugins to find the first one that knows how to handle
the combination of layers and is able to write the file. If no plugins
offer :meth:`~napari.plugins.hook_specifications.napari_get_writer` for
that combination of layers then the default
:meth:`~napari.plugins.hook_specifications.napari_get_writer` will
create a folder and call ``napari_write_<layer_type>`` for each layer
using the ``Layer.name`` variable to modify the path such that the
layers are written to unique files in the folder.
If ``plugin`` is provided and a single layer is targeted, then we
call the ``napari_write_<layer_type>`` for that plugin, and if it fails
we error.
If ``plugin`` is provided and multiple layers are targeted, then
we call we call
:meth:`~napari.plugins.hook_specifications.napari_get_writer` for
that plugin, and if it doesn’t return a ``WriterFunction`` we error,
otherwise we call it and if that fails if it we error.
Parameters
----------
path : str
A filepath, directory, or URL to open. Extensions may be used to
specify output format (provided a plugin is available for the
requested format).
selected : bool
Optional flag to only save selected layers. False by default.
plugin : str, optional
Name of the plugin to use for saving. If None then all plugins
corresponding to appropriate hook specification will be looped
through to find the first one that can save the data.
_writer : WriterContribution, optional
private: npe2 specific writer override.
Returns
-------
list of str
File paths of any files that were written.
"""
from napari.plugins.io import save_layers
layers = (
[x for x in self if x in self.selection]
if selected
else list(self)
)
if selected:
msg = trans._("No layers selected", deferred=True)
else:
msg = trans._("No layers to save", deferred=True)
if not layers:
warnings.warn(msg)
return []
return save_layers(path, layers, plugin=plugin, _writer=_writer)