stdpipe 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stdpipe/__init__.py +9 -0
- stdpipe/artefacts.py +370 -0
- stdpipe/astrometry.py +2001 -0
- stdpipe/astrometry_quad.py +1643 -0
- stdpipe/astrometry_wcs.py +1141 -0
- stdpipe/catalogs.py +942 -0
- stdpipe/cutouts.py +570 -0
- stdpipe/data/legacysurvey_bricks.fits.gz +0 -0
- stdpipe/data/ps1skycells.txt +199708 -0
- stdpipe/db.py +130 -0
- stdpipe/fringe_removal.py +1086 -0
- stdpipe/lcs.py +440 -0
- stdpipe/photometry.py +2340 -0
- stdpipe/photometry_background.py +998 -0
- stdpipe/photometry_iraf.py +913 -0
- stdpipe/photometry_measure.py +2268 -0
- stdpipe/photometry_model.py +931 -0
- stdpipe/photometry_psf.py +1124 -0
- stdpipe/photometry_quality.py +185 -0
- stdpipe/pipeline.py +1303 -0
- stdpipe/plots.py +1582 -0
- stdpipe/psf.py +1542 -0
- stdpipe/realbogus.py +971 -0
- stdpipe/realbogus_features.py +1657 -0
- stdpipe/reproject.py +943 -0
- stdpipe/resolve.py +267 -0
- stdpipe/sfft.py +1077 -0
- stdpipe/simulation.py +2131 -0
- stdpipe/smoothing.py +488 -0
- stdpipe/subtraction.py +1324 -0
- stdpipe/templates.py +929 -0
- stdpipe/utils.py +365 -0
- stdpipe-0.4.0.dist-info/METADATA +171 -0
- stdpipe-0.4.0.dist-info/RECORD +37 -0
- stdpipe-0.4.0.dist-info/WHEEL +5 -0
- stdpipe-0.4.0.dist-info/licenses/LICENSE.md +21 -0
- stdpipe-0.4.0.dist-info/top_level.txt +1 -0
stdpipe/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""
|
|
2
|
+
STDPipe - Simple Transient Detection Pipeline.
|
|
3
|
+
|
|
4
|
+
*STDPipe* is a set of Python routines for astrometry, photometry and transient
|
|
5
|
+
detection related tasks, intended for quick and easy implementation of custom
|
|
6
|
+
pipelines, as well as for interactive data analysis.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__version__ = "0.4.0"
|
stdpipe/artefacts.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
# Various artefact filtering routines
|
|
2
|
+
#
|
|
3
|
+
# For more advanced real-bogus classification with cutout-based morphological
|
|
4
|
+
# features, see the realbogus_features module:
|
|
5
|
+
#
|
|
6
|
+
# from stdpipe import realbogus_features as rbf
|
|
7
|
+
# obj = rbf.classify(obj, image, method='hybrid', classifier='scoring')
|
|
8
|
+
#
|
|
9
|
+
# The realbogus_features module provides:
|
|
10
|
+
# - Catalog-only, cutout-only, or hybrid feature extraction
|
|
11
|
+
# - Multiple classifiers: scoring (no training), IsolationForest, RandomForest
|
|
12
|
+
# - Generalized trend removal for all features
|
|
13
|
+
# - Training utilities for custom classifiers
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
from sklearn.ensemble import IsolationForest
|
|
17
|
+
|
|
18
|
+
from . import smoothing
|
|
19
|
+
from .realbogus_features import FLAG_BOGUS, FLAGS_UNRELIABLE_MASK
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _has_column(obj, name):
|
|
23
|
+
"""Check whether a table-like or dict-like catalog has a column."""
|
|
24
|
+
colnames = getattr(obj, 'colnames', None)
|
|
25
|
+
if colnames is not None:
|
|
26
|
+
return name in colnames
|
|
27
|
+
if hasattr(obj, 'keys'):
|
|
28
|
+
return name in obj.keys()
|
|
29
|
+
names = getattr(getattr(obj, 'dtype', None), 'names', None)
|
|
30
|
+
return names is not None and name in names
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_catalog_features(obj):
|
|
34
|
+
"""
|
|
35
|
+
Build the feature list from available catalog columns.
|
|
36
|
+
|
|
37
|
+
Prefers SExtractor-style columns (FLUX_RADIUS, FLUX_MAX/FLUX_AUTO) and
|
|
38
|
+
falls back to SEP-style ones (peak/flux) where possible, so the same
|
|
39
|
+
filter works on both `get_objects_sextractor()` and `get_objects_sep()`
|
|
40
|
+
catalogs.
|
|
41
|
+
"""
|
|
42
|
+
features = []
|
|
43
|
+
|
|
44
|
+
if _has_column(obj, 'FLUX_RADIUS'):
|
|
45
|
+
features.append([np.asarray(obj['FLUX_RADIUS'], dtype=float), 'FLUX_RADIUS'])
|
|
46
|
+
|
|
47
|
+
for name in ('fwhm', 'FWHM_IMAGE'):
|
|
48
|
+
if _has_column(obj, name):
|
|
49
|
+
features.append([np.asarray(obj[name], dtype=float), 'FWHM'])
|
|
50
|
+
break
|
|
51
|
+
|
|
52
|
+
for num, den in (('FLUX_MAX', 'FLUX_AUTO'), ('peak', 'flux')):
|
|
53
|
+
if _has_column(obj, num) and _has_column(obj, den):
|
|
54
|
+
with np.errstate(divide='ignore', invalid='ignore'):
|
|
55
|
+
ratio = np.asarray(obj[num], dtype=float) / np.asarray(obj[den], dtype=float)
|
|
56
|
+
features.append([ratio, f'{num} / {den}'])
|
|
57
|
+
break
|
|
58
|
+
|
|
59
|
+
return features
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def filter_sextractor_detections(
|
|
63
|
+
obj,
|
|
64
|
+
trend_cols=['x', 'y', 'MAG_AUTO'],
|
|
65
|
+
trend_scales=None,
|
|
66
|
+
contamination='auto',
|
|
67
|
+
return_features=False,
|
|
68
|
+
return_classifier=False,
|
|
69
|
+
random_state=0,
|
|
70
|
+
verbose=True,
|
|
71
|
+
**kwargs,
|
|
72
|
+
):
|
|
73
|
+
"""
|
|
74
|
+
Flag detections likely to be artefacts using IsolationForest.
|
|
75
|
+
|
|
76
|
+
Builds feature vectors from FLUX_RADIUS, FWHM, and peakiness
|
|
77
|
+
(FLUX_MAX/FLUX_AUTO, or peak/flux for SEP catalogs), using whichever
|
|
78
|
+
columns are present. Optionally removes smooth spatial trends (e.g.,
|
|
79
|
+
across x/y/MAG_AUTO) via an approximate LOESS regressor before fitting
|
|
80
|
+
the outlier model.
|
|
81
|
+
|
|
82
|
+
Expected columns in `obj`
|
|
83
|
+
-------------------------
|
|
84
|
+
Required:
|
|
85
|
+
- flags
|
|
86
|
+
- at least one of the feature columns: FLUX_RADIUS; fwhm or FWHM_IMAGE;
|
|
87
|
+
FLUX_MAX + FLUX_AUTO or peak + flux
|
|
88
|
+
Trend columns (when `trend_cols` is set):
|
|
89
|
+
- columns named in `trend_cols` (default: x, y, MAG_AUTO); missing ones
|
|
90
|
+
are dropped with a log message
|
|
91
|
+
|
|
92
|
+
Parameters
|
|
93
|
+
----------
|
|
94
|
+
obj : array-like / table
|
|
95
|
+
Detection catalog with required columns.
|
|
96
|
+
trend_cols : list[str] or None
|
|
97
|
+
Columns used to model smooth trends; set to None/[] to skip detrending.
|
|
98
|
+
trend_scales : list[float] or None
|
|
99
|
+
Per-dimension scaling for LOESS distances; must match trend_cols
|
|
100
|
+
length. If None (default), scales are auto-computed as ~3x the
|
|
101
|
+
standard deviation of each trend column.
|
|
102
|
+
contamination : float or 'auto'
|
|
103
|
+
Expected fraction of artefacts, passed to IsolationForest. With
|
|
104
|
+
'auto' (default) the threshold follows the original paper and a
|
|
105
|
+
data-dependent fraction is flagged even on clean catalogs; set an
|
|
106
|
+
explicit value (e.g. 0.02) to control the rejection rate.
|
|
107
|
+
return_features : bool
|
|
108
|
+
If True, return the feature list (arrays + labels) without fitting.
|
|
109
|
+
return_classifier : bool
|
|
110
|
+
If True, return a callable that classifies new catalogs.
|
|
111
|
+
random_state : int
|
|
112
|
+
Random seed for IsolationForest.
|
|
113
|
+
verbose : bool or callable
|
|
114
|
+
Logging control; can be a print-like function.
|
|
115
|
+
**kwargs :
|
|
116
|
+
Extra arguments passed to `ApproxLoessRegressor` (e.g., k, robust_iters).
|
|
117
|
+
|
|
118
|
+
Returns
|
|
119
|
+
-------
|
|
120
|
+
good : ndarray[bool] or callable
|
|
121
|
+
Boolean mask of “good” detections, or a classifier callable if
|
|
122
|
+
return_classifier is True.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
# Simple wrapper around print for logging in verbose mode only
|
|
126
|
+
log = (verbose if callable(verbose) else print) if verbose else lambda *args, **kwargs: None
|
|
127
|
+
|
|
128
|
+
features = _get_catalog_features(obj)
|
|
129
|
+
|
|
130
|
+
if not features:
|
|
131
|
+
raise ValueError(
|
|
132
|
+
"No usable feature columns found in the catalog "
|
|
133
|
+
"(need FLUX_RADIUS, fwhm/FWHM_IMAGE, FLUX_MAX+FLUX_AUTO or peak+flux)"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
if return_features:
|
|
137
|
+
return features
|
|
138
|
+
|
|
139
|
+
log(
|
|
140
|
+
"Using isolation forest outlier detection over columns ({})".format(
|
|
141
|
+
", ".join([_[1] for _ in features])
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
# LOESS neighbor count; popped here so it does not leak into other kwargs
|
|
146
|
+
k = kwargs.pop('k', 20)
|
|
147
|
+
|
|
148
|
+
# Exclude blends etc from the fit, as well as broken measurements;
|
|
149
|
+
# FLAG_BOGUS itself is ignored so re-running the filter is idempotent
|
|
150
|
+
idx = (obj['flags'] & FLAGS_UNRELIABLE_MASK) == 0
|
|
151
|
+
for f in features:
|
|
152
|
+
idx &= np.isfinite(f[0]) & (f[0] > 0)
|
|
153
|
+
|
|
154
|
+
pos = None
|
|
155
|
+
if trend_cols:
|
|
156
|
+
# Drop trend columns missing from the catalog (keeping user-provided
|
|
157
|
+
# scales aligned with the surviving columns)
|
|
158
|
+
available = [_ for _ in trend_cols if _has_column(obj, _)]
|
|
159
|
+
if trend_scales is not None and len(trend_scales) != len(trend_cols):
|
|
160
|
+
raise ValueError("trend_scales length is inconsistent with trend_cols length")
|
|
161
|
+
if len(available) < len(trend_cols):
|
|
162
|
+
missing = [_ for _ in trend_cols if _ not in available]
|
|
163
|
+
log("Trend columns not found in catalog: {}".format(", ".join(missing)))
|
|
164
|
+
if trend_scales is not None:
|
|
165
|
+
trend_scales = [s for c, s in zip(trend_cols, trend_scales) if c in available]
|
|
166
|
+
trend_cols = available
|
|
167
|
+
|
|
168
|
+
if trend_cols:
|
|
169
|
+
log("Removing smooth trends in {} using approximate LOESS".format(", ".join(trend_cols)))
|
|
170
|
+
|
|
171
|
+
if trend_scales is None:
|
|
172
|
+
# Auto-compute per-dimension scales as ~3 sigma of each column
|
|
173
|
+
trend_scales = []
|
|
174
|
+
for col in trend_cols:
|
|
175
|
+
scale = 3.0 * np.nanstd(np.asarray(obj[col], dtype=float))
|
|
176
|
+
if not np.isfinite(scale) or scale <= 0:
|
|
177
|
+
scale = 1.0
|
|
178
|
+
trend_scales.append(scale)
|
|
179
|
+
log(
|
|
180
|
+
"Auto trend scales: {}".format(
|
|
181
|
+
", ".join([f"{c}={s:.3g}" for c, s in zip(trend_cols, trend_scales)])
|
|
182
|
+
)
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
pos = np.column_stack([np.array(obj[_]) for _ in trend_cols])
|
|
186
|
+
# Rows with non-finite trend columns cannot be detrended and would
|
|
187
|
+
# get NaN features anyway; keep them out of the fits below
|
|
188
|
+
idx &= np.all(np.isfinite(pos), axis=1)
|
|
189
|
+
|
|
190
|
+
if np.sum(idx) < 3:
|
|
191
|
+
raise ValueError(
|
|
192
|
+
f"Too few clean detections ({np.sum(idx)}) to fit the outlier model"
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
if trend_cols:
|
|
196
|
+
trend_models = []
|
|
197
|
+
X = []
|
|
198
|
+
|
|
199
|
+
for f in features:
|
|
200
|
+
model = smoothing.ApproxLoessRegressor(k=k, scales=trend_scales, **kwargs)
|
|
201
|
+
model.fit(pos[idx], f[0][idx])
|
|
202
|
+
trend_models.append(model)
|
|
203
|
+
X.append(np.array(f[0]) - model.predict(pos))
|
|
204
|
+
|
|
205
|
+
else:
|
|
206
|
+
trend_models = None
|
|
207
|
+
X = [np.array(_[0]) for _ in features]
|
|
208
|
+
|
|
209
|
+
X = np.column_stack(X)
|
|
210
|
+
X[~np.isfinite(X)] = -100000 # Definitely outside of the good locus
|
|
211
|
+
|
|
212
|
+
clf = IsolationForest(contamination=contamination, random_state=random_state).fit(X[idx])
|
|
213
|
+
|
|
214
|
+
res = clf.predict(X)
|
|
215
|
+
|
|
216
|
+
log(f"{np.sum(res > 0)} good, {np.sum(res < 0)} outliers")
|
|
217
|
+
|
|
218
|
+
if return_classifier:
|
|
219
|
+
|
|
220
|
+
def classifier(obj):
|
|
221
|
+
features = _get_catalog_features(obj)
|
|
222
|
+
|
|
223
|
+
if trend_cols:
|
|
224
|
+
pos = np.column_stack([np.array(obj[_]) for _ in trend_cols])
|
|
225
|
+
X = []
|
|
226
|
+
|
|
227
|
+
for f, model in zip(features, trend_models):
|
|
228
|
+
X.append(np.array(f[0]) - model.predict(pos))
|
|
229
|
+
else:
|
|
230
|
+
X = [np.array(_[0]) for _ in features]
|
|
231
|
+
|
|
232
|
+
X = np.column_stack(X)
|
|
233
|
+
X[~np.isfinite(X)] = -100000 # Definitely outside of the good locus
|
|
234
|
+
|
|
235
|
+
return clf.predict(X) > 0
|
|
236
|
+
|
|
237
|
+
return classifier
|
|
238
|
+
|
|
239
|
+
return res > 0
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def filter_detections(
|
|
243
|
+
obj,
|
|
244
|
+
image=None,
|
|
245
|
+
bg=None,
|
|
246
|
+
err=None,
|
|
247
|
+
mask=None,
|
|
248
|
+
fwhm=None,
|
|
249
|
+
method='auto',
|
|
250
|
+
classifier='isolation',
|
|
251
|
+
threshold=0.5,
|
|
252
|
+
remove_trend=True,
|
|
253
|
+
trend_cols=None,
|
|
254
|
+
trend_scales=None,
|
|
255
|
+
add_score=False,
|
|
256
|
+
flag_bogus=False,
|
|
257
|
+
verbose=True,
|
|
258
|
+
**kwargs,
|
|
259
|
+
):
|
|
260
|
+
"""
|
|
261
|
+
Filter detections using feature-based real-bogus classification.
|
|
262
|
+
|
|
263
|
+
This is a convenience wrapper around realbogus_features.classify() that
|
|
264
|
+
provides a simpler interface similar to filter_sextractor_detections().
|
|
265
|
+
|
|
266
|
+
For full control over feature extraction and classification, use
|
|
267
|
+
realbogus_features.classify() directly.
|
|
268
|
+
|
|
269
|
+
Parameters
|
|
270
|
+
----------
|
|
271
|
+
obj : astropy.table.Table
|
|
272
|
+
Object catalog with 'x', 'y' columns. Modified in place when
|
|
273
|
+
`add_score` or `flag_bogus` is set.
|
|
274
|
+
image : ndarray, optional
|
|
275
|
+
Science image. If provided, cutout features will be extracted.
|
|
276
|
+
bg : ndarray or float, optional
|
|
277
|
+
Background map or scalar.
|
|
278
|
+
err : ndarray or float, optional
|
|
279
|
+
Error/noise map or scalar.
|
|
280
|
+
mask : ndarray, optional
|
|
281
|
+
Boolean mask (True = masked).
|
|
282
|
+
fwhm : float, optional
|
|
283
|
+
Image FWHM. If None, estimated from catalog.
|
|
284
|
+
method : str, optional
|
|
285
|
+
Feature extraction method:
|
|
286
|
+
- 'catalog': Catalog features only (no image needed)
|
|
287
|
+
- 'cutout': Cutout features only
|
|
288
|
+
- 'hybrid': Both catalog and cutout features
|
|
289
|
+
- 'auto': 'hybrid' if image provided, else 'catalog'
|
|
290
|
+
classifier : str, optional
|
|
291
|
+
Classifier to use:
|
|
292
|
+
- 'scoring': Rule-based scoring (no training needed)
|
|
293
|
+
- 'isolation': IsolationForest (unsupervised, default)
|
|
294
|
+
threshold : float, optional
|
|
295
|
+
Score threshold for classification. Default: 0.5.
|
|
296
|
+
remove_trend : bool, optional
|
|
297
|
+
Remove spatial trends from features. Default: True.
|
|
298
|
+
trend_cols : list of str, optional
|
|
299
|
+
Columns for trend removal. Default: ['x', 'y'].
|
|
300
|
+
trend_scales : list of float, optional
|
|
301
|
+
Scales for trend removal. Default: auto-computed.
|
|
302
|
+
add_score : bool, optional
|
|
303
|
+
Add 'rb_score' column to the input catalog. Default: False.
|
|
304
|
+
flag_bogus : bool, optional
|
|
305
|
+
Set FLAG_BOGUS (0x4000) on bogus objects in the input catalog.
|
|
306
|
+
Default: False.
|
|
307
|
+
verbose : bool, optional
|
|
308
|
+
Print progress.
|
|
309
|
+
**kwargs
|
|
310
|
+
Additional arguments passed to realbogus_features.classify().
|
|
311
|
+
|
|
312
|
+
Returns
|
|
313
|
+
-------
|
|
314
|
+
good : ndarray[bool]
|
|
315
|
+
Boolean mask of "good" (real) detections.
|
|
316
|
+
|
|
317
|
+
See Also
|
|
318
|
+
--------
|
|
319
|
+
realbogus_features.classify : Full-featured classification function.
|
|
320
|
+
filter_sextractor_detections : Original SExtractor-specific filter.
|
|
321
|
+
|
|
322
|
+
Examples
|
|
323
|
+
--------
|
|
324
|
+
>>> # Simple catalog-only filtering (like original function)
|
|
325
|
+
>>> good = filter_detections(obj, classifier='isolation')
|
|
326
|
+
|
|
327
|
+
>>> # Cutout-based filtering with scoring (no training)
|
|
328
|
+
>>> good = filter_detections(obj, image, classifier='scoring')
|
|
329
|
+
|
|
330
|
+
>>> # Hybrid with trend removal
|
|
331
|
+
>>> good = filter_detections(obj, image, method='hybrid',
|
|
332
|
+
... remove_trend=True, trend_cols=['x', 'y'])
|
|
333
|
+
"""
|
|
334
|
+
from . import realbogus_features as rbf
|
|
335
|
+
|
|
336
|
+
log = (verbose if callable(verbose) else print) if verbose else lambda *args, **kwargs: None
|
|
337
|
+
|
|
338
|
+
# Call the full classify function; the score is always needed to build
|
|
339
|
+
# the output mask
|
|
340
|
+
result = rbf.classify(
|
|
341
|
+
obj,
|
|
342
|
+
image=image,
|
|
343
|
+
bg=bg,
|
|
344
|
+
err=err,
|
|
345
|
+
mask=mask,
|
|
346
|
+
fwhm=fwhm,
|
|
347
|
+
method=method,
|
|
348
|
+
classifier=classifier,
|
|
349
|
+
threshold=threshold,
|
|
350
|
+
add_score=True,
|
|
351
|
+
flag_bogus=flag_bogus,
|
|
352
|
+
remove_trend=remove_trend,
|
|
353
|
+
trend_cols=trend_cols,
|
|
354
|
+
trend_scales=trend_scales,
|
|
355
|
+
verbose=verbose,
|
|
356
|
+
**kwargs,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
good = np.asarray(result['rb_score'] >= threshold)
|
|
360
|
+
|
|
361
|
+
# classify() works on a copy; propagate the requested columns back to
|
|
362
|
+
# the caller's catalog so add_score/flag_bogus have a visible effect
|
|
363
|
+
if add_score:
|
|
364
|
+
obj['rb_score'] = result['rb_score']
|
|
365
|
+
if flag_bogus:
|
|
366
|
+
obj['flags'] = result['flags']
|
|
367
|
+
|
|
368
|
+
log(f"{np.sum(good)} good, {np.sum(~good)} outliers")
|
|
369
|
+
|
|
370
|
+
return good
|