Post processing

class dynasor.post_processing.NeutronScatteringLengths(atom_types, abundances=None)[source]

This class provides sample weights corresponding to neutron scattering lengths. By default, the coherent and incoherent scattering lengths are weighted by the natural abundance of each isotope of the considered atomic species. This weighting can be overwritten using the abundances argument.

The scattering lengths have been extracted from this NIST database, which in turn have been taken from Table 1 of Neutron News 3, 26 (1992); doi: 10.1080/10448639208218770.

Parameters
  • atom_types (List[str]) – List of atomic species for which to retrieve scattering lengths.

  • abundances (Optional[Dict[str, Dict[int, float]]]) – Dict of the desired fractional abundance of each isotope for each species in the sample. For example, to use an equal weighting of all isotopes of oxygen, one can write abundances['O'] = dict(16=1/3, 17=1/3, 18=1/3). Note that the abundance for any isotopes that are not included in this dict is automatically set to zero. In other words, you need to ensure that the abundances provided sum up to 1. By default the neutron scattering lengths are weighted proportionally to the natural abundance of each isotope.

get_weight_coh(atom_type, q_norm=None)

Get the coherent weight for a given atom type and q-vector norm.

get_weight_incoh(atom_type, q_norm=None)

Get the incoherent weight for a given atom type and q-vector norm.

property supports_currents

Wether or not this Weights object supports weighting of current correlations.

property supports_incoherent

Whether or not this Weights object supports weighting of incoherent correlation functions.

class dynasor.post_processing.Weights(weights_coh, weights_incoh=None, supports_currents=True)[source]

Class holding weights and support functions for weighting of samples

Parameters
  • weights_coh (Dict[str, float]) – A dict with keys and values representing the atom types and their corresponding coherent scattering length, {'A': b_A }.

  • weights_incoh (Optional[Dict[str, float]]) – A dict with keys and values representing the atom types and their corresponding incoherent scattering length, {'A': b_A }.

  • supports_currents (bool) – whether or not the coherent weights should be applied to current-correlation functions

get_weight_coh(atom_type, q_norm=None)[source]

Get the coherent weight for a given atom type and q-vector norm.

get_weight_incoh(atom_type, q_norm=None)[source]

Get the incoherent weight for a given atom type and q-vector norm.

property supports_currents

Wether or not this Weights object supports weighting of current correlations.

property supports_incoherent

Whether or not this Weights object supports weighting of incoherent correlation functions.

class dynasor.post_processing.XRayFormFactors(atom_types, source='waasmaier-1995')[source]

This class generates sample weights corresponding to X-ray form factors, specifically the non-dispersion corrected parametrized form factors. In general, the form factor for may be written as

\[f(q, \omega) = f_0(q) + f'(q, \omega) + if''(q, \omega)\]

where \(q\) is a scalar, corresponding to the norm of the desired reciprocal lattice point.

The weights generated by this class corresponds to \(f_0(q)\). There are two possible parametrizations of \(f_0(q)\) to choose from, both based on a sum of exponentials of the form

\[f_0(q) = \sum_{i=1}^k a_i \exp(-b_i q^2) + c.\]

Two parametrizations are available:

  • 'waasmaier-1995' corresponds to Table 1 from D. Waasmaier, A. Kirfel, Acta Crystallographica Section A 51, 416 (1995); doi: 10.1107/S0108767394013292. This parametrization uses five exponentials (\(k=5\)) and extends up to \(6.0\,\mathrm{Å}^{-1}\).

  • 'international-iv-1974' corresponds to Table 2.2B from International Tables for X-ray Crystallography, Vol. IV, The Kynoch Press: Birmingham, England, 1974. This parametrization uses four exponentials (\(k=4\)) and extends up to \(2.0\,\mathrm{Å}^{-1}\).

In practice differences are expected to be insignificant. It is unlikely that you have to deviate from the default, which is 'waasmaier-1995'.

Parameters
  • atom_types (List[str]) – List of atomic species for which to retrieve scattering lengths.

  • source (str) – Source to use for parametrization of the form factors \(f_0(q)\). Allowed values are 'waasmaier-1995' and 'international-iv-1974' (see above).

get_weight_coh(atom_type, q_norm)[source]

Get the coherent weight for a given atom type and q-vector norm.

get_weight_incoh(atom_type, q_norm=None)

Get the incoherent weight for a given atom type and q-vector norm.

property supports_currents

Wether or not this Weights object supports weighting of current correlations.

property supports_incoherent

Whether or not this Weights object supports weighting of incoherent correlation functions.

dynasor.post_processing.fourier_cos_filon(f, dt)[source]

Calculates the direct Fourier cosine transform \(F(w)\) of a function \(f(t)\) using Filon’s integration method.

Parameters
  • f (ndarray[Any, dtype[float]]) – function values as a 2D array. second axis will be transformed. must contain an odd number of elements along second axis.

  • dt (float) – spacing of t-axis (\(\Delta t\))

Return type

Tuple[ndarray[Any, dtype[float]], ndarray[Any, dtype[float]]]

Returns

  • w – w containes values in the interval [0, pi/dt). length of w is f.shape[1] // 2 + 1. These frequencies corresponds to the frequencies from an fft. w == 2*np.pi*np.fft.rfftfreq(f.shape[1], dt)

  • F – transform of f along second axis. equivalent to np.fft.rfft(f, axis=1).real

Example

A common use case is

w, F = fourier_cos_filon(f, dt)
dynasor.post_processing.get_sample_averaged_over_independent_runs(samples, live_dangerously=False)[source]

Compute an averaged sample from multiple samples obtained from identical independent runs.

Note, all the meta_data and dimensions in all samples must be the same, else ValueError is raised (unless live_dangerously is set to True).

Parameters
  • samples (List[Sample]) – list of all sample objects to be averaged over

  • live_dangerously – setting True allows for averaging over samples which meta-data information is not identical.

Return type

Sample

dynasor.post_processing.get_spherically_averaged_sample_binned(sample, num_q_bins)[source]

Compute a spherical average over q-points for all the correlation functions in :attr:`sample.

Here, a q-binning method is used to conduct the spherical average, meaning all q-points are placed into spherical bins (shells). The corresponding function is calculated as the average of all q-points in a bin. If a q-bin does not contain any q-points, then its value is set to np.nan. The q_min and q_max are determined from min/max of |q_points|, and will determine the q-bin range. These will be set as bin-centers for the first and last bins repsectivley. The input parameter is the number of q-bins to use num_q_bins.

Parameters
  • sample (Sample) – Input sample

  • num_q_bins (int) – number of q-bins to use

Return type

Sample

dynasor.post_processing.get_spherically_averaged_sample_smearing(sample, q_norms, q_width)[source]

Compute a spherical average over q-points for all the correlation functions in sample.

In the gaussian average method each q-point contributes to the function value at given \(\vec{q}\) with a weight determined by a gaussian function. For example

\[F(q) = \sum_i w(\boldsymbol{q}_i, q) F(\boldsymbol{q}_i)\]

where

\[w(\boldsymbol{q}_i, q) \propto \exp{\left [ -\frac{1}{2} \left ( \frac{|\boldsymbol{q}_i| - q}{q_{width}} \right)^2 \right ]}\]

and

\[\sum_i w(\boldsymbol{q}_i, q) = 1.0\]

This corresponds to a gaussian smearing or convolution. The input parameters are q_norms, setting to the values of \(|\vec{q}|\), for which the function is evaluated and q_width specifying the standard deviation of the gaussian smearing.

Parameters
  • sample (Sample) – Input sample.

  • q_norms (ndarray[Any, dtype[float]]) – Values of \(|\vec{q}|\) at which to evaluate the correlation functions.

  • q_width (float) – Standard deviation of the gaussian smearing.

Return type

Sample

dynasor.post_processing.get_weighted_sample(sample, weights)[source]

Weights correlation functions with atomic weighting factors

The weighting of a partial dynamic structure factor \(S_\mathrm{AB}(\boldsymbol{q}, \omega)\) for atom types \(A\) and \(B\) is carried out as

\[S_\mathrm{AB}(\boldsymbol{q}, \omega) = f_\mathrm{A}(\boldsymbol{q}) f_\mathrm{B}(\boldsymbol{q}) S_\mathrm{AB}(\boldsymbol{q}, \omega)\]

\(f_\mathrm{A}(\boldsymbol{q})\) and \(f_\mathrm{B}(\boldsymbol{q})\) are atom-type and \(\boldsymbol{q}\)-point dependent weights.

If sample has incoherent correlation functions, but weights does not contain information on how to weight the incoherent part, then it will be dropped from the returned Sample object (and analogously for current correlation functions).

Parameters
  • sample (Sample) – input sample to be weighted

  • weights (Weights) – object containing the weights \(f_\mathrm{X}(\boldsymbol{q})\)

Return type

Sample

Returns

A Sample instance with the weighted partial and total structure factors.