-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest_tensorboard_integration.py
132 lines (104 loc) · 3.78 KB
/
test_tensorboard_integration.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
# -*- coding:utf-8 -*-
import sys
import time
import logging
import json
import pytest
from tornado.testing import AsyncHTTPTestCase
@pytest.fixture(scope="session")
def tf_logs(tmpdir_factory):
import numpy as np
try:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
except ImportError:
import tensorflow as tf
x = np.random.rand(5)
y = 3 * x + 1 + 0.05 * np.random.rand(5)
a = tf.Variable(0.1)
b = tf.Variable(0.)
err = a*x+b-y
loss = tf.norm(err)
tf.summary.scalar("loss", loss)
tf.summary.scalar("a", a)
tf.summary.scalar("b", b)
merged = tf.summary.merge_all()
optimizor = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
with tf.Session() as sess:
log_dir = tmpdir_factory.mktemp("logs", numbered=False)
log_dir = str(log_dir)
train_write = tf.summary.FileWriter(log_dir, sess.graph)
tf.global_variables_initializer().run()
for i in range(1000):
_, merged_ = sess.run([optimizor, merged])
train_write.add_summary(merged_, i)
return log_dir
@pytest.fixture(scope="session")
def nb_app():
sys.argv = ["--port=6005", "--ip=127.0.0.1", "--no-browser", "--debug"]
from notebook.notebookapp import NotebookApp
app = NotebookApp()
app.log_level = logging.DEBUG
app.ip = '127.0.0.1'
# TODO: Add auth check tests
app.token = ''
app.password = ''
app.disable_check_xsrf = True
app.initialize()
return app.web_app
class TestJupyterExtension(AsyncHTTPTestCase):
@pytest.fixture(autouse=True)
def init_jupyter(self, tf_logs, nb_app, tmpdir_factory):
self.app = nb_app
self.log_dir = tf_logs
self.tmpdir_factory = tmpdir_factory
def get_app(self):
return self.app
def test_tensorboard(self):
content = {"logdir": self.log_dir}
content_type = {"Content-Type": "application/json"}
response = self.fetch(
'/api/tensorboard',
method='POST',
body=json.dumps(content),
headers=content_type)
response = self.fetch('/api/tensorboard')
instances = json.loads(response.body.decode())
assert len(instances) > 0
response = self.fetch('/api/tensorboard/1')
instance = json.loads(response.body.decode())
instance2 = None
for inst in instances:
if inst["name"] == instance["name"]:
instance2 = inst
assert instance == instance2
response = self.fetch('/tensorboard/1/#graphs')
assert response.code == 200
response = self.fetch('/tensorboard/1/data/plugins_listing')
plugins_list = json.loads(response.body.decode())
assert plugins_list["graphs"]
assert plugins_list["scalars"]
response = self.fetch(
'/api/tensorboard/1',
method='DELETE')
assert response.code == 204
response = self.fetch('/api/tensorboard/1')
error_msg = json.loads(response.body.decode())
assert error_msg["message"].startswith(
"TensorBoard instance not found:")
def test_instance_reload(self):
content = {"logdir": self.log_dir, "reload_interval": 4}
content_type = {"Content-Type": "application/json"}
response = self.fetch(
'/api/tensorboard',
method='POST',
body=json.dumps(content),
headers=content_type)
instance = json.loads(response.body.decode())
assert instance is not None
name = instance["name"]
reload_time = instance["reload_time"]
time.sleep(5)
response = self.fetch('/api/tensorboard/{}'.format(name))
instance2 = json.loads(response.body.decode())
assert instance2["reload_time"] != reload_time