Add epilogs to each OptionParser in pysawsim.
[sawsim.git] / pysawsim / sawsim_histogram.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, Drudge's University, Physics Dept., 3141 Chestnut St.,
18 # Philadelphia PA 19104, USA.
19
20 from __future__ import with_statement
21
22 import numpy
23
24 from . import __version__, log
25 from .histogram import Histogram
26 from .manager import MANAGERS, get_manager
27 from .sawsim import SawsimRunner
28
29
30 def sawsim_histogram(sawsim_runner, param_string, N=400, bin_edges=None):
31         """Run `N` simulations and return a histogram with `bin_edges`.
32
33         If `bin_edges == None`, return a numpy array of all unfolding
34         forces.
35         """
36         events = []
37         for run in sawsim_runner(param_string=param_string, N=N):
38             events.extend([event.force for event in run
39                            if (event.initial_state == 'folded'
40                                and event.final_state == 'unfolded')])
41         events = numpy.array(events)
42         if bin_edges == None:
43             return events
44         h = Histogram()
45         h.from_data(events, bin_edges)
46         return h
47
48
49 def main(argv=None):
50     """
51     >>> main(['--sawsim', 'bin/sawsim', '-N', '2'])
52     ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
53     #Force (N)  Unfolding events
54     ...
55     """
56     from optparse import OptionParser
57     import sys
58
59     if argv == None:
60         argv = sys.argv[1:]
61
62     sr = SawsimRunner()
63
64     usage = '%prog [options]'
65     epilog = '\n'.join([
66             'Generate an unfolding force histogram from a series of `sawsim`',
67             'runs.',
68             ])
69     parser = OptionParser(usage, epilog=epilog)
70     for option in sr.optparse_options:
71         parser.add_option(option)
72     parser.add_option("-w", "--bin-width", dest="bin_width",
73                       metavar="FLOAT", type='float',
74                       help="Histogram bin width in newtons (%default).",
75                       default=10e-12)
76
77     options,args = parser.parse_args(argv)
78
79     sr_call_params = sr.initialize_from_options(options)
80     try:
81         events = sawsim_histogram(
82             sawsim_runner=sr, bin_edges=None, **sr_call_params)
83     finally:
84         sr.teardown()
85
86     if options.bin_width == None:
87         sys.stdout.write('# Unfolding force (N)\n')
88         events.tofile(sys.stdout, sep='\n')
89         sys.stdout.write('\n')
90     else:
91         h = Histogram()
92         h.from_data(events, bin_edges=h.calculate_bin_edges(
93                 events, options.bin_width))
94         h.headings = ['Force (N)', 'Unfolding events']
95         h.to_stream(sys.stdout)