Add --csv option to pysawsim.parameter_scan.
authorW. Trevor King <wking@drexel.edu>
Sat, 9 Apr 2011 17:15:52 +0000 (13:15 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 9 Apr 2011 17:15:52 +0000 (13:15 -0400)
pysawsim/parameter_scan.py

index 3d6e99bbed6c42977dde1c7ea369fe5accdfd356..439fd0b1bbfbdb6ee2dda74190c6611fb522ebe9 100644 (file)
@@ -260,7 +260,10 @@ class HistogramMatcher (object):
         log().debug('residual %s: %g' % (params, residual))
         return residual
 
-    def plot(self, param_ranges, logx=False, logy=False, contour=False):
+    def plot(self, param_ranges, logx=False, logy=False, contour=False,
+             csv=None):
+        if csv:
+            csv.write(','.join(('param 1', 'param 2', 'fit quality')) + '\n')
         xranges = param_ranges[0]
         yranges = param_ranges[1]
         if logx == False:
@@ -281,6 +284,8 @@ class HistogramMatcher (object):
                            % (i, j, i*(len(y)-1) + j, (len(x)-1)*(len(y)-1)))
                 params = (xi,yj)
                 r = self.residual(params)
+                if csv:
+                    csv.write(','.join([str(v) for v in (xi,yj,r)]) + '\n')
                 C[j,i] = numpy.log(r) # better resolution in valleys
                 if MEM_DEBUG == True:
                     log().debug('RSS: %d KB' % rss())
@@ -412,12 +417,15 @@ def main(argv=None):
     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('--csv', dest='csv', metavar='FILE',
+                      help='Save fit qualities to a comma-separated value file FILE.'),
 
     options,args = parser.parse_args(argv)
 
     initial_params = [float(p) for p in options.initial_params.split(",")]
     param_ranges = parse_param_ranges_string(options.param_range)
     histogram_file = args[0]
+    csv = None
     sr_call_params = sr.initialize_from_options(options)
 
     try:
@@ -427,7 +435,11 @@ def main(argv=None):
             sawsim_runner=sr, residual_type=options.residual,
             plot=options.plot_residuals, **sr_call_params)
         #hm.fit(initial_params)
+        if options.csv:
+            csv = open(options.csv, 'w')
         hm.plot(param_ranges, logx=options.logx, logy=options.logy,
-                contour=options.contour_plot)
+                contour=options.contour_plot, csv=csv)
     finally:
         sr.teardown()
+        if csv:
+            csv.close()