Skip to content
New issue

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

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

Already on GitHub? # to your account

Minor fixes to ParticleBeam.plot_distribution #330

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jank324
Copy link
Member

@jank324 jank324 commented Jan 29, 2025

Description

Removes (%) from unitless axes, and adds Seaborn-style passing and returning of figures and axes, allowing for easy overlaying of particle distributions, for example to compare ground truths and reconstructions. In line with that, the contour argument is also replaced with a style argument that one can set to "histogram" or "contour".

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Closes #328 as discussed in the meeting.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code and checked that formatting passes (required).
  • I have have fixed all issues found by flake8 (required).
  • I have ensured that all pytest tests pass (required).
  • I have run pytest on a machine with a CUDA GPU and made sure all tests pass (required).
  • I have checked that the documentation builds (required).

Note: We are using a maximum length of 88 characters per line.

@jank324 jank324 linked an issue Jan 29, 2025 that may be closed by this pull request
@jank324 jank324 self-assigned this Jan 29, 2025
@jank324 jank324 added the enhancement New feature or request label Jan 29, 2025
@jank324
Copy link
Member Author

jank324 commented Jan 29, 2025

Here is an example showing the modified plotting, where px, py and p are unitless, and it is possible to overlay two distributions in a similar way to how Seaborn odes it (though Seaborn does something more complex for full figures, but we will ignore that).

beam = cheetah.ParticleBeam.from_astra("../tests/resources/ACHIP_EA1_2021.1351.001")
other_beam = beam.transformed_to(sigma_x=beam.sigma_x * 2)

fig, axs = beam.plot_distribution(bin_ranges=None, plot_2d_kws={"style": "histogram"})
_, _ = other_beam.plot_distribution(
    bin_ranges=None,
    plot_2d_kws={"style": "contour"},
    plot_1d_kws={"plot_kws": {"color": "red", "linestyle": "--"}},
    axs=axs,
)

Screenshot 2025-01-29 at 11 13 18

@jp-ga, @roussel-ryan What do you think?

@jank324 jank324 marked this pull request as ready for review January 29, 2025 10:47
@jank324
Copy link
Member Author

jank324 commented Feb 27, 2025

@jp-ga ... a little reminder to have a brief look at this

@jank324 jank324 requested a review from Hespe February 28, 2025 08:39
@jank324 jank324 mentioned this pull request Feb 28, 2025
2 tasks
@@ -1227,13 +1215,23 @@ def plot_distribution(
`ParticleBeam.plot_1d_distribution` for plotting 1D histograms.
:param plot_2d_kws: Additional keyword arguments to be passed to
`ParticleBeam.plot_2d_distribution` for plotting 2D histograms.
:param axs: List of Matplotlib axes objects to use for plotting. If set to
`None`, a new figure is created. Must have the shape `(len(dimensions),
len(dimensions))`.
:return: Matplotlib figure object.
Copy link
Member

Choose a reason for hiding this comment

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

The docstring is inconsistent with the new return type.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm a little confused. Was the inconsistency an argument that the function had but which was not listed in the docstring, or the other way around?

Copy link
Member

Choose a reason for hiding this comment

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

The docstring says the function returns a figure object, but the type hint is Tuple[plt.Figure, np.ndarray].

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve plot_distribution
2 participants