ff34a44661a34a99b4ba1366a631a3b3c5926990
[sawsim.git] / pysawsim / parameter_scan.py
1 # Copyright (C) 2009-2010  W. Trevor King <wking@drexel.edu>
2 #
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.
7 #
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.
12 #
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/>.
15 #
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.
19
20 """Experiment vs. simulation comparison and scanning.
21 """
22
23 from os import getpid  # for rss()
24 import os.path
25 import pickle
26 from StringIO import StringIO
27
28 import matplotlib
29 matplotlib.use('Agg')  # select backend that doesn't require X Windows
30 import numpy
31 import pylab
32
33 from . import log
34 from .histogram import Histogram
35 from .sawsim_histogram import sawsim_histogram
36 from .sawsim import SawsimRunner
37
38
39 _multiprocess_can_split_ = True
40 """Allow nosetests to split tests between processes.
41 """
42
43 FIGURE = pylab.figure()  # avoid memory problems.
44 """`pylab` keeps internal references to all created figures, so share
45 a single instance.
46 """
47 EXAMPLE_HISTOGRAM_FILE_CONTENTS = """# Velocity histograms
48 # Other general comments...
49
50 #HISTOGRAM: -v 6e-7
51 #Force (N)\tUnfolding events
52 1.4e-10\t1
53 1.5e-10\t0
54 1.6e-10\t4
55 1.7e-10\t6
56 1.8e-10\t8
57 1.9e-10\t20
58 2e-10\t28
59 2.1e-10\t38
60 2.2e-10\t72
61 2.3e-10\t110
62 2.4e-10\t155
63 2.5e-10\t247
64 2.6e-10\t395
65 2.7e-10\t451
66 2.8e-10\t430
67 2.9e-10\t300
68 3e-10\t116
69 3.1e-10\t18
70 3.2e-10\t1
71
72 #HISTOGRAM: -v 8e-7
73 #Force (N)\tUnfolding events
74 8e-11\t1
75 9e-11\t0
76 1e-10\t0
77 1.1e-10\t1
78 1.2e-10\t0
79 1.3e-10\t0
80 1.4e-10\t0
81 1.5e-10\t3
82 1.6e-10\t3
83 1.7e-10\t4
84 1.8e-10\t4
85 1.9e-10\t13
86 2e-10\t29
87 2.1e-10\t39
88 2.2e-10\t60
89 2.3e-10\t102
90 2.4e-10\t154
91 2.5e-10\t262
92 2.6e-10\t402
93 2.7e-10\t497
94 2.8e-10\t541
95 2.9e-10\t555
96 3e-10\t325
97 3.1e-10\t142
98 3.2e-10\t50
99 3.3e-10\t13
100
101 #HISTOGRAM: -v 1e-6
102 #Force (N)\tUnfolding events
103 1.5e-10\t2
104 1.6e-10\t3
105 1.7e-10\t7
106 1.8e-10\t8
107 1.9e-10\t7
108 2e-10\t25
109 2.1e-10\t30
110 2.2e-10\t58
111 2.3e-10\t76
112 2.4e-10\t159
113 2.5e-10\t216
114 2.6e-10\t313
115 2.7e-10\t451
116 2.8e-10\t568
117 2.9e-10\t533
118 3e-10\t416
119 3.1e-10\t222
120 3.2e-10\t80
121 3.3e-10\t24
122 3.4e-10\t2
123 """
124
125
126 MEM_DEBUG = False
127
128
129
130 def rss():
131     """
132     For debugging memory usage.
133
134     resident set size, the non-swapped physical memory that a task has
135     used (in kilo-bytes).
136     """
137     call = "ps -o rss= -p %d" % getpid()
138     status,stdout,stderr = invoke(call)
139     return int(stdout)
140
141
142 class HistogramMatcher (object):
143     """Compare experimental histograms to simulated data.
144
145     The main entry points are `fit()` and `plot()`.
146
147     The input `histogram_stream` should contain a series of
148     experimental histograms with '#HISTOGRAM: <params>` lines starting
149     each histogram.  `<params>` lists the `sawsim` parameters that are
150     unique to that experiment.
151
152     >>> from .manager.thread import ThreadManager
153     >>> histogram_stream = StringIO(EXAMPLE_HISTOGRAM_FILE_CONTENTS)
154     >>> param_format_string = (
155     ...     '-s cantilever,hooke,0.05 -N1 '
156     ...     '-s folded,null -N8 '
157     ...     '-s "unfolded,wlc,{0.39e-9,28e-9}" '
158     ...     '-k "folded,unfolded,bell,{%g,%g}" -q folded')
159     >>> m = ThreadManager()
160     >>> sr = SawsimRunner(sawsim='bin/sawsim', manager=m)
161     >>> hm = HistogramMatcher(histogram_stream, param_format_string, sr, N=3)
162     >>> hm.plot([[1e-5,1e-3,3],[0.1e-9,1e-9,3]], logx=True, logy=False)
163     >>> m.teardown()
164     """
165     def __init__(self, histogram_stream, param_format_string,
166                  sawsim_runner, N=400, residual_type='jensen-shannon',
167                  plot=False):
168         self.experiment_histograms = self._read_force_histograms(
169             histogram_stream)
170         self.param_format_string = param_format_string
171         self.sawsim_runner = sawsim_runner
172         self.N = N
173         self.residual_type = residual_type
174         self._plot = plot
175
176     def _read_force_histograms(self, stream):
177         """
178         File format:
179
180           # comment and blank lines ignored
181           #HISTOGRAM: <histogram-specific params>
182           <pysawsim.histogram.Histogram-compatible histogram>
183           #HISTOGRAM: <other histogram-specific params>
184           <another pysawsim.histogram.Histogram-compatible histogram>
185           ...
186
187         >>> import sys
188         >>> stream = StringIO(EXAMPLE_HISTOGRAM_FILE_CONTENTS)
189         >>> hm = HistogramMatcher(StringIO(), None, None, None)
190         >>> histograms = hm._read_force_histograms(stream)
191         >>> sorted(histograms.iterkeys())
192         ['-v 1e-6', '-v 6e-7', '-v 8e-7']
193         >>> histograms['-v 1e-6'].to_stream(sys.stdout)
194         ... # doctest: +NORMALIZE_WHITESPACE, +REPORT_UDIFF
195         #Force (N)\tUnfolding events
196         1.5e-10\t2
197         1.6e-10\t3
198         1.7e-10\t7
199         1.8e-10\t8
200         1.9e-10\t7
201         2e-10\t25
202         2.1e-10\t30
203         2.2e-10\t58
204         2.3e-10\t76
205         2.4e-10\t159
206         2.5e-10\t216
207         2.6e-10\t313
208         2.7e-10\t451
209         2.8e-10\t568
210         2.9e-10\t533
211         3e-10\t416
212         3.1e-10\t222
213         3.2e-10\t80
214         3.3e-10\t24
215         3.4e-10\t2
216         """
217         token = '#HISTOGRAM:'
218         hist_blocks = {None: []}
219         params = None
220         for line in stream.readlines():
221             line = line.strip()
222             if line.startswith(token):
223                 params = line[len(token):].strip()
224                 assert params not in hist_blocks, params
225                 hist_blocks[params] = []
226             else:
227                 hist_blocks[params].append(line)
228
229         histograms = {}
230         for params,block in hist_blocks.iteritems():
231             if params == None:
232                 continue
233             h = Histogram()
234             h.from_stream(StringIO('\n'.join(block)))
235             histograms[params] = h
236         return histograms
237
238     def param_string(self, params, hist_params):
239         """Generate a string of options to pass to `sawsim`.
240         """
241         return '%s %s' % (
242             self.param_format_string % tuple(params), hist_params)
243
244     def residual(self, params):
245         residual = 0
246         for hist_params,experiment_hist in self.experiment_histograms.iteritems():
247             sawsim_hist = sawsim_histogram(
248                 sawsim_runner=self.sawsim_runner,
249                 param_string=self.param_string(params, hist_params),
250                 N=self.N, bin_edges=experiment_hist.bin_edges)
251             r = experiment_hist.residual(sawsim_hist, type=self.residual_type)
252             residual += r
253             if self._plot == True:
254                 title = ", ".join(["%g" % p for p in params]+[hist_params])
255                 filename = "residual-%s-%g.png" % (
256                     title.replace(', ', '_').replace(' ', '_'), r)
257                 self._plot_residual_comparison(
258                     experiment_hist, sawsim_hist, residual=r,
259                     title=title, filename=filename)
260         log().debug('residual %s: %g' % (params, residual))
261         return residual
262
263     def plot(self, param_ranges, logx=False, logy=False, contour=False):
264         xranges = param_ranges[0]
265         yranges = param_ranges[1]
266         if logx == False:
267             x = numpy.linspace(*xranges)
268         else:
269             m,M,n = xranges
270             x = numpy.exp(numpy.linspace(numpy.log(m), numpy.log(M), n))
271         if logy == False:
272             y = numpy.linspace(*yranges)
273         else:
274             m,M,n = yranges
275             y = numpy.exp(numpy.linspace(numpy.log(m), numpy.log(M), n))
276         X, Y = pylab.meshgrid(x,y)
277         C = numpy.zeros((len(y)-1, len(x)-1))
278         for i,xi in enumerate(x[:-1]):
279             for j,yj in enumerate(y[:-1]):
280                 log().info('point %d %d (%d of %d)'
281                            % (i, j, i*(len(y)-1) + j, (len(x)-1)*(len(y)-1)))
282                 params = (xi,yj)
283                 r = self.residual(params)
284                 C[j,i] = numpy.log(r) # better resolution in valleys
285                 if MEM_DEBUG == True:
286                     log().debug('RSS: %d KB' % rss())
287         C = numpy.nan_to_num(C) # NaN -> 0
288         fid = file("histogram_matcher-XYC.pkl", "wb")
289         pickle.dump([X,Y,C], fid)
290         fid.close()
291         # read in with
292         # import pickle
293         # [X,Y,C] = pickle.load(file("histogram_matcher-XYC.pkl", "rb"))
294         # ...
295         FIGURE.clear()
296         axes = FIGURE.add_subplot(111)
297         if logx == True:
298             axes.set_xscale('log')
299         if logy == True:
300             axes.set_yscale('log')
301         if contour == True:
302             p = axes.contour(X[:-1,:-1], Y[:-1,:-1], C)
303             # [:-1,:-1] to strip dummy last row & column from X&Y.
304         else: # pseudocolor plot
305             p = axes.pcolor(X, Y, C)
306             axes.autoscale_view(tight=True)
307         FIGURE.colorbar(p)
308         FIGURE.savefig("figure.png")
309
310     def _plot_residual_comparison(self, experiment_hist, theory_hist,
311                                   residual, title, filename):
312         FIGURE.clear()
313         p = pylab.plot(experiment_hist.bin_edges[:-1],
314                        experiment_hist.probabilities, 'r-',
315                        theory_hist.bin_edges[:-1],
316                        theory_hist.probabilities, 'b-')
317         pylab.title(title)
318         FIGURE.savefig(filename)
319
320
321 def parse_param_ranges_string(string):
322     """Parse parameter range stings.
323
324     '[Amin,Amax,Asteps],[Bmin,Bmax,Bsteps],...'
325       ->
326     [[Amin,Amax,Asteps],[Bmin,Bmax,Bsteps],...]
327
328     >>> parse_param_ranges_string('[1,2,3],[4,5,6]')
329     [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
330     >>> parse_param_ranges_string('[1,2,3]')
331     [[1.0, 2.0, 3.0]]
332     """
333     ranges = []
334     for range_string in string.split("],["):
335         range_number_strings = range_string.strip("[]").split(",")
336         ranges.append([float(x) for x in range_number_strings])
337     return ranges
338
339
340 def main(argv=None):
341     """
342     >>> import tempfile
343     >>> f = tempfile.NamedTemporaryFile()
344     >>> f.write(EXAMPLE_HISTOGRAM_FILE_CONTENTS)
345     >>> f.flush()
346     >>> main(['-s', 'bin/sawsim',
347     ...       '-r', '[1e-5,1e-3,3],[0.1e-9,1e-9,3]',
348     ...       '-N', '2',
349     ...       f.name])
350     >>> f.close()
351     """
352     from optparse import OptionParser
353     import sys
354
355     if argv == None:
356         argv = sys.argv[1:]
357
358     sr = SawsimRunner()
359
360     usage = '%prog [options] histogram_file'
361     epilog = '\n'.join([
362             'Compare simulated results against experimental values over a',
363             'range of parameters.  Generates a plot of fit quality over',
364             'the parameter space.  The histogram file should look something',
365             'like:',
366             '',
367             EXAMPLE_HISTOGRAM_FILE_CONTENTS,
368             ''
369             '`#HISTOGRAM: <params>` lines start each histogram.  `params`',
370             'lists the `sawsim` parameters that are unique to that',
371             'experiment.',
372             '',
373             'Each histogram line is of the format:',
374             '',
375             '<bin_edge><whitespace><count>',
376             '',
377             '`<bin_edge>` should mark the left-hand side of the bin, and',
378             'all bins should be of equal width (so we know where the last',
379             'one ends).',
380             ])
381     parser = OptionParser(usage, epilog=epilog)
382     parser.format_epilog = lambda formatter: epilog+'\n'
383     for option in sr.optparse_options:
384         if option.dest == 'param_string':
385             continue
386         parser.add_option(option)
387     parser.add_option('-f','--param-format', dest='param_format',
388                       metavar='FORMAT',
389                       help='Convert params to sawsim options (%default).',
390                       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'))
391     parser.add_option('-p','--initial-params', dest='initial_params',
392                       metavar='PARAMS',
393                       help='Initial params for fitting (%default).',
394                       default='3.3e-4,0.25e-9')
395     parser.add_option('-r','--param-range', dest='param_range',
396                       metavar='PARAMS',
397                       help='Param range for plotting (%default).',
398                       default='[1e-5,1e-3,20],[0.1e-9,1e-9,20]')
399     parser.add_option('--logx', dest='logx',
400                       help='Use a log scale for the x range.',
401                       default=False, action='store_true')
402     parser.add_option('--logy', dest='logy',
403                       help='Use a log scale for the y range.',
404                       default=False, action='store_true')
405     parser.add_option('-R','--residual', dest='residual',
406                       metavar='STRING',
407                       help='Residual type (from %s; default: %%default).'
408                       % ', '.join(Histogram().types()),
409                       default='jensen-shannon')
410     parser.add_option('-P','--plot-residuals', dest='plot_residuals',
411                       help='Generate residual difference plots for each point in the plot range.',
412                       default=False, action='store_true')
413     parser.add_option('-c','--contour-plot', dest='contour_plot',
414                       help='Select contour plot (vs. the default pseudocolor plot).',
415                       default=False, action='store_true')
416
417     options,args = parser.parse_args(argv)
418
419     initial_params = [float(p) for p in options.initial_params.split(",")]
420     param_ranges = parse_param_ranges_string(options.param_range)
421     histogram_file = args[0]
422     sr_call_params = sr.initialize_from_options(options)
423
424     try:
425         hm = HistogramMatcher(
426             file(histogram_file, 'r'),
427             param_format_string=options.param_format,
428             sawsim_runner=sr, residual_type=options.residual,
429             plot=options.plot_residuals, **sr_call_params)
430         #hm.fit(initial_params)
431         hm.plot(param_ranges, logx=options.logx, logy=options.logy,
432                 contour=options.contour_plot)
433     finally:
434         sr.teardown()