-
Notifications
You must be signed in to change notification settings - Fork 135
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
improve ASE traj #633
improve ASE traj #633
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
WalkthroughThe recent changes enhance the functionality of the Changes
Sequence Diagram(s) (Beta)sequenceDiagram
participant User
participant ASEPlugin
participant ASEIO
participant TestSuite
User->>ASEPlugin: Call to_system(data, file_name="confs.traj")
ASEPlugin->>ASEIO: Save data to trajectory file
ASEIO-->>ASEPlugin: Trajectory file saved
ASEPlugin-->>User: Return List[ase.Atoms]
User->>ASEPlugin: Call to_labeled_system(data, file_name="labeled_confs.traj")
ASEPlugin->>ASEIO: Save labeled data to trajectory file
ASEIO-->>ASEPlugin: Labeled trajectory file saved
ASEPlugin-->>User: Return List[ase.Atoms]
TestSuite->>ASEPlugin: Run TestASEtraj4
ASEPlugin-->>TestSuite: Test results
TestSuite->>ASEPlugin: Run TestASEtraj4Labeled
ASEPlugin-->>TestSuite: Test results
Tip Early Access Features
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
for more information, see https://pre-commit.ci
CodSpeed Performance ReportMerging #633 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
dpdata/plugins/ase.py
Outdated
@@ -1,6 +1,6 @@ | |||
from __future__ import annotations | |||
|
|||
from typing import TYPE_CHECKING | |||
from typing import TYPE_CHECKING, Generator, List |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using specific imports instead of TYPE_CHECKING
.
Using specific imports can improve clarity and reduce the overhead of unnecessary imports when they are not needed for type checking.
dpdata/plugins/ase.py
Outdated
@@ -10,6 +10,7 @@ | |||
|
|||
if TYPE_CHECKING: | |||
import ase | |||
from ase.io import Trajectory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move import ase.io.Trajectory
out of type-checking block.
This import is used in the runtime code, not just for type hints. It should be placed outside the if TYPE_CHECKING:
block to ensure it is always imported.
@@ -110,7 +111,7 @@ | |||
step: int | None = None, | |||
ase_fmt: str | None = None, | |||
**kwargs, | |||
) -> ase.Atoms: | |||
) -> Generator[ase.Atoms, None, None]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Generator
from typing
instead of typing.Generator
.
Python's typing module recommends using Generator
directly for type hints. This aligns with modern Python practices and improves readability.
dpdata/plugins/ase.py
Outdated
@@ -140,7 +141,7 @@ | |||
frames = ase.io.read(file_name, format=ase_fmt, index=slice(begin, end, step)) | |||
yield from frames | |||
|
|||
def to_system(self, data, **kwargs): | |||
def to_system(self, data, **kwargs) -> List[ase.Atoms]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update type annotations from List
to list
.
- def to_system(self, data, **kwargs) -> List[ase.Atoms]:
+ def to_system(self, data, **kwargs) -> list[ase.Atoms]:
- def to_labeled_system(self, data, *args, **kwargs) -> List[ase.Atoms]:
+ def to_labeled_system(self, data, *args, **kwargs) -> list[ase.Atoms]:
Using list
instead of List
for type annotations is recommended as of Python 3.9 for better readability and alignment with modern Python practices.
Also applies to: 162-162
Committable suggestion was skipped due low confidence.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
tests/test_ase_traj.py
Outdated
@unittest.skipIf(skip_ase, "skip ase related test. install ase to fix") | ||
class TestASEtraj4(unittest.TestCase, CompSys, IsPBC): | ||
def setUp(self): | ||
system_1 = dpdata.System("ase_traj/MoS2", fmt="deepmd") | ||
system_1.to(file_name="tmp.traj", fmt="ase/traj") | ||
self.system_2 = dpdata.System("tmp.traj", fmt="ase/traj") | ||
self.system_1 = system_1 | ||
self.places = 6 | ||
self.e_places = 6 | ||
self.f_places = 6 | ||
self.v_places = 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test methods to TestASEtraj4
.
This test class sets up systems but does not include any assertions to verify the correctness of operations. Would you like me to help by adding some example test methods?
tests/test_ase_traj.py
Outdated
@unittest.skipIf(skip_ase, "skip ase related test. install ase to fix") | ||
class TestASEtraj4Labeled(unittest.TestCase, CompLabeledSys, IsPBC): | ||
def setUp(self): | ||
system_1 = dpdata.LabeledSystem("ase_traj/MoS2", fmt="deepmd") | ||
system_1.to(file_name="tmp1.traj", fmt="ase/traj") | ||
self.system_2 = dpdata.LabeledSystem("tmp1.traj", fmt="ase/traj") | ||
self.system_1 = system_1 | ||
self.places = 6 | ||
self.e_places = 6 | ||
self.f_places = 6 | ||
self.v_places = 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test methods to TestASEtraj4Labeled
.
Like TestASEtraj4
, this class also prepares systems but lacks assertions to check the outcomes. I can assist in creating some test methods if you need.
for more information, see https://pre-commit.ci
Signed-off-by: C. Thang Nguyen <46436648+thangckt@users.noreply.github.com>
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
list_atoms = ASEStructureFormat().to_system(data, **kwargs) | ||
traj = Trajectory(file_name, "a") | ||
_ = [traj.write(atom) for atom in list_atoms] | ||
traj.close() | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add unit tests for the new methods to_system
and to_labeled_system
.
These methods are critical for the functionality added in this PR but are not covered by tests according to the static analysis. Would you like assistance in creating these tests?
Also applies to: 328-332
@@ -94,7 +95,7 @@ def from_labeled_system(self, atoms: ase.Atoms, **kwargs) -> dict: | |||
"forces": np.array([forces]), | |||
} | |||
try: | |||
stress = atoms.get_stress(False) | |||
stress = atoms.get_stress(voigt=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider handling PropertyNotImplementedError
more explicitly.
While the current implementation passes silently if PropertyNotImplementedError
is raised, it might be beneficial to log this occurrence or handle it in a way that does not silently ignore potential issues. Consider adding logging or another form of error handling.
@@ -300,6 +301,46 @@ def from_labeled_system( | |||
|
|||
return dict_frames | |||
|
|||
def to_system(self, data, file_name: str = "confs.traj", **kwargs) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper file handling in to_system
and to_labeled_system
.
The methods to_system
and to_labeled_system
perform file operations such as checking if a file exists and removing it. It's crucial to ensure that these operations are safe and do not lead to unintended data loss. Consider adding checks or warnings to inform the user about the file deletion.
Also applies to: 323-323
_ = [traj.write(atom) for atom in list_atoms] | ||
traj.close() | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review the use of default parameters in ASEMinimizer
.
The constructor of ASEMinimizer
uses mutable default arguments (optimizer_kwargs
). This is a common Python pitfall, as default mutable arguments can lead to unexpected behavior if the object is modified. Consider using a default value of None
and then setting the value inside the method if it is None
.
- optimizer_kwargs: dict = {}
+ optimizer_kwargs: dict | None = None
if optimizer_kwargs is None:
optimizer_kwargs = {}
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.
optimizer_kwargs: dict | None = None | |
if optimizer_kwargs is None: | |
optimizer_kwargs = {} |
@@ -140,7 +141,7 @@ def from_multi_systems( | |||
frames = ase.io.read(file_name, format=ase_fmt, index=slice(begin, end, step)) | |||
yield from frames | |||
|
|||
def to_system(self, data, **kwargs): | |||
def to_system(self, data, **kwargs) -> list[ase.Atoms]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update type annotations from List
to list
.
- def to_system(self, data, **kwargs) -> List[ase.Atoms]:
+ def to_system(self, data, **kwargs) -> list[ase.Atoms]:
- def to_labeled_system(self, data, *args, **kwargs) -> List[ase.Atoms]:
+ def to_labeled_system(self, data, *args, **kwargs) -> list[ase.Atoms]:
Using list
instead of List
for type annotations is recommended as of Python 3.9 for better readability and alignment with modern Python practices.
Also applies to: 162-162
Committable suggestion was skipped due low confidence.
add functions to convert from others formats to ASE traj format
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores