{{ message }}

# Implement SpectralGaussian model class#2244

Merged
merged 6 commits into from Jun 21, 2019
Merged

# Implement SpectralGaussian model class#2244

merged 6 commits into from Jun 21, 2019

## Conversation

### JouvinLea commented Jun 17, 2019

 Implement Gaussian model in the model Class The text was updated successfully, but these errors were encountered:
 val_at_2TeV=u.Quantity(0.3321457437841875, 'cm-2 s-1 TeV-1'), integral_1_10TeV=u.Quantity(1.6371892282407274, 'cm-2 s-1'), integral_infinity=u.Quantity(2, 'cm-2 s-1'), eflux_1_10TeV=u.Quantity(7.676635460998642, 'TeV cm-2 s-1'),

### JouvinLea Jun 17, 2019

Maybe a better test for the energy flux?

 energy_power=0, n_points=100, **kwargs self,

### cdeil Jun 17, 2019

There's a lot of unrelated lines that show up in this diff. Could you please run black gammapy/spectrum/models.py gammapy/spectrum/tests/test_models.py to go back to the black coding style?

### cdeil left a comment

I put a few superficial suggestions.

I didn't check the equations and implementation yet.
Is there a test case we could choose with a "known good answer"?
I can't think of one, but maybe there is, e.g. on Wikipedia or somewhere?

 r"""Integrate Gaussian analytically. .. math:: F(E_{min}, E_{max}) = \int_{E_{min}}^{E_{max}} \phi(E)dE = \frac{N_0}{2} \left( erf(\frac{E_{max}-

### cdeil Jun 17, 2019

Please break the line earlier and make it a bit easier to read.

 u_max = (emax - pars['mean'].quantity) / (np.sqrt(2) * pars['sigma'].quantity) a = pars['norm'].quantity * pars['sigma'].quantity / np.sqrt(2 * np.pi) b = pars['norm'].quantity * pars['mean'].quantity / 2 int = a * (np.exp(- u_min ** 2) - np.exp(-u_max ** 2)) + b * (erf(u_max) - erf(u_min))

### cdeil Jun 17, 2019

int is a keyword, shouldn't be used a local variable name. Just change int =  to return here.

 .. math:: G(E_{min}, E_{max}) = \int_{E_{min}}^{E_{max}}E \phi(E)dE = \frac{N_0 \sigma}{\sqrt{2*\pi}}* \left( \exp(\frac{E_{min}-\bar{E}}{\sqrt{2} \sigma}) - exp(\frac{E_{max} - \bar{E}}{\sqrt{2} \sigma}) \right) \\ + \frac{N_0 * \bar{E}}{2} \left( erf(\frac{E_{max}-\bar{E}}{\sqrt{2} \sigma})

### cdeil Jun 17, 2019

This is very hard to read. Do we need / want the equation, or could we link to Wikipedia or somewhere else instead?
If you keep it, suggest to remove \int_{E_{min}}^{E_{max}}E \phi(E)dE = and to break the other lines more and shorten them to make the LaTeX more readable (for review here, but also if someone looks at the help in ipython or Jupyter they will see it non-rendered)

### JouvinLea Jun 18, 2019

This is very hard to read. Do we need / want the equation, or could we link to Wikipedia or somewhere else instead?
If you keep it, suggest to remove \int_{E_{min}}^{E_{max}}E \phi(E)dE = and to break the other lines more and shorten them to make the LaTeX more readable (for review here, but also if someone looks at the help in ipython or Jupyter they will see it non-rendered)

ok, I clean a bit. I don't know on the doc I found it pretty clear to read no? I did the analytical calculation myself but I guess we could find it on wikipedia but I don't find it ugly as it is.

### cdeil commented Jun 17, 2019

 @JouvinLea - Thanks! There's a conflict in gammapy/spectrum/tests/test_models.py - please resolve it by rebasing against upstream master so that this will run in CI. I also suggest to squash the commits into one or two with a good commit message. And to merge this in as soon as it's clear the equations and implementation is good. @registerrier - Could you please review this PR and merge it in when ready? x-ref: #1411 (unsubscribing now)

 implementation Gaussian spectral model 
 36334b2 

### JouvinLea commented Jun 18, 2019

 I put a few superficial suggestions. I didn't check the equations and implementation yet. Is there a test case we could choose with a "known good answer"? I can't think of one, but maybe there is, e.g. on Wikipedia or somewhere?

gammapy.makers automation moved this from To do to Done Jun 18, 2019

### JouvinLea commented Jun 18, 2019

 Hi, Sorry for the merge conflict, I started on the morning I didn't think I could have create merge conflict in one day. Now it is fixed and I squashed also the commits!

### JouvinLea commented Jun 18, 2019

 I put a few superficial suggestions. I didn't check the equations and implementation yet. Is there a test case we could choose with a "known good answer"? I can't think of one, but maybe there is, e.g. on Wikipedia or somewhere? I don't know, I recalculate evrything my self. I think for the integral it is ok since the result is quite obvious and in the test I add a check that the integral between -inf and inf is q=equal to the norm No. For the energy flux, it is true that the test is not a super good test since I didn't what to test to know If analytical integration was correct.

gammapy.makers automation moved this from Done to In progress Jun 18, 2019

### registerrier left a comment

Thanks @JouvinLea . Looks good to me.
Some comments inline. Also run black formatter as @cdeil suggested.

Show resolved Hide resolved
Show resolved Hide resolved
Show resolved Hide resolved
gammapy/spectrum/models.py Outdated Show resolved Hide resolved
gammapy/spectrum/models.py Outdated Show resolved Hide resolved
 run black + improve docs + gaussian center on 2 TeV 
 4652c7f 
 ange name+ thinner gaussian 
 0388297 

### JouvinLea commented Jun 21, 2019

 @registerrier The test are cheked with http://m.wolframalpha.com equation are ok!

### adonath commented Jun 21, 2019

 from scipy.special import erf pars = self.parameters u_min = (emin - pars["mean"].quantity) / (np.sqrt(2) * pars["sigma"].quantity)

### registerrier Jun 21, 2019

you should add a .to_value('') for u_min and u_max.
Apparently astropy does not always handle the erf correctly for units.

### registerrier left a comment

Thanks @JouvinLea !
The fail is CI is in fact due to the handling of quantities with erf. So if you can apply a .to_value('') for your u_min and u_max argument, this should work fine.
Then we can merge once CI passes.

 add .to_value 
 41eea24 
 Update models.py 
 aafbd5b 
Changed to have u_min and u_max as floats
 Update models.py 
 3094b75 
With correct parenthesis...

### registerrier commented Jun 21, 2019

 Thanks @JouvinLea .