Skip to content

Conversation

mdhaber
Copy link
Contributor

@mdhaber mdhaber commented Mar 14, 2022

Reference issue

Closes gh-14505

What does this implement/fix?

When rv_continuous.fit fails in main, it raises a plain Exception. This is not as specific as it should be. This PR changes it to a RuntimeError as suggested in gh-14505, but ideas for an even more specific exception type are welcome.

@mdhaber mdhaber added scipy.stats maintenance Items related to regular maintenance tasks labels Mar 14, 2022
@ev-br
Copy link
Member

ev-br commented Mar 14, 2022

Maybe subclass the RuntimeException?

@mdhaber
Copy link
Contributor Author

mdhaber commented Mar 21, 2022

Thanks @tupui @ev-br @chrisb83! Suggestions implemented. I skipped CI since the last change was a simple PEP8 correction that I checked with tools/lint_diff.py locally.

@@ -1702,6 +1702,11 @@ def _get_fixed_fit_value(kwds, names):
', '.join(repeated))
return vals[0][1] if vals else None


class FitError(RuntimeError):
Copy link
Member

@tupui tupui Mar 22, 2022

Choose a reason for hiding this comment

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

If we have custom errors, they should be available in the public API so that user can rely on it in their try, except blocks. I don't think we have a central place for these now, but maybe we could have for each module scipy.xxx.exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good idea, but yes, part of a broader issue. See gh-13462. As noted there, adding FitError to the public API would require approval from the mailing list. Actually, gh-13462 would suggest that instead of FitError itself being public, it should probably be a subclass of a more general public error like SciPyStatsError to minimize the number of separate errors in the API.

Shall we merge this and I'll tackle that issue next?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tupui I started looking into this in mdhaber#74. One thought is having a submodule, like you say, but maybeit would be enough to expose just a few top-level warnings/errors as suggested in gh-13462. Anyway, there are some decisions to be made, which is why I'd suggest leaving this with FitError private in this PR, then we can continue working toward the bigger issue.

Copy link
Member

Choose a reason for hiding this comment

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

Just seeing this issue. Yes it's a good idea to just expose a few errors and do some subclassing. Only drawback of the subclassing is that we need to document what is the base class so users can catch the correct one. Ok to merge now, as you will continue working on this. In the end we just need to ensure we don't advertise for FitError if not part of the API.

Comment on lines +2574 to +2579
Raises
------
TypeError, ValueError
If an input is invalid
FitError
If fitting fails or the fit produced would be invalid
Copy link
Member

Choose a reason for hiding this comment

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

Should we generalise this? IMO yes, it's good practice.

@chrisb83
Copy link
Member

Changes look good. Given that quite a few things can go wrong when estimating the parameters of the distribution, adding FitError is a good idea. There is already one approval, so I will merge this PR tomorrow.

@chrisb83
Copy link
Member

Test failures are unrelated:

scipy/stats/tests/test_distributions.py::TestTruncnorm::test_moments

@mdhaber
Copy link
Contributor Author

mdhaber commented Mar 29, 2022

Yes, most test failures were unrelated but the CircleCI failure doesn't appear to be in main, so I merged main again and am re-running CircleCI to check.

@chrisb83 chrisb83 merged commit e3cd846 into scipy:main Mar 30, 2022
patnr added a commit to patnr/scipy that referenced this pull request Apr 5, 2022
* master: (632 commits)
  Update _dual_annealing.py (scipy#15939)
  TST: stats: make `check_sample_var` two-sided (scipy#15723)
  DOC: sparse.linalg: add citations for COLAMD
  DOC: fix missing comma in conf
  ENH/MAINT: Version switcher from the sphinx theme (scipy#15380)
  MAINT: stats: remove support for `_rvs` without `size` parameter (scipy#15917)
  BUG: Handle base case for scipy.integrate.simpson when span along the axis is 0  (scipy#15824)
  MAINT: stats: adjust tolerance for failing test only
  MAINT: stats: adjust tolerance of failing TestTruncnorm
  MAINT: special: Clean up C style in ndtr.c
  CI: remove pin on Jinja2 (scipy#15895)
  STY: Remove white spaces
  MAINT: stats: exempt gilbrat from refguide_check
  MAINT: stats: rename continuous_gilbrat->continuous_gibrat
  MAINT: stats: correct name Gilbrat -> Gibrat
  [ENH] circvar calculated simply as 1-R (scipy#5747)
  DEP: deal with deprecation of ndim >1 in bspline
  MAINT: stats: more specific error type from `rv_continuous.fit` (scipy#15778)
  DOC: fix import in example in _morestats (scipy#15900)
  [BUG] make p-values consistent with the literature (scipy#15894)
  ...
@mdhaber mdhaber added this to the 1.9.0 milestone Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimization converged to parameters that are outside the range should be a ValueError
4 participants