Allow LogParabola to be defined using natural log#295
Conversation
e146a41 to
34b9085
Compare
34b9085 to
bffea3d
Compare
| :math:`E_\text{ref}` | ||
| from_log10: bool | ||
| If True, compute the energy ration in the exponent factor | ||
| using base 10, else use natural logarithm. |
There was a problem hiding this comment.
Maybe here say that the default is base 10 log
There was a problem hiding this comment.
I don't particularly like the keyword name here, why not call this "log_base" or just pass the log function?
E.g. log=np.log10 vs. log=np.log.
There was a problem hiding this comment.
Ah, I think you took the name from https://docs.gammapy.org/0.19/api/gammapy.modeling.models.LogParabolaSpectralModel.html#gammapy.modeling.models.LogParabolaSpectralModel.from_log10
But this is not a keyword, it's an alternative constructor, that's why it starts with from_.
There was a problem hiding this comment.
yes, the idea was to keep a nomenclature analog to gammapy which is what people tends to use more lately
There was a problem hiding this comment.
But it doesn't make sense to use the name of a class method for the name of a keyword
There was a problem hiding this comment.
You could implement the same (or rather the inverse) logic as gammapy, e.g. offer a from_ln method that converts the parameters to base 10.
Or you change the name to log, maybe with an enum like this:
class LogType(Enum):
LOG10 = auto()
LN = auto()
class LogParabola:
def __init__(self, ..., log=LogType.LOG10)
| def __call__(self, energy): | ||
| e = (energy / self.e_ref).to_value(u.one) | ||
| return self.normalization * e ** (self.a + self.b * np.log10(e)) | ||
| log_factor = np.log10(e) if self.from_log10 else np.log(e) |
There was a problem hiding this comment.
I think you can make this much simpler, with no if-statements, by just using the fact that:
np.log10(x) == np.log(x)/np.log(10)So you can just define a variable like "log_base=10" and have the computation always use np.log(x)/np.log(log_base), and you can just compute the denominator once if you worry about speed. Then the user can choose any base, like np.e. No need for branches, just a single option
In some experiments or software the definition of a LogParabola spectral model might brequire to use the natural logarithm instead of the one in base 10.
This PR allows to instantiate such a model with the addition of a new class attribute analog to the one used by gammapy.modeling.models.LogParabolaSpectralModel