witnn/tools/WebVisual.py

80 lines
2.1 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():
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):
self.win.AppendData(self, y)
class LineWin():
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):
if not isinstance(y, list):
y = [y]
self.viz.line(
X=np.array(range(line.size, line.size+len(y))),
Y=np.array(y),
win=self.win,
update="append",
name=line.name
)
line.size = line.size + len(y)