File: weights.py

package info (click to toggle)
python-dynasor 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,008 kB
  • sloc: python: 5,263; sh: 20; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import Dict


class Weights:
    """
    Class holding weights and support functions for weighting of samples

    Parameters
    ----------
    weights_coh
        A dict with keys and values representing the atom types and their corresponding
        coherent scattering length, ``{'A': b_A }``.
    weights_incoh
        A dict with keys and values representing the atom types and their corresponding
        incoherent scattering length, ``{'A': b_A }``.
    supports_currents
        whether or not the coherent weights should be applied to current-correlation functions
    """

    def __init__(
        self,
        weights_coh: Dict[str, float],
        weights_incoh: Dict[str, float] = None,
        supports_currents: bool = True,
    ):
        self._weights_coh = weights_coh
        self._weights_incoh = weights_incoh
        self._supports_currents = supports_currents

    def get_weight_coh(self, atom_type, q_norm=None):
        """Get the coherent weight for a given atom type and q-vector norm."""
        return self._weights_coh[atom_type]

    def get_weight_incoh(self, atom_type, q_norm=None):
        """Get the incoherent weight for a given atom type and q-vector norm."""
        if self._weights_incoh is None:
            return None
        return self._weights_incoh[atom_type]

    @property
    def supports_currents(self):
        """
        Wether or not this :class:`Weights` object supports weighting of current correlations.
        """
        return self._supports_currents

    @property
    def supports_incoherent(self):
        """
        Whether or not this :class:`Weights` object supports weighting of incoherent
        correlation functions.
        """
        return self._weights_incoh is not None

    def __str__(self):
        s = ['weights coherent:']
        for key, val in self._weights_coh.items():
            s.append(f'  {key}: {val}')

        # Return early if incoherent weights
        # are None
        if self._weights_incoh is None:
            return '\n'.join(s)

        s.append('weights incoherent:')
        for key, val in self._weights_incoh.items():
            s.append(f'  {key}: {val}')
        return '\n'.join(s)