Add pysawsim.sawsim.main() and bin/sawsim_hist.py calling it.
authorW. Trevor King <wking@drexel.edu>
Wed, 20 Oct 2010 10:08:53 +0000 (06:08 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 20 Oct 2010 10:08:53 +0000 (06:08 -0400)
Also fix a number of bugs turned up by trial calls to the new
sawsim_hist.py.

bin/sawsim_hist.py [new file with mode: 0755]
pysawsim/sawsim_histogram.py

diff --git a/bin/sawsim_hist.py b/bin/sawsim_hist.py
new file mode 100755 (executable)
index 0000000..da573dc
--- /dev/null
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+
+from pysawsim import sawsim_histogram as sh
+
+
+sh.main()
index 848ca9091c3afc464c97846816b431c3a342c201..d5eb306c2bf6c2c4e15b6902adcc77c33e3c66b9 100644 (file)
@@ -27,7 +27,7 @@ import numpy
 
 from . import __version__, log
 from .histogram import Histogram
-from .manager import InvokeJob
+from .manager import InvokeJob, MANAGERS, get_manager
 from .sawsim import parse
 
 
@@ -56,15 +56,15 @@ class SawsimHistogram (object):
             CACHE_DIR, hashlib.sha256(param_string).hexdigest())
 
     def _make_cache(self, param_string):
-        cache_dir = self._cache_dir(params_string)
+        cache_dir = self._cache_dir(param_string)
         os.makedirs(cache_dir)
-        with open(os.path.join(d, 'param_string'), 'w') as f:
+        with open(os.path.join(cache_dir, 'param_string'), 'w') as f:
             f.write('# version: %s\n%s\n' % (__version__, param_string))
 
     def _load_cached_data(self, cache_dir, N):
         data = {}
         for i in range(N):
-            simulation_path = os.path.join(d, '%d.dat' % i)
+            simulation_path = os.path.join(cache_dir, '%d.dat' % i)
             if os.path.exists(simulation_path):
                 with open(simulation_path, 'r') as f:
                     data[i] = parse(f.read())
@@ -73,7 +73,7 @@ class SawsimHistogram (object):
         return data
 
     def _cache_run(self, cache_dir, i, stdout):
-        simulation_path = os.path.join(d, '%d.dat' % i)
+        simulation_path = os.path.join(cache_dir, '%d.dat' % i)
         with open(simulation_path, 'w') as f:
             f.write(stdout)
 
@@ -110,9 +110,9 @@ class SawsimHistogram (object):
             if i in data:
                 continue
             jobs[i] = manager.async_invoke(InvokeJob(
-                    '%s %s' % (SAWSIM, param_string)))
-        complete_jobs = manager.wait([job.id for job in jobs])
-        for i,job in jobs.iteritems:
+                    target='%s %s' % (SAWSIM, param_string)))
+        complete_jobs = manager.wait([job.id for job in jobs.itervalues()])
+        for i,job in jobs.iteritems():
             j = complete_jobs[job.id]
             assert j.status == 0, j.data
             if self._use_cache == True:
@@ -126,9 +126,75 @@ class SawsimHistogram (object):
         for d_i in data.values():
             events.extend([e.force for e in d_i
                            if e.initial_state == 'folded'])
-        events = numpy.array(d_i)
+        events = numpy.array(events)
         if bin_edges == None:
             return events
         h = Histogram()
         h.from_data(events, bin_edges)
         return h
+
+
+def main():
+    import optparse
+    import sys
+
+    global SAWSIM, CACHE_DIR
+
+    usage = "%prog [options] velocity_file"
+
+    parser = optparse.OptionParser(usage)
+    parser.add_option("-s","--sawsim", dest="sawsim",
+                      metavar="PATH",
+                      help="Set sawsim binary (%default).",
+                      default=SAWSIM)
+    parser.add_option("-p","--params", dest="params",
+                      metavar="PARAMS",
+                      help="Initial params for fitting (%default).",
+                      default=DEFAULT_PARAM_STRING)
+    parser.add_option("-N", "--number-of-runs", dest="N",
+                      metavar="INT", type='int',
+                      help="Number of sawsim runs at each point in parameter space (%default).",
+                      default=400)
+    parser.add_option("-w", "--bin-width", dest="bin_width",
+                      metavar="FLOAT", type='float',
+                      help="Histogram bin width in newtons (%default).",
+                      default=None)
+    parser.add_option("-m", "--manager", dest="manager",
+                      metavar="STRING",
+                      help="Job manager name (one of %s) (%%default)."
+                      % (', '.join(MANAGERS)),
+                      default=MANAGERS[0])
+    parser.add_option("-C","--use-cache", dest="use_cache",
+                      help="Use cached simulations if they exist (vs. running new simulations) (%default)",
+                      default=False, action="store_true")
+    parser.add_option("--clean-cache", dest="clean_cache",
+                      help="Remove previously cached simulations if they exist (%default)",
+                      default=False, action="store_true")
+    parser.add_option("-d","--cache-dir", dest="cache_dir",
+                      metavar="STRING",
+                      help="Cache directory for sawsim unfolding forces (%default).",
+                      default=CACHE_DIR)
+    options,args = parser.parse_args()
+
+    SAWSIM = options.sawsim
+    CACHE_DIR = options.cache_dir
+
+    sh = SawsimHistogram(use_cache=options.use_cache,
+                         clean_cache=options.clean_cache)
+
+    manager = get_manager(options.manager)()
+    try:
+        events = sh(param_string=options.params, N=options.N, manager=manager)
+    finally:
+        manager.teardown()
+
+    if options.bin_width == None:
+        sys.stdout.write('# Unfolding force (N)\n')
+        events.tofile(sys.stdout, sep='\n')
+        sys.stdout.write('\n')
+    else:
+        h = Histogram()
+        h.from_data(events, bin_edges=h.calculate_bin_edges(
+                events, options.bin_width))
+        h.headings = ['Force (N)', 'Unfolding events']
+        h.to_stream(sys.stdout)