Skip to content

Commit

Permalink
Adds some ruff unsafe-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdsharpe committed Aug 1, 2024
1 parent c627f7a commit 0ca7930
Show file tree
Hide file tree
Showing 27 changed files with 108 additions and 123 deletions.
2 changes: 1 addition & 1 deletion aerosandbox/aerodynamics/aero_2D/airfoil_inviscid.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def draw_streamlines(self, res=200, show=True):
V[contains] = np.nan

speed = (U**2 + V**2) ** 0.5
Cp = 1 - speed**2
# Cp = 1 - speed**2

### Draw the airfoils
for airfoil in self.airfoils:
Expand Down
1 change: 0 additions & 1 deletion aerosandbox/aerodynamics/aero_3D/aero_buildup.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ def run_with_stability_derivatives(
"r": (2 * self.op_point.velocity) / self.airplane.b_ref,
}

original_op_point = self.op_point

# Compute the point analysis, which returns a dictionary that we will later add key:value pairs to.
run_base = self.run()
Expand Down
1 change: 0 additions & 1 deletion aerosandbox/aerodynamics/aero_3D/lifting_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ def run_with_stability_derivatives(
"r": (2 * self.op_point.velocity) / self.airplane.b_ref,
}

original_op_point = self.op_point

# Compute the point analysis, which returns a dictionary that we will later add key:value pairs to.
run_base = self.run()
Expand Down
3 changes: 2 additions & 1 deletion aerosandbox/atmosphere/thermodynamics/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def __init__(
self.effective_collision_diameter = effective_collision_diameter

def __repr__(self) -> str:
f = lambda s, u: eng_string(s, unit=u, format="%.6g")
def f(val, unit):
return eng_string(val, unit=unit, format="%.6g")

return f"Gas (P = {f(self.pressure, 'Pa')}, T = {self.temperature:.6g} K, ρ = {self.density:.6g} kg/m^3, Pv^gamma = {self.pressure * self.specific_volume ** self.ratio_of_specific_heats: .6g})"

Expand Down
2 changes: 1 addition & 1 deletion aerosandbox/geometry/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ def J(self):

A = 0.5 * np.sum(a) # area

# Centroid location
x_c = 1 / (6 * A) * np.sum(a * (x + x_n))
y_c = 1 / (6 * A) * np.sum(a * (y + y_n))
centroid = np.array([x_c, y_c])

Ixx = 1 / 12 * np.sum(a * (y**2 + y * y_n + y_n**2))

Expand Down
2 changes: 1 addition & 1 deletion aerosandbox/geometry/test_geometry/test_airfoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_repanel(naca4412):


def test_containts_points(naca4412):
assert naca4412.contains_points(x=0.5, y=0) == True
assert naca4412.contains_points(x=0.5, y=0)
assert np.all(
naca4412.contains_points(x=np.array([0.5, 0.5]), y=np.array([0, -0.1]))
== np.array([True, False])
Expand Down
16 changes: 8 additions & 8 deletions aerosandbox/geometry/test_geometry/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def test_polygon_creation():
def test_contains_points():
p = Polygon(coordinates=np.array([[0, 0], [1, 0], [1, 1], [0, 1]]))

assert p.contains_points(0.5, 0.5) == True
assert p.contains_points(-0.1, 0.5) == False
assert p.contains_points(0.5, -0.1) == False
assert p.contains_points(-0.1, -0.1) == False
assert p.contains_points(1.1, 1.1) == False
assert p.contains_points(1.0, 1.0) == True
assert p.contains_points(0.5, 1.0) == True
assert p.contains_points(0.5, 1.1) == False
assert p.contains_points(0.5, 0.5)
assert not p.contains_points(-0.1, 0.5)
assert not p.contains_points(0.5, -0.1)
assert not p.contains_points(-0.1, -0.1)
assert not p.contains_points(1.1, 1.1)
assert p.contains_points(1.0, 1.0)
assert p.contains_points(0.5, 1.0)
assert not p.contains_points(0.5, 1.1)

assert np.all(
p.contains_points(
Expand Down
2 changes: 1 addition & 1 deletion aerosandbox/geometry/test_geometry/test_wingxsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def test_init(): # TODO actually test this
xsec = WingXSec(
WingXSec(
xyz_le=np.array([0, 0, 0]),
chord=1.0,
twist=0,
Expand Down
7 changes: 2 additions & 5 deletions aerosandbox/library/aerodynamics/transonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,8 @@ def approximate_CD_wave(

if __name__ == "__main__":
mc = 0.6
drag = lambda mach: approximate_CD_wave(
mach,
mach_crit=mc,
CD_wave_at_fully_supersonic=1,
)
def drag(mach):
return approximate_CD_wave(mach, mach_crit=mc, CD_wave_at_fully_supersonic=1)

import matplotlib.pyplot as plt
import aerosandbox.tools.pretty_plots as p
Expand Down
4 changes: 2 additions & 2 deletions aerosandbox/library/power_nuclear_rtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
def po210_specific_power(
days_after_formation=0,
):
half_life = 138.376 # days
# Source: https://en.wikipedia.org/wiki/Polonium-210
pure_specific_energy = (
(
(po_210_mass_amu - alpha_particle_mass_amu - pb_206_mass_amu)
* c**2
/ po_210_mass_amu
) # J/kg
# TODO finish
raise NotImplementedError()
6 changes: 4 additions & 2 deletions aerosandbox/library/weights/torenbeek_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,17 @@ def mass_fuselage(
# Torenbeek Eq. D-3
fuselage.fineness_ratio()

fuselage_quasi_slenderness_ratio = fuselage.fineness_ratio(
fuselage.fineness_ratio(
assumed_shape="sears_haack"
)

k_lambda = np.softmin(0.56 * fuselage.fineness_ratio(assumed_shape="sears_haack"))

W_sk = 0.05428 * k_lambda * S_g**1.07 * never_exceed_airspeed**0.743

W_g = W_sk + W_str + W_fr
W_sk + W_str + W_fr

raise NotImplementedError()


def mass_propeller(
Expand Down
16 changes: 8 additions & 8 deletions aerosandbox/library/winds.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ def plot_winds_at_altitude(altitude=18000):

levels = np.arange(0, 80.1, 5)
CS = plt.contour(*args, levels=levels, linewidths=0.5, colors="k", alpha=0.7)
CF = plt.contourf(
plt.contourf(
*args, levels=levels, cmap="viridis_r", alpha=0.7, extend="max"
)
cbar = plt.colorbar(label="Wind Speed [m/s]", extendrect=True)
plt.colorbar(label="Wind Speed [m/s]", extendrect=True)
ax.clabel(CS, inline=1, fontsize=9, fmt="%.0f m/s")

plt.xticks(
Expand Down Expand Up @@ -280,10 +280,10 @@ def plot_winds_at_day(day_of_year=0):

levels = np.arange(0, 80.1, 5)
CS = plt.contour(*args, levels=levels, linewidths=0.5, colors="k", alpha=0.7)
CF = plt.contourf(
plt.contourf(
*args, levels=levels, cmap="viridis_r", alpha=0.7, extend="max"
)
cbar = plt.colorbar(label="Wind Speed [m/s]", extendrect=True)
plt.colorbar(label="Wind Speed [m/s]", extendrect=True)
ax.clabel(CS, inline=1, fontsize=9, fmt="%.0f m/s")

lat_label_vals = np.arange(-80, 80.1, 20)
Expand Down Expand Up @@ -316,10 +316,10 @@ def plot_tropopause_altitude():

levels = np.arange(10, 20.1, 1)
CS = plt.contour(*args, levels=levels, linewidths=0.5, colors="k", alpha=0.7)
CF = plt.contourf(
plt.contourf(
*args, levels=levels, cmap="viridis_r", alpha=0.7, extend="both"
)
cbar = plt.colorbar(label="Tropopause Altitude [km]", extendrect=True)
plt.colorbar(label="Tropopause Altitude [km]", extendrect=True)
ax.clabel(CS, inline=1, fontsize=9, fmt="%.0f km")

plt.xticks(
Expand Down Expand Up @@ -373,10 +373,10 @@ def plot_winds_at_tropopause_altitude():

levels = np.arange(0, 80.1, 5)
CS = plt.contour(*args, levels=levels, linewidths=0.5, colors="k", alpha=0.7)
CF = plt.contourf(
plt.contourf(
*args, levels=levels, cmap="viridis_r", alpha=0.7, extend="max"
)
cbar = plt.colorbar(label="Wind Speed [m/s]", extendrect=True)
plt.colorbar(label="Wind Speed [m/s]", extendrect=True)
ax.clabel(CS, inline=1, fontsize=9, fmt="%.0f m/s")

plt.xticks(
Expand Down
6 changes: 2 additions & 4 deletions aerosandbox/modeling/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ def flatten(input):
return np.array(input).flatten()

try:
x_data = {k: flatten(v) for k, v in x_data.items()}
x_data_is_dict = True
x_data = {k: flatten(v) for k, v in x_data.items()} # type: dict
except (
AttributeError
): # If it's not a dict or dict-like, assume it's a 1D ndarray dataset
x_data = flatten(x_data)
x_data_is_dict = False
x_data = flatten(x_data) # type: np.ndarray
y_data = flatten(y_data)
n_datapoints = np.length(y_data)

Expand Down
2 changes: 1 addition & 1 deletion aerosandbox/modeling/surrogate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def axis_range(x_data_axis: np.ndarray) -> Tuple[float, float]:
y_fit = self(x_fit)

### Plot the 2D figure
fig = plt.figure(dpi=200)
plt.figure(dpi=200)
plt.plot(
x_data,
self.y_data,
Expand Down
3 changes: 2 additions & 1 deletion aerosandbox/numpy/surrogate_model_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def blend(
on the value of the 'switch' parameter.
"""
blend_function = lambda x: sigmoid(x, normalization_range=(0, 1))
def blend_function(x):
return sigmoid(x, normalization_range=(0, 1))
weight_to_value_switch_high = blend_function(switch)

blend_value = value_switch_high * weight_to_value_switch_high + value_switch_low * (
Expand Down
41 changes: 20 additions & 21 deletions aerosandbox/numpy/test_numpy/test_determine_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,59 @@


def test_int():
assert is_casadi_type(5, recursive=True) == False
assert is_casadi_type(5, recursive=False) == False
assert not is_casadi_type(5, recursive=True)
assert not is_casadi_type(5, recursive=False)


def test_float():
assert is_casadi_type(5.0, recursive=True) == False
assert is_casadi_type(5.0, recursive=False) == False
assert not is_casadi_type(5.0, recursive=True)
assert not is_casadi_type(5.0, recursive=False)


def test_numpy():
assert is_casadi_type(np.array([1, 2, 3]), recursive=True) == False
assert is_casadi_type(np.array([1, 2, 3]), recursive=False) == False
assert not is_casadi_type(np.array([1, 2, 3]), recursive=True)
assert not is_casadi_type(np.array([1, 2, 3]), recursive=False)


def test_casadi():
assert is_casadi_type(cas.MX(np.ones(5)), recursive=False) == True
assert is_casadi_type(cas.MX(np.ones(5)), recursive=True) == True
assert is_casadi_type(cas.MX(np.ones(5)), recursive=False)
assert is_casadi_type(cas.MX(np.ones(5)), recursive=True)


def test_numpy_list():
assert is_casadi_type([np.array(5), np.array(7)], recursive=False) == False
assert is_casadi_type([np.array(5), np.array(7)], recursive=True) == False
assert not is_casadi_type([np.array(5), np.array(7)], recursive=False)
assert not is_casadi_type([np.array(5), np.array(7)], recursive=True)


def test_casadi_list():
assert (
assert not (
is_casadi_type([cas.MX(np.ones(5)), cas.MX(np.ones(5))], recursive=False)
== False
)
assert (
is_casadi_type([cas.MX(np.ones(5)), cas.MX(np.ones(5))], recursive=True) == True
is_casadi_type([cas.MX(np.ones(5)), cas.MX(np.ones(5))], recursive=True)
)


def test_mixed_list():
assert is_casadi_type([np.array(5), cas.MX(np.ones(5))], recursive=False) == False
assert is_casadi_type([np.array(5), cas.MX(np.ones(5))], recursive=True) == True
assert not is_casadi_type([np.array(5), cas.MX(np.ones(5))], recursive=False)
assert is_casadi_type([np.array(5), cas.MX(np.ones(5))], recursive=True)


def test_multi_level_contaminated_list():
a = [[1 for _ in range(10)] for _ in range(10)]

assert is_casadi_type(a, recursive=False) == False
assert is_casadi_type(a, recursive=True) == False
assert not is_casadi_type(a, recursive=False)
assert not is_casadi_type(a, recursive=True)

a[5][5] = cas.MX(1)

assert is_casadi_type(a, recursive=False) == False
assert is_casadi_type(a, recursive=True) == True
assert not is_casadi_type(a, recursive=False)
assert is_casadi_type(a, recursive=True)

a[5][5] = np.array(cas.DM(1), dtype="O")

assert is_casadi_type(a, recursive=False) == False
assert is_casadi_type(a, recursive=True) == False
assert not is_casadi_type(a, recursive=False)
assert not is_casadi_type(a, recursive=True)


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions aerosandbox/numpy/test_numpy/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ def value_func_3d(x, y, z):

point = np.array([5.21, 3.12, 1.15])
with pytest.raises(ValueError):
value = np.interpn(points, values, point)
np.interpn(points, values, point)

### CasADi test
point = cas.DM(point)
with pytest.raises(ValueError):
value = np.interpn(points, values, point)
np.interpn(points, values, point)


def test_interpn_bounds_error_multiple_samples():
Expand All @@ -125,12 +125,12 @@ def value_func_3d(x, y, z):

point = np.array([[2.21, 3.12, 1.15], [3.42, 5.81, 2.43]])
with pytest.raises(ValueError):
value = np.interpn(points, values, point)
np.interpn(points, values, point)

### CasADi test
point = cas.DM(point)
with pytest.raises(ValueError):
value = np.interpn(points, values, point)
np.interpn(points, values, point)


def test_interpn_fill_value():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_log_transformed_negativity_error():
opti = asb.Opti()

with pytest.raises(ValueError):
myvar = opti.variable(log_transform=True, init_guess=-1)
opti.variable(log_transform=True, init_guess=-1)


def test_fixed_variable():
Expand Down
2 changes: 1 addition & 1 deletion aerosandbox/structures/buckling.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def thin_walled_tube_crippling_buckling_critical_load(
# Following the Air Force Stress Manual, Section 2.3.2.1, Eq. 2-20.
# The above value assumes mu = 0.3, which is a good assumption for most metals.

crippling_stress = 0.3 * (elastic_modulus * wall_thickness / radius)
crippling_stress = crippling_stress_constant * (elastic_modulus * wall_thickness / radius)

tube_xsec_area = 2 * np.pi * radius * wall_thickness

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def estimate_noise_standard_deviation(
##### Noise Variance Reconstruction #####
from scipy.special import gammaln

ln_factorial = lambda x: gammaln(x + 1)
def ln_factorial(x):
return gammaln(x + 1)

### For speed, pre-compute the log-factorial of integers from 1 to estimator_order
# ln_f = ln_factorial(np.arange(estimator_order + 1))
Expand Down Expand Up @@ -166,21 +167,26 @@ def bootstrap_fits(
y_rng = y_max - y_min

if normalize:
x_normalize = lambda x: (x - x_min) / x_rng
y_normalize = lambda y: (y - y_min) / y_rng
def x_normalize(x):
return (x - x_min) / x_rng
def y_normalize(y):
return (y - y_min) / y_rng
# x_unnormalize = lambda x_n: x_n * x_rng + x_min
y_unnormalize = lambda y_n: y_n * y_rng + y_min
def y_unnormalize(y_n):
return y_n * y_rng + y_min

x_stdev_normalized = x_noise_stdev / x_rng
x_noise_stdev / x_rng
y_stdev_normalized = y_noise_stdev / y_rng

else:
x_normalize = lambda x: x
y_normalize = lambda y: y
def x_normalize(x):
return x
def y_normalize(y):
return y
# x_unnormalize = lambda x_n: x_n
y_unnormalize = lambda y_n: y_n
def y_unnormalize(y_n):
return y_n

x_stdev_normalized = x_noise_stdev
y_stdev_normalized = y_noise_stdev

with tqdm(
Expand Down
Loading

0 comments on commit 0ca7930

Please sign in to comment.