Tools for working with physical units and quantities
Project description
OOMpy a/k/a order-of-magnitude python
OOMpy is a python package for working with physical units and quantities. Unlike astropy
it works in gaussian units, supports a multitude of physical dimensions, constants, and conversion between them.
Installation
pip install oompy
Usage
Importing the main objects:
# import units and constants
from oompy import Units as u
from oompy import Constants as c
Simple manipulations and unit conversions
Several common usage examples:
# example #1
m_m87 = 6.5e9 * u.Msun
rg_m87 = c.G * m_m87 / c.c**2
rg_m87 >> 'au'
# ^
# |
# basic conversion
#
# Output: 64.16104122314108 au
# example #2
psr_bfield = 1e12 * u.G # magnetic field in Gauss
gold_density = 19.3 * u.g / u.cm**3
((psr_bfield / c.c)**2).cgs
# ^
# |
# convert to cgs
#
# Output: 1112.6500560536185 g cm^-3
#
(psr_bfield / c.c)**2 / gold_density >> ""
#
# Output: 57.650261971690085
# example #3
gamma_factor = 1000
b_field = u.MG # = Mega Gauss
omega_B = (c.q_e * b_field / (c.m_e * c.c))
sync_omega = gamma_factor**2 * omega_B
c.hbar * sync_omega >> 'keV'
# ^
# |
# understands powers of 10 prefixes
# (from 1e-12 to 1e18)
#
# Output: 11.576759893742388 keV
# example #4
# get the reduced physical type of the quantity (i.e., dimension in base units)
~(c.hbar * sync_omega)
#
# Output: {<Type.MASS: 3>: Fraction(1, 1), <Type.LENGTH: 1>: Fraction(2, 1), <Type.TIME: 2>: Fraction(-2, 1)}
# example #5
# compare physical quantities in arbitrary units
(c.R_sun >> 'ly') == c.R_sun # True
c.M_sun < (c.m_e >> "lb") # False
c.R_sun >= (c.m_e >> "lb") # Error: incompatible units
To see all units and/or constants:
u.all
c.all
Create your own quantities:
from oompy import Quantity
# example #4
my_speed = Quantity('25 m sec^-1')
# ^
# |
# as a string
rabbit_speed = Quantity(55, 'mi hr^-1')
# ^
# |
# as a tuple
elephant_speed = Quantity('km hr^-1')
(elephant_speed * my_speed / rabbit_speed) >> 'ly Gyr^-1'
# ^
# |
# converts lightyear per Gigayear :)
#
# Output: 0.9421232705492877 ly Gyr^-1
Vague conversions
This technique enables a comparison between incompatible units under certain assumptions. For instance, one might assume that we consider a photon, and thus its energy, wavelength and frequency are connected via c
and h
.
from oompy import Assumptions as assume, Quantity
# uses h
freq = 5 * u.GHz
freq >> assume.Light >> "cm"
#
# Output: 5.995849160000001 cm
# uses h-bar as freq has a dimension of radians per second
freq = 2 * c.pi * u.rad / u.sec
freq >> assume.Light >> "eV"
#
# Output: 4.1356667496413186e-15 eV
# temperature to/from energy
10000 * u.K >> assume.Thermal >> "eV"
#
# Output: 0.8617339407568576 eV
# compute co-moving distance for a redshift
Quantity(5, "") >> assume.Redshift >> "Gly"
#
# Output: 25.878013331255335 Gly
#
# compute redshift for a co-moving distance
5 * u.Gpc >> assume.Redshift >> ""
#
# Output: 1.8018944589315433
To list all the available assumptions:
list(assume)
For developers
Testing the code is done in three steps using black
to check the formatting, mypy
to check the types and typehints, and pytest
to run the tests. First install all the dependencies:
pip install black mypy pytest
Then run the tests one-by-one:
black oom --check --diff
mypy oom
pytest
These tests are also run automatically on every commit using GitHub Actions.
To do
- add more units & constants
- (added in v1.3.5) knots
- fathom
- nautical miles
- (added in v1.1.0) comparison of quantities (
==
,!=
,>
,<
,>=
,<=
) - (added in v1.1.0) conversion with an rshift (
>>
) operator - (added in v1.1.0) base unit extraction (with
~
) - (added in v1.2.0) add a possibility to perform vague conversions (e.g. Kelvin to eV, Hz to erg) etc.
- (added in v1.3.0) unit tests
- add support for Ki, Mi, Gi (2e10, 2e20, 2e30)
- (added in v1.3.5) distance to redshift vague conversion
- (TBA in v1.4.0) work with numpy arrays
- (TBA in v1.4.0) add formatting and TeX support
- add a way to work with scaling relations
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file oompy-1.3.5.tar.gz
.
File metadata
- Download URL: oompy-1.3.5.tar.gz
- Upload date:
- Size: 41.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53861bd247e14c36893aa10701629018aafe6c1cf2c9db9d4130d0659504b25b |
|
MD5 | 7ae836de005d7f645c9b01b9dc6189e1 |
|
BLAKE2b-256 | 298251f57d0d40ee6ce436d8ce542d0a469115b35d1dd41fd9b94bb419812175 |