Skip to content

Commit 52eab5c

Browse files
EvanMadEvan Madurai
andauthored
ENH: Display more information in MonteCarlo prints and plots (#760)
* init: new prints test * move new prints into mc class itself * enh: boxplots added to histograms in montecarlo plots * DOC: update changelog * DOC: fix changelog issues * ENH: cleaner calculation of result parameters * enh: cleanup monte-carlo plots * STY: linting fixes and re-format * STL: remove extra files * BUG: fix compatability with matplotlib==3.5 done by removing height_ratios and replacing with gridspec --------- Co-authored-by: Evan Madurai <[email protected]>
1 parent cbbdb65 commit 52eab5c

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Attention: The newest changes should be on top -->
4343

4444

4545
### Changed
46-
46+
- ENH: Display more information in MonteCarlo prints and plots [#760](https://github.com/RocketPy-Team/RocketPy/pull/760)
4747
- MNT: move piecewise functions to separate file [#746](https://github.com/RocketPy-Team/RocketPy/pull/746)
4848
- DOC: flight comparison improvements [#755](https://github.com/RocketPy-Team/RocketPy/pull/755)
4949

rocketpy/plots/monte_carlo_plots.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,25 @@ def all(self, keys=None):
173173
)
174174
else:
175175
raise ValueError("The 'keys' argument must be a string, list, or tuple.")
176-
177176
for key in keys:
178-
plt.figure()
179-
plt.hist(self.monte_carlo.results[key])
180-
plt.title(f"Histogram of {key}")
181-
plt.ylabel("Number of Occurrences")
177+
# Create figure with GridSpec
178+
fig = plt.figure(figsize=(8, 8))
179+
gs = fig.add_gridspec(2, 1, height_ratios=[1, 3])
180+
181+
# Create subplots using gridspec
182+
ax1 = fig.add_subplot(gs[0])
183+
ax2 = fig.add_subplot(gs[1])
184+
185+
# Plot boxplot
186+
ax1.boxplot(self.monte_carlo.results[key], vert=False)
187+
ax1.set_title(f"Box Plot of {key}")
188+
ax1.set_yticks([])
189+
190+
# Plot histogram
191+
ax2.hist(self.monte_carlo.results[key])
192+
ax2.set_title(f"Histogram of {key}")
193+
ax2.set_ylabel("Number of Occurrences")
194+
ax1.set_xticks([])
195+
196+
plt.tight_layout()
182197
plt.show()

rocketpy/prints/monte_carlo_prints.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ def all(self):
2121
print("Data Source: ", self.monte_carlo.filename)
2222
print("Number of simulations: ", self.monte_carlo.num_of_loaded_sims)
2323
print("Results: \n")
24-
print(f"{'Parameter':>25} {'Mean':>15} {'Std. Dev.':>15}")
25-
print("-" * 60)
24+
print(
25+
f"{'Parameter':>25} {'Mean':>15} {'Median':>15} {'Std. Dev.':>15} {'95% PI Lower':>15} {'95% PI Upper':>15}"
26+
)
27+
print("-" * 110)
2628
for key, value in self.monte_carlo.processed_results.items():
2729
try:
28-
print(f"{key:>25} {value[0]:>15.3f} {value[1]:>15.3f}")
30+
print(
31+
f"{key:>25} {value[0]:>15.3f} {value[1]:>15.3f} {value[2]:>15.3f} {value[3]:>15.3f} {value[4]:>15.3f}"
32+
)
2933
except TypeError:
30-
print(f"{key:>25} {str(value[0]):>15} {str(value[1]):>15}")
34+
print(
35+
f"{key:>25} {'N/A':>15} {'N/A':>15} {'N/A':>15} {'N/A':>15} {'N/A':>15}"
36+
)

rocketpy/simulation/monte_carlo.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,16 @@ def set_processed_results(self):
749749
mean = np.mean(values)
750750
stdev = np.std(values)
751751
self.processed_results[result] = (mean, stdev)
752+
pi_low = np.quantile(values, 0.025)
753+
pi_high = np.quantile(values, 0.975)
754+
median = np.median(values)
752755
except TypeError:
753-
self.processed_results[result] = (None, None)
756+
mean = None
757+
stdev = None
758+
pi_low = None
759+
pi_high = None
760+
median = None
761+
self.processed_results[result] = (mean, median, stdev, pi_low, pi_high)
754762

755763
# Import methods
756764

0 commit comments

Comments
 (0)