Skip to content

Commit c47a198

Browse files
committed
Fix compareNIST function
Raise errors if depository isn't found. Remove averaging and adding of training reactions in the family, assumes you have done it outside the function. Compares reverse kinetics if the NIST reaction is in the reverse direction from the family template
1 parent 2984803 commit c47a198

1 file changed

Lines changed: 44 additions & 29 deletions

File tree

testing/evaluateKinetics.py

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import math
1010
import numpy
1111
import matplotlib
12-
matplotlib.rc('mathtext', fontset='stixsans', default='regular')
13-
import matplotlib.pyplot as plt
14-
import pylab
12+
matplotlib.use('Agg')
13+
from matplotlib import pyplot as plt
1514
import re
1615
import copy
1716
import csv
@@ -28,15 +27,22 @@
2827
from rmgpy.data.rmg import RMGDatabase
2928
from rmgpy.data.kinetics.common import UndeterminableKineticsError
3029

31-
32-
def getKineticsDepository(family, depositoryLabel):
30+
def getKineticsDepository(FullDatabase, family, depositoryLabel):
31+
"""
32+
Retrieve the NIST exact and approximated kinetics for those same reactions from RMG.
33+
Note: does NOT average up the database or create any rate rules from training data.
34+
If that is desired it must be done prior to entering this function.
35+
"""
3336

3437
depository = None
3538
for tempDepository in family.depositories:
3639
if re.search(depositoryLabel, tempDepository.label):
3740
depository=tempDepository
41+
break
42+
else:
43+
print 'Depository {} not found in {} family.'.format(depositoryLabel, family.label)
44+
return
3845

39-
family.fillKineticsRulesByAveragingUp()
4046

4147
exactKinetics={}
4248
approxKinetics={}
@@ -47,9 +53,25 @@ def getKineticsDepository(family, depositoryLabel):
4753
template=family.getReactionTemplate(reaction)
4854
exactKinetics[key]=entry.data
4955
approxKinetics[key]=family.rules.estimateKinetics(template)[0]
50-
except Exception as e:
51-
print entry.item
52-
print e
56+
except UndeterminableKineticsError:
57+
# See if the reaction was written in the reverse direction
58+
reaction = Reaction(reactants = copy.deepcopy(entry.item.products),
59+
products = copy.deepcopy(entry.item.reactants),
60+
kinetics = copy.deepcopy(entry.data)
61+
)
62+
63+
template=family.getReactionTemplate(reaction)
64+
65+
# Getting thermo data erases the atomLabels, so do this after finding the template
66+
# But we need it for setting the reverse kinetics
67+
for spec in reaction.reactants + reaction.products:
68+
spec.getThermoData()
69+
70+
reverseKinetics = reaction.generateReverseRateCoefficient()
71+
reaction.kinetics = reverseKinetics
72+
73+
exactKinetics[key]=reaction.kinetics
74+
approxKinetics[key]=family.rules.estimateKinetics(template)[0]
5375

5476
return exactKinetics, approxKinetics
5577

@@ -59,17 +81,11 @@ def getKineticsLeaveOneOut(family):
5981
the original exact nodes and a dictionary of the new averaged nodes.
6082
The returned dictionary entries will be of a KineticModel class
6183
"""
62-
63-
entryKeys=family.rules.entries.keys()
64-
6584
exactKinetics={}
6685
approxKinetics={}
67-
index=0
68-
for entryKey in entryKeys:
69-
index+=1
70-
71-
nodes = entryKey.split(';')
72-
template = [family.groups.entries[node] for node in nodes]
86+
87+
for entryKey in family.rules.entries.keys():
88+
template = family.retrieveTemplate(entryKey)
7389
exactKinetics[entryKey], exactKineticsEntry=family.rules.estimateKinetics(template)
7490

7591
familyCopy=copy.deepcopy(family)
@@ -123,7 +139,6 @@ def calculateQ(parityData):
123139

124140
def createParityPlot(parityData):
125141

126-
#unpack the data
127142
keyList=parityData.keys()
128143
xAxis=[]
129144
yAxis=[]
@@ -194,19 +209,19 @@ def countNodesAll(FullDatabase, trialDir):
194209
csvwriter.writerow(value)
195210

196211

197-
def NISTExact(FullDatabase, trialDir):
198-
199-
trialDir=os.path.join(trialDir, 'NISTExact')
212+
def compareNIST(FullDatabase, trialDir):
213+
"""
214+
Compare NIST reaction kinetics with estimates from RMG. Creates parity plot and
215+
calculates the predicted root mean square error from the families.
216+
Note: does NOT average up the database or create any rate rules from training data.
217+
If that is desired it must be done prior to entering this function.
218+
"""
219+
trialDir=os.path.join(trialDir, 'compareNIST')
200220
if not os.path.exists(trialDir):
201221
os.makedirs(trialDir)
202222

203-
for family in FullDatabase.kinetics.families.values():
204-
family.addKineticsRulesFromTrainingSet(thermoDatabase=FullDatabase.thermo)
205-
206223

207224
allFamilyNames=FullDatabase.kinetics.families.keys()
208-
# familyName='Disproportionation'
209-
# allFamilyNames=[familyName]
210225

211226
QDict={}
212227

@@ -216,7 +231,7 @@ def NISTExact(FullDatabase, trialDir):
216231
if len(family.rules.entries) < 2:
217232
print ' Skipping', familyName, ': only has one node...'
218233
else:
219-
exactKinetics, approxKinetics =getKineticsDepository(family, 'NIST')
234+
exactKinetics, approxKinetics = getKineticsDepository(FullDatabase, family, 'NIST')
220235

221236
#prune for exact matches only
222237
keysToRemove=[]
@@ -322,7 +337,7 @@ def leaveOneOut(FullDatabase, trialDir):
322337
if not os.path.exists(trialDir):
323338
os.makedirs(trialDir)
324339
print 'Evaluating the NIST Kinetics against the RMG estimates...'
325-
NISTExact(FullDatabase, trialDir)
340+
compareNIST(FullDatabase, trialDir)
326341

327342
print 'Counting the rate rules in the families...'
328343
countNodesAll(FullDatabase, trialDir)

0 commit comments

Comments
 (0)