88 lines
2.2 KiB
Python
88 lines
2.2 KiB
Python
from visdom import Visdom
|
|
import random
|
|
import numpy as np
|
|
import os
|
|
import time
|
|
|
|
|
|
def InitVisdom():
|
|
# nohub python3 -m visdom.server > visdom.server.log 2 > &1
|
|
# subprocess.Popen("python3 -m visdom.server > visdom.server.log 2>&1", shell=True)
|
|
|
|
os.popen("python3 -m visdom.server > visdom.server.log 2>&1")
|
|
time.sleep(3) # to wait vindom.server started
|
|
# viz = Visdom()
|
|
# assert viz.check_connection()
|
|
# viz.close()
|
|
print("Connect http://localhost:8097")
|
|
|
|
|
|
class Line(object):
|
|
def __init__(self, windows, name, size):
|
|
super(Line, self).__init__()
|
|
self.name = name
|
|
self.size = size
|
|
self.win = windows
|
|
|
|
def __init__(self, windows):
|
|
super(Line, self).__init__()
|
|
self.name = str(random.random())
|
|
self.size = 0
|
|
self.win = windows
|
|
|
|
def __init__(self, windows, name):
|
|
super(Line, self).__init__()
|
|
self.name = name
|
|
self.size = 0
|
|
self.win = windows
|
|
|
|
def AppendData(self, y, x=None):
|
|
self.win.AppendData(self, y, x)
|
|
|
|
|
|
class LineWin(object):
|
|
def __init__(self):
|
|
super(LineWin, self).__init__()
|
|
self.viz = Visdom()
|
|
self.name = str(random.random())
|
|
self.win = self.viz.line(
|
|
X=np.array([0]),
|
|
Y=np.array([0]),
|
|
name=self.name
|
|
)
|
|
self.data = np.array([])
|
|
|
|
def AppendLine(self, y):
|
|
if not isinstance(y, list):
|
|
y = [y]
|
|
linename = str(random.random())
|
|
self.viz.line(
|
|
X=np.array(range(len(y))),
|
|
Y=np.array(y),
|
|
opts=dict(markercolor=np.array([50]),
|
|
markersymbol='dot',),
|
|
win=self.win,
|
|
update="new",
|
|
name=linename,
|
|
)
|
|
return Line(linename, len(y))
|
|
|
|
def AppendData(self, line, y, x=None):
|
|
y = [y]
|
|
Y = np.array(y).reshape((-1))
|
|
X = []
|
|
if x:
|
|
x = [x]
|
|
X = np.array(X).reshape((-1))
|
|
else:
|
|
X = np.array(range(line.size, line.size+len(Y)))
|
|
|
|
self.viz.line(
|
|
Y=Y,
|
|
X=X,
|
|
win=self.win,
|
|
update="append",
|
|
name=line.name
|
|
)
|
|
line.size = line.size + len(y)
|