Skip to content

Commit bb207e0

Browse files
committed
make ase converter take str or list of strs
1 parent 67d978a commit bb207e0

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/diffpy/structure/structure.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,23 @@ def convert_ase_to_diffpy_structure(
282282
) -> Structure | tuple[Structure, dict]: # noqa
283283
"""Convert ASE `Atoms` object to this `Structure` instance.
284284
285+
The conversion process involves extracting the lattice parameters, chemical symbols,
286+
and fractional coordinates from the ASE `Atoms` object and populating the corresponding
287+
attributes of this `Structure` instance. The `lattice` attribute is set based on the
288+
cell parameters of the ASE `Atoms` object, and each `Atom` in this `Structure` is created
289+
with the chemical symbol and fractional coordinates extracted from the ASE `Atoms`.
290+
285291
Parameters
286292
----------
287293
ase_structure : ase.Atoms
288294
The ASE `Atoms` object to be converted.
289-
lost_info : list of str, optional
290-
The list of attribute names to extract from the ASE `Atoms`
295+
lost_info : str or list of str, optional
296+
The method(s) or attribute(s) to extract from the ASE `Atoms`
291297
object that do not have a direct equivalent in the `Structure` class.
292-
object that is not currently available in the `Structure` class.
293-
Default is False.
298+
This will be provided in a dictionary format where keys are the
299+
method/attribute name(s) and value(s) are the corresponding data
300+
extracted from the ASE `Atoms` object.
301+
Default is None. See `Examples` for usage.
294302
295303
Returns
296304
-------
@@ -324,7 +332,7 @@ def convert_ase_to_diffpy_structure(
324332
325333
# Convert to a diffpy Structure object
326334
structure = Structure()
327-
structure.convert_ase_to_diffpy(ase_atoms,
335+
structure.convert_ase_to_diffpy(ase_atoms)
328336
329337
330338
To extract additional information from the ASE `Atoms` object that is not
@@ -333,10 +341,10 @@ def convert_ase_to_diffpy_structure(
333341
a list of strings in `lost_info` list. For example,
334342
335343
.. code-block:: python
336-
lost_info = structure.convert_ase_to_diffpy(
337-
ase_atoms,
338-
lost_info=['get_magnetic_moments']
339-
)
344+
lost_info = structure.convert_ase_to_diffpy(
345+
ase_atoms,
346+
lost_info='get_magnetic_moments'
347+
)
340348
341349
will return a dictionary with the magnetic moments of the atoms in the ASE `Atoms` object.
342350
"""
@@ -348,11 +356,13 @@ def convert_ase_to_diffpy_structure(
348356
self.lattice = Lattice(base=numpy.array(cell))
349357
symbols = ase_atoms.get_chemical_symbols()
350358
scaled_positions = ase_atoms.get_scaled_positions()
351-
for sym, xyz in zip(symbols, scaled_positions):
352-
self.append(Atom(sym, xyz=xyz))
359+
for atom_symbol, frac_coord in zip(symbols, scaled_positions):
360+
self.append(Atom(atom_symbol, xyz=frac_coord))
353361
if lost_info is None:
354362
return
355363
extracted_info = {}
364+
if isinstance(lost_info, str):
365+
lost_info = [lost_info]
356366
for name in lost_info:
357367
if not hasattr(ase_atoms, name):
358368
raise ValueError(f"ASE.Atoms object has no attribute '{name}'.")

tests/test_structure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def test_convert_ase_to_diffpy_structure(input, build_ase_atom_object, build_dif
735735
# set the lost_info variable, which gets the attribute of method from ASE.Atoms object, gets the values
736736
# and stores it in a dict. This is used because ASE.Atoms stores more/different
737737
# info that a diffpy.structure object
738-
lost_info_dict = actual_structure.convert_ase_to_diffpy_structure(ase_zb, lost_info=["get_masses"])
738+
lost_info_dict = actual_structure.convert_ase_to_diffpy_structure(ase_zb, lost_info="get_masses")
739739
actual_masses = lost_info_dict["get_masses"]
740740
expected_masses = ase_zb.get_masses()
741741
assert numpy.allclose(actual_masses, expected_masses)

0 commit comments

Comments
 (0)