reflection-method 0.3.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.
- reflection_method/__init__.py +47 -0
- reflection_method/cli.py +210 -0
- reflection_method/core.py +820 -0
- reflection_method/io.py +221 -0
- reflection_method/plot.py +459 -0
- reflection_method/result.py +140 -0
- reflection_method-0.3.0.dist-info/METADATA +370 -0
- reflection_method-0.3.0.dist-info/RECORD +12 -0
- reflection_method-0.3.0.dist-info/WHEEL +5 -0
- reflection_method-0.3.0.dist-info/entry_points.txt +2 -0
- reflection_method-0.3.0.dist-info/licenses/LICENSE +674 -0
- reflection_method-0.3.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""reflection-method: Eclipsing binary minimum finder using the reflection method.
|
|
2
|
+
|
|
3
|
+
This library provides a functional API for finding the minimum of an
|
|
4
|
+
eclipsing star's light curve using the reflection method (symmetry about
|
|
5
|
+
the eclipse minimum).
|
|
6
|
+
|
|
7
|
+
All functions operate on linear time units (JD, HJD, MJD, phase, minutes, etc.).
|
|
8
|
+
The library performs no I/O or time conversion.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from .core import (
|
|
12
|
+
fit_spline,
|
|
13
|
+
spline_variance,
|
|
14
|
+
combine,
|
|
15
|
+
find_x0,
|
|
16
|
+
refine_x0_minimum,
|
|
17
|
+
prepare_plot_data,
|
|
18
|
+
bootstrap_x0,
|
|
19
|
+
find_minimum,
|
|
20
|
+
)
|
|
21
|
+
from .result import MinimumResult
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
from .plot import plot_original_spline, plot_scan, plot_all
|
|
25
|
+
except ImportError:
|
|
26
|
+
plot_original_spline = plot_scan = plot_all = None
|
|
27
|
+
|
|
28
|
+
__version__ = "0.3.0"
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"MinimumResult",
|
|
32
|
+
"fit_spline",
|
|
33
|
+
"spline_variance",
|
|
34
|
+
"combine",
|
|
35
|
+
"find_x0",
|
|
36
|
+
"refine_x0_minimum",
|
|
37
|
+
"prepare_plot_data",
|
|
38
|
+
"bootstrap_x0",
|
|
39
|
+
"find_minimum",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
if plot_all is not None:
|
|
43
|
+
__all__.extend([
|
|
44
|
+
"plot_original_spline",
|
|
45
|
+
"plot_scan",
|
|
46
|
+
"plot_all",
|
|
47
|
+
])
|
reflection_method/cli.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""CLI for reflection-method: configuration, time conversion and core calls."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
import yaml
|
|
13
|
+
except ImportError:
|
|
14
|
+
yaml = None
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
from reflection_method import find_minimum
|
|
18
|
+
from reflection_method.io import read_csv_file
|
|
19
|
+
except ImportError:
|
|
20
|
+
# Allow running from source without install
|
|
21
|
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
22
|
+
from reflection_method import find_minimum
|
|
23
|
+
from reflection_method.io import read_csv_file
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@click.command()
|
|
28
|
+
@click.argument("input_file", type=click.Path(exists=True, path_type=Path))
|
|
29
|
+
@click.option("-x", "--x-col", default="DATE-OBS", help="X column name (time)")
|
|
30
|
+
@click.option("-y", "--y-col", default="MAG", help="Y column name (magnitude)")
|
|
31
|
+
@click.option("-w", "--w-col", default=None, help="Weight/error column name (optional)")
|
|
32
|
+
@click.option("-t", "--time-format", type=click.Choice(["iso", "jd"]), default="iso", help="Time axis: iso (UTC, default) or jd (Julian Date, on request)")
|
|
33
|
+
@click.option("-k", "--pts-per-knot", default=10, type=int, help="Points per spline knot")
|
|
34
|
+
@click.option("-d", "--degree", default=3, type=int, help="Spline degree (1-5)")
|
|
35
|
+
@click.option("-n", "--n-scan", default=200, type=int, help="Scan resolution for x0 search")
|
|
36
|
+
@click.option("-W", "--x0-window", default=0.1, type=float, help="Scan window as fraction of x-range")
|
|
37
|
+
@click.option("-P", "--parabola-window", default=0.05, type=float, help="Fraction of scan range for polynomial refinement of x0")
|
|
38
|
+
@click.option("-b", "--n-bootstrap", default=60, type=int, help="Bootstrap iterations")
|
|
39
|
+
@click.option("-B", "--n-scan-boot", default=80, type=int, help="Scan resolution per bootstrap iteration")
|
|
40
|
+
@click.option("-s", "--seed", default=None, type=int, help="Random seed for reproducibility")
|
|
41
|
+
@click.option("-o", "--output", type=click.Path(path_type=Path), default=None, help="Output JSON file (default: stdout)")
|
|
42
|
+
@click.option("-p", "--plot", type=click.Path(path_type=Path), default=None, help="Save plot to file (requires [plot] extra)")
|
|
43
|
+
@click.option("-F", "--plot-format", type=click.Choice(["png", "pdf", "svg"]), default="png", help="Plot format")
|
|
44
|
+
@click.option("--format", type=click.Choice(["yaml", "json"]), default="yaml", help="Output format: yaml (default, human-readable) or json (machine-readable)")
|
|
45
|
+
def find(
|
|
46
|
+
input_file: Path,
|
|
47
|
+
x_col: str,
|
|
48
|
+
y_col: str,
|
|
49
|
+
w_col: Optional[str],
|
|
50
|
+
time_format: str,
|
|
51
|
+
pts_per_knot: int,
|
|
52
|
+
degree: int,
|
|
53
|
+
n_scan: int,
|
|
54
|
+
x0_window: float,
|
|
55
|
+
parabola_window: float,
|
|
56
|
+
n_bootstrap: int,
|
|
57
|
+
n_scan_boot: int,
|
|
58
|
+
seed: Optional[int],
|
|
59
|
+
output: Optional[Path],
|
|
60
|
+
plot: Optional[Path],
|
|
61
|
+
plot_format: str,
|
|
62
|
+
format: str,
|
|
63
|
+
):
|
|
64
|
+
"""Find an eclipse minimum in a light curve using the reflection method.
|
|
65
|
+
|
|
66
|
+
Reads a CSV file (AAVSO-style extended format with a # comment header is
|
|
67
|
+
supported) and runs the reflection method via reflection_method.
|
|
68
|
+
find_minimum with find_peak=True (an eclipse is a maximum of the
|
|
69
|
+
magnitude light curve). Prints a JSON report with the minimum location
|
|
70
|
+
and its uncertainties. An optional diagnostic plot can be written with
|
|
71
|
+
--plot (requires the [plot] extra).
|
|
72
|
+
|
|
73
|
+
Magnitudes are used directly on the logarithmic scale. We operate on
|
|
74
|
+
observation times as given. By default (--time-format iso) the minimum
|
|
75
|
+
is reported as a UTC time; conversions (e.g. to JD) are done only at the
|
|
76
|
+
very end, on request via --time-format jd. Heliocentric and
|
|
77
|
+
barycentric corrections (HJD, BJD) are out of scope.
|
|
78
|
+
"""
|
|
79
|
+
# Read data
|
|
80
|
+
try:
|
|
81
|
+
x, y, w, t0_utc = read_csv_file(
|
|
82
|
+
str(input_file), x_col, y_col, w_col, time_format
|
|
83
|
+
)
|
|
84
|
+
except Exception as e:
|
|
85
|
+
click.echo(f"Error reading data: {e}", err=True)
|
|
86
|
+
sys.exit(1)
|
|
87
|
+
|
|
88
|
+
click.echo(f"Loaded {len(x)} points", err=True)
|
|
89
|
+
if t0_utc is not None:
|
|
90
|
+
click.echo(f"Time origin (UTC): {np.datetime_as_string(t0_utc, unit='s')}Z", err=True)
|
|
91
|
+
|
|
92
|
+
# RNG
|
|
93
|
+
rng = np.random.default_rng(seed) if seed is not None else None
|
|
94
|
+
|
|
95
|
+
# Run core algorithm
|
|
96
|
+
try:
|
|
97
|
+
result, x0_boot, x0_grid, sigma2 = find_minimum(
|
|
98
|
+
x, y,
|
|
99
|
+
pts_per_knot=pts_per_knot,
|
|
100
|
+
degree=degree,
|
|
101
|
+
w=w,
|
|
102
|
+
n_scan=n_scan,
|
|
103
|
+
x0_window=x0_window,
|
|
104
|
+
parabola_window=parabola_window,
|
|
105
|
+
n_bootstrap=n_bootstrap,
|
|
106
|
+
n_scan_boot=n_scan_boot,
|
|
107
|
+
find_peak=True,
|
|
108
|
+
rng=rng,
|
|
109
|
+
return_samples=True,
|
|
110
|
+
return_scan=True,
|
|
111
|
+
)
|
|
112
|
+
except Exception as e:
|
|
113
|
+
click.echo(f"Error in algorithm: {e}", err=True)
|
|
114
|
+
sys.exit(1)
|
|
115
|
+
|
|
116
|
+
# Prepare output
|
|
117
|
+
out_dict = result.to_dict(t0_utc, time_format)
|
|
118
|
+
out_dict["input_file"] = str(input_file)
|
|
119
|
+
out_dict["time_format"] = time_format
|
|
120
|
+
out_dict["parameters"] = {
|
|
121
|
+
"pts_per_knot": pts_per_knot,
|
|
122
|
+
"degree": degree,
|
|
123
|
+
"n_scan": n_scan,
|
|
124
|
+
"x0_window": x0_window,
|
|
125
|
+
"parabola_window": parabola_window,
|
|
126
|
+
"n_bootstrap": n_bootstrap,
|
|
127
|
+
"n_scan_boot": n_scan_boot,
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
# Convert numpy types to native Python for clean YAML/JSON
|
|
131
|
+
def _to_native(obj):
|
|
132
|
+
if isinstance(obj, dict):
|
|
133
|
+
return {k: _to_native(v) for k, v in obj.items()}
|
|
134
|
+
if isinstance(obj, list):
|
|
135
|
+
return [_to_native(v) for v in obj]
|
|
136
|
+
if isinstance(obj, (np.integer, np.floating)):
|
|
137
|
+
return obj.item()
|
|
138
|
+
if isinstance(obj, np.ndarray):
|
|
139
|
+
return obj.tolist()
|
|
140
|
+
if isinstance(obj, np.bool_):
|
|
141
|
+
return bool(obj)
|
|
142
|
+
if isinstance(obj, np.datetime64):
|
|
143
|
+
return str(obj)
|
|
144
|
+
if isinstance(obj, np.str_):
|
|
145
|
+
return str(obj)
|
|
146
|
+
return obj
|
|
147
|
+
|
|
148
|
+
out_dict = _to_native(out_dict)
|
|
149
|
+
|
|
150
|
+
# Write output in requested format
|
|
151
|
+
if format == "json":
|
|
152
|
+
out_str = json.dumps(out_dict, indent=2)
|
|
153
|
+
else:
|
|
154
|
+
if yaml is None:
|
|
155
|
+
click.echo("Error: PyYAML not installed. Install with 'pip install pyyaml' or use --format json", err=True)
|
|
156
|
+
sys.exit(1)
|
|
157
|
+
out_str = yaml.dump(out_dict, default_flow_style=False, sort_keys=False, indent=2)
|
|
158
|
+
|
|
159
|
+
if output:
|
|
160
|
+
output.write_text(out_str)
|
|
161
|
+
click.echo(f"Result written to {output}", err=True)
|
|
162
|
+
else:
|
|
163
|
+
click.echo(out_str)
|
|
164
|
+
|
|
165
|
+
# Optional plot
|
|
166
|
+
if plot:
|
|
167
|
+
try:
|
|
168
|
+
from reflection_method import prepare_plot_data
|
|
169
|
+
from reflection_method.plot import plot_all
|
|
170
|
+
import matplotlib
|
|
171
|
+
matplotlib.use("Agg")
|
|
172
|
+
import matplotlib.pyplot as plt
|
|
173
|
+
|
|
174
|
+
plot_data = prepare_plot_data(
|
|
175
|
+
x, y, w, result.x0, x0_grid, sigma2,
|
|
176
|
+
pts_per_knot, degree, parabola_window,
|
|
177
|
+
)
|
|
178
|
+
spl1, spl2 = plot_data["spl1"], plot_data["spl2"]
|
|
179
|
+
|
|
180
|
+
# Determine axis labels and units
|
|
181
|
+
if time_format == "iso":
|
|
182
|
+
xlabel = "Time"
|
|
183
|
+
ylabel = "magnitude"
|
|
184
|
+
x_unit = "min"
|
|
185
|
+
utc0 = t0_utc
|
|
186
|
+
else:
|
|
187
|
+
xlabel = "Time"
|
|
188
|
+
ylabel = "magnitude"
|
|
189
|
+
x_unit = "JD"
|
|
190
|
+
utc0 = None
|
|
191
|
+
|
|
192
|
+
fig = plot_all(
|
|
193
|
+
x, y, spl1, plot_data["xr"], spl2, result.x0, result.x0_std,
|
|
194
|
+
x0_grid, sigma2, plot_data["spl_sigma"], x0_boot,
|
|
195
|
+
xlabel=xlabel, ylabel=ylabel, x_unit=x_unit,
|
|
196
|
+
invert_y=True, utc0=utc0,
|
|
197
|
+
fit_window_lo=plot_data["fit_window_lo"],
|
|
198
|
+
fit_window_hi=plot_data["fit_window_hi"],
|
|
199
|
+
)
|
|
200
|
+
fig.savefig(plot, format=plot_format, dpi=150, bbox_inches="tight")
|
|
201
|
+
plt.close(fig)
|
|
202
|
+
click.echo(f"Plot saved to {plot}", err=True)
|
|
203
|
+
except ImportError:
|
|
204
|
+
click.echo("Warning: [plot] extra not installed, skipping plot", err=True)
|
|
205
|
+
except Exception as e:
|
|
206
|
+
click.echo(f"Warning: Could not generate plot: {e}", err=True)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
if __name__ == "__main__":
|
|
210
|
+
find()
|