-
-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathtest_monte_carlo.py
More file actions
209 lines (176 loc) · 7.02 KB
/
Copy pathtest_monte_carlo.py
File metadata and controls
209 lines (176 loc) · 7.02 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
# pylint: disable=unused-argument
import os
from unittest.mock import patch
import matplotlib as plt
import numpy as np
import pytest
plt.rcParams.update({"figure.max_open_warning": 0})
def _post_test_file_cleanup():
"""Clean monte carlo files after test session if they exist."""
if os.path.exists("monte_carlo_class_example.kml"):
os.remove("monte_carlo_class_example.kml")
if os.path.exists("monte_carlo_test.errors.txt"):
os.remove("monte_carlo_test.errors.txt")
if os.path.exists("monte_carlo_test.inputs.txt"):
os.remove("monte_carlo_test.inputs.txt")
if os.path.exists("monte_carlo_test.outputs.txt"):
os.remove("monte_carlo_test.outputs.txt")
@pytest.mark.slow
@pytest.mark.parametrize("parallel", [False, True])
def test_monte_carlo_simulate(monte_carlo_calisto, parallel):
"""Tests the simulate method of the MonteCarlo class.
Parameters
----------
monte_carlo_calisto : MonteCarlo
The MonteCarlo object, this is a pytest fixture.
"""
try:
# NOTE: this is really slow, it runs 10 flight simulations
monte_carlo_calisto.simulate(
number_of_simulations=10, append=False, parallel=parallel
)
assert monte_carlo_calisto.num_of_loaded_sims == 10
assert monte_carlo_calisto.number_of_simulations == 10
assert str(monte_carlo_calisto.filename.name) == "monte_carlo_test"
assert str(monte_carlo_calisto.error_file.name) == "monte_carlo_test.errors.txt"
assert (
str(monte_carlo_calisto.output_file.name) == "monte_carlo_test.outputs.txt"
)
assert np.isclose(
monte_carlo_calisto.processed_results["apogee"][0], 4711, rtol=0.2
)
assert np.isclose(
monte_carlo_calisto.processed_results["impact_velocity"][0],
-5.234,
rtol=0.2,
)
finally:
_post_test_file_cleanup()
def test_monte_carlo_set_inputs_log(monte_carlo_calisto):
"""Tests the set_inputs_log method of the MonteCarlo class.
Parameters
----------
monte_carlo_calisto : MonteCarlo
The MonteCarlo object, this is a pytest fixture.
"""
try:
monte_carlo_calisto.input_file = "tests/fixtures/monte_carlo/example.inputs.txt"
monte_carlo_calisto.set_inputs_log()
assert len(monte_carlo_calisto.inputs_log) == 100
assert all(isinstance(item, dict) for item in monte_carlo_calisto.inputs_log)
assert all(
"gravity" in item and "elevation" in item
for item in monte_carlo_calisto.inputs_log
)
finally:
_post_test_file_cleanup()
def test_monte_carlo_set_outputs_log(monte_carlo_calisto):
"""Tests the set_outputs_log method of the MonteCarlo class.
Parameters
----------
monte_carlo_calisto : MonteCarlo
The MonteCarlo object, this is a pytest fixture.
"""
try:
monte_carlo_calisto.output_file = (
"tests/fixtures/monte_carlo/example.outputs.txt"
)
monte_carlo_calisto.set_outputs_log()
assert len(monte_carlo_calisto.outputs_log) == 100
assert all(isinstance(item, dict) for item in monte_carlo_calisto.outputs_log)
assert all(
"apogee" in item and "impact_velocity" in item
for item in monte_carlo_calisto.outputs_log
)
finally:
_post_test_file_cleanup()
# def test_monte_carlo_set_errors_log(monte_carlo_calisto):
# monte_carlo_calisto.error_file = "tests/fixtures/monte_carlo/example.errors.txt"
# monte_carlo_calisto.set_errors_log()
# assert
def test_monte_carlo_prints(monte_carlo_calisto):
"""Tests the prints methods of the MonteCarlo class."""
try:
monte_carlo_calisto.info()
monte_carlo_calisto.compare_info(monte_carlo_calisto)
finally:
_post_test_file_cleanup()
@patch("matplotlib.pyplot.show") # pylint: disable=unused-argument
def test_monte_carlo_plots(mock_show, monte_carlo_calisto_pre_loaded):
"""Tests the plots methods of the MonteCarlo class."""
try:
assert monte_carlo_calisto_pre_loaded.all_info() is None
assert (
monte_carlo_calisto_pre_loaded.compare_plots(monte_carlo_calisto_pre_loaded)
is None
)
assert (
monte_carlo_calisto_pre_loaded.compare_ellipses(
monte_carlo_calisto_pre_loaded
)
is None
)
finally:
_post_test_file_cleanup()
def test_monte_carlo_export_ellipses_to_kml(monte_carlo_calisto_pre_loaded):
"""Tests the export_ellipses_to_kml method of the MonteCarlo class.
Parameters
----------
monte_carlo_calisto_pre_loaded : MonteCarlo
The MonteCarlo object, this is a pytest fixture.
"""
try:
assert (
monte_carlo_calisto_pre_loaded.export_ellipses_to_kml(
filename="monte_carlo_class_example.kml",
origin_lat=32.990254,
origin_lon=-106.974998,
type="all",
)
is None
)
finally:
_post_test_file_cleanup()
@pytest.mark.slow
def test_monte_carlo_callback(monte_carlo_calisto):
"""Tests the data_collector argument of the MonteCarlo class.
Parameters
----------
monte_carlo_calisto : MonteCarlo
The MonteCarlo object, this is a pytest fixture.
"""
try:
# define valid data collector
valid_data_collector = {
"name": lambda flight: flight.name,
"density_t0": lambda flight: flight.env.density(0),
}
monte_carlo_calisto.data_collector = valid_data_collector
# NOTE: this is really slow, it runs 10 flight simulations
monte_carlo_calisto.simulate(number_of_simulations=10, append=False)
# tests if print works when we have None in summary
monte_carlo_calisto.info()
## tests if an error is raised for invalid data_collector definitions
# invalid type
def invalid_data_collector(flight):
return flight.name
with pytest.raises(ValueError):
monte_carlo_calisto._check_data_collector(invalid_data_collector)
# invalid key overwrite
invalid_data_collector = {"apogee": lambda flight: flight.apogee}
with pytest.raises(ValueError):
monte_carlo_calisto._check_data_collector(invalid_data_collector)
# invalid callback definition
invalid_data_collector = {"name": "Calisto"} # callbacks must be callables!
with pytest.raises(ValueError):
monte_carlo_calisto._check_data_collector(invalid_data_collector)
# invalid logic (division by zero)
invalid_data_collector = {
"density_t0": lambda flight: flight.env.density(0) / "0",
}
monte_carlo_calisto.data_collector = invalid_data_collector
# NOTE: this is really slow, it runs 10 flight simulations
with pytest.raises(ValueError):
monte_carlo_calisto.simulate(number_of_simulations=10, append=False)
finally:
_post_test_file_cleanup()