-
Notifications
You must be signed in to change notification settings - Fork 35
Pass SpectralAxis to physical models rather than energy edges #223
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
Changes from all commits
36223af
c8b9b95
c96bae5
84b07e8
d9f16b5
062c0ad
403b2ac
46e3b93
47eef10
9516b9d
0e9103f
b3be7e9
ff4c977
12c39a0
0e49d45
56c71c3
dde9e35
a0f2db5
8d11859
1b53ad6
8498325
8d4c1b1
54407a3
56b71aa
1349730
2e17bd5
b4c9782
3945f91
e32ab3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Modifies :class:`sunkit_spex.models.scaling.InverseSquareFluxScaling`, :class:`sunkit_spex.models.scaling.Constant`, :class:`sunkit_spex.models.models.GaussianModel`, :class:`sunkit_spex.models.models.StraightLineModel`, :class:`sunkit_spex.models.physical.thermal.ThermalEmission`, :class:`sunkit_spex.models.physical.thermal.LineEmission`, :class:`sunkit_spex.models.physical.thermal.ContinuumEmission`, :class:`sunkit_spex.models.physical.nonthermal.ThickTarget`, :class:`sunkit_spex.models.physical.nonthermal.ThinTarget` and :class:`sunkit_spex.models.physical.albedo.Albedo` to take :class:`sunkit_spex.spectrum.spectrum.SpectralAxis` as input and fixes tests and examples accordingly. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import warnings | ||
| from functools import lru_cache | ||
|
|
||
| import numpy as np | ||
|
|
@@ -11,6 +12,8 @@ | |
|
|
||
| from sunpy.data import cache | ||
|
|
||
| from sunkit_spex.spectrum.spectrum import SpectralAxis | ||
|
|
||
| __all__ = ["Albedo", "get_albedo_matrix"] | ||
|
|
||
|
|
||
|
|
@@ -45,11 +48,11 @@ class Albedo(FittableModel): | |
| from astropy.visualization import quantity_support | ||
|
|
||
| from sunkit_spex.models.physical.albedo import Albedo | ||
| from sunkit_spex.spectrum.spectrum import SpectralAxis | ||
|
|
||
| e_edges = np.linspace(5, 550, 600) * u.keV | ||
| e_centers = e_edges[0:-1] + (0.5 * np.diff(e_edges)) | ||
| e_centers = SpectralAxis(np.linspace(5, 550, 600) * u.keV, bin_specification='edges') | ||
| source = PowerLaw1D(amplitude=1*u.ph/(u.cm*u.s), x_0=5*u.keV, alpha=3) | ||
| albedo = Albedo(energy_edges=e_edges) | ||
| albedo = Albedo(spectral_axis=e_centers) | ||
| observed = source | albedo | ||
|
|
||
| with quantity_support(): | ||
|
|
@@ -89,15 +92,17 @@ class Albedo(FittableModel): | |
| _input_units_allow_dimensionless = True | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| self.energy_edges = kwargs.pop("energy_edges") | ||
| self.spectral_axis = kwargs.pop("spectral_axis") | ||
|
|
||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def evaluate(self, spectrum, theta, anisotropy): | ||
| if not isinstance(theta, Quantity): | ||
| theta = theta * u.deg | ||
|
|
||
| albedo_matrix = get_albedo_matrix(self.energy_edges, theta, anisotropy) | ||
| energy_edges = _check_input_type(self.spectral_axis) | ||
|
|
||
| albedo_matrix = get_albedo_matrix(energy_edges, theta, anisotropy) | ||
|
|
||
| return spectrum + spectrum @ albedo_matrix | ||
|
|
||
|
|
@@ -230,3 +235,17 @@ def get_albedo_matrix(energy_edges: Quantity[u.keV], theta: Quantity[u.deg], ani | |
| anisotropy = np.array(anisotropy).squeeze() | ||
|
|
||
| return _calculate_albedo_matrix(tuple(energy_edges.to_value(u.keV)), theta.to_value(u.deg), anisotropy.item()) | ||
|
|
||
|
|
||
| def _check_input_type(spectral_axis): | ||
| if isinstance(spectral_axis, SpectralAxis): | ||
| energy_edges = spectral_axis.bin_edges | ||
| else: | ||
| warnings.warn( | ||
| "As a SpectralAxis object was not passed, bin edges will be calculated as averages from the centers given.", | ||
| UserWarning, | ||
| ) | ||
| spectral_axis = SpectralAxis(spectral_axis, bin_specification="centers") | ||
| energy_edges = spectral_axis.bin_edges | ||
|
|
||
| return energy_edges | ||
|
Comment on lines
+240
to
+251
Contributor
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. should throw an error if spectral axis not passed, not a warning. that's the API change right? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,8 +116,8 @@ def __init__( | |
| **kwargs, | ||
| ) | ||
|
|
||
| def evaluate(self, energy_edges, p, break_energy, q, low_e_cutoff, high_e_cutoff, total_eflux): | ||
| energy_centers = energy_edges[:-1] + 0.5 * np.diff(energy_edges) | ||
| def evaluate(self, spectral_axis, p, break_energy, q, low_e_cutoff, high_e_cutoff, total_eflux): | ||
| energy_centers = spectral_axis | ||
|
Contributor
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. variable name...i'm gonna stop these comments ;P |
||
|
|
||
| if ( | ||
| hasattr(break_energy, "unit") | ||
|
|
@@ -253,8 +253,8 @@ def __init__( | |
| **kwargs, | ||
| ) | ||
|
|
||
| def evaluate(self, energy_edges, p, break_energy, q, low_e_cutoff, high_e_cutoff, total_eflux): | ||
| energy_centers = energy_edges[:-1] + 0.5 * np.diff(energy_edges) | ||
| def evaluate(self, spectral_axis, p, break_energy, q, low_e_cutoff, high_e_cutoff, total_eflux): | ||
| energy_centers = spectral_axis | ||
|
|
||
| if ( | ||
| hasattr(break_energy, "unit") | ||
|
|
@@ -1238,3 +1238,17 @@ def bremsstrahlung_thick_target(photon_energies, p, break_energy, q, low_e_cutof | |
| return (fcoeff / decoeff) * flux | ||
|
|
||
| raise Warning("The photon energies are higher than the highest electron energy or not greater than zero") | ||
|
|
||
|
|
||
| # def _check_input_type(spectral_axis): | ||
| # if isinstance(spectral_axis, SpectralAxis): | ||
| # energy_edges = spectral_axis.bin_edges | ||
| # else: | ||
| # warnings.warn( | ||
| # "As a SpectralAxis object was not passed, bin edges will be calculated as averages from the centers given.", | ||
| # UserWarning, | ||
| # ) | ||
| # spectral_axis = SpectralAxis(spectral_axis, bin_specification="centers") | ||
| # energy_edges = spectral_axis.bin_edges | ||
|
|
||
| # return energy_edges | ||
|
Comment on lines
+1243
to
+1254
Contributor
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. repeated code from before, so delete |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| from astropy.units import UnitsError | ||
|
|
||
| from sunkit_spex.models.physical.albedo import Albedo, get_albedo_matrix | ||
| from sunkit_spex.spectrum.spectrum import SpectralAxis | ||
|
|
||
|
|
||
| def test_get_albedo_matrix(): | ||
|
|
@@ -34,9 +35,9 @@ def test_get_albedo_matrix_bad_angle(): | |
|
|
||
| def test_albedo_model(): | ||
| e_edges = np.linspace(10, 300, 10) * u.keV | ||
| e_centers = e_edges[0:-1] + (0.5 * np.diff(e_edges)) | ||
| e_centers = SpectralAxis(e_edges, bin_specification="edges") | ||
|
Contributor
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. Variable name should change (here and elsewhere) to reflect the updated type |
||
| source = PowerLaw1D(amplitude=100 * u.ph, x_0=10 * u.keV, alpha=4) | ||
| observed = source | Albedo(energy_edges=e_edges) | ||
| observed = source | Albedo(spectral_axis=e_centers) | ||
| observed(e_centers) | ||
|
|
||
|
|
||
|
|
@@ -76,10 +77,11 @@ def test_albedo_idl(): | |
| 0.0013136881009379996, | ||
| ] | ||
|
|
||
| e_ph = np.arange(11) * 2 + 10 | ||
| albedo = Albedo(energy_edges=e_ph * u.keV, theta=45 * u.deg) | ||
| e_c = e_ph[:-1] + 0.5 * np.diff(e_ph) | ||
| spec_in = e_c**-2 | ||
| e_ph = (np.arange(11) * 2 + 10) * u.keV | ||
| e_c = SpectralAxis(e_ph, bin_specification="edges") | ||
|
Contributor
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. Change variable name |
||
| albedo = Albedo(spectral_axis=e_c, theta=45 * u.deg) | ||
|
|
||
| spec_in = e_c.value**-2 | ||
| spec_out = albedo(spec_in[:]) | ||
| assert_allclose(idl_spec_in, spec_in) | ||
| assert_allclose(idl_spec_out, spec_out) | ||
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.
change variable name