{"id":24475786,"url":"https://github.com/haykh/oompy","last_synced_at":"2025-03-14T16:13:48.861Z","repository":{"id":64308496,"uuid":"568349511","full_name":"haykh/oompy","owner":"haykh","description":"Python Module for calculations with physical units (supports Gaussian units).","archived":false,"fork":false,"pushed_at":"2024-06-15T15:12:27.000Z","size":692,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-25T06:16:42.104Z","etag":null,"topics":["astrophysics","conversion","physics","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/oompy/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/haykh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-20T08:47:01.000Z","updated_at":"2024-06-15T15:12:07.000Z","dependencies_parsed_at":"2024-06-15T03:23:57.562Z","dependency_job_id":"52567d15-76b2-40f2-887f-a6081fb22110","html_url":"https://github.com/haykh/oompy","commit_stats":{"total_commits":29,"total_committers":2,"mean_commits":14.5,"dds":"0.27586206896551724","last_synced_commit":"a04fddc515e1c249d79622ec3050049dcc36aa24"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haykh%2Foompy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haykh%2Foompy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haykh%2Foompy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haykh%2Foompy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haykh","download_url":"https://codeload.github.com/haykh/oompy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243606962,"owners_count":20318314,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["astrophysics","conversion","physics","python"],"created_at":"2025-01-21T09:27:27.255Z","updated_at":"2025-03-14T16:13:48.839Z","avatar_url":"https://github.com/haykh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OOMpy a/k/a order-of-magnitude python\n\n[![Python package](https://github.com/haykh/oompy/actions/workflows/github-pytest.yml/badge.svg)](https://github.com/haykh/oompy/actions/workflows/github-pytest.yml)\n\nOOMpy 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 (including vague conversions between incompatible units). \n\n## Installation\n\n```sh\npip install oompy\n```\n\n## Usage\n\nImporting the main objects:\n```python\n# import units and constants\nfrom oompy import Units as u\nfrom oompy import Constants as c\n```\n\n### Simple manipulations and unit conversions\n\nSeveral common usage examples:\n```python\n# example #1\nm_m87 = 6.5e9 * u.Msun\nrg_m87 = c.G * m_m87 / c.c**2\nrg_m87 \u003e\u003e 'au'\n#       ^\n#       |\n# basic conversion\n#\n# Output: 64.16104122314108 au\n```\n\n```python\n# example #2\npsr_bfield = 1e12 * u.G    # magnetic field in Gauss\ngold_density = 19.3 * u.g / u.cm**3\n((psr_bfield / c.c)**2).cgs\n#                        ^\n#                        |\n#                 convert to cgs\n#\n# Output: 1112.6500560536185 g cm^-3\n#\n# equivalently:\n((psr_bfield / c.c)**2).cgs \u003e\u003e \"CGS\"\n#\n# Output: 1112.6500560536185 g cm^-3\n#\n(psr_bfield / c.c)**2 / gold_density \u003e\u003e \"\"\n#\n# Output: 57.650261971690085\n```\n\n```python\n# example #3\ngamma_factor = 1000\nb_field = u.MG        # = Mega Gauss \nomega_B = (c.q_e * b_field / (c.m_e * c.c))\nsync_omega = gamma_factor**2 * omega_B\n\nc.hbar * sync_omega \u003e\u003e 'keV'\n#                       ^\n#                       |\n#             understands prefixes for the powers of 10\n#               (works from 1e-24 \"y*\" to 1e24 \"Y*\")\n#\n# Output: 11.576759893742388 keV\n```\n\n```python\n# example #4\n# compare physical quantities in arbitrary units\n(c.R_sun \u003e\u003e 'ly') == c.R_sun # True\nc.M_sun \u003c (c.m_e \u003e\u003e \"lb\") # False\nc.R_sun \u003e= (c.m_e \u003e\u003e \"lb\") # Error: incompatible units\n```\n\n```python\n# example #5\n# formatting\nprint (f\"rest-mass energy of an electron is {c.m_e * c.c**2 \u003e\u003e 'MeV':.2f}\")\n#\n# Output: rest-mass energy of an electron is 0.51 MeV\n```\n\n```python\n# example #6\n# get the reduced physical type of the quantity (i.e., dimension in base units)\n# can be utilized for further parsing, integration to other APIs, etc.\n~(c.hbar * sync_omega)\n#\n# Output: {\u003cType.MASS: 3\u003e: Fraction(1, 1), \u003cType.LENGTH: 1\u003e: Fraction(2, 1), \u003cType.TIME: 2\u003e: Fraction(-2, 1)}\n```\n\nTo see all units and/or constants:\n```python\nu.all\nc.all\n```\n\nCreate your own quantities:\n```python\nfrom oompy import Quantity\n# example #7\nmy_speed = Quantity('25 m sec^-1')\n#                      ^\n#                      |\n#                 as a string\nrabbit_speed = Quantity(55, 'mi hr^-1')\n#                         ^\n#                         |\n#                     as a tuple\nelephant_speed = Quantity('km hr^-1')\n(elephant_speed * my_speed / rabbit_speed) \u003e\u003e 'ly Gyr^-1'\n#                                                  ^\n#                                                  |\n#                                           converts lightyear per Gigayear :)\n#\n# Output: 0.9421232705492877 ly Gyr^-1\n#\n# more concise way:\nrabbit_speed2 = 55 * u.mi / u.hr\n```\n\n### Vague conversions\nThis 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`. \n\n```python\nfrom oompy import Assumptions as assume, Quantity\n\n# uses h\nfreq = 5 * u.GHz\nfreq \u003e\u003e assume.Light \u003e\u003e \"cm\"\n#\n# Output: 5.995849160000001 cm\n\n# uses h-bar as freq has a dimension of radians per second\nfreq = 2 * c.pi * u.rad / u.sec\nfreq \u003e\u003e assume.Light \u003e\u003e \"eV\"\n#\n# Output: 4.1356667496413186e-15 eV\n\n# temperature to/from energy\n10000 * u.K \u003e\u003e assume.Thermal \u003e\u003e \"eV\"\n#\n# Output: 0.8617339407568576 eV\n\n# compute co-moving distance for a redshift\nQuantity(5, \"\") \u003e\u003e assume.Redshift \u003e\u003e \"Gly\"\n#\n# Output: 25.878013331255335 Gly\n#\n# compute redshift for a co-moving distance\n5 * u.Gpc \u003e\u003e assume.Redshift \u003e\u003e \"\"\n#\n# Output: 1.8018944589315433\n```\n\nTo list all the available assumptions:\n```python\nlist(assume)\n```\n\n### Matplotlib and numpy support\n\nOne can combine dimensional quantities into arrays or lists and plot them using matplotlib:\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# call this function to enable matplotlib support\nfrom oompy import matplotlib_support\nmatplotlib_support()\n\nms = np.logspace(0, 2, 2) * u.Msun\nrs = [10 * u.cm, 2.5 * u.ft]\n#            ^           ^\n#       can have different units\n\nplt.plot(rs, ms)\n```\n![pic](demo/mpl.png)\n\n## For developers\n\nTesting 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:\n\n```sh\npip install -r requirements.txt\n```\n\nThen run the tests one-by-one:\n\n```sh\nblack oompy --check --diff\nmypy oompy\npytest\n```\n\nBuild the new version of the package using:\n\n```sh\npython -m build --sdist --outdir dist .\n```\n\nThe same tests are also run automatically on every commit using GitHub Actions.\n\n## To do\n\n- [ ] add more units \u0026 constants\n  - [x] (added in v1.3.5) knots\n  - [x] (added in v1.4.1) Rsun\n  - [x] (added in v1.4.1) fathom\n  - [x] (added in v1.4.1) nautical miles\n  - [x] (added in v2.0.0) Amps, Coulombs, Teslas (for conversion only)\n- [x] (added in v1.1.0) comparison of quantities (`==`, `!=`, `\u003e`, `\u003c`, `\u003e=`, `\u003c=`)\n- [x] (added in v1.1.0) conversion with an rshift (`\u003e\u003e`) operator\n- [x] (added in v1.1.0) base unit extraction (with `~`)\n- [x] (added in v1.2.0) add a possibility to perform vague conversions (e.g. Kelvin to eV, Hz to erg) etc.\n- [x] (added in v1.3.0) unit tests\n- [ ] add support for Ki, Mi, Gi (2e10, 2e20, 2e30)\n- [x] (added in v1.3.5) distance to redshift vague conversion\n- [x] (added in v1.4.0) work with numpy arrays\n  - [x] (added in v1.4.1) additional tests for numpy arrays\n  - [x] (added in v2.0.0) numpy array multiplication works both ways\n- [x] (added in v2.0.0) matplotlib support\n- [x] (added in v2.0.0) add formatting and TeX support\n- [ ] add a way to work with scaling relations\n- [x] (added in v1.4.1) add `__format__` for `Quantity` objects\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaykh%2Foompy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaykh%2Foompy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaykh%2Foompy/lists"}