-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_data.py
280 lines (245 loc) · 10.5 KB
/
collect_data.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
This module contains the Collectdata class, which collects data from the user
through the Excel sheet template. This is done in steps, as there are multiple
tables containing different types of data.
"""
import os
import pandas as pd
from hrs_config import HRSConfiguration
class CollectData:
"""
This class retrieves data from the Excel file, acting as the communication unit between
the program and the user. It directly stores data into objects created outside this module.
"""
def __init__(self, hrs_configs: HRSConfiguration):
# pylint: disable = W1401
"""
Creating an instance of the CollectData class requires the hrs_configuration
and filepath. This is to setup the link between the Excel template and the
program, and effectively store the data directly to the HRS configuration.
Parameters:
- hrs_config: An instance of the HRS configuration class
- filepath: The path to the Excel template.
Tips:
To easily find the correct filepath, find the template in its folder,
shift+right click and copy path. The filepath will be given by either:
r"C:\Path\To\Your\Master_project_sheet.xlsx"
"C:/Path/To/Your/Master_project_sheet.xlsx"
"""
self.hrs_config = hrs_configs
self.file_path = self.get_filepath()
self.config_sheet = "HRS_config"
self.calibration_sheet = "Calibration_uncertainty"
self.field_sheet = "Field_uncertainty"
self.single_meter_uncertainties = None
self.calibration_data = None
self.field_data = None
self.config_data = None
self.volume_data = None
self.sensor_data = None
self.annual_data = None
self.read_file()
self.set_table_1_config()
self.set_table_2_config()
self.set_table_3_config()
self.set_hrs_uncertainty()
def get_filepath(self):
"""
Returns the
"""
program_dir = os.path.dirname(os.path.abspath(__file__))
dynamic_filepath = os.path.join(program_dir, "excel_template", "configurationtemplate.xlsx")
return dynamic_filepath
def read_file(self):
"""
Reads an excel file, through the path given defined in __init__.
The data is then stored in the object parameters for further work
in subsequent methods.
Parameters:
None
Returns:
None
"""
# Collect multiple uncertainties calibration
df = pd.read_excel(
self.file_path, sheet_name=self.calibration_sheet, header=1, index_col=0
)
#print(df)
self.calibration_data = df.to_dict(orient="List")
#print(f"Calib: {self.calibration_data}")
# Collect multiple uncertaintainties field.
df = pd.read_excel(
self.file_path, sheet_name=self.field_sheet, header=1, index_col=0
).astype(float)
#print(df)
self.field_data = df.to_dict(orient="List")
# Collect decisions
df = pd.read_excel(self.file_path, sheet_name=self.config_sheet)
#print(df)
table1_specific_cells = df.iloc[[2, 3, 5, 6, 7, 8, 9, 11, 12, 13], 2] # YES/NO
#print(table1_specific_cells)
self.config_data = table1_specific_cells.to_dict()
# Collect single value calibration and field uncertainties
table1_uncertainties = df.iloc[[5, 6, 7, 8, 9, 11, 12, 13], 3].astype(float)
#print(table1_uncertainties)
self.single_meter_uncertainties = table1_uncertainties.to_dict()
#Collect years
self.annual_data = df.iloc[11,4]
# Collect table 2 volumes and related uncertainties
table2_specific_cells = df.iloc[[5, 6, 7], [7, 9]].astype(float)
self.volume_data = table2_specific_cells.to_dict(orient="index")
# Collect table 3 sensor uncertainty
table3_specific_cells = df.iloc[[10, 11], [7]].astype(float)
self.sensor_data = table3_specific_cells.to_dict(orient="index")
def convert_decision_to_bool(self, my_dict, index):
"""
The Excel file contains decisions which are set as YES or NO. This
acesses a dictionary of YES or NO, and based on its index converts
YES to boolean: True and No to boolean: False.
Parameters:
- my_dict: Dictionary containing either YES or NO.
- index: the index to check for in the dict.
"""
if my_dict[index] == "YES":
return True
elif my_dict[index] == "NO":
return False
else:
raise ValueError(
"Decision value not recognized. Make sure it is (YES) or (NO)"
)
def set_table_1_config(self):
"""
Method to be called after read_file(). This method convers the data stored
in object parameters, directly to the already initialized hrs_configuration class.
"""
# Table 1 configuration
self.hrs_config.correct_for_dead_volume_bool = self.convert_decision_to_bool(
self.config_data, 2
)
self.hrs_config.correct_for_depress_bool = self.convert_decision_to_bool(
self.config_data, 3
)
self.hrs_config.multiple_calibration_deviation_bool = (
self.convert_decision_to_bool(self.config_data, 5)
)
self.hrs_config.multiple_calibration_reference_bool = (
self.convert_decision_to_bool(self.config_data, 6)
)
self.hrs_config.multiple_calibration_repeatability_bool = (
self.convert_decision_to_bool(self.config_data, 7)
)
self.hrs_config.multiple_field_repeatability_bool = (
self.convert_decision_to_bool(self.config_data, 8)
)
self.hrs_config.multiple_field_condition_bool = self.convert_decision_to_bool(
self.config_data, 9
)
self.hrs_config.include_annual_dev_bool = self.convert_decision_to_bool(
self.config_data, 11
)
self.hrs_config.include_temp_bool = self.convert_decision_to_bool(
self.config_data, 12
)
self.hrs_config.include_pres_bool = self.convert_decision_to_bool(
self.config_data, 13
)
def set_table_2_config(self):
"""
Store the specific values associated to table 2 from the Excel template,
to the class HRSConfiguration().
"""
self.hrs_config.dead_volume = self.volume_data[5]["Unnamed: 7"] # H7
self.hrs_config.dead_volume_uncertainty = self.volume_data[5][
"Unnamed: 9"
] # J7
self.hrs_config.depressurization_vent_volume = self.volume_data[6][
"Unnamed: 7"
] # H8
self.hrs_config.depressurization_vent_volume_uncertainty = self.volume_data[6][
"Unnamed: 9"
] # J8
def set_table_3_config(self):
"""
Store the specific values associated to table 3 from the Excel template,
to the class HRSConfiguration().
"""
# Table 3 uncertainty
self.hrs_config.temperature_sensor_uncertainty = self.sensor_data[10][
"Unnamed: 7"
]
self.hrs_config.pressure_sensor_uncertainty = self.sensor_data[11]["Unnamed: 7"]
def set_hrs_uncertainty(self):
"""
Based on the decisions made in the Excel template, previously gathered,
it either sets the uncertainty related to the meter to a single value,
or as a list of flowrates, which will later be used for linear
interpolation.
Parameters:
- None
Returns:
- None
"""
self.hrs_config.flowrates_kg_min = self.calibration_data["Flowrate [kg/min]"]
# Calibration deviation correction
if self.convert_decision_to_bool(self.config_data, 5):
self.hrs_config.calibration_deviation_std = self.calibration_data[
"Calibration Deviation u(cal,dev) [%]"
]
#print(self.hrs_config.calibration_deviation_std)
else:
self.hrs_config.calibration_deviation_std = self.single_meter_uncertainties[
5
]
# Calibration reference # # # # # # # #
if self.convert_decision_to_bool(self.config_data, 6):
self.hrs_config.calibraiton_reference_std = self.calibration_data[
"Calibration Reference u(cal,ref) [%]"
]
else:
self.hrs_config.calibraiton_reference_std = self.single_meter_uncertainties[
6
]
# Calibration repeatability # # # # # # # #
if self.convert_decision_to_bool(self.config_data, 7):
self.hrs_config.calibration_repeatability_std = self.calibration_data[
"Calibration Repeatability u(cal,rept) [%]"
]
else:
self.hrs_config.calibration_repeatability_std = (
self.single_meter_uncertainties[7]
)
# Field repeatability# # # # #
if self.convert_decision_to_bool(self.config_data, 8):
self.hrs_config.field_repeatability_std = self.field_data[
"Field Repeatability u(field,rept) [%]"
]
else:
self.hrs_config.field_repeatability_std = self.single_meter_uncertainties[8]
# Field condition # # # # # # # #
if self.convert_decision_to_bool(self.config_data, 9):
self.hrs_config.field_condition_std = self.field_data[
"Field Condition u(field,cond) [%]"
]
else:
self.hrs_config.field_condition_std = self.single_meter_uncertainties[9]
# Annual deviation
if self.convert_decision_to_bool(self.config_data, 11):
print(self.annual_data)
self.hrs_config.annual_deviation = self.single_meter_uncertainties[11]
self.hrs_config.years_since_calibration = self.annual_data
else:
self.hrs_config.annual_deviation = 0
# Pressure contribution
if self.convert_decision_to_bool(self.config_data, 12):
self.hrs_config.pressure_contribution = self.single_meter_uncertainties[12]
else:
self.hrs_config.pressure_contribution = 0
#Temperature contribution
if self.convert_decision_to_bool(self.config_data, 13):
self.hrs_config.temperature_contribution = self.single_meter_uncertainties[13]
else:
self.hrs_config.temperature_contribution = 0
hrs_config = HRSConfiguration()
data_reader = CollectData(hrs_config)
#print(vars(hrs_config))