question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FittingWithOutlierRemoval says "niter" is optional but crashes if niter is None

See original GitHub issue

Description

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.

  1. Change the docstring so niter must be an integer 😃
  2. 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 the fit_info.
  3. 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:closed
  • Created 3 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
pllimcommented, Dec 11, 2020

No worries, @chris-simpson ! I wasn’t aware there exists alternative definition of “optional,” so TIL as well.

1reaction
chris-simpsoncommented, Dec 11, 2020

Fair enough, I’ve been working with a package where “optional” is defined as None is an acceptable value, whereas I do see that astropy is consistently using “optional” to mean there exists a default value. Sorry for wasting everyone’s time; it’s been a long year!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found