1 # Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # The author may be contacted at <wking@drexel.edu> on the Internet, or
17 # write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
18 # Philadelphia PA 19104, USA.
20 """Experiment vs. simulation comparison and scanning.
23 from os import getpid # for rss()
26 from StringIO import StringIO
29 matplotlib.use('Agg') # select backend that doesn't require X Windows
34 from .histogram import Histogram
35 from .sawsim_histogram import sawsim_histogram
36 from .sawsim import SawsimRunner
39 FIGURE = pylab.figure() # avoid memory problems.
40 """`pylab` keeps internal references to all created figures, so share
43 EXAMPLE_HISTOGRAM_FILE_CONTENTS = """# Velocity histograms
44 # Other general comments...
47 #Force (N)\tUnfolding events
69 #Force (N)\tUnfolding events
98 #Force (N)\tUnfolding events
128 For debugging memory usage.
130 resident set size, the non-swapped physical memory that a task has
131 used (in kilo-bytes).
133 call = "ps -o rss= -p %d" % getpid()
134 status,stdout,stderr = invoke(call)
138 class HistogramMatcher (object):
139 """Compare experimental histograms to simulated data.
141 The main entry points are `fit()` and `plot()`.
143 The input `histogram_stream` should contain a series of
144 experimental histograms with '#HISTOGRAM: <params>` lines starting
145 each histogram. `<params>` lists the `sawsim` parameters that are
146 unique to that experiment.
148 >>> from .manager.thread import ThreadManager
149 >>> histogram_stream = StringIO(EXAMPLE_HISTOGRAM_FILE_CONTENTS)
150 >>> param_format_string = (
151 ... '-s cantilever,hooke,0.05 -N1 '
152 ... '-s folded,null -N8 '
153 ... '-s "unfolded,wlc,{0.39e-9,28e-9}" '
154 ... '-k "folded,unfolded,bell,{%g,%g}" -q folded')
155 >>> m = ThreadManager()
156 >>> sr = SawsimRunner(sawsim='bin/sawsim', manager=m)
157 >>> hm = HistogramMatcher(histogram_stream, param_format_string, sr, N=3)
158 >>> hm.plot([[1e-5,1e-3,3],[0.1e-9,1e-9,3]], logx=True, logy=False)
161 def __init__(self, histogram_stream, param_format_string,
162 sawsim_runner, N=400, residual_type='jensen-shannon',
164 self.experiment_histograms = self._read_force_histograms(
166 self.param_format_string = param_format_string
167 self.sawsim_runner = sawsim_runner
169 self.residual_type = residual_type
172 def _read_force_histograms(self, stream):
176 # comment and blank lines ignored
177 #HISTOGRAM: <histogram-specific params>
178 <pysawsim.histogram.Histogram-compatible histogram>
179 #HISTOGRAM: <other histogram-specific params>
180 <another pysawsim.histogram.Histogram-compatible histogram>
184 >>> stream = StringIO(EXAMPLE_HISTOGRAM_FILE_CONTENTS)
185 >>> hm = HistogramMatcher(StringIO(), None, None, None)
186 >>> histograms = hm._read_force_histograms(stream)
187 >>> sorted(histograms.iterkeys())
188 ['-v 1e-6', '-v 6e-7', '-v 8e-7']
189 >>> histograms['-v 1e-6'].to_stream(sys.stdout)
190 ... # doctest: +NORMALIZE_WHITESPACE, +REPORT_UDIFF
191 #Force (N)\tUnfolding events
213 token = '#HISTOGRAM:'
214 hist_blocks = {None: []}
216 for line in stream.readlines():
218 if line.startswith(token):
219 params = line[len(token):].strip()
220 assert params not in hist_blocks, params
221 hist_blocks[params] = []
223 hist_blocks[params].append(line)
226 for params,block in hist_blocks.iteritems():
230 h.from_stream(StringIO('\n'.join(block)))
231 histograms[params] = h
234 def param_string(self, params, hist_params):
235 """Generate a string of options to pass to `sawsim`.
238 self.param_format_string % tuple(params), hist_params)
240 def residual(self, params):
242 for hist_params,experiment_hist in self.experiment_histograms.iteritems():
243 sawsim_hist = sawsim_histogram(
244 sawsim_runner=self.sawsim_runner,
245 param_string=self.param_string(params, hist_params),
246 N=self.N, bin_edges=experiment_hist.bin_edges)
247 r = experiment_hist.residual(sawsim_hist, type=self.residual_type)
249 if self._plot == True:
250 title = ", ".join(["%g" % p for p in params]+[hist_params])
251 filename = "residual-%s-%g.png" % (
252 title.replace(', ', '_').replace(' ', '_'), r)
253 self._plot_residual_comparison(
254 experiment_hist, sawsim_hist, residual=r,
255 title=title, filename=filename)
256 log().debug('residual %s: %g' % (params, residual))
259 def plot(self, param_ranges, logx=False, logy=False, contour=False):
260 xranges = param_ranges[0]
261 yranges = param_ranges[1]
263 x = numpy.linspace(*xranges)
266 x = numpy.exp(numpy.linspace(numpy.log(m), numpy.log(M), n))
268 y = numpy.linspace(*yranges)
271 y = numpy.exp(numpy.linspace(numpy.log(m), numpy.log(M), n))
272 X, Y = pylab.meshgrid(x,y)
273 C = numpy.zeros((len(y)-1, len(x)-1))
274 for i,xi in enumerate(x[:-1]):
275 for j,yj in enumerate(y[:-1]):
276 log().info('point %d %d (%d of %d)'
277 % (i, j, i*(len(y)-1) + j, (len(x)-1)*(len(y)-1)))
279 r = self.residual(params)
280 C[j,i] = numpy.log(r) # better resolution in valleys
281 if MEM_DEBUG == True:
282 log().debug('RSS: %d KB' % rss())
283 C = numpy.nan_to_num(C) # NaN -> 0
284 fid = file("histogram_matcher-XYC.pkl", "wb")
285 pickle.dump([X,Y,C], fid)
289 # [X,Y,C] = pickle.load(file("histogram_matcher-XYC.pkl", "rb"))
292 axes = FIGURE.add_subplot(111)
294 axes.set_xscale('log')
296 axes.set_yscale('log')
298 p = axes.contour(X[:-1,:-1], Y[:-1,:-1], C)
299 # [:-1,:-1] to strip dummy last row & column from X&Y.
300 else: # pseudocolor plot
301 p = axes.pcolor(X, Y, C)
302 axes.autoscale_view(tight=True)
304 FIGURE.savefig("figure.png")
306 def _plot_residual_comparison(self, experiment_hist, theory_hist,
307 residual, title, filename):
309 p = pylab.plot(experiment_hist.bin_edges[:-1],
310 experiment_hist.probabilities, 'r-',
311 theory_hist.bin_edges[:-1],
312 theory_hist.probabilities, 'b-')
314 FIGURE.savefig(filename)
317 def parse_param_ranges_string(string):
318 """Parse parameter range stings.
320 '[Amin,Amax,Asteps],[Bmin,Bmax,Bsteps],...'
322 [[Amin,Amax,Asteps],[Bmin,Bmax,Bsteps],...]
324 >>> parse_param_ranges_string('[1,2,3],[4,5,6]')
325 [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
326 >>> parse_param_ranges_string('[1,2,3]')
330 for range_string in string.split("],["):
331 range_number_strings = range_string.strip("[]").split(",")
332 ranges.append([float(x) for x in range_number_strings])
339 >>> f = tempfile.NamedTemporaryFile()
340 >>> f.write(EXAMPLE_HISTOGRAM_FILE_CONTENTS)
342 >>> main(['-s', 'bin/sawsim',
343 ... '-r', '[1e-5,1e-3,3],[0.1e-9,1e-9,3]',
348 from optparse import OptionParser
356 usage = '%prog [options] histogram_file'
358 'Compare simulated results against experimental values over a',
359 'range of parameters. Generates a plot of fit quality over',
360 'the parameter space. The histogram file should look something',
363 EXAMPLE_HISTOGRAM_FILE_CONTENTS,
365 '`#HISTOGRAM: <params>` lines start each histogram. `params`',
366 'lists the `sawsim` parameters that are unique to that',
369 'Each histogram line is of the format:',
371 '<bin_edge><whitespace><count>',
373 '`<bin_edge>` should mark the left-hand side of the bin, and',
374 'all bins should be of equal width (so we know where the last',
377 parser = OptionParser(usage, epilog=epilog)
378 parser.format_epilog = lambda formatter: epilog+'\n'
379 for option in sr.optparse_options:
380 if option.dest == 'param_string':
382 parser.add_option(option)
383 parser.add_option('-f','--param-format', dest='param_format',
385 help='Convert params to sawsim options (%default).',
386 default=('-s cantilever,hooke,0.05 -N1 -s folded,null -N8 -s "unfolded,wlc,{0.39e-9,28e-9}" -k "folded,unfolded,bell,{%g,%g}" -q folded'))
387 parser.add_option('-p','--initial-params', dest='initial_params',
389 help='Initial params for fitting (%default).',
390 default='3.3e-4,0.25e-9')
391 parser.add_option('-r','--param-range', dest='param_range',
393 help='Param range for plotting (%default).',
394 default='[1e-5,1e-3,20],[0.1e-9,1e-9,20]')
395 parser.add_option('--logx', dest='logx',
396 help='Use a log scale for the x range.',
397 default=False, action='store_true')
398 parser.add_option('--logy', dest='logy',
399 help='Use a log scale for the y range.',
400 default=False, action='store_true')
401 parser.add_option('-R','--residual', dest='residual',
403 help='Residual type (from %s; default: %%default).'
404 % ', '.join(Histogram().types()),
405 default='jensen-shannon')
406 parser.add_option('-P','--plot-residuals', dest='plot_residuals',
407 help='Generate residual difference plots for each point in the plot range.',
408 default=False, action='store_true')
409 parser.add_option('-c','--contour-plot', dest='contour_plot',
410 help='Select contour plot (vs. the default pseudocolor plot).',
411 default=False, action='store_true')
413 options,args = parser.parse_args(argv)
415 initial_params = [float(p) for p in options.initial_params.split(",")]
416 param_ranges = parse_param_ranges_string(options.param_range)
417 histogram_file = args[0]
418 sr_call_params = sr.initialize_from_options(options)
421 hm = HistogramMatcher(
422 file(histogram_file, 'r'),
423 param_format_string=options.param_format,
424 sawsim_runner=sr, residual_type=options.residual,
425 plot=options.plot_residuals, **sr_call_params)
426 #hm.fit(initial_params)
427 hm.plot(param_ranges, logx=options.logx, logy=options.logy,
428 contour=options.contour_plot)