Skip to content

Commit 7e0c9c5

Browse files
authored
Merge pull request #757 from kevin218/dev/ra/optimizer/jwst
Eureka! optimizer for Stages 1, 3, and 4.
2 parents d2fd8a9 + 49b54f0 commit 7e0c9c5

37 files changed

Lines changed: 2563 additions & 68 deletions

demos/JWST/CalibratedStellarSpectra/run_eureka.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import eureka.S3_data_reduction.s3_reduce as s3
66
import eureka.S4cal_StellarSpectra.s4cal_StellarSpec as s4cal
77

8+
# Set up some parameters to make plots look nicer.
9+
# You can set usetex=True if you have LaTeX installed
10+
eureka.lib.plots.set_rc(style='eureka', usetex=False, filetype='.png')
11+
812
# eventlabel = 'imaging_template'
913
# eventlabel = 'miri_lrs_template'
1014
eventlabel = 'nirspec_fs_template'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Eureka! Control File for Optimizer Stage 1
2+
3+
# Optimizer Documentation: https://eurekadocs.readthedocs.io/en/latest/ecf.html#stages-1-3-4-optimizer
4+
# Rename this file to match the event label given in run_eureka_optimization.py
5+
# and run_eureka.py (e.g., S1opt_nirspec_fs_template.ecf).
6+
7+
# Fitness Scaling
8+
# Use these settings to override the default fitness scaling factors.
9+
scaling_MAD_spec 0.01 # Optimizer scaling for 2D MAD value
10+
scaling_MAD_white 1.0 # Optimizer scaling for WLC MAD value
11+
12+
# List of parameters to optimize in Stage 1
13+
# Commenting out this line will use all single parameters listed below.
14+
# Single parameter options: jump_rejection_threshold, expand_mask, bg_deg, bg_method, p3thresh, window_len
15+
# Double parameter options: any combination of the above, joined by two underscores (e.g., expand_mask__p3thresh)
16+
params_to_optimize_s1 ['jump_rejection_threshold', 'expand_mask', 'p3thresh']
17+
18+
# Range for parameter values to optimize
19+
# Only specify if overriding the default values in S1opt_meta.py
20+
# For single parameters, use format: sweep_<parameter_name> = range(min, max+1) or np.arange(min, max+step, step) if step != 1
21+
# For double parameters, use format: sweep_<parameter1>__<parameter2> = [range(min1, max1+1), range(min2, max2+1)]
22+
sweep_expand_mask range(5,10) # Example: optimize BG inner edge between 5 and 9 pixels
23+
24+
# Diagnostics
25+
isplots_S1opt 1 # Generate none (0), few (1), some (3), or many (5) figures (Options: 1 - 5)
26+
delete_intermediate True # If True, intermediate directories will be deleted after each optimization step
27+
delete_final True # If True, intermediate directories will be deleted upon completion of optimization run
28+
verbose True # If True, more details will be printed about steps in the optimization process
29+
hide_plots True # If True, plots will automatically be closed rather than popping up
30+
31+
# Project directory
32+
topdir /home/User/Data/JWST-Sim/optimizer/
33+
34+
# Directories relative to topdir
35+
inputdir Stage0
36+
outputdir Stage1opt
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Eureka! Control File for Optimizer Stage 3
2+
3+
# Optimizer Documentation: https://eurekadocs.readthedocs.io/en/latest/ecf.html#stages-1-3-4-optimizer
4+
# Rename this file to match the event label given in run_eureka_optimization.py
5+
# and run_eureka.py (e.g., S3opt_nirspec_fs_template.ecf).
6+
7+
# Fitness Scaling
8+
# Use these settings to override the default fitness scaling factors.
9+
scaling_MAD_spec 0.01 # Optimizer scaling for 2D MAD value
10+
scaling_MAD_white 1.0 # Optimizer scaling for WLC MAD value
11+
12+
# List of single parameter options to optimize in Stage 3:
13+
# dqmask, bg_deg, bg_thresh, bg_hw, bg_method, p3thresh, spec_hw, median_thresh, window_len, p7thresh
14+
# Double parameter options: any combination of the above, joined by two underscores (e.g., bg_hw__p3thresh)
15+
# Special cases: spec_hw__bg_hw (requires spec_hw < bg_hw)
16+
params_to_optimize_s3 ['spec_hw__bg_hw', 'dqmask', 'bg_deg', 'bg_thresh', 'bg_method', 'p3thresh', 'median_thresh__window_len', 'p7thresh']
17+
18+
# List of single parameter options to optimize in Stage 4:
19+
# mad_sigma, mad_box_width, sigma, box_width
20+
# Suggested double parameter options:
21+
# mad_sigma__mad_box_width, sigma__box_width
22+
params_to_optimize_s4 ['mad_sigma__mad_box_width', 'sigma__box_width']
23+
24+
# Range for parameter values to optimize
25+
# Only specify if overriding the default values in S3opt_meta.py
26+
# For single parameters, use format: sweep_<parameter_name> = range(min, max+1) or np.arange(min, max+step, step) if step != 1
27+
# For double parameters, use format: sweep_<parameter1>__<parameter2> = [range(min1, max1+1), range(min2, max2+1)]
28+
sweep_bg_thresh np.arange(3,5.5,0.5) # Example: optimize BG threshold between 3 and 5 sigma, in steps of 0.5
29+
30+
# Diagnostics
31+
isplots_S3opt 1 # Generate none (0), few (1), some (3), or many (5) figures (Options: 1 - 5)
32+
delete_intermediate False # If True, intermediate directories will be deleted after each optimization step
33+
delete_final True # If True, intermediate directories will be deleted upon completion of optimization run
34+
verbose True # If True, more details will be printed about steps in the optimization process
35+
hide_plots True # If True, plots will automatically be closed rather than popping up
36+
37+
# Project directory
38+
topdir /home/User/Data/JWST-Sim/optimizer/
39+
40+
# Directories relative to topdir
41+
inputdir Stage2
42+
outputdir Stage3opt
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import eureka.lib.plots
3+
import eureka.optimizer.S1opt_optimizer as s1opt
4+
import eureka.optimizer.S3opt_optimizer as s3opt
5+
6+
# Set up some parameters to make plots look nicer.
7+
# You can set usetex=True if you have LaTeX installed
8+
eureka.lib.plots.set_rc(style='eureka', usetex=False, filetype='.png')
9+
10+
"""
11+
Eureka! Optimization
12+
--------------------
13+
14+
Description:
15+
This script is designed to optimize the Stage 1, 3, and 4 ECF parameters for
16+
JWST time-series observations.
17+
18+
Inputs:
19+
- Loaded from a Eureka! control file, see S1opt_template.ecf/S3opt_template.ecf.
20+
- Parameters not specified for optimization will adopt the values listed in the
21+
standard ECF (e.g., S1_<eventlabel>.ecf or S3_<eventlabel>.ecf).
22+
23+
Outputs:
24+
- The metadata object
25+
- The fitness score after optimizing each parameter.
26+
- The best parameter values found during the optimization.
27+
"""
28+
29+
eventlabel = 'nirspec_fs_template'
30+
ecf_path = '.'+os.sep
31+
32+
if __name__ == "__main__":
33+
# To skip one or more stages that were already run,
34+
# just comment them out below
35+
36+
# Stage 1 optimization
37+
s1opt_meta, history, best = s1opt.wrapper(eventlabel,
38+
ecf_path=ecf_path,
39+
initial_run=True,
40+
final_run=True)
41+
42+
# Stages 3 and 4 optimization
43+
s3opt_meta, history, best = s3opt.wrapper(eventlabel,
44+
ecf_path=ecf_path,
45+
initial_run=True,
46+
final_run=True)

demos/JWST/run_eureka.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import eureka.S5_lightcurve_fitting.s5_fit as s5
88
import eureka.S6_planet_spectra.s6_spectra as s6
99

10+
# Set up some parameters to make plots look nicer.
11+
# You can set usetex=True if you have LaTeX installed
12+
eureka.lib.plots.set_rc(style='eureka', usetex=False, filetype='.png')
13+
1014
# eventlabel = 'imaging_template'
1115
# eventlabel = 'miri_lrs_template'
1216
eventlabel = 'nirspec_fs_template'

docs/source/ecf.rst

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,89 @@ In short this weights each pixel, :math:`i`, within a slope following :math:`w_i
257257
``custom``: As with default, except a custom SNR to :math:`P` lookup table can be defined through the ``default_ramp_fit_custom_snr_bounds`` and ``default_ramp_fit_custom_exponents`` (see example .ecf file).
258258

259259

260+
261+
Stages 1/3/4 Optimizer
262+
----------------------
263+
This tool allows users to optimize the parameter values of Stages 1, 3, and 4 by performing a parametric sweep over a specified set of parameter values and evaluating the resulting spectra with a user-defined weighting of the fitness function. The optimizer starts with an intial run of a stage using the default parameter values, performs a sweep over the specified parameter values, and then completes a final run with the best parameter values from the sweep.
264+
265+
The fitness function is a weighted combination of the 2D MAD and the white light curve MAD. The optimizer will generate a plot showing how the fitness function improves with each parameter sweep. In the rare event that the final fitness value is worse than the initial value, users should consider trying smaller step sizes or a different set of parameter values that include the default parameter values.
266+
267+
**Important Warning:** The optimizer is designed as a tool for parameter exploration and fine-tuning, not as a black-box solution. Users must critically investigate all outputs before and after optimization to ensure the final tunings are appropriate and scientifically valid. The fitness metric is inherently imperfect and may lead to undesirable outcomes in complex scenarios, such as observations affected by nearby binary stars or other astrophysical contaminants. Always review the spectra from different parameter values in the sweep to understand their impact, and remember that optimal parameter values can vary significantly between datasets. Thus, re-running the optimizer for each new dataset is recommended.
268+
269+
scaling_MAD_spec
270+
''''''''''''''''
271+
Scaling factor applied to the pixel-level 2D Median Absolute Difference (MAD) value in the fitness function. Higher values prioritize spectral quality.
272+
273+
scaling_MAD_white
274+
'''''''''''''''''
275+
Scaling factor applied to the white light curve MAD value in the fitness function. Higher values prioritize band-integrated quality.
276+
277+
params_to_optimize_s1
278+
''''''''''''''''''''''
279+
List of parameters to optimize in Stage 1. Commenting out this line will use all single parameters. Single parameter options: jump_rejection_threshold, expand_mask, bg_deg, bg_method, p3thresh, window_len. Double parameter options: any combination of the above, joined by two underscores (e.g., expand_mask__p3thresh). Example:
280+
281+
.. code-block:: python
282+
283+
params_to_optimize = ['jump_rejection_threshold', 'expand_mask', 'p3thresh']
284+
285+
params_to_optimize_s3
286+
'''''''''''''''''''''
287+
List of parameters to optimize in Stage 3. Commenting out this line will use all single parameters. Single parameter options: dqmask, bg_deg, bg_thresh, bg_hw, bg_method, p3thresh, spec_hw, median_thresh, window_len, p7thresh. Double parameter options: any combination of the above, joined by two underscores (e.g., bg_hw__p3thresh). Special cases: spec_hw__bg_hw (requires spec_hw < bg_hw). Recommended parameters:
288+
289+
.. code-block:: python
290+
291+
params_to_optimize_s3 = ['spec_hw__bg_hw', 'dqmask', 'bg_deg', 'bg_thresh', 'bg_method', 'p3thresh', 'median_thresh__window_len', 'p7thresh']
292+
293+
params_to_optimize_s4
294+
'''''''''''''''''''''
295+
List of parameters to optimize in Stage 4. Single parameter options: mad_sigma, mad_box_width, sigma, box_width. Double parameter options: mad_sigma__mad_box_width, sigma__box_width. Recommended parameters:
296+
297+
.. code-block:: python
298+
299+
params_to_optimize_s4 = ['mad_sigma__mad_box_width', 'sigma__box_width']
300+
301+
sweep_<parameter_name>
302+
''''''''''''''''''''''
303+
Set of parameter values to optimize. Only specify if overriding the default values in the meta Python files. For single parameters, use any of the following formats:
304+
305+
.. code-block:: python
306+
307+
sweep_<parameter_name> = range(min, max+1)
308+
sweep_<parameter_name> = np.arange(min, max+step, step)
309+
sweep_<parameter_name> = [3, 5, 6, 7]
310+
sweep_<parameter_name> = [True, False]
311+
312+
sweep_<parameter1>__<parameter2>
313+
''''''''''''''''''''''''''''''''
314+
Set of parameter values to optimize. Only specify if overriding the default values in the meta Python files. For double parameters, use the following format:
315+
316+
.. code-block:: python
317+
318+
sweep_<parameter1>__<parameter2> = [range(min1, max1+1), range(min2, max2+1)]
319+
320+
isplots_S1opt and isplots_S3opt
321+
'''''''''''''''''''''''''''''''
322+
Sets how many plots should be saved during optimization. Can generate none (0), few (1), some (3), or many (5) figures (Options: 1 - 5).
323+
324+
delete_final
325+
''''''''''''
326+
If True, intermediate directories will be deleted after the optimizer finishes. Outputs from the optimizer's final run remain in the usual output location. Set to ``False`` if you want to retain all trial outputs for
327+
inspection or debugging.
328+
329+
delete_intermediate
330+
'''''''''''''''''''
331+
If True, intermediate directories will be deleted after each optimization step. Outputs from the optimizer's final run remain in the usual output location. This may be useful for Stage 1 optimization since the outputs can be quite large. For Stage 3 optimization, this can usually be set to ``False``.
332+
333+
verbose
334+
'''''''
335+
If True, more details will be printed about steps in the optimization process.
336+
337+
hide_plots
338+
''''''''''
339+
If True, plots will automatically be closed rather than popping up on the screen.
340+
341+
342+
260343
Stage 2
261344
-------
262345

@@ -727,7 +810,7 @@ Optional. The path to a file that contains the time array you want to use instea
727810

728811

729812
Stage 4
730-
--------
813+
-------
731814

732815
.. include:: ../media/S4_template.ecf
733816
:literal:

environment.yml

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ dependencies = [
8888
"pandas",
8989
"photutils",
9090
"requests",
91+
"pyyaml",
9192
"scipy>=1.12.0", # Lower limit needed for stcal.ramp_fitting.likely_fit
9293
"shapely",
9394
"tqdm",
@@ -100,7 +101,7 @@ dependencies = [
100101
{ pip = "exotic-ld @ git+https://github.com/taylorbell57/ExoTiC-LD.git@0bb2ff8" }, # Pin needed for pkg_resources deprecation
101102
{ pip = "fleck" },
102103
{ pip = "george" }, # Needed for GP
103-
{ pip = "planet-harmonica @ git+https://github.com/DavoGrant/harmonica@main" }, # PyPI build currently only supports for python 3.8 -- 3.10
104+
{ pip = "planet-harmonica @ git+https://github.com/Exo-TiC/harmonica@main" }, # PyPI build currently only supports for python 3.8 -- 3.10
104105
{ pip = "pastasoss>=1.2.3" }, # Lower limit needed for pkg_resources deprecation
105106
{ pip = "setuptools_scm>=6.2" }, # Needed when importing from a source tree without generated version.py
106107
{ pip = "stdatamodels" },
@@ -114,7 +115,7 @@ jwst = [
114115
{ pip = "stcal>=1.15.2" }, # Lower limit needed for updated stcal.ramp_fitting.ramp_fit.ramp_fit arguments
115116
]
116117
hst = [
117-
{ pip = "image_registration @ git+https://github.com/keflavich/image_registration@70e3880" }, # Need GitHub version to avoid np.float issue
118+
{ pip = "image_registration>=0.2.10" }, # Lower bound needed to avoid np.float issue
118119
]
119120
docs = [
120121
{ pip = "myst-parser>=0.18.0" }, # Lower bound for stable MyST support

src/eureka/S1_detector_processing/group_level.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def GLBS(input_model, log, meta):
6565
if meta.inst == 'miri':
6666
meta.isrotate = 0
6767

68-
if meta.isplots_S1 == 4:
68+
if meta.isplots_S1 >= 4:
6969
# Plot all groups
7070
isplots_S1 = meta.isplots_S1
7171
# Otherwise, only show plots for the last good group

src/eureka/S1_detector_processing/s1_meta.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def __init__(self, folder=None, file=None, eventlabel=None, **kwargs):
3636
# Set a default data file suffix
3737
self.suffix = getattr(self, 'suffix', 'uncal')
3838

39+
# Set optimization flag
40+
self.firstSegOnly_S1 = getattr(self, 'firstSegOnly_S1', False)
41+
self.firstSegOnly_S3 = getattr(self, 'firstSegOnly_S3', False)
42+
3943
def set_defaults(self):
4044
'''Set Stage 1 specific defaults for generic instruments.
4145
'''
@@ -188,6 +192,7 @@ def set_defaults(self):
188192
self.nplots = getattr(self, 'nplots', 5)
189193
self.hide_plots = getattr(self, 'hide_plots', True)
190194
self.testing_S1 = getattr(self, 'testing_S1', False)
195+
self.save_results = getattr(self, 'save_results', not self.testing_S1)
191196
self.verbose = getattr(self, 'verbose', True)
192197

193198
# Project directory

0 commit comments

Comments
 (0)