From ad79766ca307851bc7bc279acd1503d4f23e6e98 Mon Sep 17 00:00:00 2001 From: Julio Machado Date: Sat, 15 Jun 2024 01:44:30 -0300 Subject: [PATCH 1/7] ENH: Adding important attributes to stored_results in JSON file --- .../components/stored_results.py | 76 +++++++++++++++++-- open_rocket_serializer/ork_extractor.py | 2 +- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/open_rocket_serializer/components/stored_results.py b/open_rocket_serializer/components/stored_results.py index 7db43d0..f48aa84 100644 --- a/open_rocket_serializer/components/stored_results.py +++ b/open_rocket_serializer/components/stored_results.py @@ -1,6 +1,6 @@ import logging import pickle - +import numpy as np import yaml from .._helpers import _dict_to_string @@ -8,7 +8,7 @@ logger = logging.getLogger(__name__) -def search_stored_results(bs): +def search_stored_results(bs, datapoints, data_labels, time_vector, burnout_position): """Search for the stored simulation results in the bs and return the settings as a dict. @@ -25,11 +25,9 @@ def search_stored_results(bs): "flighttime", "groundhitvelocity" and "launchrodvelocity". """ settings = {} - sim = bs.find("simulation") sim_data = sim.find("flightdata") logger.info("Found the 'flightdata' tag in the 'simulation' tag.") - name_map = { "max_altitude": "maxaltitude", "max_velocity": "maxvelocity", @@ -44,9 +42,77 @@ def search_stored_results(bs): for key, value in name_map.items(): settings[key] = float(sim_data.get(value, 0)) logger.info(f"Retrieved the '{key}' value from the .ork file: {settings[key]}") - + + settings["final_latitude"] = __get_parameter(datapoints, data_labels, time_vector, "Latitude", position = "last") + settings["final_longitude"] = __get_parameter(datapoints, data_labels, time_vector, "Longitude", position="last") + settings["max_stability_margin"] = __get_parameter(datapoints, data_labels, time_vector, "Stability margin calibers", + position="max") + settings["min_stability_margin"] = __get_parameter(datapoints, data_labels, time_vector, "Stability margin calibers", + position="min") + settings["burnout_stability_margin"] = __get_parameter(datapoints, data_labels, time_vector, "Stability margin calibers", + position=burnout_position) + settings["max_thrust"] = __get_parameter(datapoints, data_labels, time_vector, "Thrust", position="max") + settings["max_mach_number"] = __get_parameter(datapoints, data_labels, time_vector, "Mach number", position="max") + logger.info( "The flight data was successfully retrieved:\n%s", _dict_to_string(settings, indent=23), ) return settings + +def __get_parameter(datapoints, data_labels, time_vector, label, position): + """Get the latitude and longitude from the .ork file. + Parameters + ---------- + label : str + Latitude or longitude. + datapoints : list + List of datapoints from the .ork file. + data_labels : list + List of labels for the datapoints. + time_vector : list + The time vector of the simulation. + position : str or int + The position to get the value from. Can be "last", "first", "max", "min" or an integer. + """ + + parameter = [ + float(datapoint.text.split(",")[data_labels.index(label)]) + for datapoint in datapoints + ] + # convert to numpy array + parameter= np.array([time_vector, parameter]).T + # sort by time + parameter = parameter[parameter[:, 0].argsort()] + # clip the curve to remove negative values + parameter[parameter[:, 1] < 0, 1] = 0 + # Assuming parameter is a NumPy array and 'NaN' values are represented as np.nan + # This will keep rows where the second column is not NaN + parameter = parameter[~np.isnan(parameter[:, 1])] + + logger.info("parameters acquired successfully") + + if type(position) is str: + if position == "last": + return parameter[-1, 1] # return the end point (final time, final value) + else: + pass + if position == "first": + return parameter[0, 1] + else: + pass + if position == "max": + return np.max(parameter[:, 1]) + else: + pass + if position == "min": + return np.min(parameter[:, 1]) + else: + pass + else: + pass + if type(position) is np.int64: + return parameter[position, 1] + else: + logger.error("Invalid position parameter") + return "Error in position parameter" diff --git a/open_rocket_serializer/ork_extractor.py b/open_rocket_serializer/ork_extractor.py index e5133c3..f281543 100644 --- a/open_rocket_serializer/ork_extractor.py +++ b/open_rocket_serializer/ork_extractor.py @@ -89,7 +89,7 @@ def ork_extractor(bs, filepath, output_folder, ork, eng): transitions = search_transitions(bs, elements, ork, rocket_radius) rail_buttons = search_rail_buttons(bs, elements) parachutes = search_parachutes(bs) - stored_results = search_stored_results(bs) + stored_results = search_stored_results(bs, datapoints, data_labels, time_vector, burnout_position) # save everything to a dictionary settings["id"] = id_info From e79ace67d645787f3898748a8bc3e57ff729743e Mon Sep 17 00:00:00 2001 From: Julio Machado Date: Sat, 15 Jun 2024 02:23:26 -0300 Subject: [PATCH 2/7] ENH: small improvements. --- examples/EPFL--BellaLui--2020/parameters.json | 7 +++++ .../components/stored_results.py | 29 +++++++------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/examples/EPFL--BellaLui--2020/parameters.json b/examples/EPFL--BellaLui--2020/parameters.json index 90f819a..41cc692 100644 --- a/examples/EPFL--BellaLui--2020/parameters.json +++ b/examples/EPFL--BellaLui--2020/parameters.json @@ -66,13 +66,20 @@ "radius": 0.078 }, "stored_results": { + "burnout_stability_margin": 3.1703, + "final_latitude": 0.49934, + "final_longitude": 0.0, "flight_time": 92.782, "ground_hit_velocity": 26.142, "launch_rod_velocity": 14.728, "max_acceleration": 122.07, "max_altitude": 1190.0, "max_mach": 0.5587, + "max_mach_number": 0.5587, + "max_stability_margin": 4.8193, + "max_thrust": 1287.5, "max_velocity": 189.56, + "min_stability_margin": 0.85185, "time_to_apogee": 14.851 }, "tails": { diff --git a/open_rocket_serializer/components/stored_results.py b/open_rocket_serializer/components/stored_results.py index f48aa84..74236f2 100644 --- a/open_rocket_serializer/components/stored_results.py +++ b/open_rocket_serializer/components/stored_results.py @@ -90,29 +90,20 @@ def __get_parameter(datapoints, data_labels, time_vector, label, position): # This will keep rows where the second column is not NaN parameter = parameter[~np.isnan(parameter[:, 1])] - logger.info("parameters acquired successfully") - if type(position) is str: - if position == "last": - return parameter[-1, 1] # return the end point (final time, final value) - else: - pass - if position == "first": - return parameter[0, 1] - else: - pass - if position == "max": - return np.max(parameter[:, 1]) - else: - pass - if position == "min": - return np.min(parameter[:, 1]) - else: - pass + match position: + case "last": + return parameter[-1, 1] # return the end point (final time, final value) + case "first": + return parameter[0, 1] # return the first point (initial time, initial value) + case "max": + return np.max(parameter[:, 1]) # return the maximum value + case "min": + return np.min(parameter[:, 1]) # return the minimum value else: pass if type(position) is np.int64: - return parameter[position, 1] + return parameter[position, 1] # return the value at the specified position else: logger.error("Invalid position parameter") return "Error in position parameter" From 9a17c4c992e3a077c854bcef4c525b54f39d6b1d Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 15 Jun 2024 15:35:14 -0300 Subject: [PATCH 3/7] STY: apply black --- rocketserializer/ork_extractor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rocketserializer/ork_extractor.py b/rocketserializer/ork_extractor.py index 21d12a3..fc5280c 100644 --- a/rocketserializer/ork_extractor.py +++ b/rocketserializer/ork_extractor.py @@ -88,7 +88,9 @@ def ork_extractor(bs, filepath, output_folder, ork, eng): transitions = search_transitions(bs, elements, ork) rail_buttons = search_rail_buttons(bs, elements) parachutes = search_parachutes(bs) - stored_results = search_stored_results(bs, datapoints, data_labels, time_vector, burnout_position) + stored_results = search_stored_results( + bs, datapoints, data_labels, time_vector, burnout_position + ) # save everything to a dictionary settings["id"] = id_info From 830d741440308c084dd3835734e0bbe515a52cc9 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 15 Jun 2024 13:59:49 -0300 Subject: [PATCH 4/7] MNT: refactors the stored_results.py --- rocketserializer/components/stored_results.py | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/rocketserializer/components/stored_results.py b/rocketserializer/components/stored_results.py index 8a5e801..4cee5de 100644 --- a/rocketserializer/components/stored_results.py +++ b/rocketserializer/components/stored_results.py @@ -44,12 +44,6 @@ def search_stored_results(bs, datapoints, data_labels, time_vector, burnout_posi "Retrieved the '%s' value from the .ork file: %s", key, settings[key] ) - settings["final_latitude"] = __get_parameter( - datapoints, data_labels, time_vector, "Latitude", position="last" - ) - settings["final_longitude"] = __get_parameter( - datapoints, data_labels, time_vector, "Longitude", position="last" - ) settings["max_stability_margin"] = __get_parameter( datapoints, data_labels, @@ -74,9 +68,6 @@ def search_stored_results(bs, datapoints, data_labels, time_vector, burnout_posi settings["max_thrust"] = __get_parameter( datapoints, data_labels, time_vector, "Thrust", position="max" ) - settings["max_mach_number"] = __get_parameter( - datapoints, data_labels, time_vector, "Mach number", position="max" - ) logger.info( "The flight data was successfully retrieved:\n%s", @@ -98,7 +89,8 @@ def __get_parameter(datapoints, data_labels, time_vector, label, position): time_vector : list The time vector of the simulation. position : str or int - The position to get the value from. Can be "last", "first", "max", "min" or an integer. + The position to get the value from. Can be "last", "first", "max", "min" + or an integer. """ parameter = [ @@ -115,23 +107,20 @@ def __get_parameter(datapoints, data_labels, time_vector, label, position): # This will keep rows where the second column is not NaN parameter = parameter[~np.isnan(parameter[:, 1])] - if type(position) is str: - match position: - case "last": - return parameter[ - -1, 1 - ] # return the end point (final time, final value) - case "first": - return parameter[ - 0, 1 - ] # return the first point (initial time, initial value) - case "max": - return np.max(parameter[:, 1]) # return the maximum value - case "min": - return np.min(parameter[:, 1]) # return the minimum value + if isinstance(position, str): + if position == "last": + # return the end point (final time, final value) + return parameter[-1, 1] + elif position == "first": + # return the first point (initial time, initial value) + return parameter[0, 1] + elif position == "max": + return np.max(parameter[:, 1]) # return the maximum value + elif position == "min": + return np.min(parameter[:, 1]) # return the minimum value else: pass - if type(position) is np.int64: + if isinstance(position, np.int64): return parameter[position, 1] # return the value at the specified position else: logger.error("Invalid position parameter") From 26daae25c83e1277c704119e68d59563abdb76ab Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 15 Jun 2024 14:00:56 -0300 Subject: [PATCH 5/7] TST: update tests and examples results --- Makefile | 20 +++++++++++++++++++ examples/EPFL--BellaLui--2020/parameters.json | 7 ++----- examples/NDRT--Rocket--2020/parameters.json | 5 +++++ .../parameters.json | 5 +++++ pyproject.toml | 1 + run-tests.ps1 | 6 ++++++ .../EPFL--BellaLui--2020/parameters.json | 6 +++++- .../NDRT--Rocket--2020/parameters.json | 6 +++++- .../parameters.json | 6 +++++- 9 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4a6e122 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +lint: isort black + +isort: + isort . + +black: + black . + +pylint: + pylint rocketserializer/ --output="pylint_report.txt" + +tests: + pytest + +# tests-unit: + +# tests-acceptance: + +# tests-integration: + diff --git a/examples/EPFL--BellaLui--2020/parameters.json b/examples/EPFL--BellaLui--2020/parameters.json index 41cc692..581e7a9 100644 --- a/examples/EPFL--BellaLui--2020/parameters.json +++ b/examples/EPFL--BellaLui--2020/parameters.json @@ -42,7 +42,7 @@ "nozzle_radius": 0.02025, "position": 2.3379117844381234, "throat_radius": 0.0135, - "thrust_source": "examples/EPFL--BellaLui--2020/thrust_source.csv" + "thrust_source": "examples\\EPFL--BellaLui--2020\\thrust_source.csv" }, "nosecones": { "base_radius": 0.078, @@ -56,7 +56,7 @@ "rocket": { "center_of_mass_without_propellant": 1.559, "coordinate_system_orientation": "nose_to_tail", - "drag_curve": "examples/EPFL--BellaLui--2020/drag_curve.csv", + "drag_curve": "examples\\EPFL--BellaLui--2020\\drag_curve.csv", "inertia": [ 0.096246, 0.096246, @@ -67,15 +67,12 @@ }, "stored_results": { "burnout_stability_margin": 3.1703, - "final_latitude": 0.49934, - "final_longitude": 0.0, "flight_time": 92.782, "ground_hit_velocity": 26.142, "launch_rod_velocity": 14.728, "max_acceleration": 122.07, "max_altitude": 1190.0, "max_mach": 0.5587, - "max_mach_number": 0.5587, "max_stability_margin": 4.8193, "max_thrust": 1287.5, "max_velocity": 189.56, diff --git a/examples/NDRT--Rocket--2020/parameters.json b/examples/NDRT--Rocket--2020/parameters.json index 2d34c2b..0a3e057 100644 --- a/examples/NDRT--Rocket--2020/parameters.json +++ b/examples/NDRT--Rocket--2020/parameters.json @@ -1,4 +1,5 @@ { + "elliptical_fins": {}, "environment": { "base_pressure": null, "base_temperature": null, @@ -84,13 +85,17 @@ "radius": 0.1016 }, "stored_results": { + "burnout_stability_margin": 3.7288, "flight_time": 86.725, "ground_hit_velocity": 4.7426, "launch_rod_velocity": 17.993, "max_acceleration": 67.386, "max_altitude": 1085.9, "max_mach": 0.42789, + "max_stability_margin": 3.7685, + "max_thrust": 1785.0, "max_velocity": 145.01, + "min_stability_margin": 1.2489, "time_to_apogee": 15.761 }, "tails": { diff --git a/examples/ProjetoJupiter--Valetudo--2019/parameters.json b/examples/ProjetoJupiter--Valetudo--2019/parameters.json index 84168b8..f57c4e6 100644 --- a/examples/ProjetoJupiter--Valetudo--2019/parameters.json +++ b/examples/ProjetoJupiter--Valetudo--2019/parameters.json @@ -1,4 +1,5 @@ { + "elliptical_fins": {}, "environment": { "base_pressure": 94311.0, "base_temperature": 300.76, @@ -81,13 +82,17 @@ "radius": 0.04045000000000001 }, "stored_results": { + "burnout_stability_margin": 2.4363, "flight_time": 64.161, "ground_hit_velocity": 18.146, "launch_rod_velocity": 26.397, "max_acceleration": 108.52, "max_altitude": 882.42, "max_mach": 0.36177, + "max_stability_margin": 3.2714, + "max_thrust": 1067.7, "max_velocity": 125.47, + "min_stability_margin": 0.0, "time_to_apogee": 14.65 }, "tails": {}, diff --git a/pyproject.toml b/pyproject.toml index 1525d05..4a777d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -76,4 +76,5 @@ disable = """ raise-missing-from, fixme, no-else-break, + no-else-return, """ diff --git a/run-tests.ps1 b/run-tests.ps1 index cf2bc43..416eb7c 100644 --- a/run-tests.ps1 +++ b/run-tests.ps1 @@ -1,3 +1,9 @@ ork2json --filepath="examples\EPFL--BellaLui--2020\rocket.ork" ork2json --filepath="examples\NDRT--Rocket--2020\rocket.ork" ork2json --filepath="examples\ProjetoJupiter--Valetudo--2019\rocket.ork" +ork2json --filepath="examples\WERT--Prometheus--2022\rocket.ork" + +ork2ipynb --filepath="examples\EPFL--BellaLui--2020\rocket.ork" +ork2ipynb --filepath="examples\NDRT--Rocket--2020\rocket.ork" +ork2ipynb --filepath="examples\ProjetoJupiter--Valetudo--2019\rocket.ork" +ork2ipynb --filepath="examples\WERT--Prometheus--2022\rocket.ork" diff --git a/tests/acceptance/EPFL--BellaLui--2020/parameters.json b/tests/acceptance/EPFL--BellaLui--2020/parameters.json index fc7f04e..4c1151e 100644 --- a/tests/acceptance/EPFL--BellaLui--2020/parameters.json +++ b/tests/acceptance/EPFL--BellaLui--2020/parameters.json @@ -96,6 +96,10 @@ "time_to_apogee": 14.851, "flight_time": 92.782, "ground_hit_velocity": 26.142, - "launch_rod_velocity": 14.728 + "launch_rod_velocity": 14.728, + "max_stability_margin": 4.8193, + "min_stability_margin": 0.85185, + "burnout_stability_margin": 3.1703, + "max_thrust": 1287.5 } } \ No newline at end of file diff --git a/tests/acceptance/NDRT--Rocket--2020/parameters.json b/tests/acceptance/NDRT--Rocket--2020/parameters.json index 23616b8..5105b69 100644 --- a/tests/acceptance/NDRT--Rocket--2020/parameters.json +++ b/tests/acceptance/NDRT--Rocket--2020/parameters.json @@ -115,6 +115,10 @@ "time_to_apogee": 15.761, "flight_time": 86.725, "ground_hit_velocity": 4.7426, - "launch_rod_velocity": 17.993 + "launch_rod_velocity": 17.993, + "max_stability_margin": 3.7685, + "min_stability_margin": 1.2489, + "burnout_stability_margin": 3.7288, + "max_thrust": 1785.0 } } \ No newline at end of file diff --git a/tests/acceptance/ProjetoJupiter--Valetudo--2019/parameters.json b/tests/acceptance/ProjetoJupiter--Valetudo--2019/parameters.json index e410b2b..a282666 100644 --- a/tests/acceptance/ProjetoJupiter--Valetudo--2019/parameters.json +++ b/tests/acceptance/ProjetoJupiter--Valetudo--2019/parameters.json @@ -104,6 +104,10 @@ "time_to_apogee": 14.65, "flight_time": 64.161, "ground_hit_velocity": 18.146, - "launch_rod_velocity": 26.397 + "launch_rod_velocity": 26.397, + "max_stability_margin": 3.2714, + "min_stability_margin": 0.0, + "burnout_stability_margin": 2.4363, + "max_thrust": 1067.7 } } \ No newline at end of file From a0c8a3d9dd7241a43181ee5e522cc6b02919265e Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com> Date: Sat, 15 Jun 2024 14:04:59 -0300 Subject: [PATCH 6/7] MNT Use raise exception instead of returning string --- rocketserializer/components/stored_results.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rocketserializer/components/stored_results.py b/rocketserializer/components/stored_results.py index 4cee5de..d4cd511 100644 --- a/rocketserializer/components/stored_results.py +++ b/rocketserializer/components/stored_results.py @@ -124,4 +124,4 @@ def __get_parameter(datapoints, data_labels, time_vector, label, position): return parameter[position, 1] # return the value at the specified position else: logger.error("Invalid position parameter") - return "Error in position parameter" + raise ValueError("Error in position parameter") From 7a43303c03823e2e90d64686e35b6cfc2344b9de Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sat, 15 Jun 2024 14:07:54 -0300 Subject: [PATCH 7/7] DEV: bump up python version for running CI --- .github/workflows/test-pytest.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-pytest.yaml b/.github/workflows/test-pytest.yaml index 68855c2..c73dd21 100644 --- a/.github/workflows/test-pytest.yaml +++ b/.github/workflows/test-pytest.yaml @@ -15,7 +15,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - python-version: [3.8, 3.12] + python-version: [3.9, 3.12] env: OS: ${{ matrix.os }} PYTHON: ${{ matrix.python-version }}