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

Add more flexibility to PhononDOSPlotter and PhononBSPlotter #3700

Merged
merged 16 commits into from
Mar 26, 2024

Conversation

ab5424
Copy link
Contributor

@ab5424 ab5424 commented Mar 22, 2024

Summary

Major changes:

  • feature 1: Add invert_axes to PhononDOSPlotter (similar to DOSPlotter)
  • feature 2: Compare to more than one other PhononBSPlotter

Checklist

  • Google format doc strings added. Check with ruff.
  • Type annotations included. Check with mypy.
  • Tests added for new features/fixes.
  • If applicable, new classes/functions/modules have duecredit @due.dcite decorators to reference relevant papers by DOI (example)

Summary by CodeRabbit

  • New Features
    • Added an invert_axes parameter to various plotting methods for axis inversion, enhancing plot customization.
    • Enhanced color handling and customization options for comparing multiple plots.
  • Refactor
    • Renamed a parameter for clarity and adjusted plot dimensions based on new axis inversion feature.
  • Tests
    • Updated plotting tests to reflect changes in plotting methods and configurations, including the use of matplotlib.pyplot.

@@ -590,7 +619,7 @@ def get_ticks(self) -> dict[str, list]:

def plot_compare(
self,
other_plotter: PhononBSPlotter,
other_plotter: PhononBSPlotter | Sequence[PhononBSPlotter],
Copy link
Member

Choose a reason for hiding this comment

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

how about we use dict[str, PhononBSPlotter] here so we can label other band structures? would be great to add a legend mapping colors to dict keys.

see pymatviz for what that might look like
phonon-bands-mp-2758

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is also labels a few lines below, doesn't that do the same? I could refactor though.

Copy link
Member

Choose a reason for hiding this comment

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

yeah i think refactoring would make for a better API here. labels was only added 3 months in 9268942 ago so i think even a breaking change would be acceptable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed labels. I changed the logic so that no legend is shown if the Plotter is initialised without a label (i.e. if the "original" PhononBS has no label).

Copy link
Member

@janosh janosh Mar 25, 2024

Choose a reason for hiding this comment

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

it's important to still support passing in single PhononBSPlotters. made some changes to that effect in
26fb878. also i think if there are multiple band structures, we should always show a legend

could you make sure we test both the dict and single PhononBSPlotter case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a test for the single PhononBSPlottter case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@janosh Is there a best way to add custom colours for the comparison? With PhononDOSPlotter, it is done via add_dos(), but here it is not obvious to me. Is it better to add the colour to the init or passing multiple colours to plot_compare?

Copy link
Member

Choose a reason for hiding this comment

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

definitely plot_compare. usually it's best for arguments to be as close to where they're used as possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a colors variable to plot_compare().

Copy link

coderabbitai bot commented Mar 26, 2024

Walkthrough

The recent update introduces an invert_axes parameter to various plotter methods within pymatgen's phonon module, enhancing flexibility in data visualization by allowing axis inversion. Additionally, it refines the plotter's functionality with clearer variable naming, dimension adjustments based on axis orientation, and improved color customization for comparative plots. The accompanying tests have been revised to reflect these changes, utilizing matplotlib.pyplot for a more streamlined approach to plotting.

Changes

Files Changes
.../phonon/plotter.py Added invert_axes, renamed y to ys, adjusted dimensions and logic for axis inversion, enhanced color handling.
.../phonon/test_plotter.py Updated to use matplotlib.pyplot, modified plotter methods and configurations, added new instances for testing.

🐰✨🎨
In the realm of plots and lines, a change unfolds,
Axes invert, colors bold, a story told.
With invert_axes we twist and turn,
Through tests and tales, we live and learn.
Here's to the plots, now more versatile,
Crafted by code, with a rabbit's smile.
🌟📊🐇

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@janosh janosh added enhancement A new feature or improvement to an existing one data viz PRs and issues about pymatgen plotting functionality phonon Lattice vibrations labels Mar 26, 2024
Copy link
Member

@janosh janosh left a comment

Choose a reason for hiding this comment

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

thanks a lot @ab5424, nice work!

@janosh janosh enabled auto-merge (squash) March 26, 2024 07:41
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b1e5023 and 4ae33eb.
Files selected for processing (2)
  • pymatgen/phonon/plotter.py (10 hunks)
  • tests/phonon/test_plotter.py (4 hunks)
Additional comments: 9
tests/phonon/test_plotter.py (9)
  • 6-6: Consider using a more specific import for matplotlib.pyplot if only a few functions are used, to improve code readability and potentially reduce import overhead.
  • 13-13: Disabling LaTeX rendering in matplotlib globally might affect other tests if they run in the same environment. Consider using a fixture to ensure this setting is only applied to the tests that require it.
  • 20-22: Creating multiple instances of PhononDosPlotter with different configurations in the setUp method is a good practice for testing various scenarios. Ensure that each instance is adequately tested in separate test methods to cover all configurations.
  • 34-34: Using a set comparison to assert the keys in the dictionary is a robust way to ensure that all expected elements are present, regardless of order. Good use of Python's set operations for testing.
  • 41-43: Asserting the type of the returned object and the labels on the axes is a good practice to ensure the plotter's functionality. Consider also adding assertions for the plot data to ensure the correctness of the plotted values.
  • 46-49: Testing the invert_axes feature by asserting the swapped labels is a good approach. Additionally, consider verifying that the axes' limits are correctly inverted to ensure the feature works as expected.
  • 71-74: Asserting the type of the returned object and the labels on the axes for the PhononBSPlotter is consistent with the tests for PhononDosPlotter. This consistency in testing approach is commendable.
  • 89-101: Testing the plot_compare method with different scenarios and asserting the legend labels and colors is thorough. It ensures that the comparison feature works correctly for multiple cases. Consider adding a test to verify the handling of incompatible band structures if not already covered.
  • 111-120: Testing various thermodynamic properties plotting functions in ThermoPlotter and asserting the type of the returned object is a good practice. It would be beneficial to also verify the correctness of the plotted data, possibly by checking specific values at known points.

@janosh janosh disabled auto-merge March 26, 2024 07:49
@janosh
Copy link
Member

janosh commented Mar 26, 2024

@ab5424 actually, maybe these two CodeRabbit comments are worth addressing. would you be able to add these checks to tests/phonon/test_plotter.py

46-49: Testing the invert_axes feature by asserting the swapped labels is a good approach. Additionally, consider verifying that the axes' limits are correctly inverted to ensure the feature works as expected.

89-101: Testing the plot_compare method with different scenarios and asserting the legend labels and colors is thorough. It ensures that the comparison feature works correctly for multiple cases. Consider adding a test to verify the handling of incompatible band structures if not already covered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4ae33eb and 966eefd.
Files selected for processing (2)
  • pymatgen/phonon/plotter.py (10 hunks)
  • tests/phonon/test_plotter.py (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • pymatgen/phonon/plotter.py
  • tests/phonon/test_plotter.py

@ab5424
Copy link
Contributor Author

ab5424 commented Mar 26, 2024

@janosh I changed the code according to the suggestions. Interestingly, I found that I was able to get the NaCl/STO comparison though the checking block because the distances lists had the same length (but not the same shape so there was an IndexError on line 691). I changed it so that the shape must be the same.

@janosh janosh merged commit 77429a6 into materialsproject:master Mar 26, 2024
21 of 22 checks passed
@ab5424 ab5424 deleted the phonon-dos-interted branch March 26, 2024 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data viz PRs and issues about pymatgen plotting functionality enhancement A new feature or improvement to an existing one phonon Lattice vibrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants