• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

materialsproject / pymatgen / 4075885785

pending completion
4075885785

push

github

Shyue Ping Ong
Merge branch 'master' of github.com:materialsproject/pymatgen

96 of 96 new or added lines in 27 files covered. (100.0%)

81013 of 102710 relevant lines covered (78.88%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.28
/pymatgen/analysis/tests/test_molecule_structure_comparator.py
1
# Copyright (c) Pymatgen Development Team.
2
# Distributed under the terms of the MIT License.
3

4

5
from __future__ import annotations
1✔
6

7
import os
1✔
8
import unittest
1✔
9
from unittest import TestCase
1✔
10

11
from pymatgen.analysis.molecule_structure_comparator import MoleculeStructureComparator
1✔
12
from pymatgen.core.structure import Molecule
1✔
13
from pymatgen.util.testing import PymatgenTest
1✔
14

15
__author__ = "xiaohuiqu"
1✔
16

17

18
test_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "molecules", "structural_change")
1✔
19

20

21
class TestMoleculeStructureComparator(TestCase):
1✔
22
    def test_are_equal(self):
1✔
23
        msc1 = MoleculeStructureComparator()
1✔
24
        mol1 = Molecule.from_file(os.path.join(test_dir, "t1.xyz"))
1✔
25
        mol2 = Molecule.from_file(os.path.join(test_dir, "t2.xyz"))
1✔
26
        mol3 = Molecule.from_file(os.path.join(test_dir, "t3.xyz"))
1✔
27
        assert not msc1.are_equal(mol1, mol2)
1✔
28
        assert msc1.are_equal(mol2, mol3)
1✔
29
        thio1 = Molecule.from_file(os.path.join(test_dir, "thiophene1.xyz"))
1✔
30
        thio2 = Molecule.from_file(os.path.join(test_dir, "thiophene2.xyz"))
1✔
31
        # noinspection PyProtectedMember
32
        msc2 = MoleculeStructureComparator(priority_bonds=msc1._get_bonds(thio1))
1✔
33
        assert msc2.are_equal(thio1, thio2)
1✔
34
        hal1 = Molecule.from_file(os.path.join(test_dir, "molecule_with_halogen_bonds_1.xyz"))
1✔
35
        hal2 = Molecule.from_file(os.path.join(test_dir, "molecule_with_halogen_bonds_2.xyz"))
1✔
36
        msc3 = MoleculeStructureComparator(priority_bonds=msc1._get_bonds(hal1))
1✔
37
        assert msc3.are_equal(hal1, hal2)
1✔
38

39
    def test_get_bonds(self):
1✔
40
        mol1 = Molecule.from_file(os.path.join(test_dir, "t1.xyz"))
1✔
41
        msc = MoleculeStructureComparator()
1✔
42
        # noinspection PyProtectedMember
43
        bonds = msc._get_bonds(mol1)
1✔
44
        bonds_ref = [
1✔
45
            (0, 1),
46
            (0, 2),
47
            (0, 3),
48
            (0, 23),
49
            (3, 4),
50
            (3, 5),
51
            (5, 6),
52
            (5, 7),
53
            (7, 8),
54
            (7, 9),
55
            (7, 21),
56
            (9, 10),
57
            (9, 11),
58
            (9, 12),
59
            (12, 13),
60
            (12, 14),
61
            (12, 15),
62
            (15, 16),
63
            (15, 17),
64
            (15, 18),
65
            (18, 19),
66
            (18, 20),
67
            (18, 21),
68
            (21, 22),
69
            (21, 23),
70
            (23, 24),
71
            (23, 25),
72
        ]
73
        assert bonds == bonds_ref
1✔
74
        mol2 = Molecule.from_file(os.path.join(test_dir, "MgBH42.xyz"))
1✔
75
        bonds = msc._get_bonds(mol2)
1✔
76
        assert bonds == [(1, 3), (2, 3), (3, 4), (3, 5), (6, 8), (7, 8), (8, 9), (8, 10)]
1✔
77
        msc = MoleculeStructureComparator(ignore_ionic_bond=False)
1✔
78
        bonds = msc._get_bonds(mol2)
1✔
79
        assert bonds == [
1✔
80
            (0, 1),
81
            (0, 2),
82
            (0, 3),
83
            (0, 5),
84
            (0, 6),
85
            (0, 7),
86
            (0, 8),
87
            (0, 9),
88
            (1, 3),
89
            (2, 3),
90
            (3, 4),
91
            (3, 5),
92
            (6, 8),
93
            (7, 8),
94
            (8, 9),
95
            (8, 10),
96
        ]
97

98
        mol1 = Molecule.from_file(os.path.join(test_dir, "molecule_with_halogen_bonds_1.xyz"))
1✔
99
        msc = MoleculeStructureComparator()
1✔
100
        # noinspection PyProtectedMember
101
        bonds = msc._get_bonds(mol1)
1✔
102
        assert bonds == [
1✔
103
            (0, 12),
104
            (0, 13),
105
            (0, 14),
106
            (0, 15),
107
            (1, 12),
108
            (1, 16),
109
            (1, 17),
110
            (1, 18),
111
            (2, 4),
112
            (2, 11),
113
            (2, 19),
114
            (3, 5),
115
            (3, 10),
116
            (3, 20),
117
            (4, 6),
118
            (4, 10),
119
            (5, 11),
120
            (5, 12),
121
            (6, 7),
122
            (6, 8),
123
            (6, 9),
124
        ]
125

126
    def test_to_and_from_dict(self):
1✔
127
        msc1 = MoleculeStructureComparator()
1✔
128
        d1 = msc1.as_dict()
1✔
129
        d2 = MoleculeStructureComparator.from_dict(d1).as_dict()
1✔
130
        assert d1 == d2
1✔
131
        thio1 = Molecule.from_file(os.path.join(test_dir, "thiophene1.xyz"))
1✔
132
        # noinspection PyProtectedMember
133
        msc2 = MoleculeStructureComparator(bond_length_cap=0.2, priority_bonds=msc1._get_bonds(thio1), priority_cap=0.5)
1✔
134
        d1 = msc2.as_dict()
1✔
135
        d2 = MoleculeStructureComparator.from_dict(d1).as_dict()
1✔
136
        assert d1 == d2
1✔
137

138
    # def test_structural_change_in_geom_opt(self):
139
    #     qcout_path = os.path.join(test_dir, "mol_1_3_bond.qcout")
140
    #     qcout = QcOutput(qcout_path)
141
    #     mol1 = qcout.data[0]["molecules"][0]
142
    #     mol2 = qcout.data[0]["molecules"][-1]
143
    #     priority_bonds = [[0, 1], [0, 2], [1, 3], [1, 4], [1, 7], [2, 5], [2, 6], [2, 8], [4, 6], [4, 10], [6, 9]]
144
    #     msc = MoleculeStructureComparator(priority_bonds=priority_bonds)
145
    #     self.assertTrue(msc.are_equal(mol1, mol2))
146

147
    def test_get_13_bonds(self):
1✔
148
        priority_bonds = [
1✔
149
            [0, 1],
150
            [0, 2],
151
            [1, 3],
152
            [1, 4],
153
            [1, 7],
154
            [2, 5],
155
            [2, 6],
156
            [2, 8],
157
            [4, 6],
158
            [4, 10],
159
            [6, 9],
160
        ]
161
        bonds_13 = MoleculeStructureComparator.get_13_bonds(priority_bonds)
1✔
162
        ans = (
1✔
163
            (0, 3),
164
            (0, 4),
165
            (0, 5),
166
            (0, 6),
167
            (0, 7),
168
            (0, 8),
169
            (1, 2),
170
            (1, 6),
171
            (1, 10),
172
            (2, 4),
173
            (2, 9),
174
            (3, 4),
175
            (3, 7),
176
            (4, 7),
177
            (4, 9),
178
            (5, 6),
179
            (5, 8),
180
            (6, 8),
181
            (6, 10),
182
        )
183
        assert bonds_13 == tuple(ans)
1✔
184

185

186
if __name__ == "__main__":
1✔
187
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc