Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 22, 2024
1 parent 1c24082 commit 21897d9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/aiida/orm/nodes/data/array/kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,21 @@ def set_cell_from_structure(self, structuredata):
from aiida.orm.nodes.data.structure import StructureData, has_atomistic

if not isinstance(structuredata, StructureData):
error_message = 'An instance of StructureData or aiida-atomistic StructureData should be passed to ' 'the KpointsData, found instead {}'.format(
structuredata.__class__
)
error_message = (

Check warning on line 229 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L229

Added line #L229 was not covered by tests
'An instance of StructureData or aiida-atomistic StructureData should be passed to '
'the KpointsData, found instead {}'.format(structuredata.__class__)
)
if has_atomistic:
from aiida_atomistic import StructureData as AtomisticStructureData

Check warning on line 234 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L233-L234

Added lines #L233 - L234 were not covered by tests

if not isinstance(structuredata, AtomisticStructureData):
raise ValueError(error_message)

Check warning on line 237 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L236-L237

Added lines #L236 - L237 were not covered by tests
else:
cell = structuredata.cell
self.set_cell(cell, structuredata.pbc)

Check warning on line 240 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L239-L240

Added lines #L239 - L240 were not covered by tests
else:
raise ValueError(error_message)

Check warning on line 242 in src/aiida/orm/nodes/data/array/kpoints.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/array/kpoints.py#L242

Added line #L242 was not covered by tests
else:
else:
cell = structuredata.cell
self.set_cell(cell, structuredata.pbc)

Expand Down
16 changes: 10 additions & 6 deletions src/aiida/orm/nodes/data/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def has_pymatgen():
return False
return True


def has_atomistic():
""":return: True if the StructureData and StructureDataMutable from aiida-atomistic module can be imported, False otherwise."""
try:
Expand Down Expand Up @@ -1889,28 +1890,31 @@ def to_atomistic(self):
Returns the atomistic StructureData version of the orm.StructureData one.
"""
if not has_atomistic:
raise ImportError('aiida-atomistic plugin is not installed, \
please install it to have full support for atomistic structures')
raise ImportError(

Check warning on line 1893 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1892-L1893

Added lines #L1892 - L1893 were not covered by tests
'aiida-atomistic plugin is not installed, \
please install it to have full support for atomistic structures'
)
else:
from aiida_atomistic import StructureData as AtomisticStructureData
from aiida_atomistic import StructureDataMutable as AtomisticStructureDataMutable

Check warning on line 1899 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1898-L1899

Added lines #L1898 - L1899 were not covered by tests

atomistic = AtomisticStructureDataMutable()
atomistic.set_pbc(self.pbc)
atomistic.set_cell(self.cell)

Check warning on line 1903 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1901-L1903

Added lines #L1901 - L1903 were not covered by tests

for site in self.sites:
atomistic.add_atom(

Check warning on line 1906 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1905-L1906

Added lines #L1905 - L1906 were not covered by tests
**{
'symbols': self.get_kind(site.kind_name).symbol,
'masses': self.get_kind(site.kind_name).mass,
'positions': site.position,
'kinds': site.kind_name,
}
}
)

return AtomisticStructureData.from_mutable(atomistic)

Check warning on line 1915 in src/aiida/orm/nodes/data/structure.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/orm/nodes/data/structure.py#L1915

Added line #L1915 was not covered by tests


class Kind:
"""This class contains the information about the species (kinds) of the system.
Expand Down
10 changes: 6 additions & 4 deletions tests/orm/nodes/data/test_kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

skip_atomistic = pytest.mark.skipif(not has_atomistic(), reason='Unable to import aiida-atomistic')


class TestKpoints:
"""Test for the `Kpointsdata` class."""

Expand Down Expand Up @@ -84,17 +85,18 @@ def test_get_kpoints(self):
assert np.abs(kpt2.get_kpoints() - np.array(kpoints)).sum() == 0.0
assert np.abs(kpt2.get_kpoints(cartesian=True) - np.array(cartesian_kpoints)).sum() == 0.0


@skip_atomistic
class TestKpointsAtomisticStructureData:
"""Test for the `Kpointsdata` class using the new atomistic StructureData."""

@pytest.fixture(autouse=True)
def init_profile(self):
"""Initialize the profile."""

from aiida_atomistic import StructureDataMutable

from aiida_atomistic import StructureData as AtomisticStructureData

from aiida_atomistic import StructureDataMutable

alat = 5.430 # angstrom
cell = [
[
Expand Down Expand Up @@ -157,4 +159,4 @@ def test_get_kpoints(self):
kpt.store()
kpt2 = load_node(kpt.pk)
assert np.abs(kpt2.get_kpoints() - np.array(kpoints)).sum() == 0.0
assert np.abs(kpt2.get_kpoints(cartesian=True) - np.array(cartesian_kpoints)).sum() == 0.0
assert np.abs(kpt2.get_kpoints(cartesian=True) - np.array(cartesian_kpoints)).sum() == 0.0
4 changes: 3 additions & 1 deletion tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
get_formula,
get_pymatgen_version,
has_ase,
has_atomistic,
has_pymatgen,
has_spglib,
has_atomistic,
)


Expand Down Expand Up @@ -1852,6 +1852,7 @@ def test_clone(self):
for i in range(3):
assert round(abs(c.sites[1].position[i] - 1.0), 7) == 0


@skip_atomistic
class TestAtomisticStructureData:
"""Tests the creation of StructureData objects (cell and pbc), and its conversion to the new atomistic StructureData."""
Expand All @@ -1874,6 +1875,7 @@ def test_to_atomistic(self):
assert structure.properties.sites[0].positions == list(legacy.sites[0].position)
assert structure.properties.cell == legacy.cell


class TestStructureDataFromAse:
"""Tests the creation of Sites from/to a ASE object."""

Expand Down

0 comments on commit 21897d9

Please sign in to comment.