FittingWithOutlierRemoval says "niter" is optional but crashes if niter is None
See original GitHub issueDescription
The docstring for FittingWithOutlierRemoval says that the parameter “niter” (the maximum number of iterations) is optional, but calling with iter=None causes a crash because the __call__()
method expects it to be an integer.
I see three solutions, in increasing order of (I think) desirability, so I’m not submitting a PR in case whichever solution I code is not approved.
- Change the docstring so niter must be an integer 😃
- Check if
niter is None
in the__init__()
or__call__()
and, if so, set it to a large number instead. Since #10642 stops iterating once convergence has been reached, one can check whether this number of iterations occurred, in which some sort of “did not converge” flag could be added to thefit_info
. - Allow the loop to iterate infinitely, in principle, but repeatedly check that the solution is converging and break out if it doesn’t. I’m not sure this is possible though since
FittingWithOutlierRemoval
is a wrapper for other fitters, whose behaviour may not be well-prescribed.
Expected behavior
I expected it to iterate to convergence.
Actual behavior
It crashed.
Steps to Reproduce
>>> import numpy as np
>>> from astropy import models, fitting
>>> from astropy.stats import sigma_clip
>>> x=np.arange(100)
>>> y = x
>>> m = models.Polynomial1D(degree=1)
>>> f = fitting.FittingWithOutlierRemoval(fitting.LinearLSQFitter(), niter=None, outlier_func=sigma_clip)
>>> m_final = f(m, x, y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/anaconda3/envs/dev/lib/python3.7/site-packages/astropy/modeling/fitting.py", line 870, in __call__
for n in range(1, self.niter + 1):
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
System Details
Python 3.7.7 (default, May 6 2020, 04:59:01) [Clang 4.0.1 (tags/RELEASE_401/final)] Numpy 1.19.1 astropy 4.2.dev735+ga83f25b96 Scipy 1.5.2 Matplotlib 3.3.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
Source code for astropy.modeling.fitting
Basically, given a maximum number of iterations ``niter``, outliers are removed and fitting is performed for each iteration, until no new outliers are...
Read more >python-astropy-4.0.2-bp153.1.10 - SUSE Package Hub
[#10174] Fixed a problem where slicing a CompoundModel by name will crash if there fix_inputs operators are present. [#10224] Removed a limitation of...
Read more >Full Changelog — Astropy v3.2.dev994
Return a consistent type for the second value when niter=0 . ... It will continue to work in future but is no longer...
Read more >Scheduled monthly dependency update for February #185 - Github ...
Values can be set once, but will error if set a second time. ... For models ``IAU1982`` and ``IAU1994``, no such adjustments are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No worries, @chris-simpson ! I wasn’t aware there exists alternative definition of “optional,” so TIL as well.
Fair enough, I’ve been working with a package where “optional” is defined as
None
is an acceptable value, whereas I do see thatastropy
is consistently using “optional” to mean there exists a default value. Sorry for wasting everyone’s time; it’s been a long year!