Coverage for local_installation/dynasor/post_processing/weights.py: 58%

28 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-12-21 12:02 +0000

1from typing import Dict 

2 

3 

4class Weights: 

5 """ 

6 Class holding weights and support functions for weighting of samples 

7 

8 Parameters 

9 ---------- 

10 weights_coh 

11 A dict with keys and values representing the atom types and their corresponding 

12 coherent scattering length, ``{'A': b_A }``. 

13 weights_incoh 

14 A dict with keys and values representing the atom types and their corresponding 

15 incoherent scattering length, ``{'A': b_A }``. 

16 supports_currents 

17 whether or not the coherent weights should be applied to current-correlation functions 

18 """ 

19 

20 def __init__( 

21 self, 

22 weights_coh: Dict[str, float], 

23 weights_incoh: Dict[str, float] = None, 

24 supports_currents: bool = True, 

25 ): 

26 self._weights_coh = weights_coh 

27 self._weights_incoh = weights_incoh 

28 self._supports_currents = supports_currents 

29 

30 def get_weight_coh(self, atom_type, q_norm=None): 

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

32 return self._weights_coh[atom_type] 

33 

34 def get_weight_incoh(self, atom_type, q_norm=None): 

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

36 if self._weights_incoh is None: 

37 return None 

38 return self._weights_incoh[atom_type] 

39 

40 @property 

41 def supports_currents(self): 

42 """ 

43 Wether or not this :class:`Weights` object supports weighting of current correlations. 

44 """ 

45 return self._supports_currents 

46 

47 @property 

48 def supports_incoherent(self): 

49 """ 

50 Whether or not this :class:`Weights` object supports weighting of incoherent 

51 correlation functions. 

52 """ 

53 return self._weights_incoh is not None 

54 

55 def __str__(self): 

56 s = ['weights coherent:'] 

57 for key, val in self._weights_coh.items(): 

58 s.append(f' {key}: {val}') 

59 

60 # Return early if incoherent weights 

61 # are None 

62 if self._weights_incoh is None: 

63 return '\n'.join(s) 

64 

65 s.append('weights incoherent:') 

66 for key, val in self._weights_incoh.items(): 

67 s.append(f' {key}: {val}') 

68 return '\n'.join(s)