"""Consistent unitful directional scattering data for solves and cuts."""
from dataclasses import dataclass
from typing import Any, Literal
import numpy as np
from . import plotting
from .units import Quantity, validate_quantity, ureg
[docs]
@dataclass(frozen=True, kw_only=True, repr=False)
class AngularData:
"""Group coordinates, amplitudes, intensities and normalization.
Full angular data have shape (order, polar angle, azimuth). Cuts and
explicitly averaged curves have shape (order, observation). Directions
have the same observation axes and a final Cartesian axis of size three.
Amplitudes add incident-polarization and vector axes of sizes one or two and
three. Ensemble amplitudes are absent: intensities, not amplitudes, are
averaged across realizations. Arrays are copied, unitful and read-only.
"""
differential: Quantity
directions: Quantity | None = None
amplitudes: Quantity | None = None
angles: Quantity | None = None
azimuths: Quantity | None = None
term_differential: Quantity | None = None
stderr: Quantity | None = None
azimuth_stderr: Quantity | None = None
mu_s: Quantity | None = None
sample_volume: Quantity | None = None
kind: Literal["volume", "ensemble"] = "volume"
azimuth_averaged: bool = False
meridian_azimuth: Quantity | None = None
def __post_init__(self) -> None:
"""Validate, copy, and freeze all supplied data.
Raises
------
ValueError
If units, numeric values, axis lengths, or data relationships are invalid.
"""
units = {
"differential": "1 / meter / steradian",
"directions": "dimensionless",
"amplitudes": "meter",
"angles": "radian",
"azimuths": "radian",
"term_differential": "1 / meter / steradian",
"stderr": "1 / meter / steradian",
"azimuth_stderr": "1 / meter / steradian",
"mu_s": "1 / meter",
"sample_volume": "meter**3",
"meridian_azimuth": "radian",
}
for name, unit in units.items():
value = getattr(self, name)
if value is not None:
if unit == "dimensionless" and not isinstance(value, Quantity):
value = np.array(value, copy=True) * ureg.dimensionless
validate_quantity(
value,
unit=unit,
name=name,
finite=False,
)
if not np.issubdtype(np.asarray(value.magnitude).dtype, np.number):
raise ValueError(f"{name} must contain numeric values.")
quantity = np.array(value.magnitude, copy=True) * value.units
quantity.magnitude.setflags(write=False)
object.__setattr__(self, name, quantity)
shape = self.differential.shape
if len(shape) not in (2, 3) or 0 in shape:
raise ValueError("differential must have nonempty order and observation axes.")
if self.directions is not None and self.directions.shape != (*shape[1:], 3):
raise ValueError("directions must match the observation axes.")
valid_amplitudes = self.amplitudes is None or (
self.amplitudes.shape[:-2] == shape and self.amplitudes.shape[-2:] in ((1, 3), (2, 3))
)
if not valid_amplitudes:
raise ValueError("amplitudes must have shape matching the order and observation axes.")
self._validate_values()
if self.directions is None and not self.azimuth_averaged:
theta = getattr(self, "angles").to("radian").magnitude
if self.azimuths is not None:
theta, phi = np.meshgrid(theta, self.azimuths.to("radian").magnitude, indexing="ij")
else:
phi = np.zeros_like(theta)
directions = np.stack([np.sin(theta) * np.cos(phi), np.sin(theta) * np.sin(phi), np.cos(theta)], axis=-1)
quantity = directions * ureg.dimensionless
quantity.magnitude.setflags(write=False)
object.__setattr__(self, "directions", quantity)
def _validate_values(self) -> None:
"""Validate array shapes, value ranges, and cross-field constraints.
Raises
------
ValueError
If any stored quantity is inconsistent with the data contract.
"""
shape = self.differential.shape
shapes = {
"angles": (shape[1],),
"azimuths": (shape[2],) if len(shape) == 3 else (),
"term_differential": shape,
"stderr": shape,
"azimuth_stderr": shape[:2],
"mu_s": (shape[0],),
"sample_volume": (),
"meridian_azimuth": (),
}
if shape[0] > 12:
raise ValueError("differential must contain at most 12 Born orders.")
if self.kind not in ("volume", "ensemble") or not isinstance(self.azimuth_averaged, bool):
raise ValueError("kind or azimuth_averaged is invalid.")
if len(shape) == 3 and (self.angles is None or self.azimuths is None or self.azimuth_averaged):
raise ValueError("Full angular data require angles and azimuths without averaging.")
if len(shape) == 2 and self.azimuths is not None:
raise ValueError("azimuths require full angular data.")
if self.angles is None and self.directions is None:
raise ValueError("angles or directions must identify observations.")
for name in ("differential", "directions", "amplitudes", *shapes):
quantity = getattr(self, name)
if quantity is None:
continue
values = quantity.magnitude
if name in shapes and values.shape != shapes[name]:
raise ValueError(f"{name} must have shape {shapes[name]}.")
real_required = name != "amplitudes"
if not np.issubdtype(values.dtype, np.number) or (real_required and np.iscomplexobj(values)):
raise ValueError(
f"{name} must contain numeric {'real' if real_required else 'complex or real'} values."
)
unknown_error = (
name in ("stderr", "azimuth_stderr") and self.kind == "ensemble" and np.all(np.isnan(values))
)
if not unknown_error and np.any(~np.isfinite(values)):
raise ValueError(f"{name} must contain finite values.")
nonnegative = name in ("differential", "term_differential", "stderr", "azimuth_stderr", "mu_s")
if nonnegative and np.any(values < 0):
raise ValueError(f"{name} must be nonnegative.")
invalid_directions = self.directions is not None and not np.allclose(
np.linalg.norm(self.directions.magnitude, axis=-1), 1, rtol=0, atol=1e-10
)
if invalid_directions:
raise ValueError("directions must contain unit vectors.")
if self.kind != "volume" and self.angles is None:
raise ValueError("ensemble results require angles.")
if self.directions is not None and self.angles is not None:
expected_cosine = np.cos(self.angles).magnitude
if len(shape) == 3:
expected_cosine = expected_cosine[:, None]
if not np.allclose(self.directions.magnitude[..., 2], expected_cosine, rtol=0, atol=1e-10):
raise ValueError("angles must match the polar angles of directions.")
if self.angles is not None and np.any((self.angles < 0) | (self.angles > np.pi * ureg.radian)):
raise ValueError("angles must lie between zero and pi.")
if self.azimuths is not None:
phi = self.azimuths.to("radian").magnitude
invalid_phi = not 4 <= len(phi) <= 32 or not np.allclose(
phi, np.arange(len(phi)) * 2 * np.pi / len(phi), atol=1e-12, rtol=0
)
if invalid_phi:
raise ValueError("azimuths must uniformly cover [0, 2*pi), starting at zero.")
if self.sample_volume is not None and self.sample_volume.magnitude <= 0:
raise ValueError("sample_volume must describe a positive finite-sample volume.")
unnormalized_cut = (
self.kind == "volume" and len(shape) == 2 and not self.azimuth_averaged and self.meridian_azimuth is None
)
if unnormalized_cut and self.mu_s is not None:
raise ValueError("integrated coefficients are unavailable for a single-volume angular cut.")
if self.azimuth_averaged and (self.directions is not None or self.amplitudes is not None):
raise ValueError("Azimuth-averaged data have no single directions or coherent amplitudes.")
if self.kind != "volume" and self.amplitudes is not None:
raise ValueError("amplitudes are only available for a single volume.")
if self.kind != "ensemble" and (self.stderr is not None or self.azimuth_stderr is not None):
raise ValueError("stderr and azimuth_stderr are only available for an ensemble.")
@property
def phase_function(self) -> Quantity:
"""Return the differential density normalized over solid angle.
Returns
-------
Quantity
Phase function in inverse steradians, with the same shape as
``differential``.
Raises
------
ValueError
If positive finite integrated ``mu_s`` values are unavailable.
"""
if self.mu_s is None or np.any(~np.isfinite(self.mu_s.magnitude)) or np.any(self.mu_s.magnitude <= 0):
raise ValueError("Phase normalization requires positive, finite integrated mu_s.")
axes = (len(self.mu_s),) + (1,) * (self.differential.ndim - 1)
return (self.differential / self.mu_s.reshape(axes)).to("1 / steradian")
@property
def differential_cross_section(self) -> Quantity:
"""Return the finite-sample differential cross section.
Returns
-------
Quantity
Differential cross section in square metres per steradian, with
the same shape as ``differential``.
Raises
------
ValueError
If ``sample_volume`` is unavailable.
"""
if self.sample_volume is None:
raise ValueError("sample_volume is unavailable for this angular data.")
return (self.differential * self.sample_volume).to("meter**2 / steradian")
[docs]
def azimuth_average(self) -> "AngularData":
"""Average intensities over the sampled azimuth axis.
Coherent amplitudes and directions are omitted because averaging them
would not represent the intensity average.
Returns
-------
AngularData
New data with shape ``(order, polar angle)`` and
``azimuth_averaged=True``.
Raises
------
ValueError
If this object does not contain a full azimuth grid.
"""
if self.azimuths is None:
raise ValueError("Azimuth averaging requires a full angular grid.")
return AngularData(
differential=self.differential.mean(axis=-1),
angles=self.angles,
term_differential=None if self.term_differential is None else self.term_differential.mean(axis=-1),
stderr=self.azimuth_stderr,
mu_s=self.mu_s,
sample_volume=self.sample_volume,
kind=self.kind,
azimuth_averaged=True,
)
[docs]
def meridian(self, *, azimuth: Quantity, method: Literal["exact", "nearest"] = "exact") -> "AngularData":
"""Select one sampled meridian while preserving normalization.
Azimuth is periodic modulo :math:`2\\pi`. ``exact`` requires a sample
within ``1e-10`` radians; ``nearest`` selects the closest sample. No
interpolation or averaging is performed. The selected angle is stored
in ``meridian_azimuth``.
Parameters
----------
azimuth : Quantity
Requested azimuth with explicit angular units; must be scalar and finite.
method : {'exact', 'nearest'}, default='exact'
Whether to require a sampled angle or select its nearest sample.
Returns
-------
AngularData
Data for the selected meridian, with shape ``(order, polar angle)``.
Raises
------
ValueError
If the data do not have an azimuth grid, the request is invalid,
or ``method='exact'`` has no matching sample.
"""
if self.azimuths is None:
raise ValueError("Meridian selection requires a full angular grid.")
if method not in ("exact", "nearest"):
raise ValueError("method must be exact or nearest.")
validate_quantity(
azimuth,
unit="radian",
name="azimuth",
scalar=True,
)
requested = azimuth.to("radian").magnitude
if not np.isfinite(requested):
raise ValueError("azimuth must be finite.")
phi = self.azimuths.to("radian").magnitude
distance = np.abs((phi - requested + np.pi) % (2 * np.pi) - np.pi)
index = int(np.argmin(distance))
if method == "exact" and distance[index] > 1e-10:
raise ValueError("azimuth is not sampled; use method='nearest' to select the closest meridian.")
return AngularData(
differential=self.differential[..., index],
angles=self.angles,
directions=getattr(self, "directions")[:, index],
amplitudes=None if self.amplitudes is None else self.amplitudes[:, :, index],
term_differential=None if self.term_differential is None else self.term_differential[..., index],
stderr=None if self.stderr is None else self.stderr[..., index],
mu_s=self.mu_s,
sample_volume=self.sample_volume,
kind=self.kind,
meridian_azimuth=phi[index] * ureg.radian,
)
[docs]
def plot(
self,
*,
terms: bool = False,
log_y: bool = False,
title: str | None = None,
azimuth: int = 0,
) -> Any:
"""Plot differential scattering on a meridian or averaged curve.
Parameters
----------
terms : bool, default=False
Plot isolated Born terms instead of cumulative intensities.
log_y : bool, default=False
Use a logarithmic vertical axis.
title : str or None, default=None
Optional custom figure title.
azimuth : int, default=0
Sample index used when selecting a meridian from full angular data.
Returns
-------
matplotlib.figure.Figure
Figure containing the requested differential-scattering curves.
Raises
------
ValueError
If the requested meridian cannot be selected.
"""
return plotting.plot_differential(
result=self,
terms=terms,
log_y=log_y,
title=title,
azimuth=azimuth,
)
[docs]
def plot_phase_function(
self,
*,
view: Literal["angular", "polar", "3d"] = "3d",
order: int | None = None,
log_y: bool = False,
azimuth: int = 0,
backend: Literal["matplotlib", "plotly"] | None = None,
) -> Any:
"""Plot normalized directional densities.
Parameters
----------
view : {'angular', 'polar', '3d'}, default='3d'
Angular curve, polar curve, or directional 3D surface. The 3D view
uses Plotly; 2D views use Matplotlib.
order : int or None, default=None
One-based cumulative Born order. ``None`` plots all angular curves
or the final available surface.
log_y : bool, default=False
Use a logarithmic vertical axis; supported for the angular view.
azimuth : int, default=0
Sample index used for angular and polar meridian views.
backend : {'matplotlib', 'plotly'} or None, default=None
Rendering backend. Plotly is used for 3D; Matplotlib is used for 2D.
Returns
-------
matplotlib.figure.Figure or plotly.graph_objects.Figure
Figure containing the selected phase-function view.
Raises
------
ValueError
If normalization is unavailable or the view, order, backend, or
angular sampling is incompatible.
ImportError
If Plotly is unavailable for the 3D view.
"""
return plotting.plot_phase_function(
result=self,
view=view,
order=order,
log_y=log_y,
azimuth=azimuth,
backend=backend,
)
def __repr__(self) -> str:
"""Return a concise summary of the stored angular data."""
return (
f"AngularData(shape={self.differential.shape}, intensity_unit=m^-1 sr^-1, "
f"amplitudes={'available' if self.amplitudes is not None else 'absent'}, "
f"normalized={self.mu_s is not None}, azimuth_averaged={self.azimuth_averaged})"
)