-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
286 lines (230 loc) · 9.39 KB
/
main.py
File metadata and controls
286 lines (230 loc) · 9.39 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
275
276
277
278
279
280
281
282
283
284
285
286
def ingestTemporalLine(filename):
# This function returns a cleaned up array from the FLIR temporal csv export
measurements = []
with open(filename) as file:
for index, line in enumerate(file):
# First we prepare the array of items
for index, line in enumerate(file):
if index == 0:
# We remove the header row
continue
# Split at comma
line = line.split(",")
# Transform temperature to float
line[2] = float(line[2])
line[3] = float(line[3])
measurements.append(line[2:4])
return measurements
def ingestGradLine(filename):
import numpy as np
# This function returns a cleaned up array from the FLIR profile csv export
measurements = []
with open(filename) as file:
for index, line in enumerate(file):
# First we prepare the array of items
for index, line in enumerate(file):
if index == 0:
# We remove the header row
continue
# Split at comma
line = line.split(",")
# Transform temperature to float
line[0] = float(line[0])
line[1] = float(line[1])
measurements.append(line[0:2])
# We must also remove any values that are lower than predecessors at the end, this is to remove the drop off on graphs
# maximum = np.argmax(measurements[round(len(measurements)*0.8) : len(measurements)])
# max(measurements[round(len(measurements)*0.8) : len(measurements)])
return measurements
def returnDiplRoot():
import sys
return sys.path[0]
def crawlFolders(root):
import os
flatFolderList = []
# get root subdirs
rootSubDirs = [f for f in os.scandir(root) if f.is_dir()]
# crawl all root subdirs
for dir in rootSubDirs:
flatFolderList += [f for f in os.scandir(dir) if f.is_dir()]
# return a flat list of all the directories
return flatFolderList
def is_integer(n):
# https://note.nkmk.me/en/python-check-int-float/
# This is for checking if string is an integer
try:
float(n)
except ValueError:
return False
else:
return float(n).is_integer()
def processmarkerfiles(filelist):
import os
dataset = []
for file in filelist:
# Check if the last symbol in file name is a number, this means, there is no seperate heating and cooling measurement, and we don't need to concat data
name = os.path.splitext(file)[0]
if is_integer(name[-1]):
dataset.append(ingestTemporalLine(file))
continue
# If there is a letter instead of a number, then there is two sets of data that need to be joined
# First, we make sure, there is no bad data. We only accept suffix u for up-heating and d for down-cooling
#if name[-1] != "u" or name[-1] != "d":
# continue
if name[-1] == "u":
#just add the up measurement for now
dataset.append(ingestTemporalLine(file))
continue
#raise NameError(f'Wrong marker suffix in directory on file {name}')
# If we are here, we have two csv files for each marker, we must join them
return dataset
def processheatingfiles(filelist):
import os
dataset = []
# Check if length of filelist is more than one
if len(filelist) == 1:
# there is just heating
dataset.append(ingestTemporalLine(filelist[0]))
return dataset
# If we are here, we need to concat the two files
# First we ingest each file
ogrevanjelist = ingestTemporalLine(filelist[1])
hlajenjelist = ingestTemporalLine(filelist[0])
# We must offset the second part by the first part amount
hlajenjelist = [[f[0]+ogrevanjelist[-1][0], f[1]] for f in hlajenjelist]
# We concat and append
dataset.append(ogrevanjelist+hlajenjelist)
return dataset
def processDir(dir, dataMap):
import os
csvFiles = []
# This function will scan the directory, and find all relevant csv files
# List all the CSV files to the csvFiles list
with os.scandir(dir) as currentDir:
for entry in currentDir:
ext = os.path.splitext(entry)[-1].lower()
if ext == ".csv":
csvFiles.append(entry)
# For each CSV file, determine the type, and add it to a list
markerlist = [f for f in csvFiles if f.name[0] == 'm']
profilelist = [f for f in csvFiles if f.name[0] == 'p']
ogrevanjelist = [f for f in csvFiles if f.name[0] == 'o' or f.name[0] == 'h']
# Create new output dir
pyroot = returnDiplRoot()
# Get the relative path, from meritve root to the current meritev dir
dirrelpath = os.path.relpath(dir, dataMap.dataMap['meritveRoot'])
# Prepare the output directory
outputpath = os.path.join(pyroot, 'output', dirrelpath)
os.makedirs(outputpath, exist_ok=True)
# Process the data from csv files
markerdataset = processmarkerfiles(markerlist)
heatingdataset = processheatingfiles(ogrevanjelist)
# Create the plots
makemarkerplot(markerdataset, outputpath)
makeGradTimePlot(profilelist, outputpath)
makeHeatingTimePlot(heatingdataset, outputpath)
def makemarkerplot(data, directory):
# This function will create a plot with stacked temporal lines
import matplotlib.pyplot as plt
import os
# If there is no data, stop the work
if len(data) == 0:
print('\033[93m No data for Marker plot in dir: \033[0m' + directory)
return
# Process the data via an ingestion function
print('Making Marker Plot in dir: ' + directory)
#processeddata = [ingestTemporalLine(f) for f in data]
# Create a plot
fig, ax = plt.subplots()
# Add each of the csv datasets to the plot
for index, dataset in enumerate(data):
x = list(map(lambda x: x[0], dataset))
y = list(map(lambda x: x[1], dataset))
ax.plot(x, y, label=f'Točka {index+1}')
# Add the labels
ax.set_ylabel('Temperatura [°C]')
ax.set_xlabel('Čas [s]')
# Draw the legend
ax.legend()
# Save the plot
currentfolder = os.path.basename(dir)
outputpath = os.path.join(directory, f'markerplot-{currentfolder}.png')
plt.savefig(outputpath)
plt.close(fig)
def makeGradTimePlot(data,dir):
import matplotlib.pyplot as plt
import os
# This function will create a plot with stacked profile lines
# Define if we are currently making a plot for S10 or S9 pipe
currentfolder = os.path.basename(dir)
currentpipe = currentfolder[0]
# If there is no data, stop the work
if len(data) == 0:
print('\033[93mNo data for Grad Time plot in dir: \033[0m' + dir)
return
# Process the data via an ingestion function
print('Making Grad Time Plot in dir: ' + dir)
processeddata = [ingestGradLine(f) for f in data]
# Create a plot
fig, ax = plt.subplots()
# Add each of the csv datasets to the plot
# If we have a currentpipe that we know, use a specific loop
if currentpipe == 'd':
for index, dataset in enumerate(processeddata):
x = [ 72.5/len(dataset)*i for i,x in enumerate(dataset)]
y = list(map(lambda x: x[1], dataset))
ax.plot(x, y, label=f'Čas {index * 60} s')
elif currentpipe == 't':
for index, dataset in enumerate(processeddata):
x = [ 80/len(dataset)*i for i,x in enumerate(dataset)]
y = list(map(lambda x: x[1], dataset))
ax.plot(x, y, label=f'Čas {index * 60} s')
else:
for index, dataset in enumerate(processeddata):
x = list(map(lambda x: x[0], dataset))
y = list(map(lambda x: x[1], dataset))
ax.plot(x, y, label=f'Čas {index * 60} s')
# Add the labels
ax.set_ylabel('Temperatura [°C]')
ax.set_xlabel('Razdalja [mm]')
# Draw the legend
ax.legend()
# Save the plot
outputpath = os.path.join(dir, f'gradplot-{currentfolder}.png')
plt.savefig(outputpath)
plt.close(fig)
def makeHeatingTimePlot(data,dir):
import matplotlib.pyplot as plt
import os
# This function will create a plot with stacked profile lines
# If there is no data, stop the work
if len(data) == 0:
print('\033[93mNo data for Heating Time plot in dir: \033[0m' + dir)
return
# Process the data via an ingestion function
print('Making Heating Time Plot in dir: ' + dir)
#processeddata = [ingestTemporalLine(f) for f in data]
# Create a plot
fig, ax = plt.subplots()
# Add each of the csv datasets to the plot
for index, dataset in enumerate(data):
x = list(map(lambda x: x[0], dataset))
y = list(map(lambda x: x[1], dataset))
ax.plot(x, y)
ax.plot(x, y, label=f'Povprečna temperatura')
# Draw horizontal line at maximum value
ax.axhline(y=max(y), linewidth=1, label=f'Maksimalna temperatura ({round(max(y),1)}°C)', linestyle='--')
# Add the labels
ax.set_ylabel('Temperatura [°C]')
ax.set_xlabel('Čas [s]')
# Draw the legend
ax.legend()
# Save the plot
currentfolder = os.path.basename(dir)
outputpath = os.path.join(dir, f'heatingplot-{currentfolder}.png')
plt.savefig(outputpath)
plt.close(fig)
if __name__ == '__main__':
import dataMap
for dir in crawlFolders(dataMap.dataMap['meritveRoot']):
processDir(dir, dataMap)