Skip to content

Commit

Permalink
MNT: minor fixes
Browse files Browse the repository at this point in the history
- Add module docstrings
- Fix one camelCase variable
- Fix too long lines
- Adjust one unpacking tuble
  • Loading branch information
Gui-FernandesBR committed Mar 13, 2024
1 parent 17d91d0 commit 0d783df
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 31 deletions.
12 changes: 6 additions & 6 deletions rocketpy/plots/monte_carlo_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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).
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions rocketpy/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the MonteCarlo class."""

import json
from time import process_time, time

Expand All @@ -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


Expand Down Expand Up @@ -669,10 +667,7 @@ def export_ellipses_to_kml(
(
impact_ellipses,
apogee_ellipses,
_,
_,
_,
_,
*_,
) = generate_monte_carlo_ellipses(self.results)
outputs = []

Expand Down
4 changes: 4 additions & 0 deletions rocketpy/stochastic/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions rocketpy/stochastic/stochastic_aero_surfaces.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Defines the StochasticNoseCone, StochasticTrapezoidalFins,
StochasticEllipticalFins, StochasticTail and StochasticRailButtons classes."""

from rocketpy.rocket.aero_surface import (
EllipticalFins,
NoseCone,
Expand Down
15 changes: 11 additions & 4 deletions rocketpy/stochastic/stochastic_environment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticEnvironment class."""

from .stochastic_model import StochasticModel


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion rocketpy/stochastic/stochastic_flight.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticFlight class."""

from rocketpy.simulation import Flight

from .stochastic_model import StochasticModel
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions rocketpy/stochastic/stochastic_generic_motor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticGenericMotor class."""

from rocketpy.motors import GenericMotor

from .stochastic_motor_model import StochasticMotorModel
Expand Down
19 changes: 13 additions & 6 deletions rocketpy/stochastic/stochastic_model.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions rocketpy/stochastic/stochastic_motor_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticMotorModel class."""

from .stochastic_model import StochasticModel


Expand Down
2 changes: 2 additions & 0 deletions rocketpy/stochastic/stochastic_parachute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticParachute class."""

from rocketpy.rocket import Parachute

from .stochastic_model import StochasticModel
Expand Down
15 changes: 10 additions & 5 deletions rocketpy/stochastic/stochastic_rocket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticRocket class."""

import warnings
from random import choice

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions rocketpy/stochastic/stochastic_solid_motor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Defines the StochasticSolidMotor class."""

from rocketpy.motors import SolidMotor

from .stochastic_motor_model import StochasticMotorModel
Expand Down

0 comments on commit 0d783df

Please sign in to comment.