Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3357 from ericpre/use_pint_global_registry
Use `pint.get_application_registry` to improve interoperability with other modules
- Loading branch information
Showing
6 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,33 @@ | ||
.. _pint_unit_registry: | ||
|
||
Unit Handling with Pint Quantity | ||
******************************** | ||
|
||
HyperSpy uses the `pint <https://pint.readthedocs.io>`_ library to handle unit conversion. | ||
To be interoperatable with other modules, HyperSpy uses the default pint :class:`pint.UnitRegistry` | ||
provided by the :func:`pint.get_application_registry` function as described in the sections | ||
`having a shared registry <https://pint.readthedocs.io/en/stable/getting/pint-in-your-projects.html>`_ | ||
and `serialization <https://pint.readthedocs.io/en/stable/advanced/serialization.html>`_ | ||
of the pint user guide. | ||
|
||
For example, in the case of the ``scale_as_quantify`` pint quantity object from :class:`~.axes.UniformDataAxis`, | ||
the default pint :class:`pint.UnitRegistry` is used: | ||
|
||
.. code-block:: python | ||
>>> s = hs.signals.Signal1D(np.arange(10)) | ||
>>> s.axes_manager[0].scale_as_quantity | ||
<Quantity(1.0, 'dimensionless')> | ||
>>> s.axes_manager[0].scale_as_quantity = '2.5 µm' | ||
>>> s.axes_manager[0].scale_as_quantity | ||
<Quantity(2.5, 'micrometer')> | ||
Then, using :func:`pint.get_application_registry` get the handle of the same instance of :class:`pint.UnitRegistry` | ||
used by HyperSpy and use it to operate on this pint quantity: | ||
|
||
>>> import pint | ||
>>> ureg = pint.get_application_registry() | ||
>>> scale = 2E-6 * ureg.meter | ||
>>> s.axes_manager[0].scale_as_quantity += scale | ||
>>> s.axes_manager[0].scale_as_quantity | ||
<Quantity(4.5, 'micrometer')> |
This file contains 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 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 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 @@ | ||
Use :func:`pint.get_application_registry` to get :class:`pint.UnitRegistry` and facilitate interoperability of pint quantity operation with other modules . |