Repository navigation
Unified unit parser and converter for lengths - #328
Open
NJMarchese wants to merge 1 commit into
Open
NJMarchese wants to merge 1 commit into
NJMarchese wants to merge 1 commit into
Conversation
…ngles in core/utils
Collaborator
|
Would it make sense to also modify |
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.
Unified unit parser and converter in
core/utilsDataset axes store their units as free text, and different file formats spell them differently (
"nm","1/nm","Å^-1","A-1","$\mathrm{nm}^{-1}$","mrad", ...). Several modules parse these strings with their own small tables. This PR adds one shared module,quantem/core/utils/units.py, and doesn't change any existing code.What it provides
parse_units(units) -> Unit | Nonereads a unit string intoUnit(base, kind, power), for exampleUnit("Å", "length", -1)for"1/A".1/x,x^-1,x**-1,x-1,x⁻¹, or in LaTeX.Nonefor anything it doesn't recognize, such as"pixels".unit_conversion_factor(from_units, to_units, energy=None) -> floatgives the factor that converts a value or a sampling between units.energy(eV), using k = θ/λ (small-angle approx.).ValueErrorfor an unrecognized unit, for incompatible kinds such as a length and a reciprocal length, or for an angle conversion withoutenergy.LENGTH_UNITS_IN_ANGSTROMandANGLE_UNITS_IN_RADIANSare the underlying tables.Tests
tests/utils/test_units.pyhas 4 tests:electron_wavelength_angstromand as a round trip;