from . import __version__, log
from .histogram import Histogram
-from .manager import InvokeJob
+from .manager import InvokeJob, MANAGERS, get_manager
from .sawsim import parse
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())
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)
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:
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)