-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_environment.py
478 lines (405 loc) · 15.3 KB
/
test_environment.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"""Tests wombat/core/environment.py."""
from __future__ import annotations
import datetime
from pathlib import Path
import numpy as np
import pandas as pd
import polars as pl
import pytest
import numpy.testing as npt
from polars.testing import assert_series_equal
from wombat.core import RepairManager, WombatEnvironment
from wombat.windfarm import Windfarm
from tests.conftest import TEST_DATA
def test_data_dir():
"""Tests the data_dir creation."""
relative_library = Path(__file__).resolve().parent / "library"
assert TEST_DATA == relative_library
def test_setup():
"""Tests the setup of the `WombatEnvironment` and logging infrastructure."""
# Test an invalid directory
with pytest.raises(FileNotFoundError):
WombatEnvironment(
data_dir="TEST_DATA",
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=18,
random_seed=2022,
)
# Test invalid starting hour - too low
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=-1,
workday_end=18,
random_seed=2022,
)
# Test invalid starting hour - too high
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=24,
workday_end=18,
random_seed=2022,
)
# Test invalid ending hour - too low
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=-1,
random_seed=2022,
)
# Test invalid ending hour - before start
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=3,
random_seed=2022,
)
# Test invalid ending hour - at the same time as start
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=8,
random_seed=2022,
)
# Test invalid start_year - later than file limits (2002 through 2003)
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
start_year=2004,
end_year=2003,
random_seed=2022,
)
# Test invalid end_year - start year is after end year
with pytest.raises(ValueError):
WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
start_year=2003,
end_year=2002,
random_seed=2022,
)
# Test the data are read correctly with default starting and ending year
# and the logging infrastructure
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
random_seed=2022,
)
assert env.workday_start == 8
assert env.workday_end == 16
assert env.shift_length == 8
assert isinstance(env.weather, pl.DataFrame)
assert env.start_datetime == datetime.datetime(2002, 1, 1, 0, 0)
assert env.end_datetime == datetime.datetime(2003, 12, 31, 23, 0)
assert env.max_run_time == 8760 * 2 # 2 year data file
assert Path(env.events_log_fname).is_file()
assert Path(env.operations_log_fname).is_file()
# Delete the logged files, and check they not longer exist
env.cleanup_log_files()
assert not Path(env.events_log_fname).is_file()
assert not Path(env.operations_log_fname).is_file()
# Test custom start year
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
start_year=2003,
random_seed=2022,
)
assert env.start_datetime == datetime.datetime(2003, 1, 1, 0, 0)
assert env.end_datetime == datetime.datetime(2003, 12, 31, 23, 0)
env.cleanup_log_files() # delete the logged data
# Test custom end year
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
end_year=2002,
random_seed=2022,
)
assert env.start_datetime == datetime.datetime(2002, 1, 1, 0, 0)
assert env.end_datetime == datetime.datetime(2002, 12, 31, 23, 0)
env.cleanup_log_files() # delete the logged data
# Test custom start and end year
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
start_year=2003,
end_year=2003,
random_seed=2022,
)
assert env.start_datetime == datetime.datetime(2003, 1, 1, 0, 0)
assert env.end_datetime == datetime.datetime(2003, 12, 31, 23, 0)
env.cleanup_log_files() # delete the logged data
def test_timing():
"""Test basic movements and time coordination for `WombatEnvironment`.
Methods that are completely tested:
- ``date_ix``
- ``simulation_time``
- ``weather_now``
Methods that are partially tested for no inputs:
- ``hours_to_next_shift``
- ``is_workshift``
"""
# Setup a basic environment
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
random_seed=2022,
)
manager = RepairManager(env)
Windfarm(env, "layout.csv", manager)
# Run for 96 hours from the start to this point in the data
# 1/5/02 0:00,8.238614098,0.37854216
correct_index = 96
correct_date = datetime.date(2002, 1, 5)
correct_hour = 0
correct_datetime = datetime.datetime(2002, 1, 5, 0, 0, 0)
correct_wind = 8.238614098
correct_wave = 0.37854216
assert env.date_ix(correct_date) == correct_index
assert env.date_ix(correct_datetime) == correct_index
env.run(until=correct_index)
assert env.now == correct_index
assert env.simulation_time == correct_datetime
assert env.hours_to_next_shift() == 8
current_conditions = env.weather_now.to_numpy().flatten()[2:]
assert all(current_conditions == (correct_wind, correct_wave, correct_hour))
assert not env.is_workshift()
# Test for a non-even timing of 128.7 hours
# 1/6/02 8:00,8.321216829,0.795805105
until = 128.7
correct_index = 120
correct_date = datetime.date(2002, 1, 6)
correct_datetime = datetime.datetime(2002, 1, 6, 8, 42)
correct_hours_to_next_shift = 23.3
correct_hour = 8
correct_wind = 8.321216829
correct_wave = 0.795805105
assert env.date_ix(correct_date) == correct_index
assert env.date_ix(correct_datetime) == correct_index
env.run(until=until)
assert env.now == until
assert env.simulation_time == correct_datetime
assert env.hours_to_next_shift() == correct_hours_to_next_shift
current_conditions = env.weather_now.to_numpy().flatten()[2:]
assert all(current_conditions == (correct_wind, correct_wave, correct_hour))
assert env.is_workshift()
env.cleanup_log_files() # delete the logged data
def test_is_workshift():
"""Tests the ``is_workshift`` method for a variety of inputs."""
# Setup a basic environment
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
random_seed=2022,
)
manager = RepairManager(env)
Windfarm(env, "layout.csv", manager)
# Test for the 24 hours of the day with a basic 8am to 4pm workday
assert not env.is_workshift()
for i in range(23):
env.run(i + 1)
if 7 <= i < 15:
# THe hours from 8am until, but not at, 4pm are working hours
assert env.is_workshift()
else:
assert not env.is_workshift()
# Test the boundary conditions
# Start and current are the same time, but end is midnight
assert env.is_workshift(workday_start=i + 1, workday_end=24)
# Start is just before current, but the end is midnight
assert env.is_workshift(workday_start=i, workday_end=24)
# Start is just after, but the end is midnight
assert not env.is_workshift(workday_start=i + 2, workday_end=24)
# Start is before current, but end is the current hour
assert not env.is_workshift(workday_start=i, workday_end=i + 1)
env.cleanup_log_files() # delete the logged data
def test_hour_in_shift():
"""Tests the ``hour_in_shift`` method for a variety of inputs."""
# Setup a basic environment
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
random_seed=2022,
)
manager = RepairManager(env)
Windfarm(env, "layout.csv", manager)
# Test the boundary conditions at midnight
i = 0
# Start and current are the same time, but end is midnight
assert env.hour_in_shift(hour=i, workday_start=i, workday_end=24)
# Start is just before current (-1), so reverts to env workday settings
assert not env.hour_in_shift(hour=i, workday_start=-1, workday_end=24)
# Start is just after, but the end is midnight
assert not env.hour_in_shift(hour=i, workday_start=i + 1, workday_end=24)
# Start is before current, but end is the current hour
assert not env.hour_in_shift(hour=i, workday_start=i, workday_end=i)
# Test for the 24 hours of the day, post midnight
for i in range(1, 24):
# Start and current are the same time, but end is midnight
assert env.hour_in_shift(hour=i, workday_start=i, workday_end=24)
# Start is just before current, but the end is midnight
assert env.hour_in_shift(hour=i, workday_start=i - 1, workday_end=24)
# Start is just after, but the end is midnight
assert not env.hour_in_shift(hour=i, workday_start=i + 1, workday_end=24)
# Start is before current, but end is the current hour
assert not env.hour_in_shift(hour=i, workday_start=i, workday_end=i)
env.cleanup_log_files() # delete the logged data
def test_hours_to_next_shift():
"""Tests the ``hours_to_next_shift`` method for a variety of inputs."""
# Setup a basic environment
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
random_seed=2022,
)
manager = RepairManager(env)
Windfarm(env, "layout.csv", manager)
# Test that at hour 0, there are 8 hours until 8am
assert env.hours_to_next_shift() == 8
# Test that at hour 0, with start time 0, there are 24 hours until the next period
assert env.hours_to_next_shift(workday_start=0)
env.run(until=7)
# Test that there is still 1 hour left
assert env.hours_to_next_shift() == 1
# Test if the starting shift is at the same time, the we get 24 hours
assert env.hours_to_next_shift(workday_start=7) == 24
# Test that it's 24 hours left
env.run(until=8)
assert env.hours_to_next_shift() == 24
# Test for an uneven time until the next shift
env.run(until=9.3)
assert env.hours_to_next_shift() == pytest.approx(22.7, rel=1e-4) # weird floats
env.cleanup_log_files() # delete the logged data
def test_weather_forecast():
"""Tests the ``weather_forecast`` method for a variety of inputs."""
# Setup a basic environment
env = WombatEnvironment(
data_dir=TEST_DATA,
weather_file="test_weather_quick_load.csv",
workday_start=8,
workday_end=16,
simulation_name="testing_setup",
random_seed=2022,
)
manager = RepairManager(env)
Windfarm(env, "layout.csv", manager)
# Test for the next 5 hours, but at the top of the hour
# Starts with hour 0, and ends with the 5th hour following
correct_index = pl.from_pandas(
pd.date_range(
"1/1/2002 00:00:00", "1/1/2002 04:00:00", freq="1h", name="datetime"
)
)
correct_hour = np.array([0, 1, 2, 3, 4], dtype=float)
correct_wind = np.array(
[11.75561096, 10.41321252, 8.959270788, 9.10014808, 9.945059601]
)
correct_wave = np.array(
[1.281772405, 1.586584315, 1.725690828, 1.680982063, 1.552232138]
)
ix, hour, wind, wave = env.weather_forecast(hours=5)
assert ix.shape[0] == wind.shape[0] == wave.shape[0] == 5
assert_series_equal(ix, correct_index)
npt.assert_equal(hour, correct_hour)
npt.assert_equal(wind, correct_wind)
npt.assert_equal(wave, correct_wave)
# Test for 5 hours at an uneven start time increment
# Starts at hour 1, and ends with the 5th hour following
env.run(until=0.1)
correct_index = pl.from_pandas(
pd.date_range(
"1/1/2002 00:00:00", "1/1/2002 05:00:00", freq="1h", name="datetime"
)
)
correct_hour = np.arange(6, dtype=float)
correct_wind = np.array(
[11.75561096, 10.41321252, 8.959270788, 9.10014808, 9.945059601, 11.50807971]
)
correct_wave = np.array(
[1.281772405, 1.586584315, 1.725690828, 1.680982063, 1.552232138, 1.335083363]
)
ix, hour, wind, wave = env.weather_forecast(hours=5)
assert ix.shape[0] == hour.shape[0] == wind.shape[0] == wave.shape[0] == 6
assert_series_equal(ix, correct_index)
npt.assert_equal(hour, correct_hour)
npt.assert_equal(wind, correct_wind)
npt.assert_equal(wave, correct_wave)
# Test for 5.5 hours at an uneven start time increment
# Starts at hour 1, and ends at the 7th hour following
env.run(until=1.2)
correct_index = pl.from_pandas(
pd.date_range(
"1/1/2002 01:00:00", "1/1/2002 07:00:00", freq="1h", name="datetime"
)
)
correct_hour = np.arange(1, 8, dtype=float)
correct_wind = np.array(
[
10.41321252,
8.959270788,
9.10014808,
9.945059601,
11.50807971,
12.44757098,
13.33747206,
]
)
correct_wave = np.array(
[
1.586584315,
1.725690828,
1.680982063,
1.552232138,
1.335083363,
1.294392727,
1.187261471,
]
)
ix, hour, wind, wave = env.weather_forecast(hours=5.5)
assert ix.shape[0] == hour.shape[0] == wind.shape[0] == wave.shape[0] == 7
assert_series_equal(ix, correct_index)
npt.assert_equal(hour, correct_hour)
npt.assert_equal(wind, correct_wind)
npt.assert_equal(wave, correct_wave)
env.cleanup_log_files() # delete the logged data