Reduce ThreadManager default worker_thread to 2.
[sawsim.git] / pysawsim / velocity_dependant_scan.py
index 72b4d07171d8307571906e8f075c5b877433da3c..59c9cf44e9e20d9ce708a0de073c6e1dca071b66 100644 (file)
@@ -24,6 +24,7 @@ import shutil
 import sys
 
 import matplotlib
+matplotlib.use('Agg')  # select backend that doesn't require X Windows
 import numpy
 import pylab
 from scipy.optimize import leastsq
@@ -34,7 +35,6 @@ from .manager import MANAGERS, get_manager
 from . import sawsim_histogram
 
 
-matplotlib.use('Agg')  # select backend that doesn't require X Windows
 FIGURE = pylab.figure()  # avoid memory problems.
 """`pylab` keeps internal references to all created figures, so share
 a single instance.
@@ -60,14 +60,16 @@ class HistogramMatcher (object):
     The main entry points are `fit()` and `plot()`.
     """
     def __init__(self, velocity_stream, param_format_string, N=400,
-                 residual_type='jensen-shannon', plot=True, use_cache=False):
+                 manager=None, residual_type='jensen-shannon', plot=True,
+                 use_cache=False, clean_cache=False):
         self.experiment_histograms = self.read_force_histograms(velocity_stream)
         self.param_format_string = param_format_string
         self.residual_type = residual_type
         self.N = N
+        self._manager = manager
         self.plot = plot
         self.sawsim_histogram = sawsim_histogram.SawsimHistogram(
-            use_cache=self.use_cache)
+            use_cache=use_cache, clean_cach=clean_cache)
 
     def read_force_histograms(self, stream):
         """
@@ -118,7 +120,7 @@ class HistogramMatcher (object):
         for velocity,experiment_hist in self.experiment_histograms.iteritems():
             sawsim_hist = self.sawsim_histogram(
                 param_string=self.param_string(params, velocity), N=self.N,
-                bin_edges=experiment_hist.bin_edges)
+                bin_edges=experiment_hist.bin_edges, manager=self._manager)
             r = experiment_histogram.residual(
                 sawsim_hist, type=self.residual_type)
             residual += r
@@ -220,6 +222,10 @@ def main():
     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_histogram.SAWSIM)
     parser.add_option("-f","--param-format", dest="param_format",
                       metavar="FORMAT",
                       help="Convert params to sawsim options (%default).",
@@ -241,11 +247,11 @@ def main():
                       help="Job manager name (one of %s) (%%default)."
                       % (', '.join(MANAGERS)),
                       default=MANAGERS[0])
-    parser.add_option("--logx", dest="logx",
-                      help="Use a log scale for the x range.",
+    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("--logy", dest="logy",
-                      help="Use a log scale for the y range.",
+    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",
@@ -258,27 +264,31 @@ def main():
     parser.add_option("-P","--plot-residuals", dest="plot_residuals",
                       help="Generate residual difference plots for each point in the plot range.",
                       default=False, action="store_true")
+    parser.add_option("--logx", dest="logx",
+                      help="Use a log scale for the x range.",
+                      default=False, action="store_true")
+    parser.add_option("--logy", dest="logy",
+                      help="Use a log scale for the y range.",
+                      default=False, action="store_true")
     parser.add_option("-c","--contour-plot", dest="contour_plot",
                       help="Select contour plot (vs. the default pseudocolor plot).",
                       default=False, action="store_true")
-    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")
     options,args = parser.parse_args()
     initial_params = [float(p) for p in options.initial_params.split(",")]
     param_ranges = parse_param_ranges_string(options.param_range)
     velocity_file = args[0]
 
+    sawsim_histogram.SAWSIM = options.sawsim
     sawsim_histogram.CACHE_DIR = options.cache_dir
-
-    hm = HistogramMatcher(
-        file(velocity_file, 'r'), param_format_string=options.param_format,
-        N=options.N, residual_type=options.residual,
-        plot=options.plot_residuals, use_cache=options.use_cache)
-    #hm.fit(initial_params)
-    hm.plot(param_ranges, logx=options.logx, logy=options.logy,
-            contour=options.contour_plot)
-
-
-if __name__ == "__main__":
-    main()
+    manager = get_manager(options.manager)()
+    try:
+        hm = HistogramMatcher(
+            file(velocity_file, 'r'), param_format_string=options.param_format,
+            N=options.N, manager=manager, residual_type=options.residual,
+            plot=options.plot_residuals, use_cache=options.use_cache,
+            clean_cache=options.clean_cache)
+        #hm.fit(initial_params)
+        hm.plot(param_ranges, logx=options.logx, logy=options.logy,
+                contour=options.contour_plot)
+    finally:
+        manager.teardown()