-
Notifications
You must be signed in to change notification settings - Fork 0
/
resistances.py
258 lines (211 loc) · 8.31 KB
/
resistances.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
#!/usr/bin/python3
#############################################################################
## ##
## resistances.py: Loads a csv-file containing discharge resistances for ##
## specific dates and plots them ##
## It allows make a comparison between the machine ## ## learning models ##
## ##
## Phillip Blunck, 2021-10-30 ##
## ##
#############################################################################
NAME = "resistances" # Name for picture file
interactive = True # Flag for interactive mode
#############################################################################
import datetime
import matplotlib
if not interactive: matplotlib.use("agg")
import matplotlib.pyplot
import numpy
import pandas
import subprocess
import sys
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
#############################################################################
COMMA = ","
EMPTY = ""
SPACE = " "
DELIMITER = COMMA + SPACE
WIDTH = 1920
HEIGHT = 1080
RES = 100
SIZE = (WIDTH/RES, HEIGHT/RES)
#############################################################################
class version:
hashtext = "unknown"
def set(filename):
"""Set the hash of the used dataset.
"""
with open(filename) as hashfile:
version.hashtext = hashfile.read()
def get(string=EMPTY):
"""This function returns version information of the project.
The returned string contains: the current date, the hash of
the current commit and the hash of the used dataset.
In case of modification of the project source data, the commit hash
is marked as modified.
"""
# get date
date = datetime.datetime.now()
result = f"{date:%Y-%m-%d}"
result += COMMA + SPACE
# get git commit hash value
head = subprocess.check_output(
["git", "rev-parse", "--short=7", "HEAD"]
)
head = head.decode("ascii").strip()
result += "Program" + SPACE + head
if subprocess.check_output (["git", "status", "--porcelain"]):
result += SPACE + "modified"
result += COMMA + SPACE
# get data hash value
result += "Data" + SPACE + version.hashtext[:7]
if string:
result += COMMA + SPACE
result += string
# return complete version string
return result
#############################################################################
def plotdata_date(logger, time, data, models):
"""Plot datapoints for every model with date timeline.
"""
figure = matplotlib.pyplot.figure(figsize=SIZE, dpi=RES)
for num, model in enumerate(models):
x = time
y = data[:, num]
# plot data
matplotlib.pyplot.subplot(len(models), 1, num+1)
matplotlib.pyplot.ylabel(model)
matplotlib.pyplot.plot_date(x, y, 'o:')
matplotlib.pyplot.grid(True)
matplotlib.pyplot.xlabel("Time")
matplotlib.pyplot.text(
0.02, 0.02, version.get(f"Logger {logger}"),
transform=matplotlib.pyplot.gcf().transFigure
)
if interactive: matplotlib.pyplot.show(block = False)
# saving figure as png
number = (figure.number - 1) % 4 + 1
matplotlib.pyplot.savefig(f"{NAME}-{logger}-{number:02d}.png")
#############################################################################
def plotdata_date_compare(logger, time, data, models):
"""Plot datapoints for every model with date timeline.
"""
figure = matplotlib.pyplot.figure(figsize=SIZE, dpi=RES)
for num, model in enumerate(models):
x = time
y = data[:, num]
# plot data
#matplotlib.pyplot.subplot(len(models), 1, num+1)
matplotlib.pyplot.ylabel("Discharge resistances / Ohm")
matplotlib.pyplot.plot_date(x, y, 'o:',label=model[22:-7]) #markersize=1)
matplotlib.pyplot.grid(True)
matplotlib.pyplot.xlabel("Time")
matplotlib.pyplot.legend()
matplotlib.pyplot.text(
0.02, 0.02, version.get(f"Logger {logger}"),
transform=matplotlib.pyplot.gcf().transFigure
)
if interactive: matplotlib.pyplot.show(block = False)
# saving figure as png
number = (figure.number - 1) % 4 + 1
matplotlib.pyplot.savefig(f"{NAME}-{logger}-{number:02d}.png")
#############################################################################
def plotdata_date_compareLatex(logger, time, data, models):
"""Plot datapoints for every model with date timeline.
"""
figure = matplotlib.pyplot.figure(figsize=(8, 6), dpi=RES)
for num, model in enumerate(models):
x = time
y = data[:, num]
# plot data
matplotlib.pyplot.ylabel("Discharge resistances / Ohm")
matplotlib.pyplot.plot_date(x, y, 'o:',label=model[22:-7])
matplotlib.pyplot.grid(True)
matplotlib.pyplot.xlabel("Time")
matplotlib.pyplot.legend()
if interactive: matplotlib.pyplot.show(block = False)
# saving figure as pdf
number = (figure.number - 1) % 4 + 1
matplotlib.pyplot.savefig(f"{NAME}-{logger}-{number:02d}.pdf")
#############################################################################
def plotdata_raw(logger, time, data, models):
"""Plot datapoints for every signal with raw timeline.
"""
figure = matplotlib.pyplot.figure(figsize=SIZE, dpi=RES)
for num, model in enumerate(models):
x = time
y = data[:, num]
# plot data
matplotlib.pyplot.subplot(len(models), 1, num+1)
matplotlib.pyplot.ylabel(model)
matplotlib.pyplot.plot(x, y, 'o:')
matplotlib.pyplot.grid(True)
matplotlib.pyplot.xlabel("Index of value")
matplotlib.pyplot.text(
0.02, 0.02, version.get(f"Logger {logger}"),
transform=matplotlib.pyplot.gcf().transFigure
)
if interactive: matplotlib.pyplot.show(block = False)
# saving figure as png
number = (figure.number - 1) % 4 + 1
matplotlib.pyplot.savefig(f"{NAME}-{logger}-{number:03d}.png")
#############################################################################
def plotlogger(logger, data, models):
"""Plot discharge resistance value for specific models and dates.
"""
# generate timelines
time_raw = numpy.arange(0, len(data))
time_date = [
datetime.datetime.strptime(t, "%Y-%m-%d")
for t in data[:, 1]
]
data = data[:, 2:]
# plot resistances for both timelines
plotdata_raw(logger, time_raw, data, models)
plotdata_date(logger, time_date, data, models)
plotdata_date_compare(logger, time_date, data, models)
plotdata_date_compareLatex(logger, time_date, data, models)
#############################################################################
def display(stdin):
"""Try to open the csv file and plot the signals in following formats:
First: Raw signals
Second: Signals for each second.
"""
try:
# read csv
data = pandas.read_csv(stdin, delimiter=DELIMITER, engine="python")
# get ML model names
models = data.columns[2:]
# convert csv data to numpy array
data = data.to_numpy()
print(data)
# plot signals for each logger
loggers = {* data[:, 0]}
for logger in sorted(loggers):
logdata = numpy.array([d for d in data if d[0] == logger])
plotlogger(logger, logdata, models)
if interactive: matplotlib.pyplot.show(block = True)
result = 0
except pandas.errors.EmptyDataError:
print("# No data found.", file=sys.stderr)
result = 2
return result
#############################################################################
def main(argv):
"""Check the input argument vector. If correct start the script,
otherwise print usage message.
"""
if len(argv) == 2:
version.set(argv[1])
result = display(sys.stdin)
else:
program = argv[0] if argv else __file__
print(f"Usage: <pipeline> | {program} <hashfile>", file=sys.stderr)
result = 1
return result
#############################################################################
if __name__ == "__main__":
STATUS = main(sys.argv)
sys.exit(STATUS)
#############################################################################