Skip to content
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

Use pint to keep track of different units #133

Open
charlesbmi opened this issue Aug 3, 2023 · 0 comments
Open

Use pint to keep track of different units #133

charlesbmi opened this issue Aug 3, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@charlesbmi
Copy link
Contributor

charlesbmi commented Aug 3, 2023

Describe the new feature or enhancement

It would be helpful (especially for new contributers) to know exactly what units different variables use, and how to convert between the units. For example people seem to cite absorption in dB/cm or with MHz, etc.

Describe your proposed implementation

Use https://github.com/hgrecco/pint to annotate units. This would, e.g., replace utility functions like

class Conversions:
"""A set of unit-of-measurement conversion tools."""
@staticmethod
def convert(from_uom, to_uom, value):
"""Convert a value from one unit-of-measurement to another.
Conversions are requested using specific strings for the starting and desired
unit-of-measurement. The reason for this is that attempts to specify function
names that indicate a single conversion have resulted in overly verbose or
difficult names. Units-of-measurement can be written much more succinctly in a
string than in a function name, which results in much more readable function
usage with strings.
Currently supported conversions:
* W/m² to mW/cm²
* W/m² to W/cm²
If an unsupported conversion is requested, an exception will be raised.
Args:
from_uom: A string containing unit-of-measurement from which to convert.
to_uom: A string containing unit-of-measurement to which to convert.
value: the value to be converted.
Raises:
ValueError: If an unsupported conversion is requested.
Returns:
The converted value.
"""
if from_uom == "W/m²" and to_uom == "mW/cm²":
return value * 0.1
if from_uom == "W/m²" and to_uom == "W/cm²":
return value * 1e-4
raise ValueError(f"Unsupported conversion: from '{from_uom}' to '{to_uom}'")
and
We convert the absorption coefficient from Np/m/Hz to dB/m/Hz
multiplying it by 8.6860000037.
Args:
frequency (float): in Hz
Returns:
float: attenuation in dB/cm/MHz
"""
attenuation = self.a0 * (frequency / 1e6) ** self.b # Np/m
db_attenuation = attenuation * NEPER_TO_DECIBEL # dB/m
return db_attenuation / 100 # convert from dB/m to dB/cm

Describe possible alternatives

https://github.com/python-quantities/python-quantities also looks like a suitable alternative. Seems slightly less active, with less frequent releases and fewer stars.

@charlesbmi charlesbmi added enhancement New feature or request good first issue Good for newcomers labels Aug 3, 2023
@charlesbmi charlesbmi removed the good first issue Good for newcomers label Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant