rxnfit.p0_opt_fit module
Optuna search for initial rate parameters before gradient-based fitting.
P0OptFit proposes trial points with Optuna, scores them with a light
wrapper around rxnfit.expdata_fit.ExpDataFit, and returns refined
estimates for downstream scipy.optimize runs.
- class rxnfit.p0_opt_fit.P0OptFit(reaction_source: str | RxnODEbuild, df_list: list, t_range: Tuple[float, float] | None = None, encoding: str = 'utf-8', param_bounds: Tuple[float, float] | Dict[str, Tuple[float, float]] | None = None, df_names: List[str] | None = None, method: str = 'RK45', rtol: float = 1e-06, opt_method: str = 'L-BFGS-B', use_log_fit: bool = False, lower_bound: float | None = None, verbose: bool = True, storage: str | BaseStorage | None = None, seed: int | None = None, catch: Tuple[Type[BaseException], ...] | List[Type[BaseException]] | None = None)
Bases:
objectOptimize initial parameter values (p0) for ODE rate constants using Optuna, then fit with ExpDataFit.
By default no random seed is set; Optuna uses its default (TPESampler without seed). Pass seed in the constructor for reproducible search.
- optimize(n_trials: int | None = None, timeout: float | None = None, n_jobs: int = 1, show_progress_bar: bool = True, **kwargs: Any) Tuple[Dict[str, Tuple[float, float]], Dict[str, float]]
Run Optuna optimization and return best p0 and fitted result.
- Parameters:
n_trials – Number of trials. Passed to study.optimize().
timeout – Timeout in seconds. Passed to study.optimize().
n_jobs – Number of parallel jobs. Passed to study.optimize().
show_progress_bar – Whether to show progress bar. Default True.
**kwargs – Additional arguments passed to study.optimize(). Note: ‘catch’ is ignored; use the constructor argument to set it.
- Returns:
dict: { variable_name: (optimal_initial_value, fitted_value) }
fit_metrics: Dict with keys ‘rss’, ‘tss’, ‘r2’.
- Return type:
Tuple of (dict, fit_metrics)
- Raises:
RuntimeError – If all trials fail.
- optuna_log() List[Dict[str, Any]]
Return per-trial log. Empty list if optimize() has not been run.
- Returns:
trial_No, params, rss, state.
- Return type:
List of dicts with keys