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