forked from ReactionMechanismGenerator/RMG-Py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
214 lines (177 loc) · 8.74 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
###############################################################################
# #
# RMG - Reaction Mechanism Generator #
# #
# Copyright (c) 2002-2023 Prof. William H. Green ([email protected]), #
# Prof. Richard H. West ([email protected]) and the RMG Team ([email protected]) #
# #
# Permission is hereby granted, free of charge, to any person obtaining a #
# copy of this software and associated documentation files (the 'Software'), #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# #
###############################################################################
import logging
import os.path
import matplotlib.pyplot as plt
try:
import xlwt
except ImportError:
logging.warning('Optional package dependency "xlwt" not loaded. Some output features will not work.')
xlwt = None
from rmgpy.util import make_output_subdirectory
class ExecutionStatsWriter(object):
"""
This class listens to a RMG subject
and writes an excel file with the memory footprint
requirements through the course of an RMG simulation,
It also generates a number of images with information on the core/edge
species/reaction evolutions through the course of an RMG simulation.
Files are written to the 'plot' subfolder.
A new instance of the class can be appended to a subject as follows:
rmg = ...
listener = ExecutionStatsWriter()
rmg.attach(listener)
Whenever the subject calls the .notify() method, the
.update() method of the listener will be called.
To stop listening to the subject, the class can be detached
from its subject:
rmg.detach(listener)
"""
def __init__(self, outputDirectory):
super(ExecutionStatsWriter, self).__init__()
make_output_subdirectory(outputDirectory, 'plot')
# RMG execution statistics
self.coreSpeciesCount = []
self.coreReactionCount = []
self.edgeSpeciesCount = []
self.edgeReactionCount = []
self.memoryUse = []
def update(self, rmg):
self.update_execution(rmg)
def update_execution(self, rmg):
# Update RMG execution statistics
logging.info('Updating RMG execution statistics...')
core_spec, core_reac, edge_spec, edge_reac = rmg.reaction_model.get_model_size()
self.coreSpeciesCount.append(core_spec)
self.coreReactionCount.append(core_reac)
self.edgeSpeciesCount.append(edge_spec)
self.edgeReactionCount.append(edge_reac)
elapsed = rmg.exec_time[-1]
seconds = elapsed % 60
minutes = (elapsed - seconds) % 3600 / 60
hours = (elapsed - seconds - minutes * 60) % (3600 * 24) / 3600
days = (elapsed - seconds - minutes * 60 - hours * 3600) / (3600 * 24)
logging.info(' Execution time (DD:HH:MM:SS): '
'{0:02}:{1:02}:{2:02}:{3:02}'.format(int(days), int(hours), int(minutes), int(seconds)))
try:
import psutil
process = psutil.Process(os.getpid())
memory_info = process.memory_info()
self.memoryUse.append(memory_info.rss / 1.0e6)
logging.info(' Memory used: %.2f MB' % (self.memoryUse[-1]))
except:
logging.info(' Memory used: memory usage was unable to be logged')
self.memoryUse.append(0.0)
self.save_execution_statistics(rmg)
if rmg.generate_plots:
self.generate_execution_plots(rmg)
logging.info('')
def save_execution_statistics(self, rmg):
"""
Save the statistics of the RMG job to an Excel spreadsheet for easy viewing
after the run is complete. The statistics are saved to the file
`statistics.xls` in the output directory. The ``xlwt`` package is used to
create the spreadsheet file; if this package is not installed, no file is
saved.
"""
# Attempt to import the xlwt package; return if not installed
if xlwt is None:
logging.warning('Package xlwt not loaded. Unable to save execution statistics.')
return
# Create workbook and sheet for statistics to be places
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Statistics')
# First column is execution time
sheet.write(0, 0, 'Execution time (s)')
for i, etime in enumerate(rmg.exec_time):
sheet.write(i + 1, 0, etime)
# Second column is number of core species
sheet.write(0, 1, 'Core species')
for i, count in enumerate(self.coreSpeciesCount):
sheet.write(i + 1, 1, count)
# Third column is number of core reactions
sheet.write(0, 2, 'Core reactions')
for i, count in enumerate(self.coreReactionCount):
sheet.write(i + 1, 2, count)
# Fourth column is number of edge species
sheet.write(0, 3, 'Edge species')
for i, count in enumerate(self.edgeSpeciesCount):
sheet.write(i + 1, 3, count)
# Fifth column is number of edge reactions
sheet.write(0, 4, 'Edge reactions')
for i, count in enumerate(self.edgeReactionCount):
sheet.write(i + 1, 4, count)
# Sixth column is memory used
sheet.write(0, 5, 'Memory used (MB)')
for i, memory in enumerate(self.memoryUse):
sheet.write(i + 1, 5, memory)
# Save workbook to file
fstr = os.path.join(rmg.output_directory, 'statistics.xls')
workbook.save(fstr)
def generate_execution_plots(self, rmg):
"""
Generate a number of plots describing the statistics of the RMG job,
including the reaction model core and edge size and memory use versus
execution time. These will be placed in the output directory in the plot/
folder.
"""
logging.info('Generating plots of execution statistics...')
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.semilogx(rmg.exec_time, self.coreSpeciesCount, 'o-b')
ax1.set_xlabel('Execution time (s)')
ax1.set_ylabel('Number of core species')
ax2 = ax1.twinx()
ax2.semilogx(rmg.exec_time, self.coreReactionCount, 'o-r')
ax2.set_ylabel('Number of core reactions')
plt.savefig(os.path.join(rmg.output_directory, 'plot/coreSize.svg'))
plt.close()
fig = plt.figure()
ax1 = fig.add_subplot(111)
if any(self.edgeSpeciesCount):
ax1.loglog(rmg.exec_time, self.edgeSpeciesCount, 'o-b')
else:
ax1.semilogx(rmg.exec_time, self.edgeSpeciesCount, 'o-b')
ax1.set_xlabel('Execution time (s)')
ax1.set_ylabel('Number of edge species')
ax2 = ax1.twinx()
if any(self.edgeReactionCount):
ax2.loglog(rmg.exec_time, self.edgeReactionCount, 'o-r')
else:
ax2.semilogx(rmg.exec_time, self.edgeReactionCount, 'o-r')
ax2.set_ylabel('Number of edge reactions')
plt.savefig(os.path.join(rmg.output_directory, 'plot/edgeSize.svg'))
plt.close()
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.semilogx(rmg.exec_time, self.memoryUse, 'o-k')
ax1.set_xlabel('Execution time (s)')
ax1.set_ylabel('Memory (MB)')
ax1.legend(['RAM'], loc=2)
plt.savefig(os.path.join(rmg.output_directory, 'plot/memoryUse.svg'))
plt.close()