Skip to content

Commit 3bd689c

Browse files
committed
STY: Reformat with black
1 parent 8ff0978 commit 3bd689c

3 files changed

Lines changed: 82 additions & 47 deletions

File tree

rocketpy/plots/monte_carlo_plots.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import numpy as np
77
from matplotlib.transforms import offset_copy
88

9-
from ..tools import generate_monte_carlo_ellipses, import_optional_dependency, inverted_haversine, haversine
9+
from ..tools import (
10+
generate_monte_carlo_ellipses,
11+
import_optional_dependency,
12+
inverted_haversine,
13+
haversine,
14+
)
1015
from .plot_helpers import show_or_save_plot
1116

1217

@@ -50,7 +55,7 @@ def _get_background_map(self, background, xlim, ylim):
5055
"contextily library is required for automatic map background. "
5156
"Install it via 'pip install contextily' or 'pip install rocketpy[monte-carlo]'. "
5257
"Plotting without background.",
53-
UserWarning
58+
UserWarning,
5459
)
5560
return None, None
5661

@@ -113,11 +118,18 @@ def _get_background_map(self, background, xlim, ylim):
113118
dist = (x**2 + y**2) ** 0.5
114119
# Calculate bearing: 0 is North (Y), 90 is East (X)
115120
bearing = np.degrees(np.arctan2(x, y))
116-
lat, lon = inverted_haversine(origin_lat, origin_lon, dist, bearing, earth_radius)
121+
lat, lon = inverted_haversine(
122+
origin_lat, origin_lon, dist, bearing, earth_radius
123+
)
117124
req_lats.append(lat)
118125
req_lons.append(lon)
119126

120-
west, south, east, north = min(req_lons), min(req_lats), max(req_lons), max(req_lats)
127+
west, south, east, north = (
128+
min(req_lons),
129+
min(req_lats),
130+
max(req_lons),
131+
max(req_lats),
132+
)
121133

122134
bg, mercator_extent = contextily.bounds2img(
123135
west, south, east, north, source=source_provider, ll=True
@@ -127,32 +139,52 @@ def _get_background_map(self, background, xlim, ylim):
127139
def mercator_to_wgs84(x, y):
128140
r_major = 6378137.0
129141
lon = x / r_major * 180.0 / math.pi
130-
lat = (2 * math.atan(math.exp(y / r_major)) - math.pi / 2.0) * 180.0 / math.pi
142+
lat = (
143+
(2 * math.atan(math.exp(y / r_major)) - math.pi / 2.0)
144+
* 180.0
145+
/ math.pi
146+
)
131147
return lat, lon
132148

133149
# Convert corners of the fetched image
134-
bg_lat_min, bg_lon_min = mercator_to_wgs84(mercator_extent[0], mercator_extent[2]) # Bottom-Left
135-
bg_lat_max, bg_lon_max = mercator_to_wgs84(mercator_extent[1], mercator_extent[3]) # Top-Right
150+
bg_lat_min, bg_lon_min = mercator_to_wgs84(
151+
mercator_extent[0], mercator_extent[2]
152+
) # Bottom-Left
153+
bg_lat_max, bg_lon_max = mercator_to_wgs84(
154+
mercator_extent[1], mercator_extent[3]
155+
) # Top-Right
136156

137157
# Calculate X/Y meters relative to origin (lat0, lon0) using haversine
138158
# X = Distance along longitude (East-West)
139159
# Y = Distance along latitude (North-South)
140160

141161
# Calculate X min (Left)
142-
x_min = haversine(origin_lat, origin_lon, origin_lat, bg_lon_min, earth_radius)
143-
if bg_lon_min < origin_lon: x_min = -x_min
162+
x_min = haversine(
163+
origin_lat, origin_lon, origin_lat, bg_lon_min, earth_radius
164+
)
165+
if bg_lon_min < origin_lon:
166+
x_min = -x_min
144167

145168
# Calculate X max (Right)
146-
x_max = haversine(origin_lat, origin_lon, origin_lat, bg_lon_max, earth_radius)
147-
if bg_lon_max < origin_lon: x_max = -x_max
169+
x_max = haversine(
170+
origin_lat, origin_lon, origin_lat, bg_lon_max, earth_radius
171+
)
172+
if bg_lon_max < origin_lon:
173+
x_max = -x_max
148174

149175
# Calculate Y min (Bottom)
150-
y_min = haversine(origin_lat, origin_lon, bg_lat_min, origin_lon, earth_radius)
151-
if bg_lat_min < origin_lat: y_min = -y_min
176+
y_min = haversine(
177+
origin_lat, origin_lon, bg_lat_min, origin_lon, earth_radius
178+
)
179+
if bg_lat_min < origin_lat:
180+
y_min = -y_min
152181

153182
# Calculate Y max (Top)
154-
y_max = haversine(origin_lat, origin_lon, bg_lat_max, origin_lon, earth_radius)
155-
if bg_lat_max < origin_lat: y_max = -y_max
183+
y_max = haversine(
184+
origin_lat, origin_lon, bg_lat_max, origin_lon, earth_radius
185+
)
186+
if bg_lat_max < origin_lat:
187+
y_max = -y_max
156188

157189
return bg, [x_min, x_max, y_min, y_max]
158190

@@ -216,7 +248,7 @@ def ellipses(
216248
"The image file was not found. Please check the path."
217249
) from e
218250

219-
bg ,local_extent = None, None
251+
bg, local_extent = None, None
220252
if image is None and background is not None:
221253
bg, local_extent = self._get_background_map(background, xlim, ylim)
222254

@@ -311,19 +343,14 @@ def ellipses(
311343
)
312344

313345
elif bg is not None and local_extent is not None:
314-
plt.imshow(
315-
bg,
316-
extent=local_extent,
317-
zorder=0,
318-
interpolation="bilinear"
319-
)
346+
plt.imshow(bg, extent=local_extent, zorder=0, interpolation="bilinear")
320347

321348
plt.axhline(0, color="black", linewidth=0.5)
322349
plt.axvline(0, color="black", linewidth=0.5)
323350
plt.xlim(*xlim)
324351
plt.ylim(*ylim)
325352
# Set equal aspect ratio to ensure consistent display regardless of background
326-
ax.set_aspect('equal')
353+
ax.set_aspect("equal")
327354

328355
if save:
329356
plt.savefig(
@@ -511,8 +538,7 @@ def ellipses_comparison(
511538
"The image file was not found. Please check the path."
512539
) from e
513540

514-
515-
bg ,local_extent = None, None
541+
bg, local_extent = None, None
516542
if image is None and background is not None:
517543
bg, local_extent = self._get_background_map(background, xlim, ylim)
518544

@@ -637,18 +663,13 @@ def ellipses_comparison(
637663
],
638664
)
639665
elif bg is not None and local_extent is not None:
640-
plt.imshow(
641-
bg,
642-
extent=local_extent,
643-
zorder=0,
644-
interpolation="bilinear"
645-
)
666+
plt.imshow(bg, extent=local_extent, zorder=0, interpolation="bilinear")
646667

647668
plt.axhline(0, color="black", linewidth=0.5)
648669
plt.axvline(0, color="black", linewidth=0.5)
649670
plt.xlim(*xlim)
650671
plt.ylim(*ylim)
651-
plt.aspect('equal')
672+
plt.aspect("equal")
652673

653674
if save:
654675
plt.savefig(

tests/integration/simulation/test_monte_carlo_plots_background_kennedy.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
def create_kennedy_environment():
2020
"""Create an Environment object for Kennedy Space Center.
21-
21+
2222
Kennedy Space Center coordinates:
2323
- Latitude: 28.5721° N
2424
- Longitude: -80.6480° W
2525
- Elevation: ~3 meters
26-
26+
2727
Returns
2828
-------
2929
Environment
@@ -44,7 +44,7 @@ def create_kennedy_environment():
4444

4545
def create_simulated_monte_carlo_data():
4646
"""Create a MonteCarlo object with simulated results data.
47-
47+
4848
Returns
4949
-------
5050
MonteCarlo
@@ -62,7 +62,6 @@ def create_simulated_monte_carlo_data():
6262
longitude=None,
6363
)
6464

65-
6665
from unittest.mock import MagicMock
6766

6867
stochastic_rocket = MagicMock()
@@ -100,7 +99,7 @@ def create_simulated_monte_carlo_data():
10099

101100
def test_all_background_options():
102101
"""Test all background map options and save images.
103-
102+
104103
This function tests:
105104
- None (no background)
106105
- satellite
@@ -157,4 +156,4 @@ def test_all_background_options():
157156

158157

159158
if __name__ == "__main__":
160-
test_all_background_options()
159+
test_all_background_options()

tests/unit/simulation/test_monte_carlo_plots_background.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ def test_ellipses_image_takes_precedence_over_background(
138138
# Test that when both image and background are provided, image takes precedence
139139
# This should not attempt to download background map
140140
import numpy as np
141+
141142
mock_image = np.zeros((100, 100, 3), dtype=np.uint8) # RGB image
142-
143+
143144
with patch("imageio.imread") as mock_imread:
144145
mock_imread.return_value = mock_image
145146
result = monte_carlo_calisto_pre_loaded.plots.ellipses(
@@ -171,7 +172,9 @@ def __init__(self):
171172
mock_monte_carlo = MockMonteCarlo()
172173
plots = _MonteCarloPlots(mock_monte_carlo)
173174

174-
with pytest.raises(ValueError, match="MonteCarlo object must have an 'environment' attribute"):
175+
with pytest.raises(
176+
ValueError, match="MonteCarlo object must have an 'environment' attribute"
177+
):
175178
plots.ellipses(background="satellite")
176179

177180

@@ -191,12 +194,16 @@ def test_ellipses_background_no_latitude_longitude():
191194

192195
plots = _MonteCarloPlots(mock_monte_carlo)
193196

194-
with pytest.raises(ValueError, match="Environment must have 'latitude' and 'longitude' attributes"):
197+
with pytest.raises(
198+
ValueError, match="Environment must have 'latitude' and 'longitude' attributes"
199+
):
195200
plots.ellipses(background="satellite")
196201

197202

198203
@patch("matplotlib.pyplot.show")
199-
def test_ellipses_background_contextily_not_installed(mock_show, monte_carlo_calisto_pre_loaded):
204+
def test_ellipses_background_contextily_not_installed(
205+
mock_show, monte_carlo_calisto_pre_loaded
206+
):
200207
"""Test that a warning is issued when contextily is not installed.
201208
202209
Parameters
@@ -215,19 +222,28 @@ def mock_import_optional_dependency(name):
215222
raise ImportError("No module named 'contextily'")
216223
return original_import(name)
217224

218-
with patch("rocketpy.plots.monte_carlo_plots.import_optional_dependency", side_effect=mock_import_optional_dependency):
225+
with patch(
226+
"rocketpy.plots.monte_carlo_plots.import_optional_dependency",
227+
side_effect=mock_import_optional_dependency,
228+
):
219229
with warnings.catch_warnings(record=True) as w:
220230
warnings.simplefilter("always")
221-
result = monte_carlo_calisto_pre_loaded.plots.ellipses(background="satellite")
231+
result = monte_carlo_calisto_pre_loaded.plots.ellipses(
232+
background="satellite"
233+
)
222234
assert result is None
223235
assert len(w) > 0
224-
assert any("contextily" in str(warning.message).lower() for warning in w)
236+
assert any(
237+
"contextily" in str(warning.message).lower() for warning in w
238+
)
225239
finally:
226240
_post_test_file_cleanup()
227241

228242

229243
@patch("matplotlib.pyplot.show")
230-
def test_ellipses_background_with_custom_xlim_ylim(mock_show, monte_carlo_calisto_pre_loaded):
244+
def test_ellipses_background_with_custom_xlim_ylim(
245+
mock_show, monte_carlo_calisto_pre_loaded
246+
):
231247
"""Test using background with custom xlim and ylim.
232248
233249
Parameters
@@ -270,4 +286,3 @@ def test_ellipses_background_save(mock_show, monte_carlo_calisto_pre_loaded):
270286
assert os.path.exists("monte_carlo_test.png")
271287
finally:
272288
_post_test_file_cleanup()
273-

0 commit comments

Comments
 (0)