Skip to content

Commit

Permalink
fix outlier removal bug (lightkurve#1313)
Browse files Browse the repository at this point in the history
* fix outlier removal bug

* update test

* add nan to test

* update comment

---------

Co-authored-by: Christina Hedges <14965634+christinahedges@users.noreply.github.com>
  • Loading branch information
danhey and christinahedges committed Aug 22, 2023
1 parent 8962d85 commit edcdd65
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/lightkurve/lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,11 @@ def remove_outliers(
# a local import here.
from astropy.stats.sigma_clipping import sigma_clip

# astropy.stats.sigma_clip won't work with masked ndarrays so we convert to regular arrays
flux = self.flux.copy()
if isinstance(flux, Masked):
flux = flux.filled(np.nan)

# First, we create the outlier mask using AstroPy's sigma_clip function
with warnings.catch_warnings(): # Ignore warnings due to NaNs or Infs
warnings.simplefilter("ignore")
Expand Down
5 changes: 4 additions & 1 deletion tests/test_lightcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,6 @@ def test_remove_nans():
lc_clean = lc.remove_nans("flux_err")
assert_array_equal(lc_clean.flux, [])


def test_remove_outliers():
# Does `remove_outliers()` remove outliers?
lc = LightCurve(time=[1, 2, 3, 4], flux=[1, 1, 1000, 1])
Expand All @@ -1025,6 +1024,10 @@ def test_remove_outliers():
lc_clean = lc.remove_outliers(sigma_lower=float("inf"), sigma_upper=1)
assert_array_equal(lc_clean.time.value, [1, 3, 4, 5])
assert_array_equal(lc_clean.flux, [1, 1, -1000, 1])
# Ensure that we can sigma clip masked arrays
lc = LightCurve(time=[1, 2, 3, 4, 5], flux=Masked([1, 1, 1000, 1, np.nan]))
lc_clean = lc.remove_outliers(sigma=1)
assert_array_equal(lc_clean.time.value, [1, 2, 4])


@pytest.mark.remote_data
Expand Down

0 comments on commit edcdd65

Please sign in to comment.