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

Fix for writing non-unique site labels in CifWriter #3767

Merged
merged 13 commits into from
Apr 25, 2024

Conversation

stefsmeets
Copy link
Contributor

@stefsmeets stefsmeets commented Apr 18, 2024

This PR addresses an issue where the Structure contains non-unique user-defined labels causing duplicate labels in the cif file. It achieves this by adding a warning when writing such a cif file. There is also a new method that helps circumvent the issue by relabeling sites, suffixing _1, _2, ..., _n for duplicates.

Closes #3761

Summary

Major changes:

  • feature 1: Add SiteCollection.relabel_sites() method that suffixes _$NUM for duplicate labels
  • fix 1: Warn user when writing non-unique atom site labels in CifWriter

Todos

  • Check written labels make sense in tests

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)

Tip: Install pre-commit hooks to auto-check types and linting before every commit:

pip install -U pre-commit
pre-commit install

Copy link

coderabbitai bot commented Apr 18, 2024

Warning

Rate Limit Exceeded

@janosh has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 35 minutes and 34 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between d2257a5 and 3201e04.

Walkthrough

The recent updates in pymatgen involve modifications to the CifWriter to ensure unique atom site labels in CIF files, addressing issues with non-unique and incorrectly rewritten labels. This includes a method update in cif.py for checking and conditionally updating site labels, alongside a new test in test_cif.py to validate these changes.

Changes

File Change Summary
pymatgen/io/cif.py, tests/io/test_cif.py Updated __init__ in cif.py to ensure unique atom_site_label. Added test_cif_writer_non_unique_labels in test_cif.py.
pymatgen/core/structure.py Added relabel_sites method to Structure class for ensuring unique site labels.
tests/core/test_structure.py Added test_relabel_sites to test the relabel_sites function.

Assessment against linked issues

Objective Addressed Explanation
Ensure unique atom_site_label in CIF files (#3761) The addition of relabel_sites method in Structure class ensures unique labels, addressing the issue of non-unique labels in CIF files.
Prevent CifWriter from rewriting unique atom labels (#3772) The update in CifWriter to ensure label uniqueness now prevents the issue of rewriting unique atom labels, aligning with the expected behavior.

Possibly related issues


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 testing code 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 testing code 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 testing code.
    • @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.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@stefsmeets
Copy link
Contributor Author

This PR is ready. I'm a bit puzzled why the test fails...:

E       FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/pymatgen/pymatgen/tests/files/garnet.cif'

tests/io/test_cif.py Outdated Show resolved Hide resolved
@janosh janosh added io Input/output functionality fix Bug fix PRs cif Crystallographic information file labels Apr 22, 2024
Co-authored-by: Janosh Riebesell <janosh.riebesell@gmail.com>
Signed-off-by: Stef Smeets <stefsmeets@users.noreply.github.com>
@fxcoudert
Copy link

Wondering: why make the situation asymmetric? i.e. in the case of your four Ca1 atoms, one will remain Ca1 while the others will become Ca1_1, Ca1_2, …

@janosh
Copy link
Member

janosh commented Apr 22, 2024

i think @fxcoudert has a point. in case of relabeling, let's sequentially suffix all duplicate site labels for consistency.

How about an option to CifWriter to make labels unique.

this also sounds sensible to give the user control over relabeling behavior

@stefsmeets
Copy link
Contributor Author

A matter of taste I presume :-)

Adding an option and making sure the first atom is also labeled may be a bit more involved. I will have a go at tackling #3772 as well in this PR then.

@janosh
Copy link
Member

janosh commented Apr 22, 2024

A matter of taste I presume :-)

very true! hopefully, adding an option can make everyone happy. maybe make it deduplicate_site_labels: Literal["sequential", None] = "sequential" so it's easy to add additional options in the future.

@stefsmeets
Copy link
Contributor Author

maybe make it deduplicate_site_labels: Literal["sequential", None] = "sequential"

In that case I think this would be better as a method on Structure to make it generally available, together with a warning in CifWriter (one of the other suggestions in the issue). The label duplication happens only if the user passed a structure with custom labels and does not specify symprec which avoids this path.

@janosh
Copy link
Member

janosh commented Apr 22, 2024

In that case I think this would be better as a method on Structure to make it generally available

sure

together with a warning in CifWriter (one of the other suggestions in the issue).

definitely. a warning that the labels were suffixed with a link to the spec to explain why this was done would be great!

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.

Actionable comments posted: 4

pymatgen/core/structure.py Outdated Show resolved Hide resolved
pymatgen/core/structure.py Outdated Show resolved Hide resolved
@janosh janosh changed the title Fix for writing non-unique site labels in CifWriter Fix for writing non-unique site labels in CifWriter Apr 25, 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 @stefsmeets, very nice work!

@janosh janosh merged commit f52c921 into materialsproject:master Apr 25, 2024
21 of 22 checks passed
@stefsmeets stefsmeets deleted the cif-non-unique-labels branch April 26, 2024 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cif Crystallographic information file fix Bug fix PRs io Input/output functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

atom_site_label in CIF file are not unique
3 participants