-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
215 lines (175 loc) · 6.5 KB
/
monitor.py
File metadata and controls
215 lines (175 loc) · 6.5 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
import tensorflow as tf
from sys import stdout
from filesystem import mainFileName, mkd
import matplotlib.pyplot as plt
class Figure(object):
def __init__(self):
plt.ion()
self.fig = None
self.xlim = None
self.ylim = None
def make(self, xlim=None, ylim=None, figsize=None, equal=True):
self.fig, self.ax = plt.subplots(1, 1, figsize=figsize)
if xlim is not None:
self.ax.set_xlim(xlim)
if ylim is not None:
self.ax.set_ylim(ylim)
if equal:
plt.gca().set_aspect('equal', adjustable='box')
def imshow(self, value, *args, **kwargs):
if self.fig is None:
self.make()
self.img_handle = self.ax.imshow(
value, *args, **kwargs)
self.imshow = self._udpate_image
def _udpate_image(self, value, *args, **kwargs):
self.img_handle.set_data(value)
def plot(self, x, y, *args, **kwargs):
if self.fig is None:
self.fig, self.ax = plt.subplots(1, 1)
self.plot_handle = self.ax.plot(x, y, *args, **kwargs)[0]
self.plot = self._update_plot
def _update_plot(self, x, y, *args, **kwargs):
self.plot_handle.set_data(x, y)
def setLimit(self, y_limits=None, x_limits=None):
if x_limits is not None:
assert len(x_limits) == 2
self.ax.set_xlim(x_limits)
if y_limits is not None:
assert len(y_limits) == 2
self.ax.set_ylim(y_limits)
def show(self):
self.fig.canvas.draw()
class Tensorboard(object):
def __init__(self, path=None, sub='', session=None, verbose=True):
if path is None:
path = mainFileName()
path = 'summary/' + path + '/' + sub
path = mkd(path)
if verbose:
print("[INFO] Tensorboard object message: Saving monitor files to", path)
if session is None:
self.writer = tf.summary.FileWriter(path)
else:
self.writer = tf.summary.FileWriter(path, session.graph)
self.variables = {}
self.strings = []
self.iteration = 0
self.summaries = []
self.graph = tf.Graph()
with self.graph.as_default():
self.init = tf.global_variables_initializer()
self.session = tf.Session(graph=self.graph)
self.session.run(self.init)
def scalar(self, iterable):
assert type(iterable) in [list, tuple, dict]
if type(iterable) in [list, tuple]:
iterable = {item.name: item for item in iterable}
with self.graph.as_default():
for name, var in iterable.items():
if type(var) != tf.Variable:
var = tf.Variable(var, name=name)
self.summaries.append(tf.summary.scalar(name, var))
self.op = tf.summary.merge(self.summaries)
def histogram(self, iterable):
if type(iterable) in [list, tuple]:
iterable = {item.name: item for item in iterable}
with self.graph.as_default():
for name, var in iterable.items():
if type(var) != tf.Variable:
var = tf.Variable(var, name=name)
self.summaries.append(tf.summary.histogram(name, var))
self.op = tf.summary.merge(self.summaries)
def update(self, dictionary={}, i=None):
availables = self.variables.keys()
new_d = {}
with self.graph.as_default():
for name, value in dictionary.items():
if name not in availables:
var = tf.Variable(value, name=name)
self.variables[name] = var
self.summaries.append(
tf.summary.scalar(name, self.variables[name]))
self.op = tf.summary.merge(self.summaries)
new_d[self.variables[name]] = value
# new_d = {self.variables[d]: dictionary[d] for d in dictionary.keys()}
if i is None:
summary = self.session.run(self.op, feed_dict=new_d)
self.writer.add_summary(summary, self.iteration)
self.iteration += 1
else:
summary = self.session.run(self.op, feed_dict=new_d)
self.writer.add_summary(summary, i)
self.writer.flush()
class Display(object):
def __init__(self):
# rows, columns = subprocess.check_output(['stty', 'size']).decode().split()
self.lines = 0
def print(self, *args):
print(*args)
self.lines += 1
def clear(self, title=None):
stdout.write("\033[F\033[K" * self.lines)
self.new(title)
def new(self, seperator=None):
if seperator:
print(seperator)
self.lines = 0
def breakLine(text, wrap=80):
if len(text) > wrap:
char = wrap
while char > 0 and text[char] != ' ':
char -= 1
if char:
text = [text[:char]] + breakLine(text[char + 1:], wrap)
else:
text = [text[:wrap - 1] + '-'] + breakLine(text[wrap - 1:], wrap)
return text
else:
return [cleanLine(text)]
def cleanLine(text):
if text[-1] == ' ':
text = text[:-1]
elif text[0] == ' ':
text = text[1:]
else:
return text
return cleanLine(text)
def boxPrint(text, wrap=0):
line_style = '-'
text = str(text)
paragraph = text.split('\n')
if wrap > 0:
index = 0
while index < len(paragraph):
paragraph[index] = cleanLine(paragraph[index])
if len(paragraph[index]) > wrap:
paragraph = paragraph[:index] + \
breakLine(paragraph[index], wrap) + paragraph[index + 1:]
index += 1
length = (max([len(line) for line in paragraph]))
print('+' + line_style * length + '+')
for line in paragraph:
print('|' + line + ' ' * (length - len(line)) + '|')
print('+' + line_style * length + '+')
if __name__ == "__main__":
text = "Some text comes here to be printed in a box!!!"
boxPrint(text, 20)
text = "Title:\nBody lorem ipsum something body\ncheers,"
boxPrint(text, 20)
boxPrint(text)
text = "No Space:\nTextHasNoSpaceForWrappingWhichGetsBrokenUsingDashes"
boxPrint(text, 20)
if __name__ == "__main__":
from time import sleep
disp = Display()
for i in range(10):
disp.clear()
sleep(1.)
disp.print("this", i)
disp.print("%i" % (10 - 2 * i))
disp.new("----------")
for i in range(5):
disp.print("this %i" % i)
disp.clear()
sleep(.1)