-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtest_simulation_execution_options.py
More file actions
359 lines (306 loc) · 13.5 KB
/
test_simulation_execution_options.py
File metadata and controls
359 lines (306 loc) · 13.5 KB
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
import os
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import Mock, patch
from kwave import PLATFORM
from kwave.ksensor import kSensor
from kwave.options.simulation_execution_options import SimulationExecutionOptions
OMP_BINARY_NAME = "kspaceFirstOrder-OMP{}".format(".exe" if PLATFORM == "windows" else "")
CUDA_BINARY_NAME = "kspaceFirstOrder-CUDA{}".format(".exe" if PLATFORM == "windows" else "")
class TestSimulationExecutionOptions(unittest.TestCase):
def setUp(self):
self.default_options = SimulationExecutionOptions()
self.mock_sensor = Mock(spec=kSensor)
self.mock_sensor.record = ["p", "u_max"]
self.mock_sensor.record_start_index = 10
def test_default_initialization(self):
"""Test default values during initialization."""
options = self.default_options
self.assertFalse(options.is_gpu_simulation)
self.assertIsNone(options._binary_path)
self.assertIsNone(options._binary_name)
self.assertEqual(options.kwave_function_name, "kspaceFirstOrder3D")
self.assertTrue(options.delete_data)
self.assertIsNone(options.device_num)
self.assertEqual(options._num_threads, os.cpu_count())
self.assertIsNone(options.thread_binding)
self.assertEqual(options.verbose_level, 0)
self.assertTrue(options.auto_chunking)
self.assertTrue(options.show_sim_log)
self.assertIsNone(options.checkpoint_interval)
self.assertIsNone(options.checkpoint_file)
def test_num_threads_setter_valid(self):
"""Test setting a valid number of threads."""
options = self.default_options
options.num_threads = os.cpu_count()
self.assertEqual(options.num_threads, os.cpu_count())
options.num_threads = "all"
self.assertEqual(options.num_threads, os.cpu_count())
def test_num_threads_setter_invalid(self):
"""Test setting an invalid number of threads."""
options = self.default_options
with self.assertRaises(ValueError):
options.num_threads = -1
with self.assertRaises(ValueError):
options.num_threads = "invalid_value"
def test_verbose_level_setter_valid(self):
"""Test setting a valid verbose level."""
options = self.default_options
for level in range(3):
options.verbose_level = level
self.assertEqual(options.verbose_level, level)
def test_verbose_level_setter_invalid(self):
"""Test setting an invalid verbose level."""
options = self.default_options
with self.assertRaises(ValueError):
options.verbose_level = 3
with self.assertRaises(ValueError):
options.verbose_level = -1
def test_is_gpu_simulation_setter(self):
"""Test setting is_gpu_simulation and its impact on binary_name."""
options = self.default_options
options.is_gpu_simulation = True
self.assertTrue(options.is_gpu_simulation)
self.assertEqual(options.binary_name, CUDA_BINARY_NAME)
options.is_gpu_simulation = False
self.assertFalse(options.is_gpu_simulation)
self.assertEqual(options.binary_name, OMP_BINARY_NAME)
def test_device_num_setter_invalid(self):
"""Test setting an invalid device number."""
options = self.default_options
def test_binary_name_custom(self):
"""Test setting a custom binary name."""
options = self.default_options
options.binary_name = "custom_binary"
self.assertEqual(options.binary_name, "custom_binary")
def test_binary_name_extension_on_windows(self):
with patch("kwave.options.simulation_execution_options.PLATFORM", "windows"):
options = SimulationExecutionOptions()
self.assertTrue(options.binary_name.endswith(".exe"))
@patch("kwave.options.simulation_execution_options.PLATFORM", "darwin")
def test_get_options_string_darwin(self):
"""Test the get_options_string method with a mock sensor."""
options = self.default_options
options.device_num = 1
options.num_threads = 1
options.verbose_level = 2
options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
"1",
"-t",
"1",
"--verbose",
"2",
"--p_raw",
"--u_max",
"-s",
f"{self.mock_sensor.record_start_index}", # Updated to use self.mock_sensor
]
self.assertListEqual(expected_elements, options_list)
@patch("kwave.options.simulation_execution_options.PLATFORM", "windows")
def test_as_list_windows(self):
"""Test the list representation of options on Windows."""
options = self.default_options
options.device_num = 1
options.num_threads = 1
options.verbose_level = 2
options_list = options.as_list(self.mock_sensor)
expected_elements = ["-g", "1", "--verbose", "2", "--p_raw", "--u_max", "-s", f"{self.mock_sensor.record_start_index}"]
self.assertListEqual(expected_elements, options_list)
@patch("kwave.options.simulation_execution_options.PLATFORM", "darwin")
def test_as_list_darwin(self):
"""Test the list representation of options on macOS."""
options = self.default_options
options.device_num = 1
options.num_threads = 1
options.verbose_level = 2
options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",
"-t",
f"1",
"--verbose",
"2",
"--p_raw",
"--u_max",
"-s",
f"{self.mock_sensor.record_start_index}", # Updated to use self.mock_sensor
]
self.assertListEqual(expected_elements, options_list)
def test_as_list_custom_record(self):
"""Test the list representation with a custom record configuration."""
options = self.default_options
self.mock_sensor.record = ["p_max", "u_min", "I_avg"]
options.device_num = 1
options.num_threads = 1
options.verbose_level = 1
options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
"1",
"--verbose",
"1",
"--p_max",
"--u_min",
"--u_non_staggered_raw",
"--p_raw",
"-s",
"10",
]
if not PLATFORM == "windows":
expected_elements.insert(2, "-t")
expected_elements.insert(3, f"1")
self.assertListEqual(expected_elements, options_list)
def test_as_list_with_invalid_values(self):
"""Test the behavior of as_list when there are invalid values."""
options = self.default_options
with self.assertRaises(ValueError):
options.device_num = -1
def test_as_list_no_record(self):
"""Test the list representation when there is no record."""
options = self.default_options
self.mock_sensor.record = None
options.device_num = 1
options.num_threads = 1
options.verbose_level = 0
options_list = options.as_list(self.mock_sensor)
expected_elements = [
"-g",
f"{options.device_num}",
"--p_raw", # Default value
"-s", # start timestep index
"10",
]
if not PLATFORM == "windows":
expected_elements.insert(2, "-t")
expected_elements.insert(3, "1")
self.assertListEqual(expected_elements, options_list)
def test_list_compared_to_string(self):
"""Test the list representation compared to the string representation."""
options = self.default_options
options.device_num = 1
options.num_threads = os.cpu_count()
options.verbose_level = 1
options_list = options.as_list(self.mock_sensor)
options_string = options.get_options_string(self.mock_sensor)
self.assertEqual(" ".join(options_list), options_string)
def test_as_list_with_valid_checkpoint_interval_options(self):
"""Test the checkpoint interval options."""
options = self.default_options
options.num_threads = 1
options.checkpoint_interval = 10
options.checkpoint_file = "checkpoint.h5"
options_list = options.as_list(self.mock_sensor)
expected_elements = [
"--checkpoint_interval",
"10",
"--checkpoint_file",
"checkpoint.h5",
"--p_raw",
"--u_max",
"-s",
"10",
]
if not PLATFORM == "windows":
expected_elements.insert(0, "-t")
expected_elements.insert(1, f"1")
self.assertListEqual(expected_elements, options_list)
def test_as_list_with_valid_checkpoint_timesteps_options(self):
"""Test the checkpoint interval options."""
options = self.default_options
options.num_threads = 1
options.checkpoint_timesteps = 10
options.checkpoint_file = "checkpoint.h5"
options_list = options.as_list(self.mock_sensor)
expected_elements = [
"--checkpoint_timesteps",
"10",
"--checkpoint_file",
"checkpoint.h5",
"--p_raw",
"--u_max",
"-s",
"10",
]
if not PLATFORM == "windows":
expected_elements.insert(0, "-t")
expected_elements.insert(1, f"1")
self.assertListEqual(expected_elements, options_list)
def test_initialization_with_invalid_checkpoint_options(self):
"""Test initialization with invalid checkpoint options."""
with TemporaryDirectory() as temp_dir:
checkpoint_file = Path(temp_dir) / "checkpoint.h5"
# Test with invalid checkpoint_file type
with self.assertRaises(ValueError):
SimulationExecutionOptions(checkpoint_file=12345, checkpoint_timesteps=10)
# Test with both checkpoint options - should raise ValueError
with self.assertRaises(ValueError):
SimulationExecutionOptions(checkpoint_timesteps=10, checkpoint_interval=20, checkpoint_file=checkpoint_file)
# Test with just checkpoint_file (should raise ValueError)
with self.assertRaises(ValueError):
SimulationExecutionOptions(checkpoint_file=checkpoint_file)
def test_initialization_with_valid_checkpoint_options(self):
"""Test initialization with valid checkpoint options."""
with TemporaryDirectory() as temp_dir:
checkpoint_file = Path(temp_dir) / "checkpoint.h5"
# Test with valid checkpoint_timesteps
options = SimulationExecutionOptions(checkpoint_timesteps=10, checkpoint_file=checkpoint_file)
self.assertEqual(options.checkpoint_timesteps, 10)
self.assertEqual(options.checkpoint_file, checkpoint_file)
# Test with valid checkpoint_interval
options = SimulationExecutionOptions(checkpoint_interval=20, checkpoint_file=checkpoint_file)
self.assertEqual(options.checkpoint_interval, 20)
self.assertEqual(options.checkpoint_file, checkpoint_file)
def test_checkpoint_interval_validation(self):
"""Test validation of checkpoint_interval property."""
options = self.default_options
# Test valid values
options.checkpoint_interval = 0
self.assertEqual(options.checkpoint_interval, 0)
options.checkpoint_interval = 100
self.assertEqual(options.checkpoint_interval, 100)
# Test invalid values
with self.assertRaises(ValueError):
options.checkpoint_interval = -1
with self.assertRaises(ValueError):
options.checkpoint_interval = "invalid"
with self.assertRaises(ValueError):
options.checkpoint_interval = 1.5
def test_checkpoint_timesteps_validation(self):
"""Test validation of checkpoint_timesteps property."""
options = self.default_options
# Test valid values
options.checkpoint_timesteps = 0
self.assertEqual(options.checkpoint_timesteps, 0)
options.checkpoint_timesteps = 100
self.assertEqual(options.checkpoint_timesteps, 100)
# Test invalid values
with self.assertRaises(ValueError):
options.checkpoint_timesteps = -1
with self.assertRaises(ValueError):
options.checkpoint_timesteps = "invalid"
with self.assertRaises(ValueError):
options.checkpoint_timesteps = 1.5
def test_checkpoint_file_validation(self):
"""Test validation of checkpoint_file property."""
options = self.default_options
# Test valid values
with TemporaryDirectory() as temp_dir:
valid_path = Path(temp_dir) / "checkpoint.h5"
options.checkpoint_file = valid_path
self.assertEqual(options.checkpoint_file, valid_path)
# Test string path
options.checkpoint_file = str(valid_path)
self.assertEqual(options.checkpoint_file, valid_path)
# Test invalid values
with self.assertRaises(ValueError):
options.checkpoint_file = "invalid.extension"
with self.assertRaises(ValueError):
options.checkpoint_file = 123
with self.assertRaises(FileNotFoundError):
options.checkpoint_file = "/nonexistent/path/checkpoint.h5"
if __name__ == "__main__":
unittest.main()