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

Recreate structure fresh during get_primitive_cell #144

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

pmrv
Copy link
Contributor

@pmrv pmrv commented Feb 14, 2024

This avoids a problem, when you pass standardize=True and the resulting cell would be larger than the original.

Summary by CodeRabbit

  • New Features

    • Added a method for obtaining irreducible reciprocal meshes in symmetry analysis.
  • Bug Fixes

    • Improved handling of custom arrays and periodic structures during symmetry operations.
  • Tests

    • Updated tests to include periodic boundary conditions in symmetry-related calculations.

This avoids a problem, when you pass standardize=True and the resulting cell would be larger than the original.
@pmrv pmrv added bug Something isn't working enhancement New feature or request labels Feb 14, 2024
@pmrv pmrv requested a review from samwaseda February 14, 2024 17:01
@jan-janssen
Copy link
Member

======================================================================
ERROR: test_get_primitive_cell (test_symmetry.TestAtoms)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/structuretoolkit/structuretoolkit/tests/test_symmetry.py", line 149, in test_get_primitive_cell
    self.assertEqual(len(basis), len(sym.get_primitive_cell(standardize=True)))
  File "/home/runner/work/structuretoolkit/structuretoolkit/structuretoolkit/analyse/symmetry.py", line 349, in get_primitive_cell
    raise ValueError("Can only symmetrize periodic structures.")
ValueError: Can only symmetrize periodic structures.

======================================================================
ERROR: test_get_primitive_cell_hex (test_symmetry.TestAtoms)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/structuretoolkit/structuretoolkit/tests/test_symmetry.py", line 175, in test_get_primitive_cell_hex
    structure_prim_base = sym.get_primitive_cell()
  File "/home/runner/work/structuretoolkit/structuretoolkit/structuretoolkit/analyse/symmetry.py", line 349, in get_primitive_cell
    raise ValueError("Can only symmetrize periodic structures.")
ValueError: Can only symmetrize periodic structures.

----------------------------------------------------------------------

I mark this pull request as draft until the tests pass.

@jan-janssen jan-janssen marked this pull request as draft February 14, 2024 17:14
@pmrv pmrv marked this pull request as ready for review February 14, 2024 17:26
@pmrv
Copy link
Contributor Author

pmrv commented Feb 14, 2024

I made tests pass by passing pbc=True, I think this was intended anyway.

The trouble is that when spglib returns the symmetrized cell, it may permute, add or remove atoms
so that we cannot tell anymore which values from the original arrays we would need to copy or remove.
@pmrv pmrv requested a review from samwaseda February 15, 2024 13:41
# Conflicts:
#	structuretoolkit/analyse/symmetry.py
@jan-janssen jan-janssen self-requested a review as a code owner July 18, 2024 06:34
Copy link

coderabbitai bot commented Jul 18, 2024

Walkthrough

The recent changes to the structuretoolkit enhance the Symmetry class, specifically the get_primitive_cell method, by improving the handling of custom arrays and periodic boundary conditions. It also introduces an additional method for obtaining irreducible reciprocal meshes. Corresponding unit tests have been updated to reflect these modifications and ensure proper handling of periodic boundary conditions.

Changes

Files / Grouped Files Change Summary
structuretoolkit/analyse/symmetry.py Enhanced get_primitive_cell with custom array warnings, periodic structure checks; added get_ir_reciprocal_mesh method.
tests/test_symmetry.py Updated Atoms constructors in test_get_primitive_cell and test_get_primitive_cell_hex to include pbc=True.

Poem

In the realm of code so fine,
Symmetry gets a new design.
Custom arrays, now beware,
For your copies may not be there.
Periodic checks enhance the flow,
New meshes in reciprocal glow.
A toolkit's grace, for all to see,
In structures' dance, symmetry.


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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

CodeRabbit Configuration 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.

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: 1

Outside diff range, codebase verification and nitpick comments (1)
structuretoolkit/analyse/symmetry.py (1)

388-390: Add a period at the end of the warning message.

The warning message should end with a period for consistency.

-            Custom arrays defined in the base structures
-            :attr:`ase.atoms.Atoms.arrays` are not copied to the new structure!
+            Custom arrays defined in the base structures
+            :attr:`ase.atoms.Atoms.arrays` are not copied to the new structure!.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8fc7273 and 8f5fbab.

Files selected for processing (2)
  • structuretoolkit/analyse/symmetry.py (1 hunks)
  • tests/test_symmetry.py (2 hunks)
Additional context used
Ruff
structuretoolkit/analyse/symmetry.py

421-421: Undefined name warning

(F821)

Additional comments not posted (3)
tests/test_symmetry.py (2)

176-178: LGTM! The addition of pbc=True is correct.

The change ensures that the structure is treated as periodic, which is necessary for the get_primitive_cell function to work correctly.


204-204: LGTM! The addition of pbc=True is correct.

The change ensures that the structure is treated as periodic, which is necessary for the get_primitive_cell function to work correctly.

structuretoolkit/analyse/symmetry.py (1)

392-393: Good check for periodic structures.

The check ensures that the function only operates on periodic structures, which is necessary for symmetrization.

Comment on lines +407 to +421
symbols = [indices_dict[i] for i in indices]
arrays = {
k: self._structure.arrays[k]
for k in self._structure.arrays
if k not in ("numbers", "positions")
}
new_structure = type(self._structure)(
symbols=symbols,
scaled_positions=scaled_positions,
cell=cell,
pbc=[True, True, True],
)
keys = set(arrays) - {"numbers", "positions"}
if len(keys) > 0:
warning(f"Custom arrays {keys} do not carry over to new structure!")
Copy link

Choose a reason for hiding this comment

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

Fix the undefined name warning.

The warning function is not defined. Use the warnings.warn function instead.

-        if len(keys) > 0:
-            warning(f"Custom arrays {keys} do not carry over to new structure!")
+        if len(keys) > 0:
+            import warnings
+            warnings.warn(f"Custom arrays {keys} do not carry over to new structure!")
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
symbols = [indices_dict[i] for i in indices]
arrays = {
k: self._structure.arrays[k]
for k in self._structure.arrays
if k not in ("numbers", "positions")
}
new_structure = type(self._structure)(
symbols=symbols,
scaled_positions=scaled_positions,
cell=cell,
pbc=[True, True, True],
)
keys = set(arrays) - {"numbers", "positions"}
if len(keys) > 0:
warning(f"Custom arrays {keys} do not carry over to new structure!")
symbols = [indices_dict[i] for i in indices]
arrays = {
k: self._structure.arrays[k]
for k in self._structure.arrays
if k not in ("numbers", "positions")
}
new_structure = type(self._structure)(
symbols=symbols,
scaled_positions=scaled_positions,
cell=cell,
pbc=[True, True, True],
)
keys = set(arrays) - {"numbers", "positions"}
if len(keys) > 0:
import warnings
warnings.warn(f"Custom arrays {keys} do not carry over to new structure!")
Tools
Ruff

421-421: Undefined name warning

(F821)

@jan-janssen jan-janssen marked this pull request as draft July 18, 2024 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request format_black
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants