-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_util.py
More file actions
274 lines (231 loc) · 8.56 KB
/
reader_util.py
File metadata and controls
274 lines (231 loc) · 8.56 KB
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
""" EXAMPLE DATA FILE
% Model: COPY__DISCO_thermal_analysis.mph
% Version: COMSOL 6.2.0.415
% Date: Mar 25 2025, 09:23
% Dimension: 0
% Nodes: 526
% Expressions: 526
% Description: Temperature
% Length unit: m
% X
0.0535
% Y
0.026
% Z
0.2615
% T (K) @ t=0
267.15000000000043
% T (K) @ t=0.40462
267.1499991540429
% T (K) @ t=0.79143
267.149998910309
% T (K) @ t=1.5651
267.149998767943
% T (K) @ t=3.1123
267.1500109978694
"""
import numpy as np
import os
import matplotlib.pyplot as plt
from typing import Tuple, List
from dataclasses import dataclass
import io
# ========== Data Classes ==========
@dataclass
class Event:
t_start: float
t_stop: float
color: str = "grey"
label: str = "Event"
@dataclass
class Scenario:
name: str
events: list[Event]
@dataclass
class StateColors:
no_states: str = "grey"
standby: str = "blue"
nominal: str = "green"
observe: str = "orange"
load: str = "red"
transmit: str = "purple"
def get(self, state:str) -> str:
if state.lower() == "no_states":
return self.no_states
elif state.lower() == "standby":
return self.standby
elif state.lower() == "nominal":
return self.nominal
elif state.lower() == "observe":
return self.observe
elif state.lower() == "load":
return self.load
# MIN_TEMP = -84.44 # deg C
# MAX_TEMP = 48.88 # deg C
# ========== Functions ==========
def read_data(path:str) -> Tuple[np.ndarray, np.ndarray, Tuple[float, float, float]]:
with open(path, "r") as file:
data = file.readlines()
if len(data) < 8:
raise Exception(f'Invalid data file: {path}')
# Ignore first 8 lines
data = data[8:]
# Extracting data
x = float(data[1])
y = float(data[3])
z = float(data[5])
# Extracting temperature data
temp = []
for i in range(7, len(data), 2):
temp.append(float(data[i].strip()))
temp = np.array(temp)
# Extracting time data
time = []
for i in range(6, len(data), 2):
t_val = data[i].split("=")[1].strip()
time.append(float(t_val))
time = np.array(time)
return (temp, time, (x, y, z))
def convert_temp(temp:float, convert_unit:str) -> float:
"""
Convert temperature from one unit to another.
"""
if convert_unit == "K->C":
return temp - 273.15
elif convert_unit == "K->F":
return (temp - 273.15) * 9/5 + 32
elif convert_unit == "K->R":
return (temp - 273.15) * 9/5 + 491.67
elif convert_unit == "K->K":
return temp
elif convert_unit == "C->K":
return temp + 273.15
elif convert_unit == "F->K":
return (temp - 32) * 5/9 + 273.15
elif convert_unit == "R->K":
return (temp - 491.67) * 5/9 + 273.15
else:
raise ValueError(f"Invalid conversion unit: {convert_unit}")
def _within_bounds(value:float, min_value:float, max_value:float) -> bool:
"""
Check if a value is within the specified bounds.
"""
return min_value <= value <= max_value
def plot_data(
data_path:str,
probes:list = None,
scenario:Scenario = None,
convert_unit:str = "K->C",
show_marks:bool = False,
title_addition:str = "",
title_overwrite:str="",
ignore_missing_data=False,
save_path: str = None,
fileobj = None, # For returning a file object instead of saving to disk
xLookUp: Tuple[int, int] = None
) -> None:
if len(title_addition) > 0 and len(title_overwrite) > 0:
raise ValueError("title_addition and title_overwrite cannot be both set")
if len(title_addition) > 0:
title_addition = f" - {title_addition}"
_print_temp_len_once = True #False
# read all files in directory
paths = []
files = [file for file in os.listdir(data_path) if file.endswith(".txt")]
if ignore_missing_data:
files = [file for file in files if "T12" not in file]
if probes:
for file in files:
if any(probe in file for probe in probes):
paths.append(os.path.join(data_path, file))
else:
paths.extend(os.path.join(data_path, file) for file in files)
# print(f"paths: {paths}")
# big plot
# print(f"Number of files: {len(paths)}")
if len(paths) == 0:
raise ValueError(f"No matching datasets found in {data_path}")
row = len(paths) // 3
if len(paths) % 3 != 0:
row += 1
col = 3
if row == 1 and len(paths) < 3:
col = len(paths)
# print(f"row and col: {row}, {col}")
fig, ax = plt.subplots(row, col, figsize=(5*col, 3.5 * row))
fig.subplots_adjust(hspace=0.4, wspace=0.4)
for i, path in enumerate(paths):
try:
if not os.path.exists(path):
raise Exception(f"File not found: {path}")
if os.path.exists(path) and os.path.getsize(path) == 0:
raise Exception(f"Empty file: {path}")
if col == 1 or row == 1:
ax_index = ax[i]
else:
ax_index = ax[i//row, i%col]
temp, time, (x, y, z) = read_data(path)
if not _print_temp_len_once:
# print(f"Length of temp: {len(temp)}, Length of time: {len(time)}")
_print_temp_len_once = True
# Check if there is any data
if len(temp) == 0 or len(time) == 0:
ax_index.set_title(f"{path.split('ata - ')[1].split('.')[0]}")
raise Exception(f"Invalid data: {path}; Length of temp: {len(temp)}, Length of time: {len(time)}")
# Check if the data is valid for plotting
if len(temp) != len(time):
ax_index.set_title(f"{path.split('ata - ')[1].split('.')[0]}")
raise Exception(f"Invalid data: {path}; Length of temp: {len(temp)}, Length of time: {len(time)}")
# Check if data contains any NaN values
if np.isnan(temp).any() or np.isnan(time).any():
ax_index.set_title(f"{path.split('ata - ')[1].split('.')[0]}")
raise Exception(f"Invalid data: {path}; Contains NaN values")
# Plotting
temp = convert_temp(temp, convert_unit)
if show_marks:
ax_index.plot(time, temp, label="Temperature", marker='o', markersize=3)
else:
ax_index.plot(time, temp, label="Temperature")
ax_index.set_title(f"{path.split('ata - ')[1].split('.')[0]}")
ax_index.set_xlabel("Time (s)")
ax_index.set_ylabel(f"Temperature ({convert_unit.split('->')[1]})")
# ax_index.set_ylabel("Temperature (K)")
ax_index.legend()
# Include scenario events legend
if scenario:
for event in scenario.events:
ax_index.axvspan(event.t_start, event.t_stop, color=event.color, alpha=0.2, label=event.label)
# Include max and min temperature lines
# ax[i//col, i%col].axhline(y=MAX_TEMP, color='r', linestyle='--', label=f"Max Temp: {MAX_TEMP}°C")
# ax[i//col, i%col].axhline(y=MIN_TEMP, color='b', linestyle='--', label=f"Min Temp: {MIN_TEMP}°C")
# Add x-axis limits if xLookUp is provided
if xLookUp:
ax_index.set_xlim(xLookUp[0], xLookUp[1])
except ValueError as e:
print(f"ValueError: {e} \n at {path}")
continue
# except Exception as e:
# print(f"Error: {e}")
# continue
labels = []
handles = []
if scenario:
# Add a legend for the scenario events
for event in scenario.events:
if event.label in labels:
continue
labels.append(event.label)
handles.append(plt.Line2D([0], [0], color=event.color, lw=4, alpha=0.4))
if title_overwrite:
fig.suptitle(f"Temperature vs Time - {title_overwrite}", fontsize=16)
else:
fig.suptitle(f"Temperature vs Time - {scenario.name}{title_addition}", fontsize=16)
fig.legend(handles, labels, loc='upper left', fontsize=10)
plt.tight_layout()
# plt.show()
if save_path:
fig.savefig(save_path, bbox_inches='tight')
# print(f"Plot saved to {save_path}")
elif fileobj is not None:
fig.savefig(fileobj, format="png", bbox_inches='tight')
plt.close(fig)