Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# SPDX-FileCopyrightText: 2020 Jonathan Pieper <jpieper@stud.uni-frankfurt.de> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5""" 

6MeasurementClass 

7================= 

8 

9Main parent class for all measurements. 

10These Measurements are represent by one data variable. 

11 

12 

13All measurements can be represented by 

14> m = ana.SingleMeasurement() 

15> m 

16{name} (Nr. {Nr)) 

17> print(m) 

18{name} (Nr. {Nr)) 

19{info.key} {info.value} 

20{info.key} {info.value} 

21{info.key} {info.value} 

22""" 

23 

24import os 

25import re 

26import numpy as np 

27 

28from .plotting import PlottingClass 

29 

30class MeasurementClass(object): 

31 """ 

32 The main measurement class for all measurements. 

33 """ 

34 def __init__(self): 

35 """ 

36 .. note:: **MeasurementClass:** 

37 This Class is the parent of all other measurement classes. 

38 

39 

40 All measurements have the following variables: 

41  

42 name: str/None 

43 The name of the Measurement Class (e.g. SA, RAW, etc.) 

44 

45 info: dict 

46 Contains informations about the measurement. Typical keys are:  

47 Nr: float, the number of the measurement, 

48 

49 """ 

50 self.name = '' 

51 self.data = {} 

52 self.info = {'Nr': 0} 

53 self.info_file = {} 

54 self.style = PlottingClass() 

55 

56 def i(self, *key): 

57 """ 

58 Get a specific measurement number information. 

59 

60  

61 :param *key: Will be passed to info.get 

62  

63 :return: info.get(*key) 

64 

65 """ 

66 return self.info.get(*key) 

67 

68 

69 __repr__ = lambda self: '%s (Nr. %s)\n' % (self.name, self.i('Nr', 0)) 

70 

71 __str__ = lambda self: '%s (Nr. %s)\n' % (self.name, self.i('Nr', 0)) + \ 

72 '\n'.join(['%s:\t%s' % (key, val) for key, val in self.info.items()]) 

73 

74 

75 def get_info_from_name(self, name, **kwargs): 

76 """ 

77 Extracting information from filenames. 

78 Using a default regex first or trying to extract 'key-value' pairs 

79 separated by '_'. 

80  

81 :param name: filename that contains informations. 

82 :param kwargs: 

83 :str regex: Regular expression for the filename 

84 :return: dict(key=value) 

85 """ 

86 

87 # Default Regex Structure 

88 # Nr, Struct, deg, Type1 

89 # Type2, Field, Date, Time 

90 # I1, I2, Lock-In (Connections) 

91 # Voltage In, Resistors (R11, R12) 

92 # R13, R21 

93 # Capacitors (C11, C21) 

94 # Temp 

95 def_regex = ".*[Mm]([0-9.]*)_([A-Za-z]*)_([0-9.-]*)deg_([A-Za-z]*)_" \ 

96 "([A-Za-z]*)_*B_([0-9.-]*)T_([0-9]*)_([0-9]*)_I1-([" \ 

97 "0-9\\-]*)_I2-([0-9\\-]*)_G[PB]I[BP].-([0-9\\-]*)_Vin-([" \ 

98 "0-9.]*)V_R11-([0-9]*.)O_R12-([0-9.]*.)O_R13-([" \ 

99 "0-9.]*.)_R21-([0-9.]*.)O_C11-([0-9]*)_C21-([0-9]*)_T-([" \ 

100 "0-9]*)K.*" 

101 regex = kwargs.get('regex', def_regex) 

102 reg = re.match(regex, name) 

103 

104 info_dict = dict() 

105 if not reg: 

106 filename = name.split(os.sep)[-1][:-4] 

107 raw_info = filename.split('_') 

108 for element in raw_info: 

109 regex_dict = { 

110 'nr': 'm([0-9.]*)', 

111 'deg': '((neg)?[0-9.-]*)deg', 

112 'dir': '([Uu]p|[Dd]own)', 

113 'date': '(20[12][90][0-9]*)', 

114 'time': '([012][0-9][0-6]*)', 

115 'type1': '(RAW|Hloop)', 

116 'type2': '(Parallel|Gradio)', 

117 'struct': '([Pp]lusses|[Cc]rosses|[Ee]mpty)', 

118 'field': 'p?m?([0-9.-]+)m?T', 

119 } 

120 if re.match(regex_dict['field'], element): 

121 info_dict['field'] = re.match(regex_dict['field'], element).groups()[0] 

122 continue 

123 

124 items = element.split('-') 

125 if len(items) == 1: 

126 for key, single_regex in regex_dict.items(): 

127 reg = re.match(single_regex, items[0]) 

128 if reg: 

129 info_dict[key] = reg.groups()[0] 

130 elif len(items) == 2: 

131 key, value = items 

132 info_dict[key] = value 

133 elif len(items) > 2: 

134 value = '-'.join(items[1:]) 

135 info_dict[items[0]] = value 

136 else: 

137 nr, struct, deg, type1, \ 

138 type2, field, date, time, \ 

139 i1, i2, lock_in, \ 

140 vin, r11, r12, r13, r21, \ 

141 c11, c21, temp = reg.groups() 

142 try: 

143 info_dict['nr'] = float(nr) 

144 info_dict['field'] = float(field) 

145 except ValueError: 

146 print('Expected float field and nr, got:' 

147 ' %s, %s' % (field, nr)) 

148 info_dict['srtuct'] = struct 

149 info_dict['deg'] = deg 

150 info_dict['type1'] = type1 

151 info_dict['type2'] = type2 

152 info_dict['date'] = date 

153 info_dict['time'] = time 

154 info_dict['i1'] = i1 

155 info_dict['i2'] = i2 

156 info_dict['lock_in'] = lock_in 

157 info_dict['r11'] = r11 

158 info_dict['r12'] = r12 

159 info_dict['r13'] = r13 

160 info_dict['r21'] = r21 

161 info_dict['vin'] = vin 

162 info_dict['c11'] = c11 

163 info_dict['c21'] = c21 

164 info_dict['temp'] = temp 

165 

166 self.info_file = info_dict 

167 

168 return info_dict 

169 

170 def calc_log(self, df, keys=['f', 'Vx', 'Vy']): 

171 for key in keys: 

172 df['ln%s' % key] = np.log10(df[key])