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

32 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2025-04-16 06:13 +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 if atom_type not in self._weights_coh.keys(): 

33 raise ValueError(f'Coherent weights for {atom_type} have not been specified') 

34 return self._weights_coh[atom_type] 

35 

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

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

38 if self._weights_incoh is None: 

39 return None 

40 if atom_type not in self._weights_incoh.keys(): 40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true

41 raise ValueError(f'Incoherent weights for {atom_type} have not been specified') 

42 return self._weights_incoh[atom_type] 

43 

44 @property 

45 def supports_currents(self): 

46 """ 

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

48 """ 

49 return self._supports_currents 

50 

51 @property 

52 def supports_incoherent(self): 

53 """ 

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

55 correlation functions. 

56 """ 

57 return self._weights_incoh is not None 

58 

59 def __str__(self): 

60 s = ['weights coherent:'] 

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

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

63 

64 # Return early if incoherent weights 

65 # are None 

66 if self._weights_incoh is None: 

67 return '\n'.join(s) 

68 

69 s.append('weights incoherent:') 

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

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

72 return '\n'.join(s)