jwstark 0.0.1__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.
- jstark/__init__.py +6 -0
- jstark/data/README.rst +6 -0
- jstark/extraction/__init__.py +37 -0
- jstark/extraction/background.py +150 -0
- jstark/extraction/bspline.py +291 -0
- jstark/extraction/config.py +116 -0
- jstark/extraction/contamination.py +153 -0
- jstark/extraction/covariance.py +123 -0
- jstark/extraction/diagnostics.py +253 -0
- jstark/extraction/extract.py +235 -0
- jstark/extraction/extractor.py +1992 -0
- jstark/extraction/metrics.py +88 -0
- jstark/extraction/photometry.py +1626 -0
- jstark/extraction/psf.py +277 -0
- jstark/extraction/source.py +177 -0
- jstark/extraction/trace.py +368 -0
- jstark/fitting/__init__.py +33 -0
- jstark/fitting/fit.py +782 -0
- jstark/fitting/lightcurve.py +796 -0
- jstark/fitting/plots.py +1678 -0
- jstark/fitting/priors.py +283 -0
- jstark/fitting/spectroscopic.py +792 -0
- jstark/fitting/utils.py +776 -0
- jstark/plotting.py +153 -0
- jstark/reduction/__init__.py +21 -0
- jstark/reduction/_compat.py +51 -0
- jstark/reduction/bias.py +111 -0
- jstark/reduction/cleaning.py +1261 -0
- jstark/reduction/diagnostics.py +222 -0
- jstark/reduction/instruments.py +325 -0
- jstark/reduction/io.py +182 -0
- jstark/reduction/jump.py +149 -0
- jstark/reduction/masks.py +269 -0
- jstark/reduction/onef.py +573 -0
- jstark/reduction/robust.py +139 -0
- jstark/reduction/stage1.py +763 -0
- jstark/reduction/stage2.py +257 -0
- jstark/tests/__init__.py +3 -0
- jstark/version.py +24 -0
- jwstark-0.0.1.dist-info/METADATA +779 -0
- jwstark-0.0.1.dist-info/RECORD +43 -0
- jwstark-0.0.1.dist-info/WHEEL +5 -0
- jwstark-0.0.1.dist-info/top_level.txt +1 -0
jstark/__init__.py
ADDED
jstark/data/README.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Spectral extraction and photometry of cleaned JWST time-series data.
|
|
2
|
+
|
|
3
|
+
The spline-PSF optimal extraction is a port of ``stark``
|
|
4
|
+
(A. Brandeker & J. A. Patel; https://github.com/Jayshil/stark), extended to
|
|
5
|
+
work segment by segment on exposures of any length (see
|
|
6
|
+
:mod:`jstark.extraction.psf` for the differences).
|
|
7
|
+
|
|
8
|
+
Main classes
|
|
9
|
+
------------
|
|
10
|
+
SpectralExtractor
|
|
11
|
+
Trace, background, PSF and extraction of every order of a spectroscopic
|
|
12
|
+
exposure; writes one spectra file per segment and a combined file.
|
|
13
|
+
VisitExtractor
|
|
14
|
+
The exposures of one visit; by default one PSF model fitted jointly from
|
|
15
|
+
all of them (``shared_psf=True``), each exposure extracted separately.
|
|
16
|
+
Spectra
|
|
17
|
+
The combined extracted spectra (white light, binned light curves).
|
|
18
|
+
Photometry
|
|
19
|
+
Aperture and PSF-weighted photometry of imaging exposures (e.g. MIRI
|
|
20
|
+
F1500W), segment by segment; ``ApPhoto`` is the in-memory aperture/PLD
|
|
21
|
+
helper (moved here from ``jstark.jstark``).
|
|
22
|
+
ExposureSource
|
|
23
|
+
Chunked, segment-by-segment access to the cleaned data.
|
|
24
|
+
|
|
25
|
+
Field-star contamination (NIRISS/SOSS): ``SpectralExtractor.find_contaminants``
|
|
26
|
+
masks zeroth orders found in the F277W exposure, ``add_field_trace`` models and
|
|
27
|
+
subtracts field-star traces (:mod:`jstark.extraction.contamination`).
|
|
28
|
+
"""
|
|
29
|
+
from .source import ExposureSource # noqa: F401
|
|
30
|
+
from .trace import Trace, find_trace # noqa: F401
|
|
31
|
+
from .psf import SplinePSF # noqa: F401
|
|
32
|
+
from .extractor import SpectralExtractor, VisitExtractor, Spectra # noqa: F401
|
|
33
|
+
from .metrics import p2p_sigma, p2p_noise # noqa: F401
|
|
34
|
+
from .photometry import Photometry, PhotometryResult, ApPhoto # noqa: F401
|
|
35
|
+
|
|
36
|
+
__all__ = ['SpectralExtractor', 'VisitExtractor', 'Spectra', 'Photometry', 'PhotometryResult', 'ApPhoto',
|
|
37
|
+
'ExposureSource', 'Trace', 'SplinePSF']
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Integration-level background subtraction before extraction.
|
|
2
|
+
|
|
3
|
+
The background of every integration is estimated separately for every
|
|
4
|
+
spectral pixel (column of the canonical frame) from pixels at a distance of
|
|
5
|
+
at least ``inner`` (and at most ``outer``) pixels from every spectral trace.
|
|
6
|
+
For imaging, see :mod:`jstark.extraction.photometry`.
|
|
7
|
+
"""
|
|
8
|
+
import numpy as np
|
|
9
|
+
import bottleneck as bn
|
|
10
|
+
|
|
11
|
+
from ..reduction.robust import MAD_TO_SIGMA
|
|
12
|
+
|
|
13
|
+
__all__ = ['background_region', 'estimate_background', 'subtract_background']
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def background_region(shape, traces, inner, outer=None, good=None, min_pixels=4):
|
|
17
|
+
"""Static background mask ``(nspat, nspec)`` from the trace positions.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
shape : tuple
|
|
22
|
+
Canonical frame shape.
|
|
23
|
+
traces : list of Trace
|
|
24
|
+
Every order on the detector (pixels near any of them are excluded).
|
|
25
|
+
inner : float
|
|
26
|
+
Minimum distance from every trace.
|
|
27
|
+
outer : float or None
|
|
28
|
+
Maximum distance from the nearest trace.
|
|
29
|
+
good : ndarray of bool or None
|
|
30
|
+
Persistent good-pixel map.
|
|
31
|
+
min_pixels : int
|
|
32
|
+
Columns with fewer background pixels are flagged in the returned
|
|
33
|
+
``ok`` array (their background is interpolated from neighbours).
|
|
34
|
+
"""
|
|
35
|
+
nspat, nspec = shape
|
|
36
|
+
rows = np.arange(nspat)[:, None].astype(float)
|
|
37
|
+
dmin = np.full(shape, np.inf)
|
|
38
|
+
for tr in traces:
|
|
39
|
+
y = np.full(nspec, np.nan)
|
|
40
|
+
y[tr.cols] = tr.y0 + (np.nanmedian(tr.dy) if tr.dy is not None else 0.)
|
|
41
|
+
# extend the trace beyond its column range (pixels there may hold PSF wings)
|
|
42
|
+
yfill = np.interp(np.arange(nspec), tr.cols, tr.y0)
|
|
43
|
+
y = np.where(np.isfinite(y), y, yfill)
|
|
44
|
+
dmin = np.minimum(dmin, np.abs(rows - y[None, :]))
|
|
45
|
+
mask = dmin >= inner
|
|
46
|
+
if outer is not None:
|
|
47
|
+
mask &= dmin <= outer
|
|
48
|
+
if good is not None:
|
|
49
|
+
mask &= good
|
|
50
|
+
ok = mask.sum(axis=0) >= min_pixels
|
|
51
|
+
return mask, ok
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def estimate_background(data, err, good, mask, method='median', nsigma=3., maxiters=3,
|
|
55
|
+
poly_order=1, cols=None, ok=None):
|
|
56
|
+
"""Background of every integration and spectral pixel.
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
data, err : ndarray ``(n, nspat, nspec)``
|
|
61
|
+
good : ndarray of bool ``(n, nspat, nspec)``
|
|
62
|
+
mask : ndarray of bool ``(nspat, nspec)``
|
|
63
|
+
Background region.
|
|
64
|
+
method : {'median', 'poly', 'none'}
|
|
65
|
+
Sigma-clipped median of the background pixels, or a polynomial along
|
|
66
|
+
the spatial direction fitted to them (``poly_order``).
|
|
67
|
+
cols : ndarray or None
|
|
68
|
+
Spectral pixels to process (default: all).
|
|
69
|
+
ok : ndarray of bool or None
|
|
70
|
+
Columns with enough background pixels; the others are interpolated.
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
bkg : ndarray
|
|
75
|
+
``(n, ncol)`` for ``'median'``, ``(n, nspat, ncol)`` for ``'poly'``.
|
|
76
|
+
var : ndarray ``(n, ncol)``
|
|
77
|
+
Variance of the background estimate (at the frame centre for
|
|
78
|
+
``'poly'``).
|
|
79
|
+
"""
|
|
80
|
+
cols = np.arange(data.shape[2]) if cols is None else np.asarray(cols)
|
|
81
|
+
n, nspat = data.shape[:2]
|
|
82
|
+
if method in (None, 'none'):
|
|
83
|
+
return np.zeros((n, len(cols)), np.float32), np.zeros((n, len(cols)), np.float32)
|
|
84
|
+
m = mask[None, :, cols] & good[:, :, cols]
|
|
85
|
+
d = np.where(m, data[:, :, cols], np.nan).astype(np.float32)
|
|
86
|
+
e2 = np.where(m, err[:, :, cols].astype(np.float32) ** 2, np.nan)
|
|
87
|
+
for _ in range(maxiters if nsigma else 0):
|
|
88
|
+
med = bn.nanmedian(d, axis=1)
|
|
89
|
+
dev = np.abs(d - med[:, None, :])
|
|
90
|
+
sig = MAD_TO_SIGMA * bn.nanmedian(dev, axis=1)
|
|
91
|
+
bad = dev > nsigma * np.maximum(sig, 1e-30)[:, None, :]
|
|
92
|
+
if not bad.any():
|
|
93
|
+
break
|
|
94
|
+
d[bad] = np.nan
|
|
95
|
+
e2[bad] = np.nan
|
|
96
|
+
npix = np.sum(np.isfinite(d), axis=1)
|
|
97
|
+
if method == 'median':
|
|
98
|
+
bkg = bn.nanmedian(d, axis=1)
|
|
99
|
+
# variance of a median of npix values: (pi/2) * mean variance / npix
|
|
100
|
+
var = (np.pi / 2) * bn.nanmean(e2, axis=1) / np.maximum(npix, 1)
|
|
101
|
+
elif method == 'poly':
|
|
102
|
+
k = int(poly_order) + 1
|
|
103
|
+
x = (np.arange(nspat) - 0.5 * (nspat - 1)) / max(nspat / 2, 1)
|
|
104
|
+
V = np.vander(x, k, increasing=True) # (nspat, k)
|
|
105
|
+
fin = np.isfinite(d)
|
|
106
|
+
w = np.where(fin, 1. / np.where(fin, e2, 1.), 0.)
|
|
107
|
+
dz = np.where(fin, d, 0.)
|
|
108
|
+
G = np.einsum('nrc,ri,rj->ncij', w, V, V)
|
|
109
|
+
rhs = np.einsum('nrc,ri,nrc->nci', w, V, dz)
|
|
110
|
+
G += 1e-12 * np.trace(G, axis1=-2, axis2=-1)[..., None, None] * np.eye(k)
|
|
111
|
+
coef = np.linalg.solve(G, rhs[..., None])[..., 0] # (n, ncol, k)
|
|
112
|
+
bkg = np.einsum('ri,nci->nrc', V, coef).astype(np.float32)
|
|
113
|
+
cov = np.linalg.inv(G)
|
|
114
|
+
v0 = V[nspat // 2]
|
|
115
|
+
var = np.einsum('i,ncij,j->nc', v0, cov, v0)
|
|
116
|
+
else:
|
|
117
|
+
raise ValueError(f'Unknown background method {method!r}')
|
|
118
|
+
bad_col = npix < 1
|
|
119
|
+
if ok is not None:
|
|
120
|
+
bad_col |= ~ok[cols][None, :]
|
|
121
|
+
if bad_col.any():
|
|
122
|
+
bkg, var = _interp_columns(bkg, var, bad_col)
|
|
123
|
+
return bkg, np.nan_to_num(var).astype(np.float32)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _interp_columns(bkg, var, bad):
|
|
127
|
+
"""Interpolate the background of bad columns from good neighbours."""
|
|
128
|
+
bkg = np.array(bkg, dtype=np.float32)
|
|
129
|
+
var = np.array(var, dtype=np.float32)
|
|
130
|
+
x = np.arange(bad.shape[1])
|
|
131
|
+
for i in range(bad.shape[0]):
|
|
132
|
+
b = bad[i]
|
|
133
|
+
if not b.any() or b.all():
|
|
134
|
+
continue
|
|
135
|
+
if bkg.ndim == 2:
|
|
136
|
+
bkg[i, b] = np.interp(x[b], x[~b], bkg[i, ~b])
|
|
137
|
+
else:
|
|
138
|
+
for r in range(bkg.shape[1]):
|
|
139
|
+
bkg[i, r, b] = np.interp(x[b], x[~b], bkg[i, r, ~b])
|
|
140
|
+
var[i, b] = np.interp(x[b], x[~b], var[i, ~b])
|
|
141
|
+
return bkg, var
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def subtract_background(data, bkg, cols):
|
|
145
|
+
"""Subtract ``bkg`` (from :func:`estimate_background`) in place from ``data[:, :, cols]``."""
|
|
146
|
+
if bkg.ndim == 2:
|
|
147
|
+
data[:, :, cols] -= bkg[:, None, :]
|
|
148
|
+
else:
|
|
149
|
+
data[:, :, cols] -= bkg
|
|
150
|
+
return data
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"""Vectorised cubic B-spline basis, penalties and streamed least-squares sums.
|
|
2
|
+
|
|
3
|
+
The PSF models of :mod:`jstark.extraction` are tensor-product cubic
|
|
4
|
+
B-splines. Instead of handing every pixel to
|
|
5
|
+
``scipy.interpolate.LSQBivariateSpline`` (which needs all of them in memory),
|
|
6
|
+
we accumulate the least-squares *normal equations* chunk by chunk:
|
|
7
|
+
|
|
8
|
+
.. math::
|
|
9
|
+
|
|
10
|
+
\\Big(\\sum_\\mathrm{pix} w\\, B B^T\\Big)\\, c = \\sum_\\mathrm{pix} w\\, B\\, y
|
|
11
|
+
|
|
12
|
+
Both sides are sums over pixels, so they can be built one segment (or one
|
|
13
|
+
chunk of integrations) at a time; the solution is identical to fitting all
|
|
14
|
+
pixels at once. Only 4 basis functions are non-zero at any point, so each
|
|
15
|
+
pixel touches a 4 x 4 block of the matrix.
|
|
16
|
+
"""
|
|
17
|
+
import numpy as np
|
|
18
|
+
|
|
19
|
+
__all__ = ['clamped_knots', 'basis', 'n_basis', 'second_difference', 'integrals',
|
|
20
|
+
'NormalSums', 'BandedSystem']
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def clamped_knots(lo, hi, interior, k=3):
|
|
24
|
+
"""Full clamped knot vector for ``interior`` knots on ``[lo, hi]``."""
|
|
25
|
+
interior = np.asarray(interior, dtype=float)
|
|
26
|
+
interior = interior[(interior > lo) & (interior < hi)]
|
|
27
|
+
return np.concatenate([[lo] * (k + 1), np.sort(interior), [hi] * (k + 1)])
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def n_basis(t, k=3):
|
|
31
|
+
return len(t) - k - 1
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def basis(x, t, k=3, deriv=0):
|
|
35
|
+
"""Non-zero cubic B-spline basis values at ``x``.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
x : ndarray
|
|
40
|
+
Evaluation points; values outside ``[t[0], t[-1]]`` are clipped to the
|
|
41
|
+
boundary (callers mask them).
|
|
42
|
+
t : ndarray
|
|
43
|
+
Clamped knot vector (see :func:`clamped_knots`).
|
|
44
|
+
k : int
|
|
45
|
+
Degree (3 = cubic).
|
|
46
|
+
deriv : int
|
|
47
|
+
0 for the values, 1 for the first derivative.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
j0 : ndarray of int
|
|
52
|
+
Index of the first non-zero basis function for each point; the
|
|
53
|
+
non-zero functions are ``j0 ... j0 + k``.
|
|
54
|
+
vals : ndarray
|
|
55
|
+
``(len(x), k + 1)`` basis values (or derivatives).
|
|
56
|
+
"""
|
|
57
|
+
x = np.clip(np.asarray(x, dtype=float).ravel(), t[0], t[-1])
|
|
58
|
+
nb = n_basis(t, k)
|
|
59
|
+
# interval m with t[m] <= x < t[m+1], k <= m <= nb - 1
|
|
60
|
+
m = np.searchsorted(t, x, side='right') - 1
|
|
61
|
+
m = np.clip(m, k, nb - 1)
|
|
62
|
+
# Cox-de Boor triangle (degree up to k - deriv), vectorised over points
|
|
63
|
+
kk = k - deriv
|
|
64
|
+
N = np.zeros((len(x), kk + 1))
|
|
65
|
+
N[:, 0] = 1.
|
|
66
|
+
for d in range(1, kk + 1):
|
|
67
|
+
new = np.zeros_like(N)
|
|
68
|
+
for r in range(d + 1):
|
|
69
|
+
# basis index i = m - d + r
|
|
70
|
+
i = m - d + r
|
|
71
|
+
if r > 0: # left term: N_{i, d-1} (stored at r-1)
|
|
72
|
+
den = t[i + d] - t[i]
|
|
73
|
+
with np.errstate(divide='ignore', invalid='ignore'):
|
|
74
|
+
new[:, r] += np.where(den > 0, (x - t[i]) / den, 0.) * N[:, r - 1]
|
|
75
|
+
if r < d: # right term: N_{i+1, d-1} (stored at r)
|
|
76
|
+
den = t[i + d + 1] - t[i + 1]
|
|
77
|
+
with np.errstate(divide='ignore', invalid='ignore'):
|
|
78
|
+
new[:, r] += np.where(den > 0, (t[i + d + 1] - x) / den, 0.) * N[:, r]
|
|
79
|
+
N = new
|
|
80
|
+
if deriv == 0:
|
|
81
|
+
return m - k, N
|
|
82
|
+
if deriv != 1:
|
|
83
|
+
raise ValueError('only deriv = 0 or 1 is implemented')
|
|
84
|
+
# derivative of degree-k basis from degree-(k-1) values N (indices m-k+1 ... m)
|
|
85
|
+
out = np.zeros((len(x), k + 1))
|
|
86
|
+
for r in range(k + 1):
|
|
87
|
+
i = m - k + r # degree-k function i
|
|
88
|
+
# B'_{i,k} = k [B_{i,k-1}/(t_{i+k}-t_i) - B_{i+1,k-1}/(t_{i+k+1}-t_{i+1})]
|
|
89
|
+
if r - 1 >= 0:
|
|
90
|
+
den = t[i + k] - t[i]
|
|
91
|
+
with np.errstate(divide='ignore', invalid='ignore'):
|
|
92
|
+
out[:, r] += np.where(den > 0, k / den, 0.) * N[:, r - 1]
|
|
93
|
+
if r <= k - 1:
|
|
94
|
+
den = t[i + k + 1] - t[i + 1]
|
|
95
|
+
with np.errstate(divide='ignore', invalid='ignore'):
|
|
96
|
+
out[:, r] -= np.where(den > 0, k / den, 0.) * N[:, r]
|
|
97
|
+
return m - k, out
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def integrals(t, k=3):
|
|
101
|
+
"""Integral of every basis function over the whole knot range."""
|
|
102
|
+
nb = n_basis(t, k)
|
|
103
|
+
i = np.arange(nb)
|
|
104
|
+
return (t[i + k + 1] - t[i]) / (k + 1)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def second_difference(n):
|
|
108
|
+
"""``(D^T D)`` for the second-difference operator on ``n`` coefficients."""
|
|
109
|
+
if n < 3:
|
|
110
|
+
return np.zeros((n, n))
|
|
111
|
+
D = np.diff(np.eye(n), n=2, axis=0)
|
|
112
|
+
return D.T @ D
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class NormalSums:
|
|
116
|
+
"""Per-column normal equations of a 1D spline in the spatial coordinate.
|
|
117
|
+
|
|
118
|
+
For every spectral column ``x`` we keep ``A_x = sum w B B^T``,
|
|
119
|
+
``b_x = sum w B y`` and ``s_x = sum w y^2``, separately for even and odd
|
|
120
|
+
integrations (for cross-validation). A tensor-product (2D) spline in
|
|
121
|
+
(spatial offset, column) is assembled from these afterwards
|
|
122
|
+
(:meth:`assemble`), so the column knots can be changed without reading the
|
|
123
|
+
data again, and summing over columns gives the 1D (column-independent) fit.
|
|
124
|
+
|
|
125
|
+
Memory: ``2 * ncol * nb**2`` doubles (e.g. 2 x 1500 x 35^2 = 30 MB).
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
def __init__(self, ncol, t, k=3):
|
|
129
|
+
self.t = np.asarray(t, float)
|
|
130
|
+
self.k = k
|
|
131
|
+
self.nb = n_basis(self.t, k)
|
|
132
|
+
self.ncol = int(ncol)
|
|
133
|
+
self.A = np.zeros((2, self.ncol, self.nb, self.nb))
|
|
134
|
+
self.b = np.zeros((2, self.ncol, self.nb))
|
|
135
|
+
self.s = np.zeros((2, self.ncol))
|
|
136
|
+
self.n = np.zeros((2, self.ncol), dtype=np.int64)
|
|
137
|
+
|
|
138
|
+
def add(self, u, col, y, w, parity):
|
|
139
|
+
"""Add pixels.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
u : ndarray
|
|
144
|
+
Spatial coordinate (offset from the trace) of each pixel.
|
|
145
|
+
col : ndarray of int
|
|
146
|
+
Column index (0 ... ncol-1) of each pixel.
|
|
147
|
+
y, w : ndarray
|
|
148
|
+
Value and weight (1/variance) of each pixel.
|
|
149
|
+
parity : ndarray of int
|
|
150
|
+
0 for even, 1 for odd integrations.
|
|
151
|
+
"""
|
|
152
|
+
if len(u) == 0:
|
|
153
|
+
return
|
|
154
|
+
j0, B = basis(u, self.t, self.k)
|
|
155
|
+
nb, nc = self.nb, self.ncol
|
|
156
|
+
col = np.asarray(col, np.int64)
|
|
157
|
+
par = np.asarray(parity, np.int64)
|
|
158
|
+
base = (par * nc + col)
|
|
159
|
+
size2 = 2 * nc * nb
|
|
160
|
+
for p in range(self.k + 1):
|
|
161
|
+
wp = w * B[:, p]
|
|
162
|
+
ip = base * nb + j0 + p
|
|
163
|
+
self.b += np.bincount(ip, weights=wp * y, minlength=size2).reshape(2, nc, nb)
|
|
164
|
+
for q in range(self.k + 1):
|
|
165
|
+
iq = ip * nb + j0 + q
|
|
166
|
+
self.A += np.bincount(iq, weights=wp * B[:, q],
|
|
167
|
+
minlength=size2 * nb).reshape(2, nc, nb, nb)
|
|
168
|
+
self.s += np.bincount(base, weights=w * y * y, minlength=2 * nc).reshape(2, nc)
|
|
169
|
+
self.n += np.bincount(base, minlength=2 * nc).reshape(2, nc)
|
|
170
|
+
|
|
171
|
+
def assemble(self, bx_j0, bx, nbx, cols=None):
|
|
172
|
+
"""Block-banded normal equations of the tensor spline, for each parity.
|
|
173
|
+
|
|
174
|
+
Coefficients are ordered column-basis-major (``index = j * nb + i``),
|
|
175
|
+
so the matrix is block-banded: block ``(j, j + d)`` (``d`` = 0 ... 3)
|
|
176
|
+
couples column basis functions ``j`` and ``j + d``.
|
|
177
|
+
|
|
178
|
+
Parameters
|
|
179
|
+
----------
|
|
180
|
+
bx_j0, bx : ndarray
|
|
181
|
+
Column-direction basis (from :func:`basis`) for every column.
|
|
182
|
+
nbx : int
|
|
183
|
+
Number of column-direction basis functions.
|
|
184
|
+
cols : ndarray of bool or None
|
|
185
|
+
Columns to include (default: all).
|
|
186
|
+
|
|
187
|
+
Returns
|
|
188
|
+
-------
|
|
189
|
+
Mb : ndarray ``(2, nbx, nd, nb, nb)``
|
|
190
|
+
Upper blocks ``(j, j + d)`` (``nd = max(kx, 3)``).
|
|
191
|
+
r : ndarray ``(2, nbx, nb)``
|
|
192
|
+
s : ndarray ``(2,)``
|
|
193
|
+
"""
|
|
194
|
+
nb = self.nb
|
|
195
|
+
kx = bx.shape[1]
|
|
196
|
+
nd = max(kx, 3)
|
|
197
|
+
Mb = np.zeros((2, nbx + kx, nd, nb, nb))
|
|
198
|
+
R2 = np.zeros((2, nbx + kx, nb))
|
|
199
|
+
use = np.ones(self.ncol, bool) if cols is None else np.asarray(cols, bool)
|
|
200
|
+
for j in np.unique(bx_j0[use]):
|
|
201
|
+
sel = (bx_j0 == j) & use
|
|
202
|
+
A = self.A[:, sel]
|
|
203
|
+
b = self.b[:, sel]
|
|
204
|
+
for p in range(kx):
|
|
205
|
+
R2[:, j + p] += np.einsum('pcn,c->pn', b, bx[sel, p])
|
|
206
|
+
for q in range(p, kx):
|
|
207
|
+
Mb[:, j + p, q - p] += np.einsum('pcnm,c->pnm', A, bx[sel, p] * bx[sel, q])
|
|
208
|
+
return Mb[:, :nbx], R2[:, :nbx], self.s[:, use].sum(axis=1)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class BandedSystem:
|
|
212
|
+
"""Symmetric block-banded matrix (see :meth:`NormalSums.assemble`)."""
|
|
213
|
+
|
|
214
|
+
def __init__(self, Mb):
|
|
215
|
+
self.Mb = np.array(Mb, dtype=float)
|
|
216
|
+
self.nbx, self.nd, self.nb, _ = self.Mb.shape
|
|
217
|
+
|
|
218
|
+
@property
|
|
219
|
+
def n(self):
|
|
220
|
+
return self.nbx * self.nb
|
|
221
|
+
|
|
222
|
+
def trace(self):
|
|
223
|
+
return float(np.einsum('jii->', self.Mb[:, 0]))
|
|
224
|
+
|
|
225
|
+
def copy(self):
|
|
226
|
+
return BandedSystem(self.Mb.copy())
|
|
227
|
+
|
|
228
|
+
def add_u_penalty(self, lam, DtD):
|
|
229
|
+
"""``+ lam * (I_x kron DtD)`` (smoothness along the spatial offset)."""
|
|
230
|
+
self.Mb[:, 0] += lam * DtD
|
|
231
|
+
|
|
232
|
+
def add_x_penalty(self, lam, DtD):
|
|
233
|
+
"""``+ lam * (DtD kron I_u)`` (smoothness along the columns)."""
|
|
234
|
+
eye = np.eye(self.nb)
|
|
235
|
+
for d in range(min(3, self.nd)):
|
|
236
|
+
dg = np.diagonal(DtD, offset=d)
|
|
237
|
+
self.Mb[:len(dg), d] += lam * dg[:, None, None] * eye
|
|
238
|
+
|
|
239
|
+
def add_ridge(self, eps):
|
|
240
|
+
self.Mb[:, 0] += eps * np.eye(self.nb)
|
|
241
|
+
|
|
242
|
+
def matvec(self, c):
|
|
243
|
+
c = np.asarray(c).reshape(self.nbx, self.nb)
|
|
244
|
+
y = np.einsum('jab,jb->ja', self.Mb[:, 0], c)
|
|
245
|
+
for d in range(1, self.nd):
|
|
246
|
+
y[:-d] += np.einsum('jab,jb->ja', self.Mb[:-d, d], c[d:])
|
|
247
|
+
y[d:] += np.einsum('jab,ja->jb', self.Mb[:-d, d], c[:-d])
|
|
248
|
+
return y.ravel()
|
|
249
|
+
|
|
250
|
+
def to_lower_banded(self):
|
|
251
|
+
nb, nbx, nd = self.nb, self.nbx, self.nd
|
|
252
|
+
kd = nd * nb - 1
|
|
253
|
+
ab = np.zeros((kd + 1, self.n))
|
|
254
|
+
a, b = np.meshgrid(np.arange(nb), np.arange(nb), indexing='ij')
|
|
255
|
+
for d in range(nd):
|
|
256
|
+
for j in range(nbx - d):
|
|
257
|
+
blk = self.Mb[j, d]
|
|
258
|
+
r = (j + d) * nb + b # lower triangle: transpose of the upper block
|
|
259
|
+
c = j * nb + a
|
|
260
|
+
m = r >= c
|
|
261
|
+
ab[(r - c)[m], c[m]] = blk[m]
|
|
262
|
+
return ab
|
|
263
|
+
|
|
264
|
+
def to_dense(self):
|
|
265
|
+
nb, n = self.nb, self.n
|
|
266
|
+
M = np.zeros((n, n))
|
|
267
|
+
for d in range(self.nd):
|
|
268
|
+
for j in range(self.nbx - d):
|
|
269
|
+
M[j * nb:(j + 1) * nb, (j + d) * nb:(j + d + 1) * nb] = self.Mb[j, d]
|
|
270
|
+
if d:
|
|
271
|
+
M[(j + d) * nb:(j + d + 1) * nb, j * nb:(j + 1) * nb] = self.Mb[j, d].T
|
|
272
|
+
return M
|
|
273
|
+
|
|
274
|
+
def solve(self, rhs, dense_max=3000):
|
|
275
|
+
"""Solve ``M c = rhs`` (dense Cholesky for moderate sizes, banded otherwise)."""
|
|
276
|
+
from scipy.linalg import (cholesky_banded, cho_solve_banded, cho_factor, cho_solve,
|
|
277
|
+
LinAlgError)
|
|
278
|
+
if self.n <= dense_max:
|
|
279
|
+
M = self.to_dense()
|
|
280
|
+
try:
|
|
281
|
+
return cho_solve(cho_factor(M, check_finite=False), np.ravel(rhs), check_finite=False)
|
|
282
|
+
except LinAlgError:
|
|
283
|
+
return np.linalg.lstsq(M, np.ravel(rhs), rcond=None)[0]
|
|
284
|
+
ab = self.to_lower_banded()
|
|
285
|
+
try:
|
|
286
|
+
cb = cholesky_banded(ab, lower=True, check_finite=False)
|
|
287
|
+
return cho_solve_banded((cb, True), np.ravel(rhs), check_finite=False)
|
|
288
|
+
except LinAlgError:
|
|
289
|
+
from scipy.linalg import solveh_banded
|
|
290
|
+
return solveh_banded(ab + 1e-9 * np.abs(ab[0]).max() * (np.arange(len(ab))[:, None] == 0),
|
|
291
|
+
np.ravel(rhs), lower=True, check_finite=False)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""Default extraction parameters per observing mode.
|
|
2
|
+
|
|
3
|
+
Parameters are grouped by step:
|
|
4
|
+
|
|
5
|
+
``columns``
|
|
6
|
+
Column (spectral pixel) range of each order. ``columns=(x0, x1)`` or
|
|
7
|
+
``wavelength_range=(lam0, lam1)`` override the automatic choice (largest
|
|
8
|
+
contiguous run of columns with a valid wavelength, enough good pixels and
|
|
9
|
+
a per-integration S/N above ``snr_min``). MIRI/LRS defaults to
|
|
10
|
+
``wavelength_range=(5, 12)``; ``ex.set('columns', wavelength_range=None)``
|
|
11
|
+
restores the automatic choice.
|
|
12
|
+
``trace``
|
|
13
|
+
``halfwidth`` of the centre-of-flux window, ``nknots`` of the spline
|
|
14
|
+
describing the trace shape, ``clip``; ``reference='stage2'`` places the
|
|
15
|
+
reference traces of the Stage 2 wavelength file (NIRISS/SOSS) with a
|
|
16
|
+
fitted polynomial correction of degree ``ref_order``.
|
|
17
|
+
``background``
|
|
18
|
+
``method`` ('median', 'poly' or 'none'), ``inner`` / ``outer`` distance
|
|
19
|
+
from the traces, ``nsigma``, ``poly_order``, ``min_pixels``.
|
|
20
|
+
``psf``
|
|
21
|
+
``halfwidth`` of the profile window, ``oversample`` (knots per pixel
|
|
22
|
+
across the trace), ``knot_col`` (knots along the dispersion; 0 = 1D
|
|
23
|
+
profile), ``smooth`` ('cv', a number, a pair, or 0 for none), ``clip``
|
|
24
|
+
(outlier rejection, in sigma), ``init_ints`` (integrations of the
|
|
25
|
+
first, in-memory fit), ``ints`` ('all' or a number of integrations
|
|
26
|
+
spread over the exposure), ``second_pass``, ``pixel_correction`` (fit a
|
|
27
|
+
static response factor per detector pixel, see
|
|
28
|
+
:meth:`SpectralExtractor.fit_psf`), ``cv`` (cross-validation folds for
|
|
29
|
+
``smooth='cv'``: 'phase' = sub-pixel phase of the offset from the trace,
|
|
30
|
+
'ints' = even/odd integrations, 'columns' = column blocks).
|
|
31
|
+
``extract``
|
|
32
|
+
``method`` ('optimal', 'box' or 'covariance'), ``aperture`` (half-width),
|
|
33
|
+
``variance`` ('mean' = time-averaged per-pixel variance, or 'data'),
|
|
34
|
+
``reject`` (sigma), ``reject_iters`` and ``reject_mode`` ('temporal':
|
|
35
|
+
a pixel is rejected only if its residual deviates from its median
|
|
36
|
+
residual over the chunk, so static model misfit never causes rejections;
|
|
37
|
+
'model': deviation from the model itself, as in Horne 1986),
|
|
38
|
+
``fit_background``,
|
|
39
|
+
``refine_shift`` (Gauss-Newton iterations of the trace offset),
|
|
40
|
+
``joint_iters`` (alternating iterations of the joint multi-order
|
|
41
|
+
extraction, ``method='joint'``), ``max_maskfrac`` (columns of an
|
|
42
|
+
integration with a larger fraction of the profile on masked pixels get
|
|
43
|
+
NaN: the remaining pixels hold too little of the light, e.g. under a
|
|
44
|
+
masked field-star contaminant), ``min_groups`` (columns in which any
|
|
45
|
+
pixel of the aperture keeps fewer usable groups before saturation are
|
|
46
|
+
set to NaN in all integrations; needs the Stage 1 saturation map. The
|
|
47
|
+
default 2 removes columns whose core keeps only group 0: on WASP-39 b
|
|
48
|
+
PRISM their flux came from the wings and their transit depth was
|
|
49
|
+
biased deep by ~7000 ppm, see ``docs/extraction_notes.md`` §5.14),
|
|
50
|
+
``saturated_box`` (columns in which any aperture pixel saturates use the
|
|
51
|
+
box sum instead of the optimal estimate: optimal weights follow the
|
|
52
|
+
number of groups and pick up the in-transit PSF-shape change; needs the
|
|
53
|
+
Stage 1 saturation map).
|
|
54
|
+
|
|
55
|
+
The values below were chosen from one-segment tests (see
|
|
56
|
+
``docs/extraction_notes.md``).
|
|
57
|
+
"""
|
|
58
|
+
import copy
|
|
59
|
+
|
|
60
|
+
__all__ = ['DEFAULTS', 'MODE_DEFAULTS', 'get_defaults']
|
|
61
|
+
|
|
62
|
+
DEFAULTS = {
|
|
63
|
+
'columns': dict(columns=None, wavelength_range=None, snr_min=5., min_good_frac=0.5, gap=5),
|
|
64
|
+
'trace': dict(halfwidth=None, nknots=8, clip=3., reference=None, ref_order=1),
|
|
65
|
+
'background': dict(method='median', inner=8., outer=None, nsigma=3., maxiters=3,
|
|
66
|
+
poly_order=1, min_pixels=4),
|
|
67
|
+
'psf': dict(halfwidth=7., oversample=2., knot_col=20, smooth='cv', clip=6.,
|
|
68
|
+
init_ints=200, ints='all', second_pass=False, pixel_correction=True, cv='phase'),
|
|
69
|
+
'extract': dict(method='optimal', aperture=5., variance='mean', reject=6., reject_iters=3,
|
|
70
|
+
reject_mode='temporal', fit_background=False, refine_shift=2, joint_iters=3,
|
|
71
|
+
max_maskfrac=0.98, min_groups=2, cov_scale=1., saturated_box=False),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
MODE_DEFAULTS = {
|
|
75
|
+
'NIRSpec': {'orders': [1]},
|
|
76
|
+
'NIRSpec/PRISM': {'orders': [1]},
|
|
77
|
+
'NIRISS/SOSS': {
|
|
78
|
+
'orders': [1, 2],
|
|
79
|
+
'trace': dict(halfwidth=12., reference='stage2', ref_order=1),
|
|
80
|
+
'background': dict(method='poly', inner=20., min_pixels=10),
|
|
81
|
+
'psf': dict(halfwidth=16., oversample=2., knot_col=100),
|
|
82
|
+
'extract': dict(aperture=15., method='joint'),
|
|
83
|
+
},
|
|
84
|
+
'NIRCam': {
|
|
85
|
+
'orders': [1],
|
|
86
|
+
'background': dict(inner=12., min_pixels=10),
|
|
87
|
+
'psf': dict(halfwidth=9., oversample=2., knot_col=20),
|
|
88
|
+
# 1/f row offsets are shared by the ~500 columns of an amplifier: weights for
|
|
89
|
+
# sums of columns (C = V + 500 K) lowered the white-light scatter by ~10% on
|
|
90
|
+
# HD 207496 F444W / F322W2 (extraction_notes.md, 5.13)
|
|
91
|
+
'extract': dict(aperture=6., method='covariance', cov_scale=500.),
|
|
92
|
+
},
|
|
93
|
+
'MIRI/LRS': {
|
|
94
|
+
'orders': [1],
|
|
95
|
+
# below ~4.5 um the prism dispersion folds back (several wavelengths on the
|
|
96
|
+
# same pixels) and 5-6 um receives some shorter-wavelength light; beyond
|
|
97
|
+
# ~12 um the throughput is low and the background dominates
|
|
98
|
+
'columns': dict(wavelength_range=(5., 12.)),
|
|
99
|
+
'background': dict(inner=10., min_pixels=10),
|
|
100
|
+
'psf': dict(halfwidth=7., oversample=2., knot_col=15),
|
|
101
|
+
'extract': dict(aperture=5.),
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def get_defaults(mode):
|
|
107
|
+
"""Merged default parameters (and orders) for a jstark mode name."""
|
|
108
|
+
p = copy.deepcopy(DEFAULTS)
|
|
109
|
+
orders = [1]
|
|
110
|
+
for key in (mode.split('/')[0], mode):
|
|
111
|
+
over = MODE_DEFAULTS.get(key, {})
|
|
112
|
+
orders = over.get('orders', orders)
|
|
113
|
+
for step, kw in over.items():
|
|
114
|
+
if step != 'orders':
|
|
115
|
+
p[step].update(kw)
|
|
116
|
+
return p, list(orders)
|