Skip to content

Commit 7ab4edd

Browse files
committed
MNT: adjusting minor changes to .rpy functions and tests.
Formatted docstrings correctly. Reverted duplication of `test_encoding.py` files. Version warning will be called when loaded version is more recent.
1 parent b8d3d46 commit 7ab4edd

4 files changed

Lines changed: 71 additions & 201 deletions

File tree

rocketpy/utilities.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,9 @@ def get_instance_attributes(instance):
692692
return attributes_dict
693693

694694

695-
def save_to_rpy(flight: Flight, filename: str, include_output = False):
696-
"""Saves a .rpy file into the given path, containing key simulation informations to reproduce the results.
695+
def save_to_rpy(flight: Flight, filename: str, include_outputs = False):
696+
"""Saves a .rpy file into the given path, containing key simulation
697+
informations to reproduce the results.
697698
698699
Parameters
699700
----------
@@ -702,7 +703,8 @@ def save_to_rpy(flight: Flight, filename: str, include_output = False):
702703
filename : str
703704
Path where the file will be saved in
704705
include_output : bool, optional
705-
If True, the function will include extra outputs into the file, by default False
706+
If True, the function will include extra outputs into the file,
707+
by default False
706708
707709
Returns
708710
-------
@@ -720,7 +722,7 @@ def save_to_rpy(flight: Flight, filename: str, include_output = False):
720722
f,
721723
cls=RocketPyEncoder,
722724
indent=2,
723-
include_outputs=include_output,
725+
include_outputs=include_outputs,
724726
)
725727

726728

@@ -732,7 +734,8 @@ def load_from_rpy(filename: str, resimulate = False):
732734
filename : str
733735
Path where the file to be loaded is
734736
resimulate : bool, optional
735-
If True, the function will resimulate the object Flight, by default False
737+
If True, the function will resimulate the object Flight,
738+
by default False
736739
737740
Returns
738741
-------
@@ -743,10 +746,15 @@ def load_from_rpy(filename: str, resimulate = False):
743746
if ext == ".rpy":
744747
with open(filename, "r") as f:
745748
data = json.load(f)
746-
if data["version"] < version("rocketpy"):
747-
warnings.warn("The file was saved in an older version of RocketPy than the one in the current environment")
749+
if data["version"] > version("rocketpy"):
750+
warnings.warn("The file was saved in an updated version of",
751+
f"RocketPy (v{data["version"]}), the current",
752+
f"imported module is v{version('rocketpy')}")
748753
simulation = json.dumps(data["simulation"])
749-
flight = json.loads(simulation, cls=RocketPyDecoder, resimulate=resimulate)
754+
flight = json.loads(
755+
simulation,
756+
cls=RocketPyDecoder,
757+
resimulate=resimulate)
750758
return flight
751759
else:
752760
raise ValueError(f"Invalid file extension: {ext}. Allowed: .rpy")

tests/integration/test_flight_save_load_resimulate.py renamed to tests/integration/test_encoding

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,50 @@
77
from rocketpy._encoders import RocketPyDecoder, RocketPyEncoder
88

99

10+
@pytest.mark.parametrize(
11+
["flight_name", "include_outputs"],
12+
[
13+
("flight_calisto", False),
14+
("flight_calisto", True),
15+
("flight_calisto_robust", True),
16+
("flight_calisto_liquid_modded", False),
17+
("flight_calisto_hybrid_modded", False),
18+
],
19+
)
20+
def test_flight_save_load_no_resimulate(flight_name, include_outputs, request):
21+
"""Test encoding a ``rocketpy.Flight``.
22+
23+
Parameters
24+
----------
25+
flight_name : str
26+
Name flight fixture to encode.
27+
request : pytest.FixtureRequest
28+
Pytest request object.
29+
"""
30+
flight_to_save = request.getfixturevalue(flight_name)
31+
32+
with open("flight.json", "w") as f:
33+
json.dump(
34+
flight_to_save,
35+
f,
36+
cls=RocketPyEncoder,
37+
indent=2,
38+
include_outputs=include_outputs,
39+
)
40+
41+
with open("flight.json", "r") as f:
42+
flight_loaded = json.load(f, cls=RocketPyDecoder, resimulate=False)
43+
44+
assert np.isclose(flight_to_save.t_initial, flight_loaded.t_initial)
45+
assert np.isclose(flight_to_save.out_of_rail_time, flight_loaded.out_of_rail_time)
46+
assert np.isclose(flight_to_save.apogee_time, flight_loaded.apogee_time)
47+
48+
# Higher tolerance due to random parachute trigger
49+
assert np.isclose(flight_to_save.t_final, flight_loaded.t_final, rtol=1e-3)
50+
51+
os.remove("flight.json")
52+
53+
1054
@pytest.mark.slow
1155
@pytest.mark.parametrize(
1256
["flight_name", "include_outputs"],
@@ -18,7 +62,7 @@
1862
("flight_calisto_hybrid_modded", False),
1963
],
2064
)
21-
def test_flight_save_load(flight_name, include_outputs, request):
65+
def test_flight_save_load_resimulate(flight_name, include_outputs, request):
2266
"""Test encoding a ``rocketpy.Flight``.
2367

2468
Parameters
@@ -69,7 +113,7 @@ def test_function_encoder(function_name, request):
69113

70114
json_encoded = json.dumps(function_to_encode, cls=RocketPyEncoder)
71115

72-
function_loaded = json.loads(json_encoded, cls=RocketPyDecoder, resimulate=True)
116+
function_loaded = json.loads(json_encoded, cls=RocketPyDecoder)
73117

74118
assert isinstance(function_loaded, type(function_to_encode))
75119
assert np.isclose(function_to_encode(0), function_loaded(0))
@@ -92,7 +136,7 @@ def test_environment_encoder(environment_name, request):
92136

93137
json_encoded = json.dumps(env_to_encode, cls=RocketPyEncoder)
94138

95-
env_loaded = json.loads(json_encoded, cls=RocketPyDecoder, resimulate=True)
139+
env_loaded = json.loads(json_encoded, cls=RocketPyDecoder)
96140

97141
test_heights = np.linspace(0, 10000, 100)
98142

@@ -136,7 +180,7 @@ def test_motor_encoder(motor_name, request):
136180

137181
json_encoded = json.dumps(motor_to_encode, cls=RocketPyEncoder)
138182

139-
motor_loaded = json.loads(json_encoded, cls=RocketPyDecoder, resimulate=True)
183+
motor_loaded = json.loads(json_encoded, cls=RocketPyDecoder)
140184

141185
sample_times = np.linspace(*motor_to_encode.burn_time, 100)
142186

@@ -172,7 +216,7 @@ def test_rocket_encoder(rocket_name, request):
172216

173217
json_encoded = json.dumps(rocket_to_encode, cls=RocketPyEncoder)
174218

175-
rocket_loaded = json.loads(json_encoded, cls=RocketPyDecoder, resimulate=True)
219+
rocket_loaded = json.loads(json_encoded, cls=RocketPyDecoder)
176220

177221
sample_times = np.linspace(*rocket_to_encode.motor.burn_time, 100)
178222

tests/integration/test_flight_save_load_no_resimulate

Lines changed: 0 additions & 185 deletions
This file was deleted.

tests/unit/test_utilities.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ def test_check_constant(f, eps, expected):
213213

214214

215215
def test_save_to_rpy(flight_calisto_robust):
216-
"""Tests if the save_to_rpy function correctly saves the data to the correct file.
216+
"""Tests if the save_to_rpy function correctly saves the data to the
217+
correct file.
217218
218219
Parameters
219220
----------
@@ -222,13 +223,15 @@ def test_save_to_rpy(flight_calisto_robust):
222223
in the conftest.py file.
223224
"""
224225
utilities.save_to_rpy(flight_calisto_robust, "flight_calisto_robust.rpy")
225-
assert os.path.splitext(os.path.basename("flight_calisto_robust.rpy")) == ("flight_calisto_robust", ".rpy")
226+
assert (os.path.splitext(os.path.basename("flight_calisto_robust.rpy"))
227+
== ("flight_calisto_robust", ".rpy"))
226228
assert os.path.getsize("flight_calisto_robust.rpy") > 0
227229
os.remove("flight_calisto_robust.rpy")
228230

229231
@patch("matplotlib.pyplot.show")
230232
def test_load_from_rpy(mock_show): # pylint: disable=unused-argument
231-
"""Tests if the load_from_rpy function correctly loads the data into a flight object.
233+
"""Tests if the load_from_rpy function correctly loads the data into a
234+
flight object.
232235
233236
Parameters
234237
----------

0 commit comments

Comments
 (0)