Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Display more information in MonteCarlo prints and plots #760

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Attention: The newest changes should be on top -->

- REL: update version to 1.7.1 in configuration files [#750](https://github.com/RocketPy-Team/RocketPy/pull/750)
- MNT: Refactor Tank's testing Assertion with CAD data. [#678](https://github.com/RocketPy-Team/RocketPy/pull/678)
ENH: Display more information in MonteCarlo prints and plots [#760](https://github.com/RocketPy-Team/RocketPy/pull/760)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor, but new lines should be add to the top, not the bottom of sections. This is common confusion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, it's also in the wrong section. fixed now


### Fixed

Expand Down
18 changes: 13 additions & 5 deletions rocketpy/plots/monte_carlo_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,18 @@ def all(self, keys=None):
)
else:
raise ValueError("The 'keys' argument must be a string, list, or tuple.")

for key in keys:
plt.figure()
plt.hist(self.monte_carlo.results[key])
plt.title(f"Histogram of {key}")
plt.ylabel("Number of Occurrences")
fig, (ax1, ax2) = plt.subplots(2, 1, height_ratios=[1, 3], figsize=(8, 8))

# Plot boxplot
ax1.boxplot(self.monte_carlo.results[key], vert=False)
ax1.set_xlabel(key)
ax1.set_title(f"Box Plot of {key}")

# Plot histogram
ax2.hist(self.monte_carlo.results[key])
ax2.set_title(f"Histogram of {key}")
ax2.set_ylabel("Number of Occurrences")

plt.tight_layout()
plt.show()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lucas-Prates should we really plot the 3 plot altogether? Perhaps letting the user activate/deactivate some of them if needed?

Copy link
Contributor

@Lucas-Prates Lucas-Prates Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could provide the option to activate/deactivate, but I see no harm in plotting these together. I mean, we get more information and the plot is not convoluted, looks fine to me. Moreover, I only see two plots (histogram + boxplot).

17 changes: 13 additions & 4 deletions rocketpy/prints/monte_carlo_prints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import numpy as np


class _MonteCarloPrints:
"""Class to print the monte carlo analysis results."""

Expand All @@ -21,10 +24,16 @@ def all(self):
print("Data Source: ", self.monte_carlo.filename)
print("Number of simulations: ", self.monte_carlo.num_of_loaded_sims)
print("Results: \n")
print(f"{'Parameter':>25} {'Mean':>15} {'Std. Dev.':>15}")
print("-" * 60)
print(
f"{'Parameter':>25} {'Mean':>15} {'Median':>15} {'Std. Dev.':>15} {'95% PI Lower':>15} {'95% PI Upper':>15}"
)
print("-" * 110)
for key, value in self.monte_carlo.processed_results.items():
try:
print(f"{key:>25} {value[0]:>15.3f} {value[1]:>15.3f}")
print(
f"{key:>25} {value[0]:>15.3f} {value[1]:>15.3f} {value[2]:>15.3f} {value[3]:>15.3f} {value[4]:>15.3f}"
)
except TypeError:
print(f"{key:>25} {str(value[0]):>15} {str(value[1]):>15}")
print(
f"{key:>25} {str(value[0]):>15} {str(value[1]):>15} {'N/A':>15} {'N/A':>15}"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should make everything print "N/A". Moreover, it seems to be missing one more value so that they align correctly with the other rows, as shown in the last row for the image below.

image

Copy link
Author

@EvanMad EvanMad Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this, thanks, fixed it now :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this very useful review btw, I have fixed the aesthetic issues aswell as the linting CI failure.

I can see the issue with matplot lib, I am using the height_ratios property which was introduced in matplotlib 3.6, I haven't fixed this yet, will look into it in the next couple of days

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the issues on matplotlib==3.5! Let me know what else needs to be done to get this merged.

Do i need to worry about code coverage?

7 changes: 5 additions & 2 deletions rocketpy/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,12 @@ def set_processed_results(self):
try:
mean = np.mean(values)
stdev = np.std(values)
self.processed_results[result] = (mean, stdev)
pi_low = np.quantile(values, 0.025)
pi_high = np.quantile(values, 0.975)
median = np.median(values)
self.processed_results[result] = (mean, median, stdev, pi_low, pi_high)
except TypeError:
self.processed_results[result] = (None, None)
self.processed_results[result] = (None, None, None, None, None)

# Import methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try:
    mean = np.mean(values)
    stdev = np.std(values)
    self.processed_results[result] = (mean, stdev)
    pi_low = np.quantile(values, 0.025)
    pi_high = np.quantile(values, 0.975)
    median = np.median(values)
except TypeError:
    mean = None
    stdev = None
    pi_low = None
    pi_high = None
    median = None

 self.processed_results[result] = (mean, median, stdev, pi_low, pi_high)

Alternatively, we could make 5 try/except blocks instead of one single.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah sorry i missed that, I have committed this suggestion

I don't think 5 try/except blocks is necessary. A situation where only one of these calculations fails seems unlikely to me. ie: either they all fail or none fail so catching them all in the same makes sense.


Expand Down