accelmo.poco.fitter

Levenberg-Marquardt base fitter of POCO and the measured-optics observables.

accelmo.poco.fitter.MEASUREMENT_COLUMNS: dict[str, tuple[str, str]] = {'alfx': ('ALFX', 'ERRALFX'), 'alfy': ('ALFY', 'ERRALFY'), 'betx': ('BETX', 'ERRBETX'), 'bety': ('BETY', 'ERRBETY'), 'dpx': ('DPX', 'ERRDPX'), 'dpy': ('DPY', 'ERRDPY'), 'dx': ('DX', 'ERRDX'), 'dy': ('DY', 'ERRDY'), 'mu1': ('MUX', 'mu1_var'), 'mu2': ('MUY', 'mu2_var'), 'x': ('X', 'ERRX'), 'y': ('Y', 'ERRY')}

Measurement columns backing each fittable observable, as observable -> (value column, error column) in the measured optics frame.

mu1/mu2 are special: the measured frame carries the cumulative phase and its cumulative variance, which are differenced into per-interval advances (with the variance difference) by _advance_targets().

betx/bety/alfx/alfy are the physical-plane projections, never beta11/beta22/alfa11/alfa22 (MAD-NG’s generalised-mode columns, only meaningful with coupling=true – see run_closed_twiss_init.mad). Phase advance has no such projected name in MAD-NG (confirmed against its own optical-function name list: only mu/dmu with a mode-index suffix exist, no mux/muy), so mu1/mu2 remain mode-indexed on both sides here.

accelmo.poco.fitter.DEFAULT_OBSERVABLES: tuple[str, ...] = ('x', 'y', 'betx', 'bety', 'alfx', 'alfy', 'mu1', 'mu2', 'dx', 'dy')

everything the closed twiss and an omc3 optics measurement independently have in common. All of them are fitted by default: each family constrains a different combination of the knobs, and inverse-variance weighting gives a noisy family little weight. Narrow this only to isolate an effect, as the tests do.

dpx/dpy are the one exclusion, and it is required for correctness: omc3 does not measure them independently but derives them from DX/DY through the model transfer matrix, so including them counts one measurement twice. In the vertical it is worse than redundant - the derivation assumes no vertical dispersion source exists anywhere, which is exactly the hypothesis a Dy fit is testing. They remain available for callers whose dispersion errors come from somewhere else.

Type:

Observables fitted when the caller does not choose

class accelmo.poco.fitter.LMFitter(accelerator, sequence_config, *, runtime, lm_config=None, initial_knob_strengths=None, true_strengths=None, use_errors=True, knob_sigmas=None, output_config=None, b2_errors=None, machine_state=None, debug=False)[source]

Bases: object

Shared lifecycle and Levenberg-Marquardt solve of the full-ring closed-orbit/closed-twiss fitters.

Subclasses set worker_class and build worker_payloads, one (WorkerConfig, data) per worker, in their own __init__.

Parameters:
  • accelerator (Accelerator)

  • sequence_config (SequenceConfig)

  • runtime (WorkerRuntime)

  • lm_config (LevenbergMarquardtConfig | None)

  • initial_knob_strengths (dict[str, float] | None)

  • true_strengths (Path | dict[str, float] | None)

  • use_errors (bool)

  • knob_sigmas (Mapping[str, float] | None)

  • output_config (OutputConfig | None)

  • b2_errors (Path | None)

  • machine_state (MachineState | None)

  • debug (bool)

worker_class: type
log_suffix: str
fit_label: str
supports_energy: bool = False

pt is then fitted as the last knob.

Type:

Whether accelerator.optimise_energy is supported

loss_only_trials: bool = False

Whether a rejected step costs a plain-orbit chi² instead of a full evaluation (orbit-only workers).

diagnostics: FitDiagnostics | None
history: list[tuple[dict[str, float], float]]

(knobs, loss) of every accepted iteration, in order

posterior_normal: Optional[ndarray]

Posterior inverse covariance (data + prior) at the best knobs of the last run, in knob order.

worker_payloads: list
b2_errors

Known LHC dipole b2 error table, applied to every worker’s sequence; the interface also needs the tune knobs that compensate it, from machine_state.

knob_sigma

Gaussian prior width of each knob (0 = free); see knob_sigma_array().

close()[source]

Shut down the MAD process of the model; call once the fit (and any use of its model) is done.

Return type:

None

run()[source]

Run the Levenberg-Marquardt fit on one worker per payload.

Return type:

FitResult

begin_dashboard_fit(param_names, initial, gain_names=None)[source]

Start the dashboard’s fit stage for parameters param_names starting at initial.

Return type:

None

Parameters:
  • param_names (list[str])

  • initial (numpy.ndarray)

  • gain_names (list[str] | None)

accelmo.poco.fitter.load_measurement(measurement, observables)[source]

Validate a measured optics set (DataFrame indexed by BPM name) for the fit.

Parameters:
  • measurement (DataFrame) – DataFrame carrying the columns listed in MEASUREMENT_COLUMNS.

  • observables (tuple[str, ...]) – Observable names that must be present.

Return type:

DataFrame

Returns:

Copy of measurement. Missing error columns are filled with NaN. Individual NaN errors drop their own points; an observable whose errors are all unusable is rejected by build_observable() unless the caller passes use_errors=False, because a family with no variances cannot be weighted commensurably against families that have them.

Parameters:
  • measurement (pd.DataFrame)

  • observables (tuple[str, ...])

accelmo.poco.fitter.measured_bpms(all_bpms, measurement, label)[source]

The model BPMs that have a measurement, in model order; at least two.

Return type:

list[str]

Parameters:
  • all_bpms (list[str])

  • measurement (pd.DataFrame)

  • label (str)

accelmo.poco.fitter.build_observable(name, measurement, bpms, use_errors, pt)[source]

Extract one observable’s targets and variances from a measurement frame.

Return type:

Observable

Parameters:
  • name (str)

  • measurement (pd.DataFrame)

  • bpms (list[str])

  • use_errors (bool)

  • pt (float)