-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from lincc-frameworks/light-curve-package
Support of light-curve package
- Loading branch information
Showing
7 changed files
with
620 additions
and
74 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .base import AnalysisFunction # noqa | ||
from .feature_extractor import FeatureExtractor # noqa | ||
from .light_curve import LightCurve # noqa | ||
from .stetsonj import * # noqa | ||
from .structurefunction2 import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
""" | ||
Auxiliary code for time-series feature extraction with "light-curve" package | ||
""" | ||
|
||
from typing import List | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from light_curve.light_curve_ext import _FeatureEvaluator as BaseLightCurveFeature | ||
|
||
from tape.analysis.base import AnalysisFunction | ||
|
||
|
||
__all__ = ["FeatureExtractor", "BaseLightCurveFeature"] | ||
|
||
|
||
class FeatureExtractor(AnalysisFunction): | ||
"""Apply light-curve package feature extractor to a light curve | ||
Parameters | ||
---------- | ||
feature : light_curve.light_curve_ext._FeatureEvaluator | ||
Feature extractor to apply, see "light-curve" package for more details. | ||
Attributes | ||
---------- | ||
feature : light_curve.light_curve_ext._FeatureEvaluator | ||
Feature extractor to apply, see "light-curve" package for more details. | ||
""" | ||
|
||
def __init__(self, feature: BaseLightCurveFeature): | ||
self.feature = feature | ||
|
||
def cols(self, ens: "Ensemble") -> List[str]: | ||
return [ens._time_col, ens._flux_col, ens._err_col, ens._band_col] | ||
|
||
def meta(self, ens: "Ensemble") -> pd.DataFrame: | ||
"""Return the schema of the analysis function output. | ||
It always returns a pandas.DataFrame with the same columns as | ||
`self.feature.names` and dtype `np.float64`. However, if | ||
input columns are all single precision floats then the output dtype | ||
will be `np.float32`. | ||
""" | ||
return pd.DataFrame(dtype=np.float64, columns=self.feature.names) | ||
|
||
def on(self, ens: "Ensemble") -> List[str]: | ||
return [ens._id_col] | ||
|
||
def __call__(self, time, flux, err, band, *, band_to_calc: str, **kwargs) -> pd.DataFrame: | ||
""" | ||
Apply a feature extractor to a light curve, concatenating the results over | ||
all bands. | ||
Parameters | ||
---------- | ||
time : `numpy.ndarray` | ||
Time values | ||
flux : `numpy.ndarray` | ||
Brightness values, flux or magnitudes | ||
err : `numpy.ndarray` | ||
Errors for "flux" | ||
band : `numpy.ndarray` | ||
Passband names. | ||
band_to_calc : `str` | ||
Name of the passband to calculate features for. | ||
**kwargs : `dict` | ||
Additional keyword arguments to pass to the feature extractor. | ||
Returns | ||
------- | ||
features : pandas.DataFrame | ||
Feature values for each band, dtype is a common type for input arrays. | ||
""" | ||
|
||
# Select passband to calculate | ||
band_mask = band == band_to_calc | ||
time, flux, err = (a[band_mask] for a in (time, flux, err)) | ||
|
||
# Sort inputs by time if not already sorted | ||
if not kwargs.get("sorted", False): | ||
sort_idx = np.argsort(time) | ||
time, flux, err, band = (a[sort_idx] for a in (time, flux, err, band)) | ||
# Now we can update the kwargs for better performance | ||
kwargs = kwargs.copy() | ||
kwargs["sorted"] = True | ||
|
||
# Convert the numerical arrays to a common dtype | ||
dtype = np.find_common_type([a.dtype for a in (time, flux, err)], []) | ||
time, flux, err = (a.astype(dtype) for a in (time, flux, err)) | ||
|
||
values = self.feature(time, flux, err, **kwargs) | ||
|
||
series = pd.Series(dict(zip(self.feature.names, values))) | ||
return series |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Test feature extraction with light_curve package""" | ||
|
||
import light_curve as licu | ||
import numpy as np | ||
from numpy.testing import assert_array_equal, assert_allclose | ||
|
||
from tape import Ensemble | ||
from tape.analysis.feature_extractor import FeatureExtractor | ||
from tape.utils import ColumnMapper | ||
|
||
|
||
def test_stetsonk(): | ||
stetson_k = licu.StetsonK() | ||
|
||
time = np.array([5.0, 4.0, 3.0, 2.0, 1.0, 0.0] * 2) | ||
flux = 1.0 + time**2.0 | ||
err = np.full_like(time, 0.1, dtype=np.float32) | ||
band = np.r_[["g"] * 6, ["r"] * 6] | ||
|
||
extract_features = FeatureExtractor(stetson_k) | ||
result = extract_features(time=time, flux=flux, err=err, band=band, band_to_calc="g") | ||
assert result.shape == (1,) | ||
assert_array_equal(result.index, ["stetson_K"]) | ||
assert_allclose(result.values, 0.84932, rtol=1e-5) | ||
assert_array_equal(result.dtypes, np.float64) | ||
|
||
|
||
def test_stetsonk_with_ensemble(dask_client): | ||
n = 5 | ||
|
||
object1 = { | ||
"id": np.full(n, 1), | ||
"time": np.arange(n, dtype=np.float64), | ||
"flux": np.linspace(1.0, 2.0, n), | ||
"err": np.full(n, 0.1), | ||
"band": np.full(n, "g"), | ||
} | ||
object2 = { | ||
"id": np.full(2 * n, 2), | ||
"time": np.arange(2 * n, dtype=np.float64), | ||
"flux": np.r_[np.linspace(1.0, 2.0, n), np.linspace(1.0, 2.0, n)], | ||
"err": np.full(2 * n, 0.01), | ||
"band": np.r_[np.full(n, "g"), np.full(n, "r")], | ||
} | ||
rows = {column: np.concatenate([object1[column], object2[column]]) for column in object1} | ||
|
||
cmap = ColumnMapper(id_col="id", time_col="time", flux_col="flux", err_col="err", band_col="band") | ||
ens = Ensemble(dask_client).from_source_dict(rows, cmap) | ||
|
||
stetson_k = licu.Extractor(licu.AndersonDarlingNormal(), licu.InterPercentileRange(0.25), licu.StetsonK()) | ||
result = ens.batch( | ||
stetson_k, | ||
band_to_calc="g", | ||
) | ||
|
||
assert result.shape == (2, 3) | ||
assert_array_equal(result.columns, ["anderson_darling_normal", "inter_percentile_range_25", "stetson_K"]) | ||
assert_allclose(result, [[0.114875, 0.625, 0.848528]] * 2, atol=1e-5) |