Skip to content

Commit 68bb109

Browse files
committed
Update fails_species_constraints
- returns only either False or a str, instead of a tuple - user must check if fails_species_constraints != False (or lazily if just fails_species_constraints) as both will return the reason for failing constraints; if no failure it will return False - Fixes a bug with implementation
1 parent 2f274da commit 68bb109

File tree

5 files changed

+51
-78
lines changed

5 files changed

+51
-78
lines changed

rmgpy/constraints.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def pass_cutting_threshold(species):
6161
def fails_species_constraints(species):
6262
"""
6363
Pass in either a `Species` or `Molecule` object and checks whether it passes
64-
the speciesConstraints set by the user. If not, returns a tuple `(True, reason)` for failing speciesConstraints,
65-
where `reason` is a string describing which constraint failed. If all constraints pass, returns `(False, None)`.
64+
the speciesConstraints set by the user. If the species fails constraints, returns
65+
a string `reason` describing which constraint failed. If all constraints pass, returns `False`.
6666
"""
6767

6868
from rmgpy.rmg.input import get_input
@@ -82,63 +82,63 @@ def fails_species_constraints(species):
8282
explicitly_allowed_molecules = species_constraints.get('explicitlyAllowedMolecules', [])
8383
for molecule in explicitly_allowed_molecules:
8484
if struct.is_isomorphic(molecule):
85-
return (False, None)
85+
return False
8686

8787
max_carbon_atoms = species_constraints.get('maximumCarbonAtoms', -1)
8888
if max_carbon_atoms != -1:
8989
if struct.get_num_atoms('C') > max_carbon_atoms:
90-
return (True, f"Exceeded maximumCarbonAtoms: {struct.get_num_atoms('C')} > {max_carbon_atoms}")
90+
return f"Exceeded maximumCarbonAtoms: {struct.get_num_atoms('C')} > {max_carbon_atoms}"
9191

9292
max_oxygen_atoms = species_constraints.get('maximumOxygenAtoms', -1)
9393
if max_oxygen_atoms != -1:
9494
if struct.get_num_atoms('O') > max_oxygen_atoms:
95-
return (True, f"Exceeded maximumOxygenAtoms: {struct.get_num_atoms('O')} > {max_oxygen_atoms}")
95+
return f"Exceeded maximumOxygenAtoms: {struct.get_num_atoms('O')} > {max_oxygen_atoms}"
9696

9797
max_nitrogen_atoms = species_constraints.get('maximumNitrogenAtoms', -1)
9898
if max_nitrogen_atoms != -1:
9999
if struct.get_num_atoms('N') > max_nitrogen_atoms:
100-
return (True, f"Exceeded maximumNitrogenAtoms: {struct.get_num_atoms('N')} > {max_nitrogen_atoms}")
100+
return f"Exceeded maximumNitrogenAtoms: {struct.get_num_atoms('N')} > {max_nitrogen_atoms}"
101101

102102
max_silicon_atoms = species_constraints.get('maximumSiliconAtoms', -1)
103103
if max_silicon_atoms != -1:
104104
if struct.get_num_atoms('Si') > max_silicon_atoms:
105-
return (True, f"Exceeded maximumSiliconAtoms: {struct.get_num_atoms('Si')} > {max_silicon_atoms}")
105+
return f"Exceeded maximumSiliconAtoms: {struct.get_num_atoms('Si')} > {max_silicon_atoms}"
106106

107107
max_sulfur_atoms = species_constraints.get('maximumSulfurAtoms', -1)
108108
if max_sulfur_atoms != -1:
109109
if struct.get_num_atoms('S') > max_sulfur_atoms:
110-
return (True, f"Exceeded maximumSulfurAtoms: {struct.get_num_atoms('S')} > {max_sulfur_atoms}")
110+
return f"Exceeded maximumSulfurAtoms: {struct.get_num_atoms('S')} > {max_sulfur_atoms}"
111111

112112
max_heavy_atoms = species_constraints.get('maximumHeavyAtoms', -1)
113113
if max_heavy_atoms != -1:
114114
heavy_atoms = struct.get_num_atoms() - struct.get_num_atoms('H')
115115
if heavy_atoms > max_heavy_atoms:
116-
return (True, f"Exceeded maximumHeavyAtoms: {heavy_atoms} > {max_heavy_atoms}")
116+
return f"Exceeded maximumHeavyAtoms: {heavy_atoms} > {max_heavy_atoms}"
117117

118118
max_surface_sites = species_constraints.get('maximumSurfaceSites', -1)
119119
if max_surface_sites != -1:
120120
if struct.get_num_atoms('X') > max_surface_sites:
121-
return (True, f"Exceeded maximumSurfaceSites: {struct.get_num_atoms('X')} > {max_surface_sites}")
121+
return f"Exceeded maximumSurfaceSites: {struct.get_num_atoms('X')} > {max_surface_sites}"
122122

123123
max_surface_bond_order = species_constraints.get('maximumSurfaceBondOrder', -1)
124124
if max_surface_bond_order != -1:
125125
for site in struct.get_surface_sites():
126126
if site.get_total_bond_order() > max_surface_bond_order:
127-
return (True, f"Exceeded maximumSurfaceBondOrder at site: {site.get_total_bond_order()} > {max_surface_bond_order}")
127+
return f"Exceeded maximumSurfaceBondOrder at site: {site.get_total_bond_order()} > {max_surface_bond_order}"
128128

129129
max_radicals = species_constraints.get('maximumRadicalElectrons', -1)
130130
if max_radicals != -1:
131131
if struct.get_radical_count() > max_radicals:
132-
return (True, f"Exceeded maximumRadicalElectrons: {struct.get_radical_count()} > {max_radicals}")
132+
return f"Exceeded maximumRadicalElectrons: {struct.get_radical_count()} > {max_radicals}"
133133

134134
max_carbenes = species_constraints.get('maximumSingletCarbenes', 1)
135-
if max_radicals != -1:
135+
if max_carbenes != -1:
136136
if struct.get_singlet_carbene_count() > max_carbenes:
137-
return (True, f"Exceeded maximumSingletCarbenes: {struct.get_singlet_carbene_count()} > {max_carbenes}")
137+
return f"Exceeded maximumSingletCarbenes: {struct.get_singlet_carbene_count()} > {max_carbenes}"
138138

139139
max_carbene_radicals = species_constraints.get('maximumCarbeneRadicals', 0)
140140
if max_carbene_radicals != -1:
141141
if struct.get_singlet_carbene_count() > 0 and struct.get_radical_count() > max_carbene_radicals:
142-
return (True, f"Exceeded maximumCarbeneRadicals: {struct.get_radical_count()} > {max_carbene_radicals}")
142+
return f"Exceeded maximumCarbeneRadicals: {struct.get_radical_count()} > {max_carbene_radicals}"
143143

144-
return (False, None)
144+
return False

rmgpy/data/kinetics/family.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,8 +1654,8 @@ def _generate_product_structures(self, reactant_structures, maps, forward, relab
16541654
for struct in product_structures:
16551655
if self.is_molecule_forbidden(struct):
16561656
raise ForbiddenStructureException()
1657-
failed, reason = fails_species_constraints(struct)
1658-
if failed:
1657+
if fails_species_constraints(struct):
1658+
reason = fails_species_constraints(struct)
16591659
raise ForbiddenStructureException(
16601660
"Species constraints forbids product species {0}. Please "
16611661
"reformulate constraints, or explicitly "

rmgpy/rmg/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,11 @@ def initialize(self, **kwargs):
689689
"Input species {0} is globally forbidden. You may explicitly "
690690
"allow it by adding 'input species' to the `generatedSpeciesConstraints` `allowed` list.".format(spec.label)
691691
)
692-
failed, reason = fails_species_constraints(spec)
693-
if failed:
692+
if fails_species_constraints(spec):
694693
if "allowed" in self.species_constraints and "input species" in self.species_constraints["allowed"]:
695694
self.species_constraints["explicitlyAllowedMolecules"].append(spec.molecule[0])
696695
else:
696+
reason = fails_species_constraints(spec)
697697
raise ForbiddenStructureException(
698698
"Species constraints forbids input species {0}. Please "
699699
"reformulate constraints, remove the species, or explicitly "

rmgpy/rmg/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,11 +1673,11 @@ def add_seed_mechanism_to_core(self, seed_mechanism, react=False, requires_rms=F
16731673
"found in a seed mechanism or reaction "
16741674
"library.".format(spec.label, seed_mechanism.label)
16751675
)
1676-
failed, reason = fails_species_constraints(spec)
1677-
if failed:
1676+
if fails_species_constraints(spec):
16781677
if "allowed" in rmg.species_constraints and "seed mechanisms" in rmg.species_constraints["allowed"]:
16791678
rmg.species_constraints["explicitlyAllowedMolecules"].extend(spec.molecule)
16801679
else:
1680+
reason = fails_species_constraints(spec)
16811681
raise ForbiddenStructureException(
16821682
"Species constraints forbids species {0} from seed mechanism {1}."
16831683
" Please reformulate constraints, remove the species, or"
@@ -1801,11 +1801,11 @@ def add_reaction_library_to_edge(self, reaction_library, requires_rms=False):
18011801
"inert unless found in a seed mechanism or reaction "
18021802
"library.".format(spec.label, reaction_library.label)
18031803
)
1804-
failed, reason = fails_species_constraints(spec)
1805-
if failed:
1804+
if fails_species_constraints(spec):
18061805
if "allowed" in rmg.species_constraints and "reaction libraries" in rmg.species_constraints["allowed"]:
18071806
rmg.species_constraints["explicitlyAllowedMolecules"].extend(spec.molecule)
18081807
else:
1808+
reason = fails_species_constraints(spec)
18091809
raise ForbiddenStructureException(
18101810
"Species constraints forbids species {0} from reaction library "
18111811
"{1}. Please reformulate constraints, remove the species, or "

test/rmgpy/constraintsTest.py

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def test_constraints_not_loaded(self, mock_logging):
8383
rmgpy.rmg.input.rmg = None
8484

8585
mol = Molecule(smiles="C")
86-
failed, _ = fails_species_constraints(mol)
87-
assert not failed
86+
assert not fails_species_constraints(mol)
8887

8988
mock_logging.debug.assert_called_with("Species constraints could not be found.")
9089

@@ -97,80 +96,67 @@ def test_species_input(self):
9796
"""
9897
spc = Species().from_smiles("C")
9998

100-
failed, _ = fails_species_constraints(spc)
101-
assert not failed
99+
assert not fails_species_constraints(spc)
102100

103101
def test_explicitly_allowed_molecules(self):
104102
"""
105103
Test that we can explicitly allow molecules in species constraints.
106104
"""
107105
mol = Molecule(smiles="CCCC")
108-
failed, _ = fails_species_constraints(mol)
109-
assert failed
106+
assert fails_species_constraints(mol)
110107

111108
self.rmg.species_constraints["explicitlyAllowedMolecules"] = [Molecule(smiles="CCCC")]
112-
failed, _ = fails_species_constraints(mol)
113-
assert not failed
109+
assert not fails_species_constraints(mol)
114110

115111
def test_carbon_constraint(self):
116112
"""
117113
Test that we can constrain the max number of carbon atoms.
118114
"""
119115
mol1 = Molecule(smiles="CC")
120-
failed, _ = fails_species_constraints(mol1)
121-
assert not failed
116+
assert not fails_species_constraints(mol1)
122117

123118
mol2 = Molecule(smiles="CCC")
124-
failed, _ = fails_species_constraints(mol2)
125-
assert failed
119+
assert fails_species_constraints(mol2)
126120

127121
def test_oxygen_constraint(self):
128122
"""
129123
Test that we can constrain the max number of oxygen atoms.
130124
"""
131125
mol1 = Molecule(smiles="C=O")
132-
failed, _ = fails_species_constraints(mol1)
133-
assert not failed
126+
assert not fails_species_constraints(mol1)
134127

135128
mol2 = Molecule(smiles="OC=O")
136-
failed, _ = fails_species_constraints(mol2)
137-
assert failed
129+
assert fails_species_constraints(mol2)
138130

139131
def test_nitrogen_constraint(self):
140132
"""
141133
Test that we can constrain the max number of nitrogen atoms.
142134
"""
143135
mol1 = Molecule(smiles="CN")
144-
failed, _ = fails_species_constraints(mol1)
145-
assert not failed
136+
assert not fails_species_constraints(mol1)
146137

147138
mol2 = Molecule(smiles="NCN")
148-
failed, _ = fails_species_constraints(mol2)
149-
assert failed
139+
assert fails_species_constraints(mol2)
150140

151141
def test_silicon_constraint(self):
152142
"""
153143
Test that we can constrain the max number of silicon atoms.
154144
"""
155145
mol1 = Molecule(smiles="[SiH4]")
156-
failed, _ = fails_species_constraints(mol1)
157-
assert not failed
146+
assert not fails_species_constraints(mol1)
158147

159148
mol2 = Molecule(smiles="[SiH3][SiH3]")
160-
failed, _ = fails_species_constraints(mol2)
161-
assert failed
149+
assert fails_species_constraints(mol2)
162150

163151
def test_sulfur_constraint(self):
164152
"""
165153
Test that we can constrain the max number of sulfur atoms.
166154
"""
167155
mol1 = Molecule(smiles="CS")
168-
failed, _ = fails_species_constraints(mol1)
169-
assert not failed
156+
assert not fails_species_constraints(mol1)
170157

171158
mol2 = Molecule(smiles="SCS")
172-
failed, _ = fails_species_constraints(mol2)
173-
assert failed
159+
assert fails_species_constraints(mol2)
174160

175161
def test_surface_site_constraint(self):
176162
"""
@@ -222,17 +208,13 @@ def test_surface_site_constraint(self):
222208
self.rmg.species_constraints["maximumCarbonAtoms"] = 3
223209
self.rmg.species_constraints["maximumHeavyAtoms"] = 6
224210

225-
failed, _ = fails_species_constraints(mol_1site)
226-
assert not failed
211+
assert not fails_species_constraints(mol_1site)
227212

228-
failed, _ = fails_species_constraints(mol_2site)
229-
assert not failed
213+
assert not fails_species_constraints(mol_2site)
230214

231-
failed, _ = fails_species_constraints(mol_3site_vdW)
232-
assert failed
215+
assert fails_species_constraints(mol_3site_vdW)
233216

234-
failed, _ = fails_species_constraints(mol_3site)
235-
assert failed
217+
assert fails_species_constraints(mol_3site)
236218

237219
self.rmg.species_constraints["maximumCarbonAtoms"] = max_carbon
238220
self.rmg.species_constraints["maximumHeavyAtoms"] = max_heavy_atoms
@@ -247,32 +229,27 @@ def test_surface_bond_order_constraint(self):
247229
2 X u0 p0 c0 {1,Q}
248230
"""
249231
)
250-
failed, _ = fails_species_constraints(mol_1site)
251-
assert failed
232+
assert fails_species_constraints(mol_1site)
252233

253234
def test_heavy_constraint(self):
254235
"""
255236
Test that we can constrain the max number of heavy atoms.
256237
"""
257238
mol1 = Molecule(smiles="CCO")
258-
failed, _ = fails_species_constraints(mol1)
259-
assert not failed
239+
assert not fails_species_constraints(mol1)
260240

261241
mol2 = Molecule(smiles="CCN=O")
262-
failed, _ = fails_species_constraints(mol2)
263-
assert failed
242+
assert fails_species_constraints(mol2)
264243

265244
def test_radical_constraint(self):
266245
"""
267246
Test that we can constrain the max number of radical electrons.
268247
"""
269248
mol1 = Molecule(smiles="[CH2][CH2]")
270-
failed, _ = fails_species_constraints(mol1)
271-
assert not failed
249+
assert not fails_species_constraints(mol1)
272250

273251
mol2 = Molecule(smiles="[CH2][CH][CH2]")
274-
failed, _ = fails_species_constraints(mol2)
275-
assert failed
252+
assert fails_species_constraints(mol2)
276253

277254
def test_carbene_constraint(self):
278255
"""
@@ -285,8 +262,7 @@ def test_carbene_constraint(self):
285262
3 H u0 p0 c0 {1,S}
286263
"""
287264
)
288-
failed, _ = fails_species_constraints(mol1)
289-
assert not failed
265+
assert not fails_species_constraints(mol1)
290266

291267
mol2 = Molecule().from_adjacency_list(
292268
"""
@@ -296,8 +272,7 @@ def test_carbene_constraint(self):
296272
4 H u0 p0 c0 {3,S}
297273
"""
298274
)
299-
failed, _ = fails_species_constraints(mol2)
300-
assert failed
275+
assert fails_species_constraints(mol2)
301276

302277
def test_carbene_radical_constraint(self):
303278
"""
@@ -310,8 +285,7 @@ def test_carbene_radical_constraint(self):
310285
3 H u0 p0 c0 {1,S}
311286
"""
312287
)
313-
failed, _ = fails_species_constraints(mol1)
314-
assert not failed
288+
assert not fails_species_constraints(mol1)
315289

316290
mol2 = Molecule().from_adjacency_list(
317291
"""
@@ -322,5 +296,4 @@ def test_carbene_radical_constraint(self):
322296
5 H u0 p0 c0 {3,S}
323297
"""
324298
)
325-
failed, _ = fails_species_constraints(mol2)
326-
assert failed
299+
assert fails_species_constraints(mol2)

0 commit comments

Comments
 (0)