Skip to content

Commit 1dd9162

Browse files
authored
Replace use of properties with features (#425)
# References and relevant issues Part of napari/napari#3911 # Description This replaces use of `properties`, `property_choices`, and `current_properties` with `features` and `feature_defaults`. The motivation for doing this now is to make the latest documentation consistent with existing code and examples on `main` (here and on napari/napari) for the upcoming v0.5 release. As we also plan to deprecate properties in v0.5 (napari/napari#3911), this documentation update is necessary for that.
1 parent ef267d3 commit 1dd9162

File tree

6 files changed

+181
-223
lines changed

6 files changed

+181
-223
lines changed

docs/howtos/extending/magicgui.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ The following are all valid {attr}`napari.types.LayerDataTuple` examples:
539539
# an empty points layer
540540
(None, {}, 'points')
541541

542-
# points with properties
543-
(np.random.rand(20, 2), {'properties': {'values': np.random.rand(20)}}, 'points')
542+
# points with features
543+
(np.random.rand(20, 2), {'features': {'values': np.random.rand(20)}}, 'points')
544544
```
545545

546546
An example of using a {attr}`~napari.types.LayerDataTuple` return annotation in
@@ -553,8 +553,8 @@ import napari.types
553553
@magicgui(call_button='Make Points')
554554
def make_points(n_points=40) -> napari.types.LayerDataTuple:
555555
data = 500 * np.random.rand(n_points, 2)
556-
props = {'values': np.random.rand(n_points)}
557-
return (data, {'properties': props}, 'points')
556+
features = {'values': np.random.rand(n_points)}
557+
return (data, {'features': features}, 'points')
558558
559559
viewer = napari.Viewer()
560560
viewer.window.add_dock_widget(make_points)

docs/howtos/layers/points.md

Lines changed: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ kernelspec:
1515

1616
```{Admonition} DEPRECATED ATTRIBUTES
1717
:class: warning
18-
As of napari 0.5.0, `edge_*` attributes are being renamed to
19-
`border_*` attributes. We have yet to update the images and/or videos in
18+
As of napari 0.5.0, `edge_*` attributes are being renamed to
19+
`border_*` attributes. We have yet to update the images and/or videos in
2020
this tutorial. Please use `border` in place of `edge` for all `Points` attributes moving forward.
2121
2222
The code in this tutorial uses the latest API. Only images and videos may be out of date.
@@ -37,11 +37,11 @@ points independently. You can also adjust `opacity` and `symbol` representing
3737
all the points simultaneously.
3838

3939
Each data point can have annotations associated with it using the
40-
`Points.properties` dictionary. These properties can be used to set the face and
40+
`Points.features` table. These features can be used to set the face and
4141
border colors of the points. For example, when displaying points of different
4242
classes/types, one could automatically set color the individual points by their
43-
respective class/type. For more details on point properties, see
44-
[](#setting-point-border-and-face-color-with-properties) or
43+
respective class/type. For more details on point features, see
44+
[](#setting-point-border-and-face-color-with-features) or
4545
[point annotation tutorial](../../tutorials/annotation/annotate_points).
4646

4747
## Creating and editing the `points` layer using the GUI
@@ -116,7 +116,7 @@ layer:
116116
to move around the `points` layer as you create your selection.
117117

118118
* **Pan/zoom**
119-
![image: Pan/zoom tool](../../images/pan-zoom-tool.png)
119+
![image: Pan/zoom tool](../../images/pan-zoom-tool.png)
120120

121121
The default mode of the points layer supports panning and zooming, as in the
122122
image layer. This mode is represented by the magnifying glass in the layers
@@ -194,7 +194,7 @@ layer:
194194
Note that when entering 3D rendering mode the GUI `Add point`,
195195
`Delete selected points`, and `Select points` tools are all disabled. Those
196196
options are supported only when viewing a layer using 2D rendering.
197-
197+
198198
* `ctrl-c` and `ctrl-v` (copying and pasting points)
199199

200200
Copy and paste any selected points using `ctrl-c` and `ctrl-v`, respectively.
@@ -214,12 +214,12 @@ the same. In these examples we'll mainly use `add_points` to overlay points onto
214214
on an existing image.
215215

216216
Each data point can have annotations associated with it using the
217-
`Points.properties` dictionary. These properties can be used to set the face and
217+
`Points.features` table. These features can be used to set the face and
218218
border colors of the points. For example, when displaying points of different
219219
classes/types, one could automatically set the color of the individual points by
220-
their respective class/type. For more details on point properties, see
221-
[](#setting-point-border-and-face-color-with-properties) below or the
222-
[Point annotation tutorial](../../tutorials/annotation/annotate_points).
220+
their respective class/type. For more details on point features, see
221+
[](#setting-point-border-and-face-color-with-features) below or the
222+
[Point annotation tutorial](annotating-points).
223223

224224
In this example, we will overlay some points on the image of an astronaut:
225225

@@ -267,14 +267,40 @@ the same as the ordering of the dimensions for image layers. This array is
267267
always accessible through the `layer.data` property and will grow or shrink as
268268
new points are either added or deleted.
269269

270-
### Using the points properties dictionary
270+
(points-features-table)=
271+
272+
### Using the points features table
273+
274+
The `Points` layer can contain features that annotate each point.
275+
`Points.features` stores the features in a table or data frame where each column
276+
represents a feature and each row represents a point.
277+
Therefore, the table has N rows for the N points in `Points.data`.
278+
This table can be provided as a dictionary that maps from feature names to
279+
the columns of feature values.
280+
For example, the following dictionary can be used as the value for the `features`
281+
parameter in {meth}`Viewer.add_points<napari.Viewer.add_points>`
282+
283+
```python
284+
features = {
285+
'good_point': [True, True, False],
286+
'confidence': [0.99, 0.8, 0.2],
287+
}
288+
```
289+
290+
and corresponds to the following features table
291+
292+
| point index | good_point | confidence |
293+
| ----------- | ---------- | ---------- |
294+
| 0 | True | 0.99 |
295+
| 1 | True | 0.8 |
296+
| 2 | False | 0.2 |
297+
298+
where the point index is the index for a point in both `data` and its corresponding
299+
row in the `features` table.
271300

272-
The `points` layer can contain properties that annotate each point.
273-
`Points.properties` stores the properties in a dictionary where each key is the
274-
name of the property and the values are NumPy arrays with a value for each point
275-
(i.e., length N for N points in `Points.data`). As we will see below, we can use
276-
the values in a property to set the display properties of the points (e.g., face
277-
color or border color). To see the points properties in action, please see the
301+
As we will see below, we can use feature values to determine the display properties
302+
of the points (e.g., face color or border color).
303+
To see the points features in action, please see the
278304
[Point annotation tutorial](annotating-points).
279305

280306

@@ -338,17 +364,17 @@ properties are different from the `layer.current_border_color` and
338364
`layer.current_face_color` properties that will determine the color of the next
339365
point to be added or any currently selected points.
340366

341-
### Setting point border and face color with properties
367+
### Setting point border and face color with features
342368

343-
Point border and face colors can be set as a function of a property in
344-
`Points.properties`. There are two ways the values in `Points.properties` can be
369+
Point border and face colors can be set as a function of a feature in
370+
`Points.features`. There are two ways that these feature values can be
345371
mapped to colors: (1) color cycles and (2) colormaps.
346372

347-
Color cycles are sets of colors that are mapped to categorical properties. The
348-
colors are repeated if the number of unique property values is greater than the
349-
number of colors in the color cycle.
373+
Color cycles are sets of colors that are mapped to categorical features.
374+
The colors are repeated if the number of unique feature values is greater
375+
than the number of colors in the color cycle.
350376

351-
Colormaps are a continuum of colors that are mapped to a continuous property
377+
Colormaps are a continuum of colors that are mapped to a continuous feature
352378
value. The available colormaps are listed below (colormaps are from
353379
[vispy](https://vispy.org/api/vispy.color.colormap.html#vispy.color.colormap.Colormap)).
354380
For guidance on choosing colormaps, see the
@@ -360,21 +386,21 @@ list(napari.utils.colormaps.AVAILABLE_COLORMAPS)
360386

361387
### Setting border or face color with a color cycle
362388

363-
Here we will set the border color of the markers with a color cycle on a property.
389+
Here we will set the border color of the markers with a color cycle on a feature.
364390
To do the same for a face color, substitute `face_color` for `border_color` in the
365391
example snippet below.
366392

367393
```{code-cell} python
368394
viewer = napari.view_image(data.astronaut(), rgb=True)
369395
points = np.array([[100, 100], [200, 200], [300, 100]])
370-
point_properties = {
371-
'good_point': np.array([True, True, False]),
372-
'confidence': np.array([0.99, 0.8, 0.2]),
396+
point_features = {
397+
'good_point': [True, True, False],
398+
'confidence': [0.99, 0.8, 0.2],
373399
}
374400
375401
points_layer = viewer.add_points(
376402
points,
377-
properties=point_properties,
403+
features=point_features,
378404
border_color='good_point',
379405
border_color_cycle=['magenta', 'green'],
380406
border_width=0.5,
@@ -392,33 +418,34 @@ nbscreenshot(viewer, alt_text="3 points overlaid on an astronaut image, where th
392418
viewer.close()
393419
```
394420

395-
In the example above, the `point_properties` were provided as a dictionary with
396-
two properties: `good_point` and `confidence`. The values of each property are
397-
stored in a NumPy ndarray with length 3 since there were 3 coordinates provided
398-
in `points`. We set the border color as a function of the `good_point` property by
399-
providing the keyword argument `border_color='good_point'` to the
400-
`viewer.add_points()` method. The color cycle is set via the `border_color_cycle`
401-
keyword argument, `border_color_cycle=['magenta', 'green']`. The color cycle can
402-
be provided as a list of colors (as a list of strings or a (M x 4) array of M
403-
RGBA colors).
421+
In the example above, the `point_features` table was provided as a
422+
dictionary with two keys or features: `good_point` and `confidence`
423+
as described in [](points-features-table).
424+
The values of each feature are stored in a list of length 3 since there were three
425+
coordinates provided in `points`. We set the border color as a function of the
426+
`good_point` feature by providing the keyword argument
427+
`border_color='good_point'` to the `viewer.add_points()` method.
428+
The color cycle is set via the `border_color_cycle` keyword argument,
429+
`border_color_cycle=['magenta', 'green']`. The color cycle can be provided as a
430+
list of colors (a list of strings or a (M x 4) array of M RGBA colors).
404431

405432
### Setting border or face color with a colormap
406433

407434
In the example snippet below, we set the face color of the markers with a
408-
colormap on a property. To do the same for a border color, substitute `face` for
435+
colormap on a feature. To do the same for a border color, substitute `face` for
409436
`border`.
410437

411438
```{code-cell} python
412439
viewer = napari.view_image(data.astronaut(), rgb=True)
413440
points = np.array([[100, 100], [200, 200], [300, 100]])
414-
point_properties = {
415-
'good_point': np.array([True, True, False]),
416-
'confidence': np.array([0.99, 0.8, 0.2]),
441+
point_features = {
442+
'good_point': [True, True, False],
443+
'confidence': [0.99, 0.8, 0.2],
417444
}
418445
419446
points_layer = viewer.add_points(
420447
points,
421-
properties=point_properties,
448+
features=point_features,
422449
face_color='confidence',
423450
face_colormap='viridis',
424451
)
@@ -435,13 +462,15 @@ nbscreenshot(viewer, alt_text="3 points overlaid on an astronaut image, where th
435462
viewer.close()
436463
```
437464

438-
In the example above, the `point_properties` were provided as a dictionary with
439-
two properties: `good_point` and `confidence`. The values of each property are
440-
stored in a NumPy ndarray with length 3 since there were 3 coordinates provided
441-
in `points`. We set the face color as a function of the `confidence` property by
442-
providing the keyword argument `face_color='confidence'` to the
443-
`viewer.add_points()` method. We set the colormap to viridis using the
444-
`face_colormap` keyword argument as `face_colormap='viridis'`.
465+
In the example above, the `point_features` table was provided as a
466+
dictionary with two keys or features: `good_point` and `confidence`
467+
as described in [](points-features-table).
468+
The values of each feature are stored in a list of length 3 since there were three
469+
coordinates provided in `points`.
470+
We set the face color as a function of the `confidence` feature by providing the
471+
keyword argument `face_color='confidence'` to the `viewer.add_points()` method.
472+
We set the colormap to viridis using the `face_colormap` keyword argument
473+
as `face_colormap='viridis'`.
445474

446475
### Changing the points symbol
447476

@@ -452,7 +481,7 @@ layer using the `symbol` keyword argument.
452481
## Putting it all together
453482

454483
Here you can see an example of adding, selecting, deleting points, and changing
455-
their properties:
484+
their features:
456485

457486
```{raw} html
458487
<figure>

0 commit comments

Comments
 (0)