Coverage for mchammer/data_containers/data_container.py: 85%

20 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-06 04:14 +0000

1""" Data container class. """ 

2 

3import numpy as np 

4from .base_data_container import BaseDataContainer 

5from ..data_analysis import analyze_data 

6 

7 

8class DataContainer(BaseDataContainer): 

9 """ 

10 Data container for storing information concerned with 

11 Monte Carlo simulations performed with :program:`mchammer`. 

12 

13 Parameters 

14 ---------- 

15 structure 

16 Reference atomic structure associated with the data container. 

17 ensemble_parameters 

18 Parameters associated with the underlying ensemble. 

19 metadata 

20 Metadata associated with the data container. 

21 """ 

22 

23 def analyze_data(self, tag: str, start: int = None, max_lag: int = None) -> dict: 

24 """ 

25 Returns detailed analysis of a scalar observerable. 

26 

27 Parameters 

28 ---------- 

29 tag 

30 Tag of field over which to average. 

31 start 

32 Minimum value of trial step to consider. By default the 

33 smallest value in the mctrial column will be used. 

34 max_lag 

35 Maximum lag between two points in data series. By default the 

36 largest length of the data series will be used. 

37 Used for computing autocorrelation. 

38 

39 Raises 

40 ------ 

41 ValueError 

42 If observable is requested that is not in data container. 

43 ValueError 

44 If observable is not scalar. 

45 ValueError 

46 If observations is not evenly spaced. 

47 

48 Returns 

49 ------- 

50 dict 

51 Calculated properties of the data including mean, 

52 standard_deviation, correlation_length and error_estimate 

53 (95% confidence). 

54 """ 

55 

56 # get data for tag 

57 if tag in ['trajectory', 'occupations']: 57 ↛ 58line 57 didn't jump to line 58, because the condition on line 57 was never true

58 raise ValueError('{} is not scalar'.format(tag)) 

59 steps, data = self.get('mctrial', tag, start=start) 

60 

61 # check that steps are evenly spaced 

62 diff = np.diff(steps) 

63 step_length = diff[0] 

64 if not np.allclose(step_length, diff): 64 ↛ 65line 64 didn't jump to line 65, because the condition on line 64 was never true

65 raise ValueError('data records must be evenly spaced.') 

66 

67 summary = analyze_data(data, max_lag=max_lag) 

68 summary['correlation_length'] *= step_length # in mc-trials 

69 return summary 

70 

71 def get_average(self, tag: str, start: int = None) -> float: 

72 """ 

73 Returns average of a scalar observable. 

74 

75 Parameters 

76 ---------- 

77 tag 

78 Tag of field over which to average. 

79 start 

80 Minimum value of trial step to consider. By default the 

81 smallest value in the mctrial column will be used. 

82 

83 Raises 

84 ------ 

85 ValueError 

86 If observable is requested that is not in data container. 

87 ValueError 

88 If observable is not scalar. 

89 """ 

90 if tag in ['trajectory', 'occupations']: 

91 raise ValueError('{} is not scalar'.format(tag)) 

92 data = self.get(tag, start=start) 

93 return np.mean(data)