-
Notifications
You must be signed in to change notification settings - Fork 29
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
Incorporating the Gammapy wrapper in agnpy #119
Conversation
agnpy/wrappers/gammapy.py
Outdated
from gammapy.modeling.models import SpectralModel, Parameter, Parameters | ||
|
||
|
||
class SpectralModelSSC(SpectralModel): |
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.
Maybe the wrapped models can follow the naming convention of gammapy models as :
SynchrotronSelfComptonSpectralModel
, ExternalComptonSpectralModel
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.
Done, renamed following the Gammapy
naming scheme.
agnpy/wrappers/gammapy.py
Outdated
R_b = Parameter("R_b", blob.R_b) | ||
parameters.extend([z, d_L, delta_D, B, R_b]) | ||
|
||
self.default_parameters = Parameters(parameters) |
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.
gammapy models parameters are usually defined at class level and come with default values, but I guess you prefer to avoid this because it's hard to define good defaults here.
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.
Yes, it is a bit difficult to establish defaults, in general. Additionally each emission region comes with its own electron distribution of which I do not know the parameters. I added functions to fetch these parameters from the physical objects and set some reasonable ranges of values.
agnpy/wrappers/gammapy.py
Outdated
mu_s = Parameter("mu_s", blob.mu_s) | ||
# distance of the emission region along the jet | ||
r = Parameter("r", r) | ||
parameters.extend([z, d_L, delta_D, B, R_b, mu_s, r]) |
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.
maybe adding something like :
self.emission_region_parameters
self.targets_parameters
could help to simplify the sorting of the parameters later in evaluate
(and could also be convenient for users that want to look only at one group of parameters)
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.
I grouped the parameters following your suggestion, and so now the user can access spectral_parameters
and emission_region_parameters
separately.
I don't know though how to re-use this grouping in the evaluate
function. I see that the NaimaSpectralModel
in Gammapy
does something similar, i.e. uses in the evaluate
some attribute of the class, but then this _update_naima_parameters
is needed https://github.com/gammapy/gammapy/blob/master/gammapy/modeling/models/spectral.py#L2107.
Let me know if you have some suggestion here.
This should be quite useful @cosimoNigro . One general comment here... Currently, gammapy It is not a blocker, as the user can still multiply |
Sorry my previous comment was not totally correct. As per the latest developments, users need to specify So one option (to simplify things for the users) here maybe be to add a |
Codecov Report
@@ Coverage Diff @@
## master #119 +/- ##
==========================================
+ Coverage 96.99% 97.43% +0.43%
==========================================
Files 30 38 +8
Lines 2233 2997 +764
==========================================
+ Hits 2166 2920 +754
- Misses 67 77 +10
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Thanks for your comments @AtreyeeS, now |
Will add some tests later. |
I think that while the synchrotron and EC flux goes as |
Yes, you are totally right! Thank you! |
@cosimoNigro : In gammapy, we tend to use |
Ok, so I added the from agnpy.emission_regions import Blob
from agnpy.wrappers import SynchrotronSelfComptonSpectralModel
blob = Blob()
ssc_model = SynchrotronSelfComptonSpectralModel(blob)
print(ssc_model.parameters.to_table()) type name value unit error min max frozen is_norm link
-------- --------- ---------- ---- --------- --------- --------- ------ ------- ----
spectral k_e 9.2748e+06 cm-3 0.000e+00 1.000e+00 1.000e+09 False False
spectral p 2.8000e+00 0.000e+00 1.000e+00 5.000e+00 False False
spectral gamma_min 1.0000e+02 0.000e+00 1.000e+00 1.000e+03 True False
spectral gamma_max 1.0000e+07 0.000e+00 1.000e+04 1.000e+08 True False
spectral z 6.9000e-02 0.000e+00 1.000e-03 1.000e+01 True False
spectral d_L 9.9203e+26 cm 0.000e+00 1.368e+25 3.271e+29 True False
spectral delta_D 1.0000e+01 0.000e+00 1.000e+00 1.000e+02 False False
spectral B 1.0000e+00 G 0.000e+00 1.000e-04 1.000e+03 False False
spectral R_b 1.0000e+16 cm 0.000e+00 1.000e+12 1.000e+18 False False
spectral norm 1.0000e+00 0.000e+00 1.000e-01 1.000e+01 True True As @AtreyeeS said, it does not appear in the "grouped" parameters print(ssc_model.spectral_parameters.to_table()) type name value unit error min max frozen is_norm link
-------- --------- ---------- ---- --------- --------- --------- ------ ------- ----
spectral k_e 9.2748e+06 cm-3 0.000e+00 1.000e+00 1.000e+09 False False
spectral p 2.8000e+00 0.000e+00 1.000e+00 5.000e+00 False False
spectral gamma_min 1.0000e+02 0.000e+00 1.000e+00 1.000e+03 True False
spectral gamma_max 1.0000e+07 0.000e+00 1.000e+04 1.000e+08 True False print(ssc_model.spectral_parameters.to_table()) type name value unit error min max frozen is_norm link
-------- ------- ---------- ---- --------- --------- --------- ------ ------- ----
spectral z 6.9000e-02 0.000e+00 1.000e-03 1.000e+01 True False
spectral d_L 9.9203e+26 cm 0.000e+00 1.368e+25 3.271e+29 True False
spectral delta_D 1.0000e+01 0.000e+00 1.000e+00 1.000e+02 False False
spectral B 1.0000e+00 G 0.000e+00 1.000e-04 1.000e+03 False False
spectral R_b 1.0000e+16 cm 0.000e+00 1.000e+12 1.000e+18 False False I integrated the computation of the SED with the |
Tests fail because the dev version of Gammapy is needed. Will fix it. |
Ok, now it's complaining about the astropy version... 😮💨 |
…y version issue in the tests persists
Ok, very well, the tests were failing because |
Will take time to make a final couple of improvements in the EC wrapper and then merge. |
…ectralModel class initialisation
…C integration in the new scheme
…model parameters using agnpy.emission_region and agnpy.targets objects
… with the two wrappers and check their outcome [ci skip]
…ing frequency before)
…ed analytical result for DT, and a numerical integration on a large grid for the disk.
Ok, I am done with this. I will make a new release after I merge. |
I think that some people are using our tutorial notebook for fitting with the Gammapy wrapper.
https://agnpy.readthedocs.io/en/latest/tutorials/ssc_gammapy_fit.html
I don't think it is particularly pretty, especially because of the large cell of code defining the wrapper.
There was some discussion in one of the developer calls of
Gammapy
if we were to include the wrapper inagnpy
, or inGammapy
, as currently done for theNaimaSpectralModel
.I think it is ok to have the wrapper within
agnpy
, we will be introducingGammapy
as an additional dependency but I think it's also fine. Now we will have a centralised wrapper, and hopefully adding a line of code we can solve the issue encountered by @guillaumegrolleron and @jepcor97 when fitting and simulating with the latestGammapy
versions (see the comments in the wrappers).I thought about having a single wrapper per each radiative processes but this implies:
Gammapy.SpectralModel
summing them.So I implemented a wrapper per each scenario / source type, I have written:
Parameters are read from an instance of the emission region and target.
Many things are missing, still I wanted to open the review to start to discuss the design and the organisation of the wrapper.
@jsitarek what do you think? Let me know what you think about the strategy, the naming scheme, the position of the new submodule.
I will also be happy to have the opinions of @QRemy and @AtreyeeS, from the
Gammapy
side - if they have time.Follows a script to cross-check the normal computation against the new wrappers implemented (we can incorporate this in the tests).