/
notebooks_getting_started.py
256 lines (213 loc) · 8.49 KB
/
notebooks_getting_started.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
try:
import shutil
import copy
import pdb
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor, CellExecutionError
from grid2op.tests.helper_path_test import *
import os
import unittest
import time
import warnings
CAN_COMPUTE = None
except ImportError as exc_:
CAN_COMPUTE = exc_
NOTEBOOK_PATHS = os.path.abspath(os.path.join(PATH_DATA_TEST, "../../getting_started"))
VERBOSE_TIMER = True
def delete_all(folder):
"""
Delete all the files in a folder recursively.
Parameters
----------
folder: ``str``
The folder in which we delete everything
Returns
-------
None
"""
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))
def export_all_notebook(folder_in):
"""
Parameters
----------
folder_in: ``str``
The folder in which we look for ipynb files
folder_out: ``str``
The folder in which we save the py file.
Returns
-------
res: ``list``
Return the list of notebooks names
"""
res = []
for filename in os.listdir(folder_in):
if os.path.splitext(filename)[1] == ".ipynb":
notebook_filename = os.path.join(folder_in, filename)
res.append(notebook_filename)
return res
class RAII_tf_log:
def __init__(self):
self.previous = None
if "TF_CPP_MIN_LOG_LEVEL" in os.environ:
self.previous = copy.deepcopy(os.environ["TF_CPP_MIN_LOG_LEVEL"])
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
def __del__(self):
if self.previous is not None:
os.environ["TF_CPP_MIN_LOG_LEVEL"] = self.previous
# notebook names are hard coded because if i change them, i need also to change the
# readme and the documentation
class RAII_Timer:
"""
class to have an approximation of the runtime of the notebook.
This is a rough approximation to reduce time spent in certain notebooks and should not be
used for another purpose.
"""
def __init__(self, str_=""):
self._time = time.perf_counter()
self.str_ = str_
def __del__(self):
if VERBOSE_TIMER:
print(
f"Execution time for {self.str_}: {time.perf_counter() - self._time:.3f} s"
)
class TestNotebook(unittest.TestCase):
def _aux_funct_notebook(self, notebook_filename):
assert os.path.exists(notebook_filename), f"{notebook_filename} do not exists!"
with open(notebook_filename) as f:
nb = nbformat.read(f, as_version=4)
try:
ep = ExecutePreprocessor(timeout=60, store_widget_state=True)
try:
ep.preprocess(nb, {"metadata": {"path": NOTEBOOK_PATHS}})
except CellExecutionError as exc_:
raise
except Exception as exc_:
# error with tqdm progress bar i believe
pass
except CellExecutionError as exc_:
raise
except Exception:
pass
def _check_for_baselines(self):
try:
import l2rpn_baselines
except ImportError as exc_:
self.skipTest("l2rpn baseline is not available")
def test_notebook0_1(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
timer = RAII_Timer("test_notebook0_1")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "00_SmallExample.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook1(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
return # takes 80s and is useless i think, as it tests only basics things
timer = RAII_Timer("test_notebook1")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "01_Grid2opFramework.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook2(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
timer = RAII_Timer("test_notebook2")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "02_Observation.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook3(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
timer = RAII_Timer("test_notebook3")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "03_Action.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook4(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
self._check_for_baselines()
raii_ = RAII_tf_log()
timer = RAII_Timer("test_notebook4")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "04_TrainingAnAgent.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook5(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
timer = RAII_Timer("test_notebook5")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "05_StudyYourAgent.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook6(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
timer = RAII_Timer("test_notebook6")
notebook_filename = os.path.join(
NOTEBOOK_PATHS, "06_Redispatching_Curtailment.ipynb"
)
self._aux_funct_notebook(notebook_filename)
def test_notebook7(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
self._check_for_baselines()
raii_ = RAII_tf_log()
timer = RAII_Timer("test_notebook7")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "07_MultiEnv.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook8(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
# display notebook, might not be super useful to test it in the unit test (saves another 1-2 minutes)
return
timer = RAII_Timer("test_notebook8")
notebook_filename = os.path.join(
NOTEBOOK_PATHS, "08_PlottingCapabilities.ipynb"
)
self._aux_funct_notebook(notebook_filename)
def test_notebook9(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
return # test the opponent and the maintenance, not much there but takes 80s so... not a lot to do
timer = RAII_Timer("test_notebook9")
notebook_filename = os.path.join(
NOTEBOOK_PATHS, "09_EnvironmentModifications.ipynb"
)
self._aux_funct_notebook(notebook_filename)
def test_notebook10(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
timer = RAII_Timer("test_notebook10")
notebook_filename = os.path.join(NOTEBOOK_PATHS, "10_StorageUnits.ipynb")
self._aux_funct_notebook(notebook_filename)
def test_notebook_aub(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
raii_ = RAII_tf_log()
timer = RAII_Timer("test_notebook_aub")
notebook_filename = os.path.join(
NOTEBOOK_PATHS,
"AUB_EECE699_20201103_ReinforcementLearningApplication.ipynb",
)
self._aux_funct_notebook(notebook_filename)
def test_notebook_ieeebda(self):
if CAN_COMPUTE is not None:
self.skipTest(f"{CAN_COMPUTE}")
# this test takes 3 mins alone, for a really small benefit, so i skip it for sake of time
return
self._check_for_baselines()
timer = RAII_Timer("test_notebook_ieeebda")
notebook_filename = os.path.join(
NOTEBOOK_PATHS, "IEEE BDA Tutorial Series.ipynb"
)
self._aux_funct_notebook(notebook_filename)
if __name__ == "__main__":
unittest.main()