-
-
Notifications
You must be signed in to change notification settings - Fork 410
/
shapes.py
2957 lines (2644 loc) · 113 KB
/
shapes.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import warnings
from contextlib import contextmanager
from copy import copy, deepcopy
from itertools import cycle
from typing import Dict, List, Tuple, Union
import numpy as np
import pandas as pd
from vispy.color import get_color_names
from napari.layers.base import Layer, no_op
from napari.layers.shapes._shape_list import ShapeList
from napari.layers.shapes._shapes_constants import (
Box,
ColorMode,
Mode,
ShapeType,
shape_classes,
)
from napari.layers.shapes._shapes_mouse_bindings import (
add_ellipse,
add_line,
add_path_polygon,
add_path_polygon_creating,
add_rectangle,
finish_drawing_shape,
highlight,
select,
vertex_insert,
vertex_remove,
)
from napari.layers.shapes._shapes_utils import (
create_box,
extract_shape_type,
get_default_shape_type,
get_shape_ndim,
number_of_shapes,
validate_num_vertices,
)
from napari.layers.utils.color_manager_utils import (
guess_continuous,
map_property,
)
from napari.layers.utils.color_transformations import (
normalize_and_broadcast_colors,
transform_color_cycle,
transform_color_with_defaults,
)
from napari.layers.utils.interactivity_utils import (
nd_line_segment_to_displayed_data_ray,
)
from napari.layers.utils.layer_utils import _FeatureTable, _unique_element
from napari.layers.utils.text_manager import TextManager
from napari.utils.colormaps import Colormap, ValidColormapArg, ensure_colormap
from napari.utils.colormaps.colormap_utils import ColorType
from napari.utils.colormaps.standardize_color import (
hex_to_name,
rgb_to_hex,
transform_color,
)
from napari.utils.events import Event
from napari.utils.events.custom_types import Array
from napari.utils.misc import ensure_iterable
from napari.utils.translations import trans
DEFAULT_COLOR_CYCLE = np.array([[1, 0, 1, 1], [0, 1, 0, 1]])
class Shapes(Layer):
"""Shapes layer.
Parameters
----------
data : list or array
List of shape data, where each element is an (N, D) array of the
N vertices of a shape in D dimensions. Can be an 3-dimensional
array if each shape has the same number of vertices.
ndim : int
Number of dimensions for shapes. When data is not None, ndim must be D.
An empty shapes layer can be instantiated with arbitrary ndim.
features : dict[str, array-like] or Dataframe-like
Features table where each row corresponds to a shape and each column
is a feature.
properties : dict {str: array (N,)}, DataFrame
Properties for each shape. Each property should be an array of length N,
where N is the number of shapes.
property_choices : dict {str: array (N,)}
possible values for each property.
text : str, dict
Text to be displayed with the shapes. If text is set to a key in properties,
the value of that property will be displayed. Multiple properties can be
composed using f-string-like syntax (e.g., '{property_1}, {float_property:.2f}).
A dictionary can be provided with keyword arguments to set the text values
and display properties. See TextManager.__init__() for the valid keyword arguments.
For example usage, see /napari/examples/add_shapes_with_text.py.
shape_type : string or list
String of shape shape_type, must be one of "{'line', 'rectangle',
'ellipse', 'path', 'polygon'}". If a list is supplied it must be
the same length as the length of `data` and each element will be
applied to each shape otherwise the same value will be used for all
shapes.
edge_width : float or list
Thickness of lines and edges. If a list is supplied it must be the
same length as the length of `data` and each element will be
applied to each shape otherwise the same value will be used for all
shapes.
edge_color : str, array-like
If string can be any color name recognized by vispy or hex value if
starting with `#`. If array-like must be 1-dimensional array with 3
or 4 elements. If a list is supplied it must be the same length as
the length of `data` and each element will be applied to each shape
otherwise the same value will be used for all shapes.
edge_color_cycle : np.ndarray, list
Cycle of colors (provided as string name, RGB, or RGBA) to map to edge_color if a
categorical attribute is used color the vectors.
edge_colormap : str, napari.utils.Colormap
Colormap to set edge_color if a continuous attribute is used to set face_color.
edge_contrast_limits : None, (float, float)
clims for mapping the property to a color map. These are the min and max value
of the specified property that are mapped to 0 and 1, respectively.
The default value is None. If set the none, the clims will be set to
(property.min(), property.max())
face_color : str, array-like
If string can be any color name recognized by vispy or hex value if
starting with `#`. If array-like must be 1-dimensional array with 3
or 4 elements. If a list is supplied it must be the same length as
the length of `data` and each element will be applied to each shape
otherwise the same value will be used for all shapes.
face_color_cycle : np.ndarray, list
Cycle of colors (provided as string name, RGB, or RGBA) to map to face_color if a
categorical attribute is used color the vectors.
face_colormap : str, napari.utils.Colormap
Colormap to set face_color if a continuous attribute is used to set face_color.
face_contrast_limits : None, (float, float)
clims for mapping the property to a color map. These are the min and max value
of the specified property that are mapped to 0 and 1, respectively.
The default value is None. If set the none, the clims will be set to
(property.min(), property.max())
z_index : int or list
Specifier of z order priority. Shapes with higher z order are
displayed ontop of others. If a list is supplied it must be the
same length as the length of `data` and each element will be
applied to each shape otherwise the same value will be used for all
shapes.
name : str
Name of the layer.
metadata : dict
Layer metadata.
scale : tuple of float
Scale factors for the layer.
translate : tuple of float
Translation values for the layer.
rotate : float, 3-tuple of float, or n-D array.
If a float convert into a 2D rotation matrix using that value as an
angle. If 3-tuple convert into a 3D rotation matrix, using a yaw,
pitch, roll convention. Otherwise assume an nD rotation. Angles are
assumed to be in degrees. They can be converted from radians with
np.degrees if needed.
shear : 1-D array or n-D array
Either a vector of upper triangular values, or an nD shear matrix with
ones along the main diagonal.
affine : n-D array or napari.utils.transforms.Affine
(N+1, N+1) affine transformation matrix in homogeneous coordinates.
The first (N, N) entries correspond to a linear transform and
the final column is a length N translation vector and a 1 or a napari
`Affine` transform object. Applied as an extra transform on top of the
provided scale, rotate, and shear values.
opacity : float
Opacity of the layer visual, between 0.0 and 1.0.
blending : str
One of a list of preset blending modes that determines how RGB and
alpha values of the layer visual get mixed. Allowed values are
{'opaque', 'translucent', and 'additive'}.
visible : bool
Whether the layer visual is currently being displayed.
cache : bool
Whether slices of out-of-core datasets should be cached upon retrieval.
Currently, this only applies to dask arrays.
Attributes
----------
data : (N, ) list of array
List of shape data, where each element is an (N, D) array of the
N vertices of a shape in D dimensions.
features : Dataframe-like
Features table where each row corresponds to a shape and each column
is a feature.
feature_defaults : DataFrame-like
Stores the default value of each feature in a table with one row.
properties : dict {str: array (N,)}, DataFrame
Properties for each shape. Each property should be an array of length N,
where N is the number of shapes.
text : str, dict
Text to be displayed with the shapes. If text is set to a key in properties,
the value of that property will be displayed. Multiple properties can be
composed using f-string-like syntax (e.g., '{property_1}, {float_property:.2f}).
For example usage, see /napari/examples/add_shapes_with_text.py.
shape_type : (N, ) list of str
Name of shape type for each shape.
edge_color : str, array-like
Color of the shape border. Numeric color values should be RGB(A).
face_color : str, array-like
Color of the shape face. Numeric color values should be RGB(A).
edge_width : (N, ) list of float
Edge width for each shape.
z_index : (N, ) list of int
z-index for each shape.
current_edge_width : float
Thickness of lines and edges of the next shape to be added or the
currently selected shape.
current_edge_color : str
Color of the edge of the next shape to be added or the currently
selected shape.
current_face_color : str
Color of the face of the next shape to be added or the currently
selected shape.
selected_data : set
List of currently selected shapes.
nshapes : int
Total number of shapes.
mode : Mode
Interactive mode. The normal, default mode is PAN_ZOOM, which
allows for normal interactivity with the canvas.
The SELECT mode allows for entire shapes to be selected, moved and
resized.
The DIRECT mode allows for shapes to be selected and their individual
vertices to be moved.
The VERTEX_INSERT and VERTEX_REMOVE modes allow for individual
vertices either to be added to or removed from shapes that are already
selected. Note that shapes cannot be selected in this mode.
The ADD_RECTANGLE, ADD_ELLIPSE, ADD_LINE, ADD_PATH, and ADD_POLYGON
modes all allow for their corresponding shape type to be added.
Notes
-----
_data_dict : Dict of ShapeList
Dictionary containing all the shape data indexed by slice tuple
_data_view : ShapeList
Object containing the currently viewed shape data.
_selected_data_history : set
Set of currently selected captured on press of <space>.
_selected_data_stored : set
Set of selected previously displayed. Used to prevent rerendering the
same highlighted shapes when no data has changed.
_selected_box : None | np.ndarray
`None` if no shapes are selected, otherwise a 10x2 array of vertices of
the interaction box. The first 8 points are the corners and midpoints
of the box. The 9th point is the center of the box, and the last point
is the location of the rotation handle that can be used to rotate the
box.
_drag_start : None | np.ndarray
If a drag has been started and is in progress then a length 2 array of
the initial coordinates of the drag. `None` otherwise.
_drag_box : None | np.ndarray
If a drag box is being created to select shapes then this is a 2x2
array of the two extreme corners of the drag. `None` otherwise.
_drag_box_stored : None | np.ndarray
If a drag box is being created to select shapes then this is a 2x2
array of the two extreme corners of the drag that have previously been
rendered. `None` otherwise. Used to prevent rerendering the same
drag box when no data has changed.
_is_moving : bool
Bool indicating if any shapes are currently being moved.
_is_selecting : bool
Bool indicating if a drag box is currently being created in order to
select shapes.
_is_creating : bool
Bool indicating if any shapes are currently being created.
_fixed_aspect : bool
Bool indicating if aspect ratio of shapes should be preserved on
resizing.
_aspect_ratio : float
Value of aspect ratio to be preserved if `_fixed_aspect` is `True`.
_fixed_vertex : None | np.ndarray
If a scaling or rotation is in progress then a length 2 array of the
coordinates that are remaining fixed during the move. `None` otherwise.
_fixed_index : int
If a scaling or rotation is in progress then the index of the vertex of
the bounding box that is remaining fixed during the move. `None`
otherwise.
_update_properties : bool
Bool indicating if properties are to allowed to update the selected
shapes when they are changed. Blocking this prevents circular loops
when shapes are selected and the properties are changed based on that
selection
_allow_thumbnail_update : bool
Flag set to true to allow the thumbnail to be updated. Blocking the thumbnail
can be advantageous where responsiveness is critical.
_clipboard : dict
Dict of shape objects that are to be used during a copy and paste.
_colors : list
List of supported vispy color names.
_vertex_size : float
Size of the vertices of the shapes and bounding box in Canvas
coordinates.
_rotation_handle_length : float
Length of the rotation handle of the bounding box in Canvas
coordinates.
_input_ndim : int
Dimensions of shape data.
_thumbnail_update_thresh : int
If there are more than this number of shapes, the thumbnail
won't update during interactive events
"""
_colors = get_color_names()
_vertex_size = 10
_rotation_handle_length = 20
_highlight_color = (0, 0.6, 1)
_highlight_width = 1.5
# If more shapes are present then they are randomly subsampled
# in the thumbnail
_max_shapes_thumbnail = 100
_drag_modes = {
Mode.PAN_ZOOM: no_op,
Mode.SELECT: select,
Mode.DIRECT: select,
Mode.VERTEX_INSERT: vertex_insert,
Mode.VERTEX_REMOVE: vertex_remove,
Mode.ADD_RECTANGLE: add_rectangle,
Mode.ADD_ELLIPSE: add_ellipse,
Mode.ADD_LINE: add_line,
Mode.ADD_PATH: add_path_polygon,
Mode.ADD_POLYGON: add_path_polygon,
Mode.TRANSFORM: no_op,
}
_move_modes = {
Mode.PAN_ZOOM: no_op,
Mode.SELECT: highlight,
Mode.DIRECT: highlight,
Mode.VERTEX_INSERT: highlight,
Mode.VERTEX_REMOVE: highlight,
Mode.ADD_RECTANGLE: no_op,
Mode.ADD_ELLIPSE: no_op,
Mode.ADD_LINE: no_op,
Mode.ADD_PATH: add_path_polygon_creating,
Mode.ADD_POLYGON: add_path_polygon_creating,
Mode.TRANSFORM: no_op,
}
_double_click_modes = {
Mode.PAN_ZOOM: no_op,
Mode.SELECT: no_op,
Mode.DIRECT: no_op,
Mode.VERTEX_INSERT: no_op,
Mode.VERTEX_REMOVE: no_op,
Mode.ADD_RECTANGLE: no_op,
Mode.ADD_ELLIPSE: no_op,
Mode.ADD_LINE: no_op,
Mode.ADD_PATH: finish_drawing_shape,
Mode.ADD_POLYGON: finish_drawing_shape,
Mode.TRANSFORM: no_op,
}
_cursor_modes = {
Mode.PAN_ZOOM: 'standard',
Mode.SELECT: 'pointing',
Mode.DIRECT: 'pointing',
Mode.VERTEX_INSERT: 'cross',
Mode.VERTEX_REMOVE: 'cross',
Mode.ADD_RECTANGLE: 'cross',
Mode.ADD_ELLIPSE: 'cross',
Mode.ADD_LINE: 'cross',
Mode.ADD_PATH: 'cross',
Mode.ADD_POLYGON: 'cross',
Mode.TRANSFORM: 'standard',
}
_interactive_modes = {
Mode.PAN_ZOOM,
}
def __init__(
self,
data=None,
*,
ndim=None,
features=None,
properties=None,
property_choices=None,
text=None,
shape_type='rectangle',
edge_width=1,
edge_color='#777777',
edge_color_cycle=None,
edge_colormap='viridis',
edge_contrast_limits=None,
face_color='white',
face_color_cycle=None,
face_colormap='viridis',
face_contrast_limits=None,
z_index=0,
name=None,
metadata=None,
scale=None,
translate=None,
rotate=None,
shear=None,
affine=None,
opacity=0.7,
blending='translucent',
visible=True,
cache=True,
experimental_clipping_planes=None,
):
if data is None:
if ndim is None:
ndim = 2
data = np.empty((0, 0, ndim))
else:
data, shape_type = extract_shape_type(data, shape_type)
data_ndim = get_shape_ndim(data)
if ndim is not None and ndim != data_ndim:
raise ValueError(
trans._(
"Shape dimensions must be equal to ndim",
deferred=True,
)
)
ndim = data_ndim
super().__init__(
data,
ndim=ndim,
name=name,
metadata=metadata,
scale=scale,
translate=translate,
rotate=rotate,
shear=shear,
affine=affine,
opacity=opacity,
blending=blending,
visible=visible,
cache=cache,
experimental_clipping_planes=experimental_clipping_planes,
)
self.events.add(
mode=Event,
edge_width=Event,
edge_color=Event,
face_color=Event,
properties=Event,
current_edge_color=Event,
current_face_color=Event,
current_properties=Event,
highlight=Event,
features=Event,
feature_defaults=Event,
)
# Flag set to false to block thumbnail refresh
self._allow_thumbnail_update = True
self._display_order_stored = []
self._ndisplay_stored = self._slice_input.ndisplay
self._feature_table = _FeatureTable.from_layer(
features=features,
properties=properties,
property_choices=property_choices,
num_data=number_of_shapes(data),
)
# The following shape properties are for the new shapes that will
# be drawn. Each shape has a corresponding property with the
# value for itself
if np.isscalar(edge_width):
self._current_edge_width = edge_width
else:
self._current_edge_width = 1
self._data_view = ShapeList(ndisplay=self._slice_input.ndisplay)
self._data_view.slice_key = np.array(self._slice_indices)[
self._slice_input.not_displayed
]
self._value = (None, None)
self._value_stored = (None, None)
self._moving_value = (None, None)
self._selected_data = set()
self._selected_data_stored = set()
self._selected_data_history = set()
self._selected_box = None
self._drag_start = None
self._fixed_vertex = None
self._fixed_aspect = False
self._aspect_ratio = 1
self._is_moving = False
# _moving_coordinates are needed for fixing aspect ratio during
# a resize, it stores the last pointer coordinate value that happened
# during a mouse move to that pressing/releasing shift
# can trigger a redraw of the shape with a fixed aspect ratio.
self._moving_coordinates = None
self._fixed_index = 0
self._is_selecting = False
self._drag_box = None
self._drag_box_stored = None
self._is_creating = False
self._clipboard = {}
# change mode once to trigger the
# Mode setting logic
self._mode = Mode.SELECT
self.mode = Mode.PAN_ZOOM
self._status = self.mode
self._init_shapes(
data,
shape_type=shape_type,
edge_width=edge_width,
edge_color=edge_color,
edge_color_cycle=edge_color_cycle,
edge_colormap=edge_colormap,
edge_contrast_limits=edge_contrast_limits,
face_color=face_color,
face_color_cycle=face_color_cycle,
face_colormap=face_colormap,
face_contrast_limits=face_contrast_limits,
z_index=z_index,
)
# set the current_* properties
if len(data) > 0:
self._current_edge_color = self.edge_color[-1]
self._current_face_color = self.face_color[-1]
elif len(data) == 0 and len(self.properties) > 0:
self._initialize_current_color_for_empty_layer(edge_color, 'edge')
self._initialize_current_color_for_empty_layer(face_color, 'face')
elif len(data) == 0 and len(self.properties) == 0:
self._current_edge_color = transform_color_with_defaults(
num_entries=1,
colors=edge_color,
elem_name="edge_color",
default="black",
)
self._current_face_color = transform_color_with_defaults(
num_entries=1,
colors=face_color,
elem_name="face_color",
default="black",
)
self._text = TextManager._from_layer(
text=text,
features=self.features,
)
# Trigger generation of view slice and thumbnail
self.refresh()
def _initialize_current_color_for_empty_layer(
self, color: ColorType, attribute: str
):
"""Initialize current_{edge,face}_color when starting with empty layer.
Parameters
----------
color : (N, 4) array or str
The value for setting edge or face_color
attribute : str in {'edge', 'face'}
The name of the attribute to set the color of.
Should be 'edge' for edge_color or 'face' for face_color.
"""
color_mode = getattr(self, f'_{attribute}_color_mode')
if color_mode == ColorMode.DIRECT:
curr_color = transform_color_with_defaults(
num_entries=1,
colors=color,
elem_name=f'{attribute}_color',
default="white",
)
elif color_mode == ColorMode.CYCLE:
color_cycle = getattr(self, f'_{attribute}_color_cycle')
curr_color = transform_color(next(color_cycle))
# add the new color cycle mapping
color_property = getattr(self, f'_{attribute}_color_property')
prop_value = self.property_choices[color_property][0]
color_cycle_map = getattr(self, f'{attribute}_color_cycle_map')
color_cycle_map[prop_value] = np.squeeze(curr_color)
setattr(self, f'{attribute}_color_cycle_map', color_cycle_map)
elif color_mode == ColorMode.COLORMAP:
color_property = getattr(self, f'_{attribute}_color_property')
prop_value = self.property_choices[color_property][0]
colormap = getattr(self, f'{attribute}_colormap')
contrast_limits = getattr(self, f'_{attribute}_contrast_limits')
curr_color, _ = map_property(
prop=prop_value,
colormap=colormap,
contrast_limits=contrast_limits,
)
setattr(self, f'_current_{attribute}_color', curr_color)
@property
def data(self):
"""list: Each element is an (N, D) array of the vertices of a shape."""
return self._data_view.data
@data.setter
def data(self, data):
self._finish_drawing()
data, shape_type = extract_shape_type(data)
n_new_shapes = number_of_shapes(data)
# not given a shape_type through data
if shape_type is None:
shape_type = self.shape_type
edge_widths = self._data_view.edge_widths
edge_color = self._data_view.edge_color
face_color = self._data_view.face_color
z_indices = self._data_view.z_indices
# fewer shapes, trim attributes
if self.nshapes > n_new_shapes:
shape_type = shape_type[:n_new_shapes]
edge_widths = edge_widths[:n_new_shapes]
z_indices = z_indices[:n_new_shapes]
edge_color = edge_color[:n_new_shapes]
face_color = face_color[:n_new_shapes]
# more shapes, add attributes
elif self.nshapes < n_new_shapes:
n_shapes_difference = n_new_shapes - self.nshapes
shape_type = (
shape_type
+ [get_default_shape_type(shape_type)] * n_shapes_difference
)
edge_widths = edge_widths + [1] * n_shapes_difference
z_indices = z_indices + [0] * n_shapes_difference
edge_color = np.concatenate(
(
edge_color,
self._get_new_shape_color(n_shapes_difference, 'edge'),
)
)
face_color = np.concatenate(
(
face_color,
self._get_new_shape_color(n_shapes_difference, 'face'),
)
)
self._data_view = ShapeList(ndisplay=self._slice_input.ndisplay)
self.add(
data,
shape_type=shape_type,
edge_width=edge_widths,
edge_color=edge_color,
face_color=face_color,
z_index=z_indices,
)
self._update_dims()
self.events.data(value=self.data)
self._set_editable()
def _on_selection(self, selected: bool):
# this method is slated for removal. don't add anything new.
if not selected:
self._finish_drawing()
@property
def features(self):
"""Dataframe-like features table.
It is an implementation detail that this is a `pandas.DataFrame`. In the future,
we will target the currently-in-development Data API dataframe protocol [1].
This will enable us to use alternate libraries such as xarray or cuDF for
additional features without breaking existing usage of this.
If you need to specifically rely on the pandas API, please coerce this to a
`pandas.DataFrame` using `features_to_pandas_dataframe`.
References
----------
.. [1]: https://data-apis.org/dataframe-protocol/latest/API.html
"""
return self._feature_table.values
@features.setter
def features(
self,
features: Union[Dict[str, np.ndarray], pd.DataFrame],
) -> None:
self._feature_table.set_values(features, num_data=self.nshapes)
if self._face_color_property and (
self._face_color_property not in self.features
):
self._face_color_property = ''
warnings.warn(
trans._(
'property used for face_color dropped',
deferred=True,
),
RuntimeWarning,
)
if self._edge_color_property and (
self._edge_color_property not in self.features
):
self._edge_color_property = ''
warnings.warn(
trans._(
'property used for edge_color dropped',
deferred=True,
),
RuntimeWarning,
)
self.text.refresh(self.features)
self.events.properties()
self.events.features()
@property
def feature_defaults(self):
"""Dataframe-like with one row of feature default values.
See `features` for more details on the type of this property.
"""
return self._feature_table.defaults
@property
def properties(self) -> Dict[str, np.ndarray]:
"""dict {str: np.ndarray (N,)}, DataFrame: Annotations for each shape"""
return self._feature_table.properties()
@properties.setter
def properties(self, properties: Dict[str, Array]):
self.features = properties
@property
def property_choices(self) -> Dict[str, np.ndarray]:
return self._feature_table.choices()
def _get_ndim(self):
"""Determine number of dimensions of the layer."""
if self.nshapes == 0:
ndim = self.ndim
else:
ndim = self.data[0].shape[1]
return ndim
@property
def _extent_data(self) -> np.ndarray:
"""Extent of layer in data coordinates.
Returns
-------
extent_data : array, shape (2, D)
"""
if len(self.data) == 0:
extrema = np.full((2, self.ndim), np.nan)
else:
maxs = np.max([np.max(d, axis=0) for d in self.data], axis=0)
mins = np.min([np.min(d, axis=0) for d in self.data], axis=0)
extrema = np.vstack([mins, maxs])
return extrema
@property
def nshapes(self):
"""int: Total number of shapes."""
return len(self._data_view.shapes)
@property
def current_edge_width(self):
"""float: Width of shape edges including lines and paths."""
return self._current_edge_width
@current_edge_width.setter
def current_edge_width(self, edge_width):
self._current_edge_width = edge_width
if self._update_properties:
for i in self.selected_data:
self._data_view.update_edge_width(i, edge_width)
self.events.edge_width()
@property
def current_edge_color(self):
"""str: color of shape edges including lines and paths."""
hex_ = rgb_to_hex(self._current_edge_color)[0]
return hex_to_name.get(hex_, hex_)
@current_edge_color.setter
def current_edge_color(self, edge_color):
self._current_edge_color = transform_color(edge_color)
if self._update_properties:
for i in self.selected_data:
self._data_view.update_edge_color(i, self._current_edge_color)
self.events.edge_color()
self._update_thumbnail()
self.events.current_edge_color()
@property
def current_face_color(self):
"""str: color of shape faces."""
hex_ = rgb_to_hex(self._current_face_color)[0]
return hex_to_name.get(hex_, hex_)
@current_face_color.setter
def current_face_color(self, face_color):
self._current_face_color = transform_color(face_color)
if self._update_properties:
for i in self.selected_data:
self._data_view.update_face_color(i, self._current_face_color)
self.events.face_color()
self._update_thumbnail()
self.events.current_face_color()
@property
def current_properties(self) -> Dict[str, np.ndarray]:
"""dict{str: np.ndarray(1,)}: properties for the next added shape."""
return self._feature_table.currents()
@current_properties.setter
def current_properties(self, current_properties):
update_indices = None
if (
self._update_properties
and len(self.selected_data) > 0
and self._mode in [Mode.SELECT, Mode.PAN_ZOOM]
):
update_indices = list(self.selected_data)
self._feature_table.set_currents(
current_properties, update_indices=update_indices
)
if update_indices is not None:
self.refresh_colors()
self.events.properties()
self.events.features()
self.events.current_properties()
self.events.feature_defaults()
@property
def shape_type(self):
"""list of str: name of shape type for each shape."""
return self._data_view.shape_types
@shape_type.setter
def shape_type(self, shape_type):
self._finish_drawing()
new_data_view = ShapeList()
shape_inputs = zip(
self._data_view.data,
ensure_iterable(shape_type),
self._data_view.edge_widths,
self._data_view.edge_color,
self._data_view.face_color,
self._data_view.z_indices,
)
self._add_shapes_to_view(shape_inputs, new_data_view)
self._data_view = new_data_view
self._update_dims()
@property
def edge_color(self):
"""(N x 4) np.ndarray: Array of RGBA face colors for each shape"""
return self._data_view.edge_color
@edge_color.setter
def edge_color(self, edge_color):
self._set_color(edge_color, 'edge')
self.events.edge_color()
self._update_thumbnail()
@property
def edge_color_cycle(self) -> np.ndarray:
"""Union[list, np.ndarray] : Color cycle for edge_color.
Can be a list of colors defined by name, RGB or RGBA
"""
return self._edge_color_cycle_values
@edge_color_cycle.setter
def edge_color_cycle(self, edge_color_cycle: Union[list, np.ndarray]):
self._set_color_cycle(edge_color_cycle, 'edge')
@property
def edge_colormap(self) -> Tuple[str, Colormap]:
"""Return the colormap to be applied to a property to get the edge color.
Returns
-------
colormap : napari.utils.Colormap
The Colormap object.
"""
return self._edge_colormap
@edge_colormap.setter
def edge_colormap(self, colormap: ValidColormapArg):
self._edge_colormap = ensure_colormap(colormap)
@property
def edge_contrast_limits(self) -> Tuple[float, float]:
"""None, (float, float): contrast limits for mapping
the edge_color colormap property to 0 and 1
"""
return self._edge_contrast_limits
@edge_contrast_limits.setter
def edge_contrast_limits(
self, contrast_limits: Union[None, Tuple[float, float]]
):
self._edge_contrast_limits = contrast_limits
@property
def edge_color_mode(self) -> str:
"""str: Edge color setting mode
DIRECT (default mode) allows each shape color to be set arbitrarily
CYCLE allows the color to be set via a color cycle over an attribute
COLORMAP allows color to be set via a color map over an attribute
"""
return str(self._edge_color_mode)
@edge_color_mode.setter
def edge_color_mode(self, edge_color_mode: Union[str, ColorMode]):
self._set_color_mode(edge_color_mode, 'edge')
@property
def face_color(self):
"""(N x 4) np.ndarray: Array of RGBA face colors for each shape"""
return self._data_view.face_color
@face_color.setter
def face_color(self, face_color):
self._set_color(face_color, 'face')
self.events.face_color()
self._update_thumbnail()
@property
def face_color_cycle(self) -> np.ndarray:
"""Union[np.ndarray, cycle]: Color cycle for face_color
Can be a list of colors defined by name, RGB or RGBA
"""
return self._face_color_cycle_values
@face_color_cycle.setter
def face_color_cycle(self, face_color_cycle: Union[np.ndarray, cycle]):
self._set_color_cycle(face_color_cycle, 'face')
@property
def face_colormap(self) -> Tuple[str, Colormap]:
"""Return the colormap to be applied to a property to get the face color.
Returns
-------
colormap : napari.utils.Colormap
The Colormap object.
"""
return self._face_colormap
@face_colormap.setter
def face_colormap(self, colormap: ValidColormapArg):
self._face_colormap = ensure_colormap(colormap)
@property
def face_contrast_limits(self) -> Union[None, Tuple[float, float]]:
"""None, (float, float) : clims for mapping the face_color
colormap property to 0 and 1
"""
return self._face_contrast_limits
@face_contrast_limits.setter
def face_contrast_limits(
self, contrast_limits: Union[None, Tuple[float, float]]
):
self._face_contrast_limits = contrast_limits
@property
def face_color_mode(self) -> str:
"""str: Face color setting mode
DIRECT (default mode) allows each shape color to be set arbitrarily
CYCLE allows the color to be set via a color cycle over an attribute
COLORMAP allows color to be set via a color map over an attribute
"""
return str(self._face_color_mode)