|
| 1 | +# Statistics Standard Schema |
| 2 | + |
| 3 | +This document describes the YAML schema used to define particle beam statistics in openPMD-beamphysics. The schema provides a structured, maintainable format for documenting statistical quantities with their mathematical definitions, units, and references. |
| 4 | + |
| 5 | +## Schema Version |
| 6 | + |
| 7 | +The current schema version is **1.0**. |
| 8 | + |
| 9 | +The schema version is specified at the top level of the YAML file: |
| 10 | + |
| 11 | +```yaml |
| 12 | +schema_version: "1.0" |
| 13 | +``` |
| 14 | +
|
| 15 | +### Versioning Policy |
| 16 | +
|
| 17 | +- **Major version** (X.0): Breaking changes that require updates to existing entries |
| 18 | +- **Minor version** (1.X): Additive changes (new optional fields, new categories) |
| 19 | +
|
| 20 | +## File Location |
| 21 | +
|
| 22 | +The statistics standard is defined in: |
| 23 | +
|
| 24 | +``` |
| 25 | +pmd_beamphysics/statistics_standard/statistics_standard.yaml |
| 26 | +``` |
| 27 | + |
| 28 | +## Top-Level Structure |
| 29 | + |
| 30 | +```yaml |
| 31 | +schema_version: "1.0" |
| 32 | + |
| 33 | +categories: |
| 34 | + - id: <category_id> |
| 35 | + name: <Category Name> |
| 36 | + description: <Category description> |
| 37 | + |
| 38 | +statistics: |
| 39 | + - label: <statistic_label> |
| 40 | + mathlabel: <LaTeX string> |
| 41 | + units: <unit string> |
| 42 | + description: <verbose description> |
| 43 | + reference: <reference string> |
| 44 | + category: <category_id> |
| 45 | + # Optional fields: |
| 46 | + formula: <LaTeX formula> |
| 47 | + reference_url: <URL> |
| 48 | + aliases: [<alias1>, <alias2>] |
| 49 | +``` |
| 50 | +
|
| 51 | +## Categories |
| 52 | +
|
| 53 | +Categories group related statistics for organized documentation. Each category has: |
| 54 | +
|
| 55 | +| Field | Type | Required | Description | |
| 56 | +|-------|------|----------|-------------| |
| 57 | +| `id` | string | Yes | Unique identifier used to reference from statistics | |
| 58 | +| `name` | string | Yes | Human-readable category name | |
| 59 | +| `description` | string | Yes | Brief description of the category | |
| 60 | + |
| 61 | +### Standard Categories |
| 62 | + |
| 63 | +| ID | Name | Description | |
| 64 | +|----|------|-------------| |
| 65 | +| `coordinates` | Phase Space Coordinates | Basic position and momentum coordinates | |
| 66 | +| `time` | Time Coordinate | Time-related quantities | |
| 67 | +| `relativistic` | Relativistic Quantities | Energy, momentum, velocity factors | |
| 68 | +| `slopes` | Transverse Slopes | Angular divergence quantities | |
| 69 | +| `polar` | Polar/Cylindrical Coordinates | Cylindrical coordinate representations | |
| 70 | +| `normalized` | Normalized Coordinates | Courant-Snyder normalized coordinates | |
| 71 | +| `emittance` | Emittance | Phase space volume measures | |
| 72 | +| `twiss` | Twiss Parameters | Courant-Snyder lattice functions | |
| 73 | +| `beam` | Beam Properties | Integrated beam properties | |
| 74 | +| `particle` | Particle Properties | Individual particle properties | |
| 75 | + |
| 76 | +## Statistics Entry Fields |
| 77 | + |
| 78 | +### Required Fields |
| 79 | + |
| 80 | +| Field | Type | Description | Example | |
| 81 | +|-------|------|-------------|---------| |
| 82 | +| `label` | string | Code key used to access the statistic in ParticleGroup | `"norm_emit_x"` | |
| 83 | +| `mathlabel` | string | LaTeX string for mathematical symbol (without `$` delimiters) | `"\epsilon_{n,x}"` | |
| 84 | +| `units` | string | Unit string compatible with `pmd_unit` | `"m"`, `"eV/c"`, `"1"` | |
| 85 | +| `description` | string | Verbose human-readable description | `"Normalized RMS emittance..."` | |
| 86 | +| `reference` | string | Citation or reference for the definition | `"Courant & Snyder, Ann. Phys. 3 (1958)"` | |
| 87 | +| `category` | string | Category ID this statistic belongs to | `"emittance"` | |
| 88 | + |
| 89 | +### Optional Fields |
| 90 | + |
| 91 | +| Field | Type | Description | Example | |
| 92 | +|-------|------|-------------|---------| |
| 93 | +| `formula` | string | LaTeX formula for derived quantities | `"\epsilon = \sqrt{\det(\Sigma)}"` | |
| 94 | +| `reference_url` | string | URL/DOI link to the reference | `"https://doi.org/10.1016/..."` | |
| 95 | +| `aliases` | list[string] | Alternative labels for the same quantity | `["emit_x", "emittance_x"]` | |
| 96 | + |
| 97 | +## Field Validation Rules |
| 98 | + |
| 99 | +### `label` |
| 100 | + |
| 101 | +- Must be a valid Python identifier or contain only alphanumeric characters, underscores, and `/` |
| 102 | +- Must be unique across all statistics entries |
| 103 | +- Should match the attribute/key name used in `ParticleGroup` |
| 104 | +- Examples: `"x"`, `"norm_emit_x"`, `"z/c"`, `"twiss_beta_x"` |
| 105 | + |
| 106 | +### `mathlabel` |
| 107 | + |
| 108 | +- Must be valid LaTeX (will be wrapped in `$...$` for rendering) |
| 109 | +- Use raw strings or escape backslashes properly in YAML |
| 110 | +- Common patterns: |
| 111 | + - Greek letters: `\alpha`, `\beta`, `\gamma`, `\epsilon` |
| 112 | + - Subscripts: `x_0`, `p_x`, `\beta_x` |
| 113 | + - Overlines: `\overline{x}` |
| 114 | + - Text in math: `\text{kinetic}`, `\mathrm{avg}` |
| 115 | + |
| 116 | +### `units` |
| 117 | + |
| 118 | +- Must be a unit string recognized by `pmd_unit` or a compound expression |
| 119 | +- Use `"1"` for dimensionless quantities |
| 120 | +- Standard units: |
| 121 | + - Length: `"m"` |
| 122 | + - Time: `"s"` |
| 123 | + - Energy: `"eV"` |
| 124 | + - Momentum: `"eV/c"` |
| 125 | + - Charge: `"C"` |
| 126 | + - Current: `"A"` |
| 127 | + - Angle: `"rad"` |
| 128 | +- Compound units: `"m*eV/c"`, `"m^2"`, `"1/m"` |
| 129 | +- Special: `"sqrt(m)"` for normalized coordinates |
| 130 | + |
| 131 | +### `description` |
| 132 | + |
| 133 | +- Should be a complete sentence or phrase |
| 134 | +- Include physical meaning and context |
| 135 | +- Use YAML multiline syntax for long descriptions: |
| 136 | + |
| 137 | +```yaml |
| 138 | +description: >- |
| 139 | + Normalized RMS emittance in the horizontal plane. |
| 140 | + This quantity is invariant under linear symplectic transport. |
| 141 | +``` |
| 142 | + |
| 143 | +### `formula` |
| 144 | + |
| 145 | +- LaTeX math expression (without delimiters) |
| 146 | +- Use standard notation consistent with references |
| 147 | +- Include defining equations for derived quantities |
| 148 | + |
| 149 | +### `reference` |
| 150 | + |
| 151 | +- Free-form string for citation |
| 152 | +- Preferred format: `"Author, Journal Volume, Pages (Year)"` |
| 153 | +- For internal standards: `"openPMD-beamphysics standard"` |
| 154 | + |
| 155 | +### `reference_url` |
| 156 | + |
| 157 | +- Full URL (preferably DOI link) |
| 158 | +- Format: `"https://doi.org/10.xxxx/xxxxx"` |
| 159 | + |
| 160 | +## Example Entry |
| 161 | + |
| 162 | +```yaml |
| 163 | +- label: norm_emit_x |
| 164 | + mathlabel: \epsilon_{n,x} |
| 165 | + units: m |
| 166 | + description: >- |
| 167 | + Normalized RMS emittance in the horizontal plane. |
| 168 | + Invariant under linear transport for a relativistic beam. |
| 169 | + formula: \epsilon_{n,x} = \frac{1}{mc} \sqrt{\langle x^2 \rangle \langle p_x^2 \rangle - \langle x p_x \rangle^2} |
| 170 | + reference: "Wiedemann, Particle Accelerator Physics (Springer)" |
| 171 | + reference_url: https://doi.org/10.1007/978-3-319-18317-6 |
| 172 | + category: emittance |
| 173 | +``` |
| 174 | + |
| 175 | +## Adding New Statistics |
| 176 | + |
| 177 | +To add a new statistic: |
| 178 | + |
| 179 | +1. Identify the appropriate category (or create a new one if needed) |
| 180 | +2. Add an entry to the `statistics` list with all required fields |
| 181 | +3. Include `formula` if the quantity is derived from other quantities |
| 182 | +4. Add `reference_url` if a DOI or stable URL is available |
| 183 | +5. Run the validation script to check for errors: |
| 184 | + ```bash |
| 185 | + python scripts/generate_statistics_docs.py --validate |
| 186 | + ``` |
| 187 | +6. Regenerate the documentation: |
| 188 | + ```bash |
| 189 | + python scripts/generate_statistics_docs.py |
| 190 | + ``` |
| 191 | + |
| 192 | +## Computed Statistics |
| 193 | + |
| 194 | +The `ParticleGroup` class supports computed statistics via bracket access with prefixes: |
| 195 | + |
| 196 | +| Prefix | Example | Computes | |
| 197 | +|--------|---------|----------| |
| 198 | +| `sigma_` | `P['sigma_x']` | Standard deviation | |
| 199 | +| `mean_` | `P['mean_x']` | Weighted average | |
| 200 | +| `min_` | `P['min_x']` | Minimum value | |
| 201 | +| `max_` | `P['max_x']` | Maximum value | |
| 202 | +| `ptp_` | `P['ptp_x']` | Peak-to-peak (max - min) | |
| 203 | +| `delta_` | `P['delta_x']` | Value minus mean | |
| 204 | +| `cov_X__Y` | `P['cov_x__px']` | Covariance element | |
| 205 | + |
| 206 | +These derived quantities do not need individual entries in the YAML file, as their units and labels are computed automatically from the base quantity. |
| 207 | + |
| 208 | +## Integration with Code |
| 209 | + |
| 210 | +The statistics standard YAML file can be loaded and used programmatically: |
| 211 | + |
| 212 | +```python |
| 213 | +import yaml |
| 214 | +from pathlib import Path |
| 215 | +
|
| 216 | +# Load the standard |
| 217 | +yaml_path = Path(__file__).parent / "statistics_standard.yaml" |
| 218 | +with open(yaml_path) as f: |
| 219 | + standard = yaml.safe_load(f) |
| 220 | +
|
| 221 | +# Access statistics |
| 222 | +for stat in standard['statistics']: |
| 223 | + print(f"{stat['label']}: {stat['units']}") |
| 224 | +``` |
0 commit comments