accelmo.tracking.uncertainty

Stopping the training workers and folding their uncertainty parts into (A, B).

class accelmo.tracking.uncertainty.UncertaintyPart(normal, reading_ids, sensitivities, variances)[source]

Bases: object

One worker’s contribution to the propagated knob covariance.

normal is Σ w J Jᵀ. Each row r of sensitivities is the change in that worker’s gradient per unit noise on reading reading_ids[r] (an observation, a start coordinate, or both), with variances[r] its declared variance.

Parameters:
  • normal (numpy.ndarray)

  • reading_ids (numpy.ndarray)

  • sensitivities (numpy.ndarray)

  • variances (numpy.ndarray)

normal: ndarray
reading_ids: ndarray
sensitivities: ndarray
variances: ndarray
classmethod empty(n_knobs)[source]

A worker that contributes nothing (disabled, or uncertainty not requested).

Return type:

UncertaintyPart

Parameters:

n_knobs (int)

accelmo.tracking.uncertainty.merge_uncertainty_parts(parts, n_knobs)[source]

Sum the normal matrices and merge rows that share a reading id.

G_e adds every part’s sensitivity to reading e (shared between workers, or used as both an observation and a start), so noise_matrix() counts its noise once. Merging a merged part with further parts gives the same result.

Return type:

UncertaintyPart

Parameters:
accelmo.tracking.uncertainty.eliminate_fitted_starts(normal, start, start_start)[source]

Schur complement of the fitted starts: (A_kk - Σ_p P_p A_sk,p, P).

normal is the knob block A_kk summed over particles; start (particles, 4, knobs) and start_start (particles, 4, 4) are each particle’s full A_sk and A_ss. The projection P = A_ks A_ss⁻¹ (particles, knobs, 4) takes a Jacobian to J - P ∂m/∂c0.

Return type:

tuple[ndarray, ndarray]

Parameters:
  • normal (numpy.ndarray)

  • start (numpy.ndarray)

  • start_start (numpy.ndarray)

accelmo.tracking.uncertainty.noise_matrix(part)[source]

B = Σ_e σ_e² G_e G_eᵀ for a merged part; readings without a finite variance add nothing.

Return type:

ndarray

Parameters:

part (UncertaintyPart)

accelmo.tracking.uncertainty.second_order_bytes(n_params, batch_size)[source]

Upper estimate of one MAD-NG process holding batch_size order-2 maps in n_params knobs.

One TPSA has a coefficient per monomial of order <= 2 in 6 variables and n_params parameters.

Return type:

float

Parameters:
  • n_params (int)

  • batch_size (int)

accelmo.tracking.uncertainty.second_order_blocker(pool, n_params, num_batches)[source]

Why the true Hessian cannot run, or None when one worker’s second-order maps fit.

n_params is the number of MAD-NG parameters (magnet knobs; gains live in Python). The check runs with the workers up, so the free memory already excludes their first-order footprint.

Return type:

str | None

Parameters:
  • pool (WorkerPool)

  • n_params (int)

  • num_batches (int)

class accelmo.tracking.uncertainty.UncertaintyCollection(matrices=dict(), skipped=dict(), seconds=0.0)[source]

Bases: object

What the uncertainty stage returned.

matrices maps each Hessian that ran to its normal matrix A and noise matrix B; skipped maps each requested Hessian that did not run to the reason; seconds is the stage’s wall time.

Parameters:
  • matrices (dict[str, tuple[numpy.ndarray, numpy.ndarray]])

  • skipped (dict[str, str])

  • seconds (float)

matrices: dict[str, tuple[ndarray, ndarray]]
skipped: dict[str, str]
seconds: float = 0.0
property primary: tuple[ndarray, ndarray]

(A, B) of the first Hessian that ran.

class accelmo.tracking.uncertainty.StageProgress(total, on_update=None, interval=PROGRESS_SECONDS)[source]

Bases: object

Counts finished batches of the second-order pass, logging elapsed time and an ETA.

on_update(done, total, elapsed, eta) is called at most every interval seconds and at the end, for the run log; eta is None until the first batch is done.

Parameters:
  • total (int)

  • on_update (Callable[[int, int, float, float | None], None] | None)

  • interval (float)

advance(_message=None)[source]

One more batch done.

Return type:

None

Parameters:

_message (UncertaintyProgress | None)

accelmo.tracking.uncertainty.drain_uncertainty(pool, n_knobs, hessians, second_order_batches=None, progress=None)[source]

Stop pool in memory-bounded chunks; return each Hessian’s normal matrix A and noise B.

Feed each pair to sandwich_uncertainties(). Reading ids are file-scoped, so once every worker of a file has reported, that file’s merged readings become its B contribution and are dropped. Workers are drained in file order, so besides the chunk being received at most one partly merged file is held. No hessians stops the workers without computing anything.

Return type:

dict[str, tuple[ndarray, ndarray]]

Parameters: