SingleLensFitter 0.1.0__tar.gz → 0.1.2__tar.gz
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.
- {singlelensfitter-0.1.0/src/SingleLensFitter.egg-info → singlelensfitter-0.1.2}/PKG-INFO +2 -2
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/README.md +1 -1
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/docs/api.md +31 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/pyproject.toml +1 -1
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/setup.py +3 -8
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2/src/SingleLensFitter.egg-info}/PKG-INFO +2 -2
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/SingleLensFitter.egg-info/SOURCES.txt +5 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/_parallax_cpp.cpp +52 -12
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/auto_init.py +26 -9
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/fitters.py +36 -18
- singlelensfitter-0.1.2/src/singlelensfitter/host_fitting.py +481 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/parallax_backend.py +67 -36
- singlelensfitter-0.1.2/src/singlelensfitter/prepared.py +157 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/singlelens_fit.py +2 -0
- singlelensfitter-0.1.2/src/singlelensfitter/vbm_library.cpp +3 -0
- singlelensfitter-0.1.2/tests/test_host_fitting.py +36 -0
- singlelensfitter-0.1.2/tests/test_prepared.py +99 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/LICENSE +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/MANIFEST.in +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/THIRD_PARTY.md +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/licenses/GPL-3.0.txt +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/licenses/VBMicrolensing-LGPL-3.0.txt +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/setup.cfg +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/SingleLensFitter.egg-info/dependency_links.txt +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/SingleLensFitter.egg-info/requires.txt +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/SingleLensFitter.egg-info/top_level.txt +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/__init__.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/_vbm_cpp.cpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/chebyshev_patch.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/ephemeris.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/linear_limb_darkening.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/mu_quadrature.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/mu_table_data.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/parallax_trajectory.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/sky_projection.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/source_limb.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/uniform_limb_table.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/uniform_table_data.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/cpp/vbm_magnification.hpp +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/data/__init__.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/data/earth_orbital_parallax_table.txt +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/fspl_initialization.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/init_config.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/jax_fspl.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/magnification.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/objective.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/parallax.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/photometry.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/pspl_fft.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/signal_scale.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/singlelens_model.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/src/singlelensfitter/trajectory.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/limb_reference.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_auto_init.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_cpp_extension.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_linear_limb_darkening.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_native_parallax_backend.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_photometry.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_pspl_fft.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_rmdc.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_signal_scale_gap.py +0 -0
- {singlelensfitter-0.1.0 → singlelensfitter-0.1.2}/tests/test_space_parallax.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SingleLensFitter
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Single-lens (PSPL / FSPL / annual+space parallax) light-curve fitting
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Repository, https://github.com/NunotaKansuke/SingleLensFitter
|
|
@@ -32,7 +32,7 @@ Requires Python 3.10+ and a C++17 compiler with OpenMP. On macOS, set
|
|
|
32
32
|
`SINGLELENSFITTER_DISABLE_OPENMP=1` to build with Apple Clang.
|
|
33
33
|
|
|
34
34
|
```sh
|
|
35
|
-
python -m pip install
|
|
35
|
+
python -m pip install SingleLensFitter
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
## Fit a light curve
|
|
@@ -11,7 +11,7 @@ Requires Python 3.10+ and a C++17 compiler with OpenMP. On macOS, set
|
|
|
11
11
|
`SINGLELENSFITTER_DISABLE_OPENMP=1` to build with Apple Clang.
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
|
-
python -m pip install
|
|
14
|
+
python -m pip install SingleLensFitter
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
## Fit a light curve
|
|
@@ -69,3 +69,34 @@ time_spec=spec)`. Use ICRF/J2000 Cartesian positions shaped (N, 3) in AU
|
|
|
69
69
|
and set `spec` to the ephemeris time convention. The ephemeris must cover
|
|
70
70
|
the requested epochs. Parallax results include optimizer and ephemeris
|
|
71
71
|
information in `fit.diagnostics`.
|
|
72
|
+
|
|
73
|
+
### Repeated fits with different masks
|
|
74
|
+
|
|
75
|
+
Prepare the observations once with any of the four fitters. A boolean mask has
|
|
76
|
+
one entry per observation; `True` excludes that point from the optimization,
|
|
77
|
+
the source/blend flux solution, and chi-square. Predictions and residuals in the
|
|
78
|
+
result cover all original epochs.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
curve = fitter.prepare(time, flux, ferr)
|
|
82
|
+
first = curve.fit()
|
|
83
|
+
refit = curve.fit(mask=excluded, start=first)
|
|
84
|
+
chi2, grad = curve.chi2_and_grad(refit.params, mask=another_mask)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Omitting `start` runs automatic initialization on the retained points.
|
|
88
|
+
A result or a vector in physical `result.params` order starts a local fit.
|
|
89
|
+
The gradient uses the same physical coordinates (including physical `tE`,
|
|
90
|
+
`rho`, and the fitted limb-darkening coefficient). Scoring a new mask at
|
|
91
|
+
the same parameters reuses the magnification calculation. Parallax fits
|
|
92
|
+
also reuse the projection at every epoch. A prepared curve owns its input
|
|
93
|
+
arrays and mutable evaluation cache; use one instance per concurrent worker.
|
|
94
|
+
|
|
95
|
+
### Host fitting for jacscanomaly
|
|
96
|
+
|
|
97
|
+
`singlelensfitter.host_fitting` provides the PSPL, FSPL and parallax fitters
|
|
98
|
+
used by jacscanomaly. They share the standalone numerical kernels and retain
|
|
99
|
+
the host search's seed order and lowest-χ² selection. Prepare each light curve
|
|
100
|
+
once. Use `curve.fit(mask=excluded)` for a fresh search on the kept samples
|
|
101
|
+
and `curve.fit(mask=excluded, start=fit.params)` to continue a previous fit.
|
|
102
|
+
The latter skips template search. Returned models cover all epochs.
|
|
@@ -67,15 +67,10 @@ class build_ext(_build_ext):
|
|
|
67
67
|
"native FSPL/parallax backend. Install VBMicrolensing>=5.5 or set "
|
|
68
68
|
"VBMICROLENSING_SOURCE_DIR."
|
|
69
69
|
)
|
|
70
|
-
|
|
70
|
+
_, vbm_include = vbm_sources
|
|
71
71
|
compile_args, link_args = _compile_and_link_args()
|
|
72
72
|
for ext in self.extensions:
|
|
73
73
|
if ext.name in {"singlelensfitter._vbm_cpp", "singlelensfitter._parallax_cpp"}:
|
|
74
|
-
# Keep the external VBM source out of ``Extension.sources``
|
|
75
|
-
# until wheel compilation. ``sdist`` otherwise tries to copy
|
|
76
|
-
# a path outside this repository. The isolated build
|
|
77
|
-
# environment installs VBMicrolensing from pyproject.toml.
|
|
78
|
-
ext.sources = list(ext.sources or []) + [str(vbm_library)]
|
|
79
74
|
ext.include_dirs = list(ext.include_dirs or []) + [str(vbm_include)]
|
|
80
75
|
if ext.name == "singlelensfitter._vbm_cpp" and os.environ.get("SINGLELENSFITTER_XLA_INCLUDE"):
|
|
81
76
|
ext.include_dirs.append(os.environ["SINGLELENSFITTER_XLA_INCLUDE"])
|
|
@@ -107,7 +102,7 @@ extensions = []
|
|
|
107
102
|
extensions.append(
|
|
108
103
|
Extension(
|
|
109
104
|
"singlelensfitter._vbm_cpp",
|
|
110
|
-
[str(VBM_CPP_SOURCE)],
|
|
105
|
+
[str(VBM_CPP_SOURCE), "src/singlelensfitter/vbm_library.cpp"],
|
|
111
106
|
include_dirs=[np.get_include(), str(VBM_CPP_SOURCE.parent)],
|
|
112
107
|
language="c++",
|
|
113
108
|
depends=[str(VBM_CPP_SOURCE), *magnification_headers],
|
|
@@ -116,7 +111,7 @@ extensions.append(
|
|
|
116
111
|
extensions.append(
|
|
117
112
|
Extension(
|
|
118
113
|
"singlelensfitter._parallax_cpp",
|
|
119
|
-
[str(PARALLAX_CPP_SOURCE)],
|
|
114
|
+
[str(PARALLAX_CPP_SOURCE), "src/singlelensfitter/vbm_library.cpp"],
|
|
120
115
|
include_dirs=[np.get_include(), str(PARALLAX_CPP_SOURCE.parent)],
|
|
121
116
|
language="c++",
|
|
122
117
|
depends=[
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: SingleLensFitter
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Single-lens (PSPL / FSPL / annual+space parallax) light-curve fitting
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Project-URL: Repository, https://github.com/NunotaKansuke/SingleLensFitter
|
|
@@ -32,7 +32,7 @@ Requires Python 3.10+ and a C++17 compiler with OpenMP. On macOS, set
|
|
|
32
32
|
`SINGLELENSFITTER_DISABLE_OPENMP=1` to build with Apple Clang.
|
|
33
33
|
|
|
34
34
|
```sh
|
|
35
|
-
python -m pip install
|
|
35
|
+
python -m pip install SingleLensFitter
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
## Fit a light curve
|
|
@@ -18,6 +18,7 @@ src/singlelensfitter/_vbm_cpp.cpp
|
|
|
18
18
|
src/singlelensfitter/auto_init.py
|
|
19
19
|
src/singlelensfitter/fitters.py
|
|
20
20
|
src/singlelensfitter/fspl_initialization.py
|
|
21
|
+
src/singlelensfitter/host_fitting.py
|
|
21
22
|
src/singlelensfitter/init_config.py
|
|
22
23
|
src/singlelensfitter/jax_fspl.py
|
|
23
24
|
src/singlelensfitter/magnification.py
|
|
@@ -25,11 +26,13 @@ src/singlelensfitter/objective.py
|
|
|
25
26
|
src/singlelensfitter/parallax.py
|
|
26
27
|
src/singlelensfitter/parallax_backend.py
|
|
27
28
|
src/singlelensfitter/photometry.py
|
|
29
|
+
src/singlelensfitter/prepared.py
|
|
28
30
|
src/singlelensfitter/pspl_fft.py
|
|
29
31
|
src/singlelensfitter/signal_scale.py
|
|
30
32
|
src/singlelensfitter/singlelens_fit.py
|
|
31
33
|
src/singlelensfitter/singlelens_model.py
|
|
32
34
|
src/singlelensfitter/trajectory.py
|
|
35
|
+
src/singlelensfitter/vbm_library.cpp
|
|
33
36
|
src/singlelensfitter/cpp/chebyshev_patch.hpp
|
|
34
37
|
src/singlelensfitter/cpp/ephemeris.hpp
|
|
35
38
|
src/singlelensfitter/cpp/linear_limb_darkening.hpp
|
|
@@ -46,9 +49,11 @@ src/singlelensfitter/data/earth_orbital_parallax_table.txt
|
|
|
46
49
|
tests/limb_reference.py
|
|
47
50
|
tests/test_auto_init.py
|
|
48
51
|
tests/test_cpp_extension.py
|
|
52
|
+
tests/test_host_fitting.py
|
|
49
53
|
tests/test_linear_limb_darkening.py
|
|
50
54
|
tests/test_native_parallax_backend.py
|
|
51
55
|
tests/test_photometry.py
|
|
56
|
+
tests/test_prepared.py
|
|
52
57
|
tests/test_pspl_fft.py
|
|
53
58
|
tests/test_rmdc.py
|
|
54
59
|
tests/test_signal_scale_gap.py
|
|
@@ -452,6 +452,22 @@ PyObject* evaluator_jacobian_fd(EvaluatorObject* self, PyObject* raw_obj, PyObje
|
|
|
452
452
|
return reinterpret_cast<PyObject*>(out);
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
+
inline void magnification_partials(const EvaluatorData& data, size_t i, double tE,
|
|
456
|
+
double rho, double piEN, double piEE,
|
|
457
|
+
double peak_slope, double* out) {
|
|
458
|
+
const auto& geometry = data.geometry[i];
|
|
459
|
+
const double tau = data.tau[i], beta = data.beta[i];
|
|
460
|
+
const double slope = data.separation[i] > 0. ? data.d_du[i] / data.separation[i] : 0.;
|
|
461
|
+
const double along = slope * tau, across = slope * beta;
|
|
462
|
+
const size_t pi_index = data.finite_source ? 4 : 3;
|
|
463
|
+
out[0] = -along * peak_slope / tE;
|
|
464
|
+
out[1] = -along * (tau + piEN * geometry.d_n + piEE * geometry.d_e);
|
|
465
|
+
out[2] = across;
|
|
466
|
+
if (data.finite_source) out[3] = data.d_drho[i] * rho;
|
|
467
|
+
out[pi_index] = -along * geometry.d_n - across * geometry.d_e;
|
|
468
|
+
out[pi_index + 1] = -along * geometry.d_e + across * geometry.d_n;
|
|
469
|
+
}
|
|
470
|
+
|
|
455
471
|
// Analytic Jacobian of the profiled residual.
|
|
456
472
|
//
|
|
457
473
|
// The magnification depends on the parameters only through u(t) (and rho), so
|
|
@@ -467,7 +483,6 @@ bool analytic_jacobian(EvaluatorObject* self, const std::vector<double>& raw, Py
|
|
|
467
483
|
unpack_raw(self, raw, t0, tE, u0, rho, piEN, piEE);
|
|
468
484
|
const size_t n = self->data.time.size();
|
|
469
485
|
const size_t np = static_cast<size_t>(self->data.nparam);
|
|
470
|
-
const size_t pi_index = self->data.finite_source ? 4 : 3;
|
|
471
486
|
std::vector<unsigned char> mask;
|
|
472
487
|
if (!active_mask(mask_obj, n, mask)) return false;
|
|
473
488
|
const double peak_slope = self->data.trajectory->source_peak_time_derivative(t0);
|
|
@@ -475,17 +490,7 @@ bool analytic_jacobian(EvaluatorObject* self, const std::vector<double>& raw, Py
|
|
|
475
490
|
const auto& mags=self->data.cached_magnifications;
|
|
476
491
|
prepare_profile(self->data,mask);
|
|
477
492
|
for (size_t i=0; i<n; ++i) {
|
|
478
|
-
|
|
479
|
-
const double tau=self->data.tau[i], beta=self->data.beta[i], u=self->data.separation[i];
|
|
480
|
-
const double d_n=self->data.geometry[i].d_n, d_e=self->data.geometry[i].d_e;
|
|
481
|
-
const double tau_rect = tau + piEN*d_n + piEE*d_e;
|
|
482
|
-
// d(tau, beta)/d(t0, ln tE, u0, piEN, piEE)
|
|
483
|
-
const double dtau[5] = {-peak_slope / tE, -tau_rect, 0.0, -d_n, -d_e};
|
|
484
|
-
const double dbeta[5] = {0.0, 0.0, 1.0, -d_e, d_n};
|
|
485
|
-
double* out = &jac[i * np];
|
|
486
|
-
const int map[5] = {0, 1, 2, static_cast<int>(pi_index), static_cast<int>(pi_index) + 1};
|
|
487
|
-
for (int j = 0; j < 5; ++j) out[map[j]] = a_u * (tau * dtau[j] + beta * dbeta[j]) / u;
|
|
488
|
-
if (self->data.finite_source) out[3] = a_rho * rho;
|
|
493
|
+
magnification_partials(self->data, i, tE, rho, piEN, piEE, peak_slope, &jac[i*np]);
|
|
489
494
|
}
|
|
490
495
|
const auto& groups=self->data.groups;
|
|
491
496
|
const size_t ng=groups.size();
|
|
@@ -559,7 +564,42 @@ PyObject* evaluator_residual_and_jacobian(EvaluatorObject* self, PyObject* args,
|
|
|
559
564
|
return Py_BuildValue("NN",make_array(residual),jac);
|
|
560
565
|
}
|
|
561
566
|
|
|
567
|
+
PyObject* evaluator_chi2_and_grad(EvaluatorObject* self, PyObject* args) {
|
|
568
|
+
PyObject *params, *active = Py_None;
|
|
569
|
+
if (!PyArg_ParseTuple(args, "O|O", ¶ms, &active)) return nullptr;
|
|
570
|
+
auto& data = self->data;
|
|
571
|
+
std::vector<double> raw;
|
|
572
|
+
std::vector<unsigned char> mask;
|
|
573
|
+
if (!parse_raw(self, params, raw) || !active_mask(active, data.time.size(), mask))
|
|
574
|
+
return nullptr;
|
|
575
|
+
try {
|
|
576
|
+
prepare_curve(self, raw);
|
|
577
|
+
prepare_profile(data, mask);
|
|
578
|
+
double t0, tE, u0, rho, north, east;
|
|
579
|
+
unpack_raw(self, raw, t0, tE, u0, rho, north, east);
|
|
580
|
+
const double peak_slope = data.trajectory->source_peak_time_derivative(t0);
|
|
581
|
+
std::vector<double> grad(data.nparam, 0.);
|
|
582
|
+
double chi2 = 0.;
|
|
583
|
+
for (size_t i = 0; i < mask.size(); ++i) {
|
|
584
|
+
if (!mask[i]) continue;
|
|
585
|
+
const auto& group = data.groups[data.dataset[i]];
|
|
586
|
+
const double residual = data.flux[i] - (group.fs * data.cached_magnifications[i] + group.fb);
|
|
587
|
+
const double weighted = data.weights[i] * residual;
|
|
588
|
+
chi2 += residual * weighted;
|
|
589
|
+
double partials[6];
|
|
590
|
+
magnification_partials(data, i, tE, rho, north, east, peak_slope, partials);
|
|
591
|
+
const double factor = -2. * group.fs * weighted;
|
|
592
|
+
for (size_t j = 0; j < grad.size(); ++j) grad[j] += factor * partials[j];
|
|
593
|
+
}
|
|
594
|
+
return Py_BuildValue("dN", chi2, make_array(grad));
|
|
595
|
+
} catch (const std::exception& exc) {
|
|
596
|
+
PyErr_SetString(PyExc_ValueError, exc.what());
|
|
597
|
+
return nullptr;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
562
601
|
PyMethodDef evaluator_methods[] = {
|
|
602
|
+
{"chi2_and_grad", reinterpret_cast<PyCFunction>(evaluator_chi2_and_grad), METH_VARARGS, "Profiled chi-square and gradient in native coordinates."},
|
|
563
603
|
{"trajectory", reinterpret_cast<PyCFunction>(evaluator_trajectory), METH_VARARGS|METH_KEYWORDS, "Evaluate tau/beta/u and optional trajectory components."},
|
|
564
604
|
{"magnification", reinterpret_cast<PyCFunction>(evaluator_magnification), METH_VARARGS|METH_KEYWORDS, "Evaluate compiled finite-source magnification."},
|
|
565
605
|
{"evaluate", reinterpret_cast<PyCFunction>(evaluator_evaluate), METH_VARARGS|METH_KEYWORDS, "Evaluate profiled model flux."},
|
|
@@ -10,6 +10,7 @@ hint. The standalone FSPL fitter, the parallax fitters and ``Finder`` all call
|
|
|
10
10
|
|
|
11
11
|
from __future__ import annotations
|
|
12
12
|
|
|
13
|
+
import hashlib
|
|
13
14
|
from dataclasses import replace
|
|
14
15
|
from types import SimpleNamespace
|
|
15
16
|
from typing import Optional, Sequence
|
|
@@ -31,6 +32,7 @@ SEED_MAX = 24
|
|
|
31
32
|
T0_HINT_TE_COEFF = 1.0
|
|
32
33
|
T0_HINT_SPAN_FRACTION = 0.05
|
|
33
34
|
T0_HINT_PEAKS_PER_TEMPLATE = 8
|
|
35
|
+
_last_search = None
|
|
34
36
|
|
|
35
37
|
|
|
36
38
|
|
|
@@ -145,15 +147,30 @@ def pspl_initial_guesses(
|
|
|
145
147
|
# brightening, so keep several local peaks per template to find the hinted one.
|
|
146
148
|
peaks = T0_HINT_PEAKS_PER_TEMPLATE if t0_hint is not None else 1
|
|
147
149
|
candidate_pool_size = max(1, n_u0 * n_tE * peaks, int(cfg.auto_init_fft_top_k))
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
# PSPL and parallax frequently use the same bank on the same observations.
|
|
151
|
+
# Compare the complete inputs, including changes to a mask or an array in place.
|
|
152
|
+
digest = hashlib.blake2b(digest_size=16)
|
|
153
|
+
for values in (time, flux_np, ferr_np):
|
|
154
|
+
digest.update(np.ascontiguousarray(values))
|
|
155
|
+
key = (time.shape, flux_np.shape, ferr_np.shape, digest.digest(),
|
|
156
|
+
u0_grid.tobytes(), tE_grid.tobytes(), candidate_pool_size, peaks,
|
|
157
|
+
cfg.auto_init_fft_grid_dt, cfg.auto_init_fft_max_grid_points,
|
|
158
|
+
cfg.auto_init_fft_workers)
|
|
159
|
+
global _last_search
|
|
160
|
+
cached = _last_search
|
|
161
|
+
if cached is not None and cached[0] == key:
|
|
162
|
+
search = cached[1]
|
|
163
|
+
else:
|
|
164
|
+
search = scanner.search_tE(
|
|
165
|
+
time,
|
|
166
|
+
flux_np,
|
|
167
|
+
ferr_np,
|
|
168
|
+
u0_grid=u0_grid,
|
|
169
|
+
tE_grid=tE_grid,
|
|
170
|
+
top_k=candidate_pool_size,
|
|
171
|
+
peaks_per_template=peaks,
|
|
172
|
+
)
|
|
173
|
+
_last_search = (key, search)
|
|
157
174
|
|
|
158
175
|
candidates = filter_pspl_fft_candidates(
|
|
159
176
|
search.candidates,
|
|
@@ -69,7 +69,7 @@ class _FluxProfile:
|
|
|
69
69
|
"""Weighted two-column regression with fixed observations during a fit."""
|
|
70
70
|
|
|
71
71
|
def __init__(self, flux, ferr):
|
|
72
|
-
self.errors = np.
|
|
72
|
+
self.errors = np.asarray(ferr, dtype=float)
|
|
73
73
|
self.weights = 1.0 / (self.errors * self.errors)
|
|
74
74
|
self.weight_sum = np.sum(self.weights)
|
|
75
75
|
self.y_mean = np.sum(self.weights * flux) / self.weight_sum
|
|
@@ -301,6 +301,11 @@ class PSPLFitter:
|
|
|
301
301
|
self.plotter = _make_plotter()
|
|
302
302
|
self._last_fit: Optional[SingleLensFitResult] = None
|
|
303
303
|
|
|
304
|
+
def prepare(self, time, flux, ferr):
|
|
305
|
+
"""Prepare one light curve for repeated masked fits and objective evaluations."""
|
|
306
|
+
from .prepared import PreparedCurve
|
|
307
|
+
return PreparedCurve(self, time, flux, ferr)
|
|
308
|
+
|
|
304
309
|
@property
|
|
305
310
|
def parameter_dimension(self) -> int:
|
|
306
311
|
return 3
|
|
@@ -327,6 +332,7 @@ class PSPLFitter:
|
|
|
327
332
|
ferr: np.ndarray,
|
|
328
333
|
q: np.ndarray,
|
|
329
334
|
result,
|
|
335
|
+
numpy_result=False,
|
|
330
336
|
) -> SingleLensFitResult:
|
|
331
337
|
magnification = self._magnification(time, q)
|
|
332
338
|
fs, fb = _solve_fs_fb_numpy(magnification, flux, ferr)
|
|
@@ -336,18 +342,19 @@ class PSPLFitter:
|
|
|
336
342
|
residual = flux - model
|
|
337
343
|
normalized = residual / ferr
|
|
338
344
|
chi2 = float(np.dot(normalized, normalized))
|
|
345
|
+
asarray = np.asarray if numpy_result else jnp.asarray
|
|
339
346
|
fit = SingleLensFitResult(
|
|
340
347
|
time=time,
|
|
341
348
|
flux=flux,
|
|
342
349
|
ferr=ferr,
|
|
343
|
-
params=
|
|
350
|
+
params=asarray(q),
|
|
344
351
|
param_names=("t0", "tE", "u0"),
|
|
345
|
-
chi2=
|
|
346
|
-
chi2_dof=
|
|
347
|
-
fs=
|
|
348
|
-
fb=
|
|
349
|
-
model_flux=
|
|
350
|
-
residual=
|
|
352
|
+
chi2=asarray(chi2),
|
|
353
|
+
chi2_dof=asarray(chi2 / max(time.size - 3, 1)),
|
|
354
|
+
fs=asarray(fs),
|
|
355
|
+
fb=asarray(fb),
|
|
356
|
+
model_flux=asarray(model),
|
|
357
|
+
residual=asarray(residual),
|
|
351
358
|
model_kind="pspl",
|
|
352
359
|
optimizer_success=bool(result.success),
|
|
353
360
|
optimizer_status=_optimizer_status(result, maxiter=self.maxiter),
|
|
@@ -371,6 +378,9 @@ class PSPLFitter:
|
|
|
371
378
|
)
|
|
372
379
|
self._last_fit = best_fit
|
|
373
380
|
return best_fit
|
|
381
|
+
return self._fit_from_seed(time_np, flux_np, ferr_np, p0)
|
|
382
|
+
|
|
383
|
+
def _fit_from_seed(self, time_np, flux_np, ferr_np, p0, *, numpy_result=False):
|
|
374
384
|
q0 = np.asarray(p0, dtype=float).reshape(-1)
|
|
375
385
|
if q0.size != 3:
|
|
376
386
|
raise ValueError("PSPL p0 must contain (t0, tE, u0).")
|
|
@@ -425,7 +435,7 @@ class PSPLFitter:
|
|
|
425
435
|
)
|
|
426
436
|
q = self._decode(np.asarray(result.x, dtype=float))
|
|
427
437
|
q[0] += time_origin
|
|
428
|
-
return self._make_fit(time_np, flux_np, ferr_np, q, result)
|
|
438
|
+
return self._make_fit(time_np, flux_np, ferr_np, q, result, numpy_result=numpy_result)
|
|
429
439
|
|
|
430
440
|
def evaluate_fixed(self, time, flux, ferr, p0) -> SingleLensFitResult:
|
|
431
441
|
time_np, flux_np, ferr_np = _fit_arrays(time, flux, ferr)
|
|
@@ -485,6 +495,11 @@ class FSPLFitter:
|
|
|
485
495
|
if self.espl_table_path is None:
|
|
486
496
|
self.espl_table_path = default_espl_table_path()
|
|
487
497
|
|
|
498
|
+
def prepare(self, time, flux, ferr):
|
|
499
|
+
"""Prepare one light curve for repeated masked fits and objective evaluations."""
|
|
500
|
+
from .prepared import PreparedCurve
|
|
501
|
+
return PreparedCurve(self, time, flux, ferr)
|
|
502
|
+
|
|
488
503
|
@property
|
|
489
504
|
def parameter_dimension(self) -> int:
|
|
490
505
|
return 5 if self.fit_limb_darkening else 4
|
|
@@ -543,6 +558,7 @@ class FSPLFitter:
|
|
|
543
558
|
ferr: np.ndarray,
|
|
544
559
|
q: np.ndarray,
|
|
545
560
|
result,
|
|
561
|
+
numpy_result=False,
|
|
546
562
|
) -> SingleLensFitResult:
|
|
547
563
|
magnification = self._magnification(time, q)
|
|
548
564
|
fs, fb = _solve_fs_fb_numpy(magnification, flux, ferr)
|
|
@@ -556,20 +572,21 @@ class FSPLFitter:
|
|
|
556
572
|
params[3] = _espl_radius(q[3])
|
|
557
573
|
a = q[4] if q.size == 5 else self.linear_limb_darkening
|
|
558
574
|
evaluation_seed = np.r_[q[:4], a]
|
|
575
|
+
asarray = np.asarray if numpy_result else jnp.asarray
|
|
559
576
|
fit = SingleLensFitResult(
|
|
560
577
|
time=time,
|
|
561
578
|
flux=flux,
|
|
562
579
|
ferr=ferr,
|
|
563
|
-
params=
|
|
580
|
+
params=asarray(params),
|
|
564
581
|
param_names=("t0", "tE", "u0", "rho") + (("a",) if q.size == 5 else ()),
|
|
565
582
|
linear_limb_darkening=float(a),
|
|
566
|
-
chi2=
|
|
567
|
-
chi2_dof=
|
|
568
|
-
fs=
|
|
569
|
-
fb=
|
|
570
|
-
model_flux=
|
|
571
|
-
residual=
|
|
572
|
-
raw_params=
|
|
583
|
+
chi2=asarray(chi2),
|
|
584
|
+
chi2_dof=asarray(chi2 / max(time.size - q.size, 1)),
|
|
585
|
+
fs=asarray(fs),
|
|
586
|
+
fb=asarray(fb),
|
|
587
|
+
model_flux=asarray(model),
|
|
588
|
+
residual=asarray(residual),
|
|
589
|
+
raw_params=asarray(q),
|
|
573
590
|
model_kind="fspl",
|
|
574
591
|
model_evaluator=lambda values, q=evaluation_seed: self._magnification(
|
|
575
592
|
np.asarray(values, dtype=float), q
|
|
@@ -595,6 +612,7 @@ class FSPLFitter:
|
|
|
595
612
|
flux_np: np.ndarray,
|
|
596
613
|
ferr_np: np.ndarray,
|
|
597
614
|
p0,
|
|
615
|
+
*, numpy_result=False,
|
|
598
616
|
) -> SingleLensFitResult:
|
|
599
617
|
q0 = self._seed(p0)
|
|
600
618
|
|
|
@@ -707,7 +725,7 @@ class FSPLFitter:
|
|
|
707
725
|
retry_count = 1
|
|
708
726
|
if retry_count:
|
|
709
727
|
result.message = f"{result.message}; basin_retry={retry_count}"
|
|
710
|
-
return self._make_fit(time_np, flux_np, ferr_np, q, result)
|
|
728
|
+
return self._make_fit(time_np, flux_np, ferr_np, q, result, numpy_result=numpy_result)
|
|
711
729
|
|
|
712
730
|
def _automatic_initial_guesses(
|
|
713
731
|
self,
|