-
Notifications
You must be signed in to change notification settings - Fork 58
Guide about unit and scale aware rendering #1032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimMonko
wants to merge
8
commits into
napari:main
Choose a base branch
from
TimMonko:units-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8f811fa
rough draft of units guide
TimMonko 863cdd2
add to toc in guide section
TimMonko 2915c87
add small blurb to guide landing page
TimMonko 7c500ae
small update to viewer tutorial about scale bar
TimMonko ddb4606
update viewer tutorial quip
TimMonko a583111
clarify how scale_bar gets units
TimMonko 502e965
Apply suggestion from @brisvag to clarify apply to transforms
TimMonko 2e24c4a
Merge branch 'units-guide' of https://github.com/TimMonko/docs into u…
TimMonko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| (units-guide)= | ||
|
|
||
| # Scale and unit-aware rendering | ||
|
|
||
| Units in napari describe the physical meaning of each layer axis. They work | ||
| alongside layer transforms such as `scale` and `translate`, but they answer a | ||
| different question: `scale` informs about the spacing of the data, while `units` | ||
| inform the meaning behind each scale value. All layers of napari contain both | ||
| `scale` and `units` metadata, and they work together to determine how the layer is | ||
| rendered and how it interacts with other unit-aware features such as the scale bar. | ||
|
|
||
| For interactive editing, the | ||
| [napari-metadata](https://napari.org/napari-metadata/) plugin can expose | ||
| layer scale and units in a dock widget. | ||
|
|
||
| ## Scale and units live on layers | ||
|
|
||
| Scale and units are layer metadata. You can provide them when creating a layer or update | ||
| them later from Python: | ||
|
|
||
| ```python | ||
| import numpy as np | ||
| import napari | ||
|
|
||
| viewer = napari.Viewer() | ||
| layer = viewer.add_image( | ||
| np.zeros((16, 32, 64)), | ||
| scale=(4, 0.25, 0.25), | ||
| units=('s', 'um', 'um'), | ||
| ) | ||
|
|
||
| layer.units = ('usec','nm', 'nm') # update units later | ||
| ``` | ||
|
|
||
| In this example, each pixel of the image layer is spaced by `0.25` micrometers along both axes. | ||
| The scale is used to transform each layer from its data coordinates into rendered world coordinates, | ||
| so both vector-based layers (points, shapes, vectors, tracks) and raster-based layers (images, labels) | ||
| are affected by scale and units. | ||
|
|
||
| Units can be any valid [Pint](https://pint.readthedocs.io/en/stable/) unit, | ||
| of which [there are a plethora](https://github.com/hgrecco/pint/blob/master/pint/default_en.txt) | ||
| to serve many different disciplines. | ||
| Pint also has logic to disambiguate unit names, for example, | ||
| `'micrometer'`, `'um'`, and `'µm'` are all valid and will equate to `'µm'` in napari. | ||
|
|
||
| If you do not set units, napari assumes pixels. | ||
| To set *no* units, use `layer.units = ('dimensionless',...)`. | ||
|
|
||
| ## When units are consistent across layers | ||
|
|
||
| napari can use units to render layers in the same physical space even when the | ||
| numerical scale values are different. This matters when, for example, one layer | ||
| is stored in nanometers and another is stored in micrometers. | ||
|
|
||
| The gallery example {ref}`sphx_glr_gallery_units_impact_rendering.py` shows | ||
| this directly: two image layers use different units and different scale values, | ||
| but napari still aligns them in the same world coordinate system. | ||
|
|
||
| ## How the scale bar gets its units | ||
|
|
||
| The scale bar is a viewer overlay, but its label is tied to the same world | ||
| coordinates used to render the scene. | ||
| If units are consistent across layers, the scale bar will automatically display | ||
| the correct units and length to scale. If you do not provide units, napari falls | ||
| back to pixels. | ||
|
|
||
| The gallery example {ref}`sphx_glr_gallery_scale_bar.py` focuses on scale bar | ||
| appearance and layout. The gallery example | ||
| {ref}`sphx_glr_gallery_units_impact_rendering.py` focuses on unit-aware world | ||
| space. Taken together, they currently provide the clearest picture of how scale | ||
| bar display and layer units interact. | ||
|
|
||
| ## What units do not do | ||
|
|
||
| Units do not rename axes. Axis labels and units are related metadata, but they | ||
| solve different problems: | ||
|
|
||
| - axis labels describe what an axis represents, such as `time`, `z`, `y`, or | ||
| `x` | ||
| - units describe the measurement system on that axis, such as `s`, `nm`, or | ||
| `pixel` | ||
|
|
||
| For more about axis labels, broadcasting, and how layer metadata maps onto the | ||
| viewer, see {ref}`axis-names`. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.