Repository navigation
ENH: add support for fit_mean=True to lombscargle_scipy - #17600
neutrinoceros wants to merge 9 commits into
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
| from astropy.utils import minversion | ||
| from astropy.utils.compat.optional_deps import HAS_SCIPY | ||
|
|
||
| SCIPY_LT_1_15 = not minversion("scipy", "1.15.0") if HAS_SCIPY else True |
There was a problem hiding this comment.
Instead of redefining it, can we import from the one in main.py?
44af5e8 to
047ecce
Compare
fit_mean=True to lombscargle_scipy
|
I'm seeing 2 issues with my current approach: I seems that test failures reveal that this implementation is more complicated than just passing an extra argument. I have close to no experience of the underlying equations and algorithms (I wasn't expecting that I would need to), so I can't really estimate how long it'd take me to get familiar enough to solve the problem myself. Maybe @jakevdp could provide some insight there. Other implementations (
|
|
Thanks for opening this – I'm not sure what the numerical differences are between scipy's
|
You're right. However, the public API |
|
I think the mismatched output is probably due to these normalization terms no longer being correct in the case of They're computed in terms of chi-square values, and adding the floating mean changes the model being fit, and thus changes the chi square value. In each of those expressions, I think you probably you need |
|
Thanks a lot, that did the trick ! |
|
This should be everything. Admittedly the situation is worse than I thought, but in hindsight, I'm even more convinced that we need to evolve the API for consistency.
My proposed deprecation path leads to resolving this conflict in the somewhat distant future when we drop support for scipy<1.14, but isn't perfect, because default values are also used internally in |
cceedb0 to
2e63b95
Compare
2e63b95 to
705a20e
Compare
| frequency. This can lead to more accurate results, especially in the | ||
| case of incomplete phase coverage. | ||
| If unspecified, power and autopower methods will use fit_mean=True in | ||
| most situations, except for method='scipy' where it is set to False. |
There was a problem hiding this comment.
How about "where it it set to False and raises a deprecation warning"
| center_data : bool, optional | ||
| if True, pre-center the data by subtracting the weighted mean | ||
| of the input data. | ||
| fit_mean : bool (default: False) |
There was a problem hiding this comment.
I think this should default to True like in other implementations. Since this is a non-public API, there is no problem with backward compatibility; we just need to make sure that the public APIs call this function in a backward-compatible way.
There was a problem hiding this comment.
Good call, although it makes me wonder what's the point for default values in these private APIs: if they're used correctly, we should actually never get to use defaults, I think ?
| if center_data: | ||
| y = y - y.mean() | ||
|
|
||
| if SCIPY_LT_1_15: |
There was a problem hiding this comment.
I think the fit_mean=True version incompatibility should be checked here:
if SCIPY_LT_1_15:
if fit_mean:
raise ValueError(...)
kwargs = {}
else:
kwargs = {"floating_mean": fit_mean}There was a problem hiding this comment.
I usually try to keep raise statements for invalid user inputs as close to public API calls as possible to minimize confusion. I'm worried that seeing an internal function raise an exception could be interpreted as a bug. If you're unconvinced I'll go with your suggestion.
There was a problem hiding this comment.
To elaborate: lombscargle_scipy should raise a ValueError if the inputs are invalid rather than silently choosing different parameters, if only for the sake of helping internal refactors to work correctly.
If we don't want this ValueError to surface directly to the user, then we should also raise it higher in the stack.
There was a problem hiding this comment.
If we go with both, we'll need a test for each, which seems excessive to me: warnings from the private function should be effectively unreachable
There was a problem hiding this comment.
ping @jakevdp
this looks like the one unresolved point in your review
| return p | ||
|
|
||
| if kwargs.get("floating_mean", False): | ||
| yoffset = y.mean() |
There was a problem hiding this comment.
This is unnecessary if center_data = True.
Rather than parameterizing via yoffset, I think we should parameterize by chi2_ref, and take into account center_data for efficiency. Maybe something like this?
if center_data or not fit_mean:
chi2_ref = 0.5 * t.size * np.mean(y ** 2)
else:
chi2_ref = 0.5 * t.size * np.mean((y - y.mean()) ** 2)| assume_regular_frequency=False, | ||
| normalization="standard", | ||
| fit_mean=True, | ||
| fit_mean=None, |
There was a problem hiding this comment.
Can we leave this at fit_mean=True, and just raise a DeprecationWarning in cases where it is currently dropped? It seems strange to change the default if we're not changing behavior.
There was a problem hiding this comment.
yes, this is redundant now anyway. Thanks for pointing it out !
| ) | ||
| # Note that this condition may be simplified (remove `and not fit_mean`) | ||
| # once fit_mean=True is made the default value for method='scipy' | ||
| prefer_scipy = "scipy" in methods and dy is None and not fit_mean |
There was a problem hiding this comment.
I think we should be able to do this now: e.g.
prefer_scipy = "scipy" in methods and dy is None and not (SCIPY_LT_1_15 and fit_mean)So long as we handle fit_mean appropriately at the place where this is called.
There was a problem hiding this comment.
good call. This makes the comment redundant and the simplification more obvious when the time comes !
|
@jakevdp thanks for your review. I've taken your suggestions into account, with one exception (see #17600 (comment)) let me know what you think, I'm happy to go with your suggestion if my argument doesn't win you over. |
|
I am just not remembering that #17170 existed... so, this is technically an alternative to it, I'll link both PRs are mutually exclusive. |
|
Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then. In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary. If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock. If you believe I commented on this pull request incorrectly, please report this here. |
|
Closing in favor of #20412 which cherry picks one of the commits from here related to normalization. |
…g-mean model, where the reference chi-squared is the variance about the mean rather than about zero (extracted from astropy#17600)
…g-mean model, where the reference chi-squared is the variance about the mean rather than about zero (extracted from astropy#17600)
Description
Scipy 1.15.0 was just released.
Fixes #17166
close #17170
opening as a draft for now because I see tests are failing at the time being but I'm running out of time for today so I'd rather have this in the open in case I forgot something obvious and someone else can point it out.