From 0d783df723c99678bfb3ede186b43d655a631a36 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Wed, 13 Mar 2024 11:24:17 -0400 Subject: [PATCH] MNT: minor fixes - Add module docstrings - Fix one camelCase variable - Fix too long lines - Adjust one unpacking tuble --- rocketpy/plots/monte_carlo_plots.py | 12 ++++++------ rocketpy/simulation/monte_carlo.py | 13 ++++--------- rocketpy/stochastic/__init__.py | 4 ++++ .../stochastic/stochastic_aero_surfaces.py | 3 +++ rocketpy/stochastic/stochastic_environment.py | 15 +++++++++++---- rocketpy/stochastic/stochastic_flight.py | 4 +++- .../stochastic/stochastic_generic_motor.py | 2 ++ rocketpy/stochastic/stochastic_model.py | 19 +++++++++++++------ rocketpy/stochastic/stochastic_motor_model.py | 2 ++ rocketpy/stochastic/stochastic_parachute.py | 2 ++ rocketpy/stochastic/stochastic_rocket.py | 15 ++++++++++----- rocketpy/stochastic/stochastic_solid_motor.py | 2 ++ 12 files changed, 62 insertions(+), 31 deletions(-) diff --git a/rocketpy/plots/monte_carlo_plots.py b/rocketpy/plots/monte_carlo_plots.py index 3f3c5b782..e03a3f4f3 100644 --- a/rocketpy/plots/monte_carlo_plots.py +++ b/rocketpy/plots/monte_carlo_plots.py @@ -13,7 +13,7 @@ def ellipses( self, image=None, actual_landing_point=None, - perimeterSize=3000, + perimeter_size=3000, xlim=(-3000, 3000), ylim=(-3000, 3000), save=False, @@ -31,7 +31,7 @@ def ellipses( Useful when comparing the Monte Carlo results with the actual landing. Must be given in tuple format, such as (x, y) in meters. By default None. - perimeterSize : int, optional + perimeter_size : int, optional The size of the perimeter to be plotted. The default is 3000. xlim : tuple, optional The limits of the x axis. The default is (-3000, 3000). @@ -127,10 +127,10 @@ def ellipses( img, zorder=0, extent=[ - -perimeterSize - dx, - perimeterSize - dx, - -perimeterSize - dy, - perimeterSize - dy, + -perimeter_size - dx, + perimeter_size - dx, + -perimeter_size - dy, + perimeter_size - dy, ], ) plt.axhline(0, color="black", linewidth=0.5) diff --git a/rocketpy/simulation/monte_carlo.py b/rocketpy/simulation/monte_carlo.py index 4ba34f920..b1e3b120d 100644 --- a/rocketpy/simulation/monte_carlo.py +++ b/rocketpy/simulation/monte_carlo.py @@ -1,3 +1,5 @@ +"""Defines the MonteCarlo class.""" + import json from time import process_time, time @@ -12,11 +14,7 @@ generate_monte_carlo_ellipses_coordinates, ) -# TODO: How to save Functions? With pickle? Save just the source? - -# TODO: create a method that recreates each flight from inputs_log -# and saves it in an attribute that is a list - +# TODO: Let Functions and Flights be json serializable # TODO: Create evolution plots to analyze convergence @@ -669,10 +667,7 @@ def export_ellipses_to_kml( ( impact_ellipses, apogee_ellipses, - _, - _, - _, - _, + *_, ) = generate_monte_carlo_ellipses(self.results) outputs = [] diff --git a/rocketpy/stochastic/__init__.py b/rocketpy/stochastic/__init__.py index 67b5855fa..74364e8ef 100644 --- a/rocketpy/stochastic/__init__.py +++ b/rocketpy/stochastic/__init__.py @@ -1,3 +1,7 @@ +"""The rocketpy.stochastic module contains classes that are used to generate +randomized objects based on the provided information. Each of the classes +defined here represent one different rocketpy class.""" + from .stochastic_aero_surfaces import ( StochasticEllipticalFins, StochasticNoseCone, diff --git a/rocketpy/stochastic/stochastic_aero_surfaces.py b/rocketpy/stochastic/stochastic_aero_surfaces.py index e5cd7191d..5d933204a 100644 --- a/rocketpy/stochastic/stochastic_aero_surfaces.py +++ b/rocketpy/stochastic/stochastic_aero_surfaces.py @@ -1,3 +1,6 @@ +"""Defines the StochasticNoseCone, StochasticTrapezoidalFins, +StochasticEllipticalFins, StochasticTail and StochasticRailButtons classes.""" + from rocketpy.rocket.aero_surface import ( EllipticalFins, NoseCone, diff --git a/rocketpy/stochastic/stochastic_environment.py b/rocketpy/stochastic/stochastic_environment.py index 7216caccd..27c55aab5 100644 --- a/rocketpy/stochastic/stochastic_environment.py +++ b/rocketpy/stochastic/stochastic_environment.py @@ -1,3 +1,5 @@ +"""Defines the StochasticEnvironment class.""" + from .stochastic_model import StochasticModel @@ -112,7 +114,10 @@ def __str__(self): if isinstance(value, tuple): try: # Format the tuple as a string with the mean and standard deviation. - value_str = f"{value[0]:.5f} ± {value[1]:.5f} (numpy.random.{value[2].__name__})" + value_str = ( + f"{value[0]:.5f} ± {value[1]:.5f} " + f"(numpy.random.{value[2].__name__})" + ) except AttributeError: # treats date attribute value_str = str(value) @@ -167,7 +172,10 @@ def _validate_ensemble(self, ensemble_member, environment): <= min(ensemble_member) <= max(ensemble_member) < environment.num_ensemble_members - ), f"`ensemble_member` must be in the range from 0 to {environment.num_ensemble_members - 1}" + ), ( + "`ensemble_member` must be in the range from 0 to " + + f"{environment.num_ensemble_members - 1}" + ) setattr(self, "ensemble_member", ensemble_member) else: # if no ensemble member is provided, get it from the environment @@ -191,8 +199,7 @@ def create_object(self): generated_dict = next(self.dict_generator()) for key, value in generated_dict.items(): # special case for ensemble member - # TODO if env.ensemble_member had a setter this create_object method - # could be generalized + # TODO: Generalize create_object() with a env.ensemble_member setter if key == "ensemble_member": self.object.select_ensemble_member(value) else: diff --git a/rocketpy/stochastic/stochastic_flight.py b/rocketpy/stochastic/stochastic_flight.py index e962b236e..f1e1559c5 100644 --- a/rocketpy/stochastic/stochastic_flight.py +++ b/rocketpy/stochastic/stochastic_flight.py @@ -1,3 +1,5 @@ +"""Defines the StochasticFlight class.""" + from rocketpy.simulation import Flight from .stochastic_model import StochasticModel @@ -95,7 +97,7 @@ def _validate_initial_solution(self, initial_solution): "e3_init, w1Init, w2Init, w3Init" ) assert all( - [isinstance(i, (int, float)) for i in initial_solution] + isinstance(i, (int, float)) for i in initial_solution ), "`initial_solution` must be a tuple of numbers" else: raise TypeError("`initial_solution` must be a tuple of numbers") diff --git a/rocketpy/stochastic/stochastic_generic_motor.py b/rocketpy/stochastic/stochastic_generic_motor.py index e0b2cfcb9..e4220e68a 100644 --- a/rocketpy/stochastic/stochastic_generic_motor.py +++ b/rocketpy/stochastic/stochastic_generic_motor.py @@ -1,3 +1,5 @@ +"""Defines the StochasticGenericMotor class.""" + from rocketpy.motors import GenericMotor from .stochastic_motor_model import StochasticMotorModel diff --git a/rocketpy/stochastic/stochastic_model.py b/rocketpy/stochastic/stochastic_model.py index 7085156d6..ec2f64709 100644 --- a/rocketpy/stochastic/stochastic_model.py +++ b/rocketpy/stochastic/stochastic_model.py @@ -1,3 +1,6 @@ +"""Defines the StochasticModel class, which will be used as a base class for all +other Stochastic classes.""" + from random import choice import numpy as np @@ -80,7 +83,10 @@ def __str__(self): continue # Skip attributes starting with underscore if isinstance(value, tuple): # Format the tuple as a string with the mean and standard deviation. - value_str = f"{value[0]:.5f} ± {value[1]:.5f} (numpy.random.{value[2].__name__})" + value_str = ( + f"{value[0]:.5f} ± {value[1]:.5f} " + f"(numpy.random.{value[2].__name__})" + ) else: # Otherwise, just use the default string representation of the value. value_str = str(value) @@ -359,16 +365,17 @@ def _validate_tuple_factor(self, input_name, factor_tuple): 2, 3, ], f"'{input_name}`: Factors tuple must have length 2 or 3" - assert all( - isinstance(item, (int, float)) for item in factor_tuple[:2] - ), f"'{input_name}`: First and second items of Factors tuple must be either an int or float" + assert all(isinstance(item, (int, float)) for item in factor_tuple[:2]), ( + f"'{input_name}`: First and second items of Factors tuple must be " + "either an int or float" + ) if len(factor_tuple) == 2: return (factor_tuple[0], factor_tuple[1], get_distribution("normal")) elif len(factor_tuple) == 3: assert isinstance(factor_tuple[2], str), ( - f"'{input_name}`: Third item of tuple must be a string containing the name " - "of a valid numpy.random distribution function" + f"'{input_name}`: Third item of tuple must be a string containing " + "the name of a valid numpy.random distribution function" ) dist_func = get_distribution(factor_tuple[2]) return (factor_tuple[0], factor_tuple[1], dist_func) diff --git a/rocketpy/stochastic/stochastic_motor_model.py b/rocketpy/stochastic/stochastic_motor_model.py index 22a5b3bf1..cc7764a15 100644 --- a/rocketpy/stochastic/stochastic_motor_model.py +++ b/rocketpy/stochastic/stochastic_motor_model.py @@ -1,3 +1,5 @@ +"""Defines the StochasticMotorModel class.""" + from .stochastic_model import StochasticModel diff --git a/rocketpy/stochastic/stochastic_parachute.py b/rocketpy/stochastic/stochastic_parachute.py index 7b958e926..03dae2f28 100644 --- a/rocketpy/stochastic/stochastic_parachute.py +++ b/rocketpy/stochastic/stochastic_parachute.py @@ -1,3 +1,5 @@ +"""Defines the StochasticParachute class.""" + from rocketpy.rocket import Parachute from .stochastic_model import StochasticModel diff --git a/rocketpy/stochastic/stochastic_rocket.py b/rocketpy/stochastic/stochastic_rocket.py index 64d029155..f629280d7 100644 --- a/rocketpy/stochastic/stochastic_rocket.py +++ b/rocketpy/stochastic/stochastic_rocket.py @@ -1,3 +1,5 @@ +"""Defines the StochasticRocket class.""" + import warnings from random import choice @@ -50,8 +52,8 @@ class is used to receive a Rocket object and information about the rail_buttons : Components A Components instance containing all the rail buttons of the rocket. parachutes : list of StochasticParachute - A list of StochasticParachute instances containing all the parachutes of the - rocket. + A list of StochasticParachute instances containing all the parachutes of + the rocket. radius : tuple, list, int, float The radius of the rocket. Follows the standard input format of Stochastic Models. @@ -195,10 +197,13 @@ def __str__(self): continue # Skip attributes starting with underscore if isinstance(value, tuple): # Format the tuple as a string with the mean and standard deviation. - value_str = f"{value[0]:.5f} ± {value[1]:.5f} (numpy.random.{value[2].__name__})" + value_str = ( + f"{value[0]:.5f} ± {value[1]:.5f} " + f"(numpy.random.{value[2].__name__})" + ) s += f"{key}: {value_str}\n" elif isinstance(value, Components): - # Format the components as a string with the mean and standard deviation. + # Format Components as string with the mean and std deviation s += f"{key}:\n" if len(value) == 0: s += "\tNone\n" @@ -248,7 +253,7 @@ def add_motor(self, motor, position=None): raise TypeError("`motor` must be a StochasticMotor or Motor type") if isinstance(motor, Motor): # create StochasticMotor - # TODO check motor type when hybrids and liquids are implemented + # TODO implement HybridMotor and LiquidMotor stochastic models if isinstance(motor, SolidMotor): motor = StochasticSolidMotor(solid_motor=motor) elif isinstance(motor, GenericMotor): diff --git a/rocketpy/stochastic/stochastic_solid_motor.py b/rocketpy/stochastic/stochastic_solid_motor.py index b250431d3..482973840 100644 --- a/rocketpy/stochastic/stochastic_solid_motor.py +++ b/rocketpy/stochastic/stochastic_solid_motor.py @@ -1,3 +1,5 @@ +"""Defines the StochasticSolidMotor class.""" + from rocketpy.motors import SolidMotor from .stochastic_motor_model import StochasticMotorModel