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
Human readable energy units string formatting for plot_interactive & plot_grid #3752
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
15da3b3
Merge branch 'master' of https://github.com/gammapy/gammapy
facero 33eb7f7
Merge branch 'master' of https://github.com/gammapy/gammapy
facero b8f9c5e
Human readable enrgy units string formatting for plot_interactive & p…
facero c208e1b
updated plot_interactive to handle edge and center energy axis
facero db93315
Update gammapy/utils/units.py
facero 9e36715
Update gammapy/utils/units.py
facero 28185dd
update units.py
facero 8b816b1
updated tests for PeV units
facero e379203
fixed gitignore and yml mistake
facero be61ccf
fixing gitignore
facero 869daf9
Update gammapy/maps/core.py
facero da84053
Update gammapy/utils/tests/test_units.py
facero 16be0a4
Update gammapy/utils/units.py
facero cd2dd1d
Update gammapy/utils/units.py
facero 6093860
Update gammapy/utils/units.py
facero 5bff8d1
Update gammapy/utils/units.py
facero d6cb81e
Update gammapy/utils/units.py
facero de85ac7
Update gammapy/utils/units.py
facero 138d465
Adding pytest parametrize and modifying `energy_unit_format` condition
facero 2bc2a88
updating tests
facero c3c2d44
Update gammapy/utils/units.py
facero 0ec2752
fixing gitignore mistake
facero e0e55e9
Tiny changes after comments
registerrier File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
from gammapy.utils.units import standardise_unit | ||
|
||
import pytest | ||
from gammapy.utils.units import standardise_unit, energy_unit_format | ||
import astropy.units as u | ||
import numpy as np | ||
from gammapy.maps import MapAxis | ||
|
||
def test_standardise_unit(): | ||
assert standardise_unit("ph cm-2 s-1") == "cm-2 s-1" | ||
assert standardise_unit("ct cm-2 s-1") == "cm-2 s-1" | ||
assert standardise_unit("cm-2 s-1") == "cm-2 s-1" | ||
|
||
|
||
axis = MapAxis.from_nodes([1e-1, 200, 3.5e3, 4.6e4], name="energy", unit="GeV") | ||
values = [ | ||
(1530 *u.eV, "1.53 keV"), | ||
(1530 *u.keV, "1.53 MeV"), | ||
(1530e4 *u.keV, "15.3 GeV"), | ||
(1530 *u.GeV, "1.53 TeV"), | ||
(1530.5e8 *u.keV, "153 TeV"), | ||
(1530.5 *u.TeV, "1.53 PeV"), | ||
(np.array([1e3,3.5e6,400.4e12,1512.5e12])*u.eV, ('1.00 keV', '3.50 MeV', '400 TeV','1.51 PeV')), | ||
([1.54e2*u.GeV, 4300*u.keV, 300.6e12*u.eV], ('154 GeV', '4.30 MeV', '301 TeV')), | ||
(axis.center, ('100 MeV', '200 GeV', '3.50 TeV', '46.0 TeV')), | ||
([u.Quantity(x) for x in axis.as_plot_labels ] ,('100 MeV', '200 GeV', '3.50 TeV', '46.0 TeV')) | ||
] | ||
|
||
@pytest.mark.parametrize("q, expect", values) | ||
def test_energy_unit_format(q, expect): | ||
assert energy_unit_format(q) == expect |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
"""Units and Quantity related helper functions""" | ||
import logging | ||
import astropy.units as u | ||
import numpy as np | ||
from math import floor | ||
|
||
__all__ = ["standardise_unit", "unit_from_fits_image_hdu"] | ||
|
||
|
@@ -64,3 +66,34 @@ def unit_from_fits_image_hdu(header): | |
unit = "" | ||
|
||
return standardise_unit(unit) | ||
|
||
def energy_unit_format(E): | ||
""" | ||
Format energy quantities to a string representation that is more comfortable to read | ||
by switching to the most relevant unit (keV, MeV, GeV, TeV) and changing the float precision. | ||
|
||
Parameters | ||
---------- | ||
E: `~astropy.units.Quantity` | ||
Quantity or list of quantities | ||
|
||
Returns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add blank line |
||
------- | ||
str : str | ||
Returns a string or tuple of strings with energy unit formatted | ||
""" | ||
try: | ||
iter(E) | ||
except TypeError: | ||
pass | ||
else: | ||
return tuple(map(energy_unit_format, E)) | ||
|
||
i = floor(np.log10(E.to_value(u.eV)) / 3) # a new unit every 3 decades | ||
unit = (u.eV, u.keV, u.MeV, u.GeV, u.TeV, u.PeV)[i] if i < 5 else u.PeV | ||
|
||
v = E.to_value(unit) | ||
i=floor(np.log10(v)) | ||
prec = (2,1,0)[i] if i < 3 else 0 | ||
|
||
return f"{v:0.{prec}f} {unit}" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add blank line before Parameters