Skip to content

Allow LogParabola to be defined using natural log#295

Open
HealthyPear wants to merge 3 commits into
mainfrom
feature-log_parabola_from_natural_log
Open

Allow LogParabola to be defined using natural log#295
HealthyPear wants to merge 3 commits into
mainfrom
feature-log_parabola_from_natural_log

Conversation

@HealthyPear
Copy link
Copy Markdown
Member

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

@HealthyPear HealthyPear added enhancement New feature or request spectral labels Jan 20, 2025
@HealthyPear HealthyPear requested a review from maxnoe as a code owner January 20, 2025 14:41
HealthyPear added a commit that referenced this pull request Jan 20, 2025
HealthyPear added a commit that referenced this pull request Jan 20, 2025
@HealthyPear HealthyPear force-pushed the feature-log_parabola_from_natural_log branch from e146a41 to 34b9085 Compare January 20, 2025 15:10
@HealthyPear HealthyPear requested review from maxnoe and removed request for maxnoe January 20, 2025 15:13
@HealthyPear HealthyPear force-pushed the feature-log_parabola_from_natural_log branch from 34b9085 to bffea3d Compare January 20, 2025 15:15
Copy link
Copy Markdown
Member

@morcuended morcuended left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Comment thread pyirf/spectral.py
:math:`E_\text{ref}`
from_log10: bool
If True, compute the energy ration in the exponent factor
using base 10, else use natural logarithm.
Copy link
Copy Markdown
Member

@morcuended morcuended Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe here say that the default is base 10 log

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the idea was to keep a nomenclature analog to gammapy which is what people tends to use more lately

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it doesn't make sense to use the name of a class method for the name of a keyword

Copy link
Copy Markdown
Member

@maxnoe maxnoe Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread pyirf/spectral.py
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)
Copy link
Copy Markdown
Member

@kosack kosack May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

@maxnoe maxnoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See Inline comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request spectral

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants