Optimisation
optimise
Choosing a data-collection setting by gradient.
:func:~pyraddose_3d.solve.solve is differentiable in every setting in
:data:~pyraddose_3d.solve.TUNABLE, and torch supplies optimisers for
several settings at once. This module adds what that leaves out for the
commonest single question -- which energy? -- and nothing more.
Energy needs care for one reason: absorption edges. At each, the crystal's
absorption jumps, and so does anything computed from it. A gradient is
blind to a jump -- it describes the slope where it stands, and the slope on
either side of an edge can point straight at it -- so a search that crosses
one can end somewhere worse than where it started. The edges are known in
advance, though, so :func:maximise_energy splits the range at them and
searches each smooth piece on its own.
Within a piece, the gradient's sign says which side of a point the best energy lies, so the search bisects on it: each run halves the interval, with no step size to tune. That assumes one maximum per piece -- true of everything tried so far, and worth checking with a coarse scan for a new objective.
Search
dataclass
One search over an interval: every point evaluated, in order.
Attributes:
-
low, high(float) –The interval searched.
-
points(list[tuple[float, float, float]]) –(x, value, slope)for each evaluation.
best
property
best: tuple[float, float]
The best point found, as (x, value).
evaluations
property
evaluations: int
How many times the objective and its gradient were computed.
diffraction_efficiency
diffraction_efficiency(
summaries: list[WedgeSummary],
) -> Any
Elastically scattered photons per MGy, over every wedge together.
RADDOSE-3D reports a diffraction efficiency per wedge: that wedge's elastic yield over its average diffraction-weighted dose. For the whole experiment this sums the yields and weights each wedge's DWD by its yield -- the dose the diffraction saw, on average. With one wedge it is that wedge's figure exactly.
Parameters:
-
summaries(list[WedgeSummary]) –What :func:
~pyraddose_3d.solve.solvereturns.
Returns:
-
Any–Photons per MGy, carrying any gradient the summaries do.
maximise_on_interval
maximise_on_interval(
objective: Callable[[Any], Any],
low: float,
high: float,
*,
tolerance: float = 0.01,
) -> Search
Find the maximum of a smooth, single-peaked function on an interval.
Bisects on the sign of the gradient: positive means the maximum lies above, negative below. If the gradient already points out of the interval at an end, the maximum is that end.
Parameters:
-
objective(Callable[[Any], Any]) –Takes a scalar float64 tensor and returns a scalar tensor that depends on it differentiably.
-
low(float) –The interval.
-
high(float) –The interval.
-
tolerance(float, default:0.01) –Stop once the maximum is bracketed this closely.
Returns:
-
Search–Every point evaluated. At most
2 + ceil(log2((high - low) / tolerance))of them.
absorption_edges
absorption_edges(
experiment: Experiment, low: float, high: float
) -> list[float]
Energies between low and high where the crystal's absorption jumps.
The edges of every element in the crystal, and of its container if it has one -- the container's transmission jumps too.
Parameters:
-
experiment(Experiment) –The configuration.
-
low(float) –The energy range, in keV.
-
high(float) –The energy range, in keV.
Returns:
-
list[float]–Ascending, strictly inside the range.
edge_free_intervals
edge_free_intervals(
low: float,
high: float,
edges: Iterable[float],
clearance: float = _EDGE_CLEARANCE,
) -> list[tuple[float, float]]
Split a range at edges into pieces an objective is smooth on.
Parameters:
-
low(float) –The range.
-
high(float) –The range.
-
edges(Iterable[float]) –Where to split; any outside the range are ignored.
-
clearance(float, default:_EDGE_CLEARANCE) –How far each piece stops short of an edge that bounds it. A piece narrower than that is dropped.
Returns:
-
list[tuple[float, float]]–Ascending pieces.
maximise_energy
maximise_energy(
experiment: Experiment,
objective: Callable[[list[WedgeSummary], Any], Any],
low: float,
high: float,
*,
edges: Iterable[float] = (),
tolerance: float = 0.01,
clearance: float = _EDGE_CLEARANCE,
) -> list[Search]
Search for the beam energy that maximises an objective.
The range is split at the crystal's and the container's absorption
edges, and at any edges the objective itself adds -- a detector's,
say -- and each piece is searched with :func:maximise_on_interval.
Parameters:
-
experiment(Experiment) –The configuration; everything but the energy is held fixed.
-
objective(Callable[[list[WedgeSummary], Any], Any]) –Takes the summaries :func:
solvereturns and the energy, both carrying the gradient, and returns the scalar to maximise. -
low(float) –The energy range, in keV -- what the beamline can reach.
-
high(float) –The energy range, in keV -- what the beamline can reach.
-
edges(Iterable[float], default:()) –Further energies at which the objective jumps.
-
tolerance(float, default:0.01) –How closely to locate each piece's maximum, in keV.
-
clearance(float, default:_EDGE_CLEARANCE) –How far to keep from each edge, in keV. The default only keeps the search off the edge itself. Absorption edges in the model are sharp steps; real ones have fine structure for tens of eV around them, and a beam has an energy spread, so a strategy meant for a beamline should keep further away.
Returns:
-
list[Search]–One per piece, in ascending energy. The best energy overall is
max(searches, key=lambda s: s.best[1]).best[0].
Detector sensors
detector
How much of the diffracted beam a detector's sensor absorbs.
Not part of RADDOSE-3D. Its diffraction efficiency counts the photons the crystal scatters elastically; whether a detector records them depends on the sensor, and above all on the energy. A thin silicon sensor stops nearly every 8 keV photon and lets most 25 keV photons straight through, so an energy that is best for the crystal can be poor for the experiment.
The fraction absorbed is 1 - exp(-(mu/rho) rho t), with mu/rho from
the photoelectric cross-sections of the same McMaster parameterisation the
crystal uses: photoabsorption is what turns a photon into a signal, and
scattering mostly does not. Treat it as an upper bound on the detector's
quantum efficiency -- it leaves out charge sharing, fluorescence escape,
counting thresholds and dead area.
Sensor
dataclass
A sensor material.
Attributes:
-
formula(dict[str, float]) –Element symbol to atoms per formula unit.
-
density(float) –Density in g/cm^3.
sensor_absorption
sensor_absorption(
energy: Array,
material: str = "Si",
thickness: float = 450.0,
) -> Array
The fraction of photons a sensor absorbs.
Parameters:
-
energy(Array) –Photon energy in keV. A tensor result carries a gradient.
-
material(str, default:'Si') –A key of :data:
SENSORS. -
thickness(float, default:450.0) –Sensor thickness in micrometres.
Returns:
-
Array–Between 0 and 1, the shape of
energy.
sensor_edges
sensor_edges(material: str) -> tuple[float, ...]
Energies at which a sensor's absorption jumps, in keV.
Parameters:
-
material(str) –A key of :data:
SENSORS.
Returns:
-
tuple[float, ...]–Ascending.
Overrides
apply_overrides
apply_overrides(
experiment: Experiment,
overrides: Mapping[str, Any] | None = None,
) -> Experiment
The experiment with its overrides written in: what is actually run.
Overrides carry values as tensors so that a gradient can reach them. This writes their current values back into the configuration as plain numbers, so the result can be saved as YAML, run again without torch, or written out as RADDOSE-3D's files -- the strategy an optimiser found, in a form anything else can use.
Parameters:
-
experiment(Experiment) –The configuration.
-
overrides(Mapping[str, Any], default:None) –Paths to values, as passed to :func:
solve.
Returns:
-
Experiment–A validated copy with each overridden setting replaced.
Raises:
-
KeyError–If a path is not tunable, names a wedge the experiment does not have, or tunes a collimation or width the beam does not have. :func:
simulatewould otherwise never read such an override, and ignore it without a word. -
ValidationError–If a value is not a valid setting -- a negative flux, say.