Reduce ThreadManager default worker_thread to 2.
[sawsim.git] / pysawsim / velocity_dependant_scan.py
index 764c03ca6e3820aea6294ffb427ccdcbada3f3bd..59c9cf44e9e20d9ce708a0de073c6e1dca071b66 100644 (file)
@@ -61,7 +61,7 @@ class HistogramMatcher (object):
     """
     def __init__(self, velocity_stream, param_format_string, N=400,
                  manager=None, residual_type='jensen-shannon', plot=True,
-                 use_cache=False):
+                 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
@@ -69,7 +69,7 @@ class HistogramMatcher (object):
         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):
         """
@@ -222,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).",
@@ -243,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",
@@ -260,28 +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
     manager = get_manager(options.manager)()
-
-    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)
-    #hm.fit(initial_params)
-    hm.plot(param_ranges, logx=options.logx, logy=options.logy,
-            contour=options.contour_plot)
-
-
-if __name__ == "__main__":
-    main()
+    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()