Fix decibel (dB) parsing of logarithmic units (#62)#75
Merged
Conversation
UCUM "dB" was parsed as deci+byte (decibyte): the prefix branch resolved atoms via pint before applying the UCUM mapping, and bare "B" is pint's byte. Logarithmic units also cannot be linearly prefixed in pint (decibel is a distinct unit from bel), so map the supported decibel forms (dB, dB[W], dB[V], ...) to pint's decibel-family units explicitly. Also correct the bel reference units, which used logfactor 10 (decibel) instead of the UCUM lg/2lg factors (1 for power, 2 for field levels), so bare B[W] is now a true bel-watt. Fixes #62
Bump pre-commit-hooks (v5->v6), ruff (v0.11->v0.15) and codespell (v2.4.1->v2.4.2). Ignore uv.lock as it is not tracked.
Owner
Author
|
Related: #76 (Clearer error message for invalid UCUM units). |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue #62 had more layers than expected from the report.
dB→ decibyte /dB[W]→ decibyte·watt: InUcumToPintTransformer.simple_unit, the prefix branch resolved the bare atom through pint before applyingMAPPINGS_UCUM_TO_PINT. Pint reads bareBas byte, sod+byte= decibyte. TheB→belmapping lived only in an unreachable fallback. And even with the mapping,dbelcan't work — pint forbids linearly prefixing a logarithmic unit, because decibel is a distinct unit from bel, not a prefixed bel.dB[mW]parse error: Genuinely correct — UCUM defines onlyB[W]andB[kW]for power; there is no milliwatt bel.dBmhas no UCUM representation. (Locked in by a test; the cryptic message is a separate grammar concern.)bel_*defs usedlogfactor: 10, making bareB[W]return decibel values (1.259 W instead of the true bel-watt 10 W).2 lg(field/amplitude levels:B[V],B[mV],B[uV],B[10.nV],B[SPL]) vslg(power levels:B,B[W],B[kW,]). So field bels need logfactor 2 and their decibels logfactor 20; power bels logfactor 1 and decibels 10.Changed files:
pint_ucum_defs.txt- correctedbel_*logfactors (1 for power, 2 for field) so bareB[...]are true bels; addeddecibel_spl/volt/millivolt/microvolt/10nanovolt/kilowatt(logfactor 20/10).ucum_pint.py- addedMAPPINGS_UCUM_PREFIXED_TO_PINT(e.g.dB→decibel,dB[W]→decibelwatt) consulted in the prefix branch of both transformers.test_ucum_pint.py- new tests covering decibel atoms, true-bel values, str-transformer consistency, and the invaliddB[mW].Fixes #62