Unit conversion in pyAML #49
Replies: 3 comments
-
|
In PAMILA, I adopted pint for unit handling. Here is a summary of what I did and why. Two distinct types of "unit conversion" in PAMILAPAMILA distinguishes two conceptually different types of unit conversion:
How pint is usedpint is used to attach units to all values that users interact with. Specifically:
Features of pint used in PAMILA
A practical issue encountered
Scope: where pint is not usedThe device-level Compatibility with blueskypint objects flowing through bluesky plans required some monkey-patching to handle them correctly. DiagramsGET: flowchart LR
PV([PV]) -->|"float [PV unit]"| SIG([Signal])
SIG -->|"Q_ [LoLv repr unit]"| STRIP
subgraph Device
direction TB
STRIP["strip unit (→ src_unit)"]
CONV["repr conv func"]
WRAP["wrap with dst_unit"]
STRIP -->|floats| CONV
CONV -->|floats| WRAP
end
WRAP -->|"Q_ [HiLv repr unit]"| USER([User])
PUT: flowchart LR
USER([User]) -->|"Q_ [HiLv repr unit]"| STRIP
subgraph Device
direction TB
STRIP["strip unit (→ src_unit)"]
CONV["repr conv func"]
WRAP["wrap with LoLv repr unit"]
STRIP -->|floats| CONV
CONV -->|floats| WRAP
end
WRAP -->|"Q_ [LoLv repr unit]"| SIG([Signal])
SIG -->|"float [PV unit]"| PV([PV])
Simple Examples# Must pass a pint Quantity object Q_ — plain float raises TypeError
mlv.put(Q_("0.1 A"))
# get() returns a Q_ object
mlv.get() # e.g., Q_("0.1 A")
mlvs["C30_C1_x_angle_SP"].get() # e.g., Q_("-10 urad") — different repr, no pint needed by user
# Extracting magnitude with .m (for display/print):
## 08_hla_disp_chrom.py
ksix = disp_chrom_data["chrom"]["x"][-2]
ksiy = disp_chrom_data["chrom"]["y"][-2]
print(f"Measured linear chromaticity = ({ksix.m:+.2f}, {ksiy.m:+.2f})")
# Arithmetic directly on Q_ objects:
## 04_mlvt.py
urad = Q_("1 urad")
cur_SP_val_d["scor"]["x"] += 5 * urad
cur_SP_val_d["QH1_K1"] -= Q_("1e-3 m^{-2}")
# MIMO put with a list of Q_:
## 09_setup_MIMO_magnet.py
mlv_I1_I2_SP.put([Q_("1 A"), Q_("2 A")])
# pint's .to() can be used for explicit unit conversion, but users usually
# don't need to call it directly — PAMILA handles unit conversion internally.
val = mlv.get() # e.g., Q_("0.1 A")
val.to("mA") # Q_("100.0 mA") Summary of decisions
|
Beta Was this translation helpful? Give feedback.
-
|
My conclusion from looking into MML is that it only does hardware to physics conversions, no universal conversations. That is handled by a doc string and if you want to use an unit other than the one that comes in from the backend you need to implement the conversion yourself. Below is an example of the relevant parts in the configuration for our BPMs.
|
Beta Was this translation helpful? Give feedback.
-
|
In Here is an example for a tune device I did as part of testing how it works. I tried to use it for the hardware to physics conversions since our tunes are read from the control system in frequency. At the signal level there is no conversions yet. I think there is a way to read the unit from the control system (but I haven't tested it myself yet). As far as I have understood this works best for TANGO and the TANGO users seem less concerned about it than the EPICS users. There is however at the moment no functionality to verify that the unit from the control system is correct. For example, at many EPICS labs this field might be unmaintained and you shouldn't blindly trust it. There has been a discussion about implementing something on the signal level (including the option to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We've decided during a maintainer's meeting to open a discussion to advance on this topic and give necessary input/requirements for the development. One of the topics is whether we benefit from using pint for unit conversion in pyAML. There are some nice desirable features of pint
One option to integrate Pint could be to have it do conversions only in two places. At the user input and use everywhere SI units (either pint objects or floats/ints). At the moment of converting the SI unit of pyAML to the unit expected by the control system.
We've agreed that as an input for development direction we will provide.
Yoshi's experience and decisions with pint in PAMILA
In PAMILA, I adopted pint for unit handling. Here is a summary of what I did and why.
Two distinct types of "unit conversion" in PAMILA
PAMILA distinguishes two conceptually different types of unit conversion:
How pint is used
pint is used to attach units to all values that users interact with. Specifically:
Features of pint used in PAMILA
A practical issue encountered
Scope: where pint is not used
The device-level
reprconversion functions (e.g., excitation curves for magnets, calibrated BPM data, MIMO ID corrector models) operate purely on plain Python float values, not pint objects. Users still receive properly unit-tagged results.Compatibility with bluesky
pint objects flowing through bluesky plans required some monkey-patching to handle them correctly.
Diagrams
GET:
flowchart LR PV([PV]) -->|"float [PV unit]"| SIG([Signal]) SIG -->|"Q_ [LoLv repr unit]"| STRIP subgraph Device direction TB STRIP["strip unit (→ src_unit)"] CONV["repr conv func"] WRAP["wrap with dst_unit"] STRIP -->|floats| CONV CONV -->|floats| WRAP end WRAP -->|"Q_ [HiLv repr unit]"| USER([User])PUT:
flowchart LR USER([User]) -->|"Q_ [HiLv repr unit]"| STRIP subgraph Device direction TB STRIP["strip unit (→ src_unit)"] CONV["repr conv func"] WRAP["wrap with LoLv repr unit"] STRIP -->|floats| CONV CONV -->|floats| WRAP end WRAP -->|"Q_ [LoLv repr unit]"| SIG([Signal]) SIG -->|"float [PV unit]"| PV([PV])Simple Examples
Summary of decisions
reprconversion functionsreprconversionMatlab Middle Layer handling of unit conversion (to be filled by @TeresiaOlsson and @marlibgin2, @gubaidulinvadim will ask Laurent Nadolski how it is done in SOLEIL MML)
Vadim recalls that in SOLEIL MML, there's some handling of unit conversion, one can choose to have RF frequency in Hz, MHz, kHz, etc. After the choice is made, this unit is displayed everywhere.
...
Ophyd/Bluesky community way of handling unit conversions (to be filled by @TeresiaOlsson)
...
Beta Was this translation helpful? Give feedback.
All reactions