From 27b7785ce475e3916fbbaf180ff5bf69cbda1773 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:14:01 +0200 Subject: [PATCH 1/9] Add get_number_species_atoms helper function --- structuretoolkit/common/helper.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/structuretoolkit/common/helper.py b/structuretoolkit/common/helper.py index 0db936641..c5034a707 100644 --- a/structuretoolkit/common/helper.py +++ b/structuretoolkit/common/helper.py @@ -5,6 +5,22 @@ from scipy.sparse import coo_matrix +def get_number_species_atoms(structure:Atoms): + """Returns a dictionary with the species in the structure and the corresponding count in the structure + + Args: + structure + + Returns: + dict: A dictionary with the species and the corresponding count + + """ + species_dict = dict() + for i in structure.get_chemical_symbols(): + species_dict[i] = species_dict.get(i, 0) + 1 + return specied_dict + + def get_extended_positions( structure: Atoms, width: float, From 94a25e81e5a451f29b1dd7619f7f3445471adb8b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 Aug 2025 09:14:20 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- structuretoolkit/common/helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structuretoolkit/common/helper.py b/structuretoolkit/common/helper.py index c5034a707..6de07bef2 100644 --- a/structuretoolkit/common/helper.py +++ b/structuretoolkit/common/helper.py @@ -5,7 +5,7 @@ from scipy.sparse import coo_matrix -def get_number_species_atoms(structure:Atoms): +def get_number_species_atoms(structure: Atoms): """Returns a dictionary with the species in the structure and the corresponding count in the structure Args: @@ -17,7 +17,7 @@ def get_number_species_atoms(structure:Atoms): """ species_dict = dict() for i in structure.get_chemical_symbols(): - species_dict[i] = species_dict.get(i, 0) + 1 + species_dict[i] = species_dict.get(i, 0) + 1 return specied_dict From d7e1cbc8d6b176c0722490d9551c62d04d016250 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:17:28 +0200 Subject: [PATCH 3/9] Fix typo --- structuretoolkit/common/helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/structuretoolkit/common/helper.py b/structuretoolkit/common/helper.py index 6de07bef2..780ee31e1 100644 --- a/structuretoolkit/common/helper.py +++ b/structuretoolkit/common/helper.py @@ -15,10 +15,10 @@ def get_number_species_atoms(structure: Atoms): dict: A dictionary with the species and the corresponding count """ - species_dict = dict() + species_dict = {} for i in structure.get_chemical_symbols(): species_dict[i] = species_dict.get(i, 0) + 1 - return specied_dict + return species_dict def get_extended_positions( From e13799d56c359fc3e66177cf1f9e59f6c6316fc7 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:34:02 +0200 Subject: [PATCH 4/9] Add function to __init__ --- structuretoolkit/__init__.py | 2 ++ structuretoolkit/common/__init__.py | 1 + 2 files changed, 3 insertions(+) diff --git a/structuretoolkit/__init__.py b/structuretoolkit/__init__.py index d8669e23a..6bc461501 100644 --- a/structuretoolkit/__init__.py +++ b/structuretoolkit/__init__.py @@ -88,6 +88,7 @@ center_coordinates_in_unit_cell, get_cell, get_extended_positions, + get_number_species_atoms, get_vertical_length, get_wrapped_coordinates, pymatgen_to_ase, @@ -113,6 +114,7 @@ "get_mean_positions", "get_neighborhood", "get_neighbors", + "get_number_species_atoms", "get_steinhardt_parameters", "get_strain", "get_symmetry", diff --git a/structuretoolkit/common/__init__.py b/structuretoolkit/common/__init__.py index 8c15b8aba..f3284c296 100644 --- a/structuretoolkit/common/__init__.py +++ b/structuretoolkit/common/__init__.py @@ -4,6 +4,7 @@ center_coordinates_in_unit_cell, get_cell, get_extended_positions, + get_number_species_atoms, get_vertical_length, get_wrapped_coordinates, select_index, From c1476f0a2a4d636bb7b81d946a551f68bdcf91a0 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:47:39 +0200 Subject: [PATCH 5/9] Add test --- tests/test_helpers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index b7e5b7dfd..0f1ceddc7 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -22,6 +22,14 @@ def test_get_cell(self): stk.get_cell(np.arange(4)) with self.assertRaises(ValueError): stk.get_cell(np.ones((4, 3))) + + def test_get_number_species_atoms(self): + with self.subTest("Fe8"): + atoms = bulk("Fe").repeat(2) + self.assertEqual(stk.get_number_species_atoms(atoms) == {'Fe': 8}) + with self.subTest('Al2Fe8'): + atoms = bulk('Fe').repeat(2) + bulk('Al').repeat((2,1,1)) + self.assertEqual(stk.get_number_species_atoms(atoms) == {'Fe': 8, 'Al':2}) if __name__ == "__main__": From 66cd119b70c173f95dc842d54f5e8dc30ba718d7 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:51:13 +0200 Subject: [PATCH 6/9] Add new functon to common.__all__ --- structuretoolkit/common/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/structuretoolkit/common/__init__.py b/structuretoolkit/common/__init__.py index f3284c296..00a506e30 100644 --- a/structuretoolkit/common/__init__.py +++ b/structuretoolkit/common/__init__.py @@ -23,6 +23,7 @@ "center_coordinates_in_unit_cell", "get_cell", "get_extended_positions", + "get_number_species_atoms", "get_vertical_length", "get_wrapped_coordinates", "select_index", From 5cdb35d534b0f875349f64676ff2fd4a09112690 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:52:23 +0200 Subject: [PATCH 7/9] correct assertEqual test --- tests/test_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 0f1ceddc7..3e6356723 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -29,7 +29,7 @@ def test_get_number_species_atoms(self): self.assertEqual(stk.get_number_species_atoms(atoms) == {'Fe': 8}) with self.subTest('Al2Fe8'): atoms = bulk('Fe').repeat(2) + bulk('Al').repeat((2,1,1)) - self.assertEqual(stk.get_number_species_atoms(atoms) == {'Fe': 8, 'Al':2}) + self.assertEqual(stk.get_number_species_atoms(atoms), {'Fe': 8, 'Al':2}) if __name__ == "__main__": From 6dbdb9088d087c42bcd0f308862ce46161e94934 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 11:55:29 +0200 Subject: [PATCH 8/9] Fix second test... --- tests/test_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 3e6356723..d754c3b83 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -26,7 +26,7 @@ def test_get_cell(self): def test_get_number_species_atoms(self): with self.subTest("Fe8"): atoms = bulk("Fe").repeat(2) - self.assertEqual(stk.get_number_species_atoms(atoms) == {'Fe': 8}) + self.assertEqual(stk.get_number_species_atoms(atoms), {'Fe': 8}) with self.subTest('Al2Fe8'): atoms = bulk('Fe').repeat(2) + bulk('Al').repeat((2,1,1)) self.assertEqual(stk.get_number_species_atoms(atoms), {'Fe': 8, 'Al':2}) From 419685a412dde5422b5e78d154f18ce580a515a9 Mon Sep 17 00:00:00 2001 From: Niklas Siemer <70580458+niklassiemer@users.noreply.github.com> Date: Mon, 18 Aug 2025 14:55:43 +0200 Subject: [PATCH 9/9] list.count instead of manual counting Co-authored-by: Jan Janssen --- structuretoolkit/common/helper.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/structuretoolkit/common/helper.py b/structuretoolkit/common/helper.py index 780ee31e1..47029c959 100644 --- a/structuretoolkit/common/helper.py +++ b/structuretoolkit/common/helper.py @@ -15,10 +15,8 @@ def get_number_species_atoms(structure: Atoms): dict: A dictionary with the species and the corresponding count """ - species_dict = {} - for i in structure.get_chemical_symbols(): - species_dict[i] = species_dict.get(i, 0) + 1 - return species_dict + elements_lst = structure.get_chemical_symbols() + return {species: elements_lst.count(species) for species in set(elements_lst)} def get_extended_positions(