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 from_ase_atoms for Molecule #4321

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

DanielYang59
Copy link
Contributor

Summary

@@ -995,19 +995,6 @@ def to_ase_atoms(self, **kwargs) -> Atoms:

return AseAtomsAdaptor.get_atoms(self, **kwargs)

def from_ase_atoms(self, **kwargs) -> Structure:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need careful check on this: AFAIK there is no other concrete class other than IMolecule/IStructure of the abstract base classSiteCollection that would reply on inheriting from_ase_atoms?

Copy link
Member

Choose a reason for hiding this comment

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

I don't believe there are. Even if there were, then presumably we would currently be missing out on such instances anyway since the pre-existing methods were on Structure and Molecule, right?

Copy link
Member

Choose a reason for hiding this comment

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

Is there a problem here in that it will produce an IStructure rather than a Structure?

Copy link
Contributor Author

@DanielYang59 DanielYang59 Mar 11, 2025

Choose a reason for hiding this comment

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

the pre-existing methods were on Structure and Molecule, right?

If I understand the question correctly, current defination of from_ase_atoms is given by the abstract base case SiteCollection (from which IStructure/IMolecule inherit):

def from_ase_atoms(self, **kwargs) -> Structure:
"""Convert ase.Atoms to pymatgen Structure.
Args:
kwargs: Passed to AseAtomsAdaptor.get_structure.
Returns:
Structure
"""
from pymatgen.io.ase import AseAtomsAdaptor
return AseAtomsAdaptor.get_structure(self, **kwargs)


Is there a problem here in that it will produce an IStructure rather than a Structure?

Thanks for catching this, this method should give the mutable Structure/Molecule by default, I would fix the type annotation:

def get_structure(atoms: Atoms, cls: type[Structure] = Structure, **cls_kwargs) -> Structure:

# Return a Molecule object if that was specifically requested;
# otherwise return a Structure object as expected
if cls == Molecule:
structure = cls(symbols, positions, properties=properties, **cls_kwargs)
else:
structure = cls(
Lattice(lattice, pbc=atoms.pbc),
symbols,
positions,
coords_are_cartesian=True,
properties=properties,
**cls_kwargs,
)

Perhaps we should give the corresponding class instead (i.e. IStructure.from_ase_atoms should give IStructure instead of Structure, the same for IMolecule), which would be less confusing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Molecule.from_ase_atoms() does not pass kwargs appropriately
2 participants