fit

Contents

fit#

scarlet2.fit(scene, observations, *args, schedule=None, max_iter=100, e_rel=0.0001, progress_bar=True, callback=None, pair_similarity=None, **kwargs)[source]#

Fit model parameters of every source in scene to match observations.

Computes the best-fit parameters of all components in every source by first-order gradient descent with the Yogi optimizer from optax.

Parameters:
  • scene (Scene) – The model of the scene.

  • observations (Observation or list) – The observations to fit the model to.

  • *args (list, optional) – Additional arguments passed. Only used for backwards (v0.3) compatibility.

  • schedule (callable, optional) – A function that maps optimizer step count to value. See optax.Schedule for details.

  • max_iter (int, optional) – Maximum number of optimizer iterations

  • e_rel (float, optional) – Upper limit for the relative change in the norm of any parameter to terminate the optimization early.

  • progress_bar (bool, optional) – Whether to show a progress bar

  • callback (callable, optional) – Function to be called on the current state of the optimized scene. Signature callback(scene, convergence, loss) -> None, where convergence is a tree of the same structure as scene, and loss is the current value of the log_posterior.

  • pair_similarity (PairSimilarity, optional) – If given, adds a pair-similarity regularizer to the loss. None (default) disables it (bit-identical to the un-regularized version). Penalizes the cosine similarity of source-pair morphologies, scaled by the cosine similarity of their SEDs. Targets parasitic flux directly via the morphology cosine. See PairSimilarity for the available options.

  • **kwargs (dict, optional) – Additional keyword arguments passed to the optax.scale_by_yogi optimizer.

Notes

Requires optax. The returned scene carries the best parameters seen during optimization (lowest loss), not the last iteration’s, since Yogi/Adam can be non-monotonic. Diagnostic info (loss history, best loss, iteration count) is attached as Scene.fit_info.

Returns:

The scene and observation(s) with best-fit parameters. scene.fit_info contains {"loss", "pair_similarity", "best_loss", "n_iter"}. The pair_similarity array report zero when the regularizer is disabled.

Return type:

Scene, list(Observation)