diff --git a/aiidalab_widgets_base/structures.py b/aiidalab_widgets_base/structures.py index cd4d7da5a..055215b63 100644 --- a/aiidalab_widgets_base/structures.py +++ b/aiidalab_widgets_base/structures.py @@ -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() diff --git a/tests/test_structures.py b/tests/test_structures.py index 63dec3770..b479a69bc 100644 --- a/tests/test_structures.py +++ b/tests/test_structures.py @@ -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):