Skip to content

Commit

Permalink
SmilesWidget: Fix generation of 1- and 2-atom molecules (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas authored Jun 27, 2023
1 parent c87b724 commit 43b209f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion aiidalab_widgets_base/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,8 @@ def _make_ase(self, species, positions, smiles):
from sklearn.decomposition import PCA

# Get the principal axes and realign the molecule along z-axis.
positions = PCA(n_components=3).fit_transform(positions)
if len(species) > 2:
positions = PCA(n_components=3).fit_transform(positions)
atoms = ase.Atoms(species, positions=positions, pbc=False)
atoms.cell = np.ptp(atoms.positions, axis=0) + 10
atoms.center()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ def test_smiles_widget():
assert isinstance(widget.structure, ase.Atoms)
assert widget.structure.get_chemical_formula() == "CH4"

# Regression test that we can generate 1-atom and 2-atom molecules
widget.smiles.value = "[O]"
widget._on_button_pressed()
assert isinstance(widget.structure, ase.Atoms)
assert widget.structure.get_chemical_formula() == "O"

widget.smiles.value = "N#N"
widget._on_button_pressed()
assert isinstance(widget.structure, ase.Atoms)
assert widget.structure.get_chemical_formula() == "N2"


@pytest.mark.usefixtures("aiida_profile_clean")
def test_basic_cell_editor_widget(structure_data_object):
Expand Down

0 comments on commit 43b209f

Please sign in to comment.