-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.py
204 lines (166 loc) · 6.28 KB
/
signals.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
#!/usr/bin/python3
#############################################################################
## ##
## signals.py: Displays all signals from the saved csv-file ##
## ##
## Phillip Blunck, 2021-07-05 ##
## ##
#############################################################################
NAME = "signals" # 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, signals):
"""Plot datapoints for every signal with date timeline.
"""
figure = matplotlib.pyplot.figure(figsize=SIZE, dpi=RES)
for num, signal in enumerate(signals):
x = time
y = data[:, num]
# plot data
matplotlib.pyplot.subplot(len(signals), 1, num+1)
matplotlib.pyplot.ylabel(signal)
matplotlib.pyplot.plot_date(x, y, markersize=1)
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) % 2 + 1
matplotlib.pyplot.savefig(f"{NAME}-{logger}-{number:02d}.png")
#############################################################################
def plotdata_raw(logger, time, data, signals):
"""Plot datapoints for every signal with raw timeline.
"""
figure = matplotlib.pyplot.figure(figsize=SIZE, dpi=RES)
for num, signal in enumerate(signals):
x = time
y = data[:, num]
# plot data
matplotlib.pyplot.subplot(len(signals), 1, num+1)
matplotlib.pyplot.ylabel(signal)
matplotlib.pyplot.plot(x, y)
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) % 2 + 1
matplotlib.pyplot.savefig(f"{NAME}-{logger}-{number:02d}.png")
#############################################################################
def plotlogger(logger, data, signals):
"""Generate timelines for raw data point and for every second.
Plot signals of every logger for each timeline.
"""
# generate timelines
time_raw = numpy.arange(0, len(data))
time_date = [
datetime.datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
for t in data[:, 1]
]
data = data[:, 2:]
# plot signals for both timelines
plotdata_raw(logger, time_raw, data, signals)
plotdata_date(logger, time_date, data, signals)
#############################################################################
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 signalnames
signals = data.columns[2:]
# convert csv data to numpy array
data = data.to_numpy()
# 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, signals)
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)
#############################################################################