Getting started¶
pyRADDOSE-3D calculates the radiation dose a crystal absorbs during a diffraction experiment, voxel by voxel and image by image, as RADDOSE-3D does. This notebook describes an experiment, runs it, reads the results and writes RADDOSE-3D's output files.
Describing an experiment¶
An experiment is a crystal, a beam, and one or more wedges of rotation.
They are usually written down in a YAML file. This is inputs/insulin.yaml,
the insulin crystal from RADDOSE-3D's own example input:
from pathlib import Path
from pyraddose_3d.models.experiment import Experiment
INPUTS = Path("inputs")
print((INPUTS / "insulin.yaml").read_text())
##############################################
### Dose calculation configuration example ###
##############################################
# The insulin crystal from RADDOSE-3D's own example input, MyInput.txt --
# insulin.txt here is that file, and describes the same experiment. Load
# with `Experiment.from_file(path)`, which reads either.
#
# A setting left out takes RADDOSE-3D's default, so leaving it out of this
# file means what leaving it out of a .txt input means. The .txt keyword for
# each setting is given in brackets.
crystal:
type: cuboid # [Type] cuboid, spherical, cylinder or polyhedron
dimensions: [100.0, 100.0, 100.0] # [Dimensions] x, y, z in micrometres
pixels_per_micron: 0.1 # [PixelsPerMicron] default 10 voxels across x
# angle_p: 0.0 # [AngleP] rotation in the plane of the loop, degrees
# angle_l: 0.0 # [AngleL] bend of the loop, degrees
# decay_model: simple # [DDM] simple, linear, bfactor or leal
composition: # [AbsCoefCalc RD3D]
method: unit_cell
unit_cell: [78.02, 78.02, 78.02] # [UnitCell] a, b, c in Angstroms
num_monomers: 24 # [NumMonomers] per unit cell
num_residues: 51 # [NumResidues] amino acids per monomer
protein_heavy_atoms: # [ProteinHeavyAtoms] atoms per monomer
Zn: 0.333
S: 6.0
solvent_heavy_conc: # [SolventHeavyConc] mmol/l
P: 425.0
solvent_fraction: 0.64 # [SolventFraction] computed if left out
# escape: # Monte Carlo, so see seed below
# photoelectron: true # [CalculatePEEscape] electrons leaving the crystal
# fluorescence: true # [CalculateFLEscape] and fluorescence photons
beam:
type: gaussian # [Type] gaussian, tophat or experimental
flux: 2.0e+12 # [Flux] photons per second
energy: 12.1 # [Energy] keV
fwhm: [20.0, 70.0] # [FWHM] x, y in micrometres
collimation: # [Collimation] leave out for no aperture
shape: rectangular # rectangular or circular
x: 100.0 # micrometres
y: 100.0
wedges: # one per [Wedge] block, exposed in order
- start: 0.0 # [Wedge] start and end angles in degrees
end: 90.0
exposure_time: 50.0 # [ExposureTime] seconds for the whole wedge
# angular_resolution: 2.0 # [AngularResolution] degrees per image
- start: 90.0
end: 180.0
exposure_time: 10.0
# seed: 20260917 # [--seed] escape's random numbers; chosen if left out
experiment = Experiment.from_file(INPUTS / "insulin.yaml")
experiment.beam
GaussianBeam(flux=2000000000000.0, energy=12.1, collimation=Collimation(shape='rectangular', x=100.0, y=100.0), energy_fwhm=None, pulse_energy=None, pulse_shape=None, pulse_window=None, type='gaussian', fwhm=(20.0, 70.0))
An existing RADDOSE-3D input file is read just as well -- from_file reads
anything not ending in .yaml or .yml as one. insulin.txt is
RADDOSE-3D's MyInput.txt, unmodified, and it describes the same
experiment:
Experiment.from_file(INPUTS / "insulin.txt") == experiment
True
Every setting is checked when it is read, so a mistake is an error at load time rather than a wrong dose later. A setting RADDOSE-3D requires is required here, and one it defaults is defaulted to the same value:
from pydantic import ValidationError
try:
Experiment.model_validate(
{**experiment.model_dump(), "beam": {"type": "gaussian", "flux": -2e12}}
)
except ValidationError as error:
print(error)
3 validation errors for Experiment
beam.gaussian.flux
Input should be greater than 0 [type=greater_than, input_value=-2000000000000.0, input_type=float]
For further information visit https://errors.pydantic.dev/2.13/v/greater_than
beam.gaussian.energy
Field required [type=missing, input_value={'type': 'gaussian', 'flux': -2000000000000.0}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.13/v/missing
beam.gaussian.fwhm
Field required [type=missing, input_value={'type': 'gaussian', 'flux': -2000000000000.0}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.13/v/missing
Running it¶
solve returns one summary per wedge: the sixteen numbers RADDOSE-3D
reports.
from pyraddose_3d.solve import solve
for summary in solve(experiment):
print(
f"wedge {summary.wedge_number}: "
f"average DWD {float(summary.average_dwd):6.2f} MGy, "
f"max dose {float(summary.max_dose):6.2f} MGy, "
f"efficiency {float(summary.diffraction_efficiency):.3g} photons/MGy"
)
wedge 1: average DWD 6.27 MGy, max dose 37.48 MGy, efficiency 3.47e+10 photons/MGy wedge 2: average DWD 8.87 MGy, max dose 44.97 MGy, efficiency 4.87e+09 photons/MGy
simulate returns everything behind those numbers: the voxel grid, the
dose in every voxel, and -- with record_images=True -- the dose and
fluence of every voxel after every image. From it, RADDOSE-3D's printed
summary:
from pyraddose_3d.outputs import summary_text
from pyraddose_3d.solve import simulate
simulation = simulate(experiment, record_images=True)
print(summary_text(simulation))
Cuboid (Polyhedron) crystal of size [100, 100, 100] um [x, y, z] at a resolution of 10.00 microns per voxel edge. Simple DDM. Gaussian beam, 100.0x100.0 um with 20.00 by 70.00 FWHM (x by y) and 2.0e+12 photons per second at 12.10 keV. Wedge 1: Collecting data for a total of 50.0s from phi = 0.0 to 90.0 deg. Crystal coefficients calculated with RADDOSE-3D. Photelectric Coefficient: 3.10e-04 /um. Inelastic Coefficient: 1.78e-05 /um. Elastic Coefficient: 2.01e-05 /um. Attenuation Coefficient: 3.48e-04 /um. Density: 1.09 g/ml. Average Diffraction Weighted Dose : 6.269605 MGy Last Diffraction Weighted Dose : 10.560162 MGy Elastic Yield : 2.18e+11 photons Diffraction Efficiency (Elastic Yield/DWD): 3.47e+10 photons/MGy Average Dose (Whole Crystal) : 5.951007 MGy Average Dose (Exposed Region) : 5.951007 MGy Max Dose : 37.477703 MGy Average Dose (95.0 % of total absorbed energy threshold (2.86 MGy)): 9.549758 MGy Dose Contrast (Max/Threshold Av.) : 3.92 Used Volume : 100.0% Absorbed Energy (this Wedge) : 6.64e-03 J. Dose Inefficiency (Max Dose/mJ Absorbed) : 5645770.7 1/g Dose Inefficiency PE (Max Dose/mJ Deposited): 5767122.3 1/g Final Dose Histogram: Bin 1, 0.0 to 0.1 MGy: 16.6 % Bin 2, 0.1 to 3.4 MGy: 25.9 % Bin 3, 3.4 to 6.7 MGy: 21.0 % Bin 4, 6.7 to 10.1 MGy: 16.2 % Bin 5, 10.1 to 13.4 MGy: 9.0 % Bin 6, 13.4 to 16.7 MGy: 5.0 % Bin 7, 16.7 to 20.0 MGy: 2.2 % Bin 8, 20.0 to 23.4 MGy: 1.4 % Bin 9, 23.4 to 26.7 MGy: 1.2 % Bin 10, 26.7 to 30.0 MGy: 1.2 % Bin 11, 30.0 MGy upwards: 0.3 % Wedge 2: Collecting data for a total of 10.0s from phi = 90.0 to 180.0 deg. Crystal coefficients calculated with RADDOSE-3D. Photelectric Coefficient: 3.10e-04 /um. Inelastic Coefficient: 1.78e-05 /um. Elastic Coefficient: 2.01e-05 /um. Attenuation Coefficient: 3.48e-04 /um. Density: 1.09 g/ml. Average Diffraction Weighted Dose : 8.872101 MGy Last Diffraction Weighted Dose : 12.867242 MGy Elastic Yield : 4.32e+10 photons Diffraction Efficiency (Elastic Yield/DWD): 4.87e+09 photons/MGy Average Dose (Whole Crystal) : 7.133724 MGy Average Dose (Exposed Region) : 7.133724 MGy Max Dose : 44.973244 MGy Average Dose (95.0 % of total absorbed energy threshold (2.30 MGy)): 9.072340 MGy Dose Contrast (Max/Threshold Av.) : 4.96 Used Volume : 100.0% Absorbed Energy (this Wedge) : 1.32e-03 J. Dose Inefficiency (Max Dose/mJ Absorbed) : 34088998.2 1/g Dose Inefficiency PE (Max Dose/mJ Deposited): 5773173.2 1/g Final Dose Histogram: Bin 1, 0.0 to 0.1 MGy: 0.0 % Bin 2, 0.1 to 3.4 MGy: 36.3 % Bin 3, 3.4 to 6.7 MGy: 22.4 % Bin 4, 6.7 to 10.1 MGy: 18.1 % Bin 5, 10.1 to 13.4 MGy: 10.1 % Bin 6, 13.4 to 16.7 MGy: 4.4 % Bin 7, 16.7 to 20.0 MGy: 3.8 % Bin 8, 20.0 to 23.4 MGy: 1.2 % Bin 9, 23.4 to 26.7 MGy: 1.2 % Bin 10, 26.7 to 30.0 MGy: 0.2 % Bin 11, 30.0 MGy upwards: 2.3 %
Writing RADDOSE-3D's files¶
write_outputs writes the files RADDOSE-3D writes by default, under the
same names and in the same formats, so anything that reads RADDOSE-3D's
output reads these unchanged.
import tempfile
from pyraddose_3d.outputs import write_outputs
directory = Path(tempfile.mkdtemp())
for path in write_outputs(simulation, prefix=str(directory / "output-")):
print(f"{path.name:28s} {path.stat().st_size:>9,d} bytes")
output-Summary.csv 607 bytes output-Summary.txt 3,160 bytes output-DoseState.csv 50,078 bytes output-DoseState.R 11,563 bytes output-RDE.csv 1,931 bytes output-DWDs.csv 12,560 bytes output-VoxelDose.csv 452,617 bytes output-VoxelFluences.csv 578,681 bytes
A first look at the dose¶
The final dose of every voxel inside the crystal, as DoseState.csv lists
it. The beam is narrower than the crystal, so most of it gets very little:
import matplotlib.pyplot as plt
from pyraddose_3d.outputs import final_dose_state
state = final_dose_state(simulation)
fig, ax = plt.subplots(figsize=(6, 3.5))
ax.hist(state["dose"], bins=50)
ax.set_xlabel("dose after both wedges (MGy)")
ax.set_ylabel("voxels")
ax.set_yscale("log")
plt.show()