calibrate.py should now work.
[calibcant.git] / calibcant / bump_analyze.py
index 3d3a891b35b6676dc8f3eb6d2780a43afe086e39..11a8f51e719f7f8b96220bec7fca47b4357ddb06 100755 (executable)
@@ -39,6 +39,10 @@ Which are related by the parameters :
  zpGain           Vzp_out / Vzp
  zpSensitivity    Zp / Vzp
  photoSensitivity Vphoto / Zcant
+
+photoSensitivity is measured by bumping the cantilever against the
+surface, where Zp = Zcant (see calibrate.bump_aquire()).  The measured
+slope Vphoto/Vout is converted to photoSensitivity with bump_analyze().
 """
 
 import numpy
@@ -47,12 +51,17 @@ import config # config module for the calibcant package
 import data_logger
 import z_piezo_utils
 import linfit
+from splittable_kwargs import splittableKwargsFunction, \
+    make_splittable_kwargs_function
 
+#@splittableKwargsFunction((bump_plot, 'data'))
+# Some of the child functions aren't yet defined, so postpone
+# make-splittable until later in the module.
 def bump_analyze(data, zpGain=config.zpGain,
                  zpSensitivity=config.zpSensitivity,
                  Vzp_out2V=config.Vzp_out2V,
                  Vphoto_in2V=config.Vphoto_in2V,
-                 textVerboseFile=None, plotVerbose=False) :
+                 **kwargs) :
     """
     Return the slope of the bump ;).
     Inputs:
@@ -69,19 +78,22 @@ def bump_analyze(data, zpGain=config.zpGain,
     and THEN converted, so we're assuming that both conversions are LINEAR.
     if they aren't, rewrite to convert before the regression.
     """
+    bump_plot_kwargs, = bump_analyze._splitargs(bump_analyze, kwargs)
     scale_Vzp_bits2V = Vzp_out2V(1) - Vzp_out2V(0)
     scale_Vphoto_bits2V = Vphoto_in2V(1) - Vphoto_in2V(0)
     Vphoto2Vzp_out_bit, intercept = \
         linfit.linregress(x=data["Z piezo output"],
                           y=data["Deflection input"],
-                          plotVerbose=plotVerbose)
-    Vphoto2Vzp_out = Vphoto2Vzp_out_bit * scale_Vphoto_bits2V / scale_Vzp_bits2V
+                          plotVerbose=False)
+    Vphoto2Vzp_out = Vphoto2Vzp_out_bit *scale_Vphoto_bits2V / scale_Vzp_bits2V
 
     #               1 / (Vzp/Vzp_out  *     Zp/Vzp       * Zcant/Zp )
     Vzp_out2Zcant = 1.0/ (zpGain      * zpSensitivity) # * 1
+    bump_plot(data, **bump_plot_kwargs)
     return Vphoto2Vzp_out * Vzp_out2Zcant
 
-def bump_save(data, log_dir) :
+@splittableKwargsFunction()
+def bump_save(data, log_dir=None) :
     "Save the dictionary data, using data_logger.data_log()"
     if log_dir != None :
         log = data_logger.data_log(log_dir, noclobber_logsubdir=False,
@@ -94,7 +106,8 @@ def bump_load(datafile) :
     data = dl.read_dict_of_arrays(datafile)
     return data
 
-def bump_plot(data, plotVerbose) :
+@splittableKwargsFunction()
+def bump_plot(data, plotVerbose=False) :
     "Plot the bump (Vphoto vs Vzp) if plotVerbose or PYLAB_VERBOSE == True"
     if plotVerbose or config.PYLAB_VERBOSE :
         common._import_pylab()
@@ -105,18 +118,19 @@ def bump_plot(data, plotVerbose) :
         common._pylab.legend(loc='upper left')
         common._flush_plot()
 
-def bump_load_analyze_tweaked(tweak_file, zpGain=config.zpGain,
-                              zpSensitivity=config.zpSensitivity,
-                              Vzp_out2V=config.Vzp_out2V,
-                              Vphoto_in2V=config.Vphoto_in2V,
-                              textVerboseFile=None, plotVerbose=False) :
+make_splittable_kwargs_function(bump_analyze, (bump_plot, 'data'))
+
+@splittableKwargsFunction((bump_analyze, 'data'))
+def bump_load_analyze_tweaked(tweak_file, **kwargs):
     "Load the output file of tweak_calib_bump.sh, return an array of slopes"
+    bump_analyze_kwargs, = \
+        bump_load_analyze_tweaked._splitargs(bump_load_analyze_tweaked, kwargs)
     photoSensitivity = []
     for line in file(tweak_file, 'r') :
         parsed = line.split()
         path = parsed[0].split('\n')[0]
-        if textVerboseFile != None :
-            print >> textVerboseFile, "Reading data from %s with ranges %s" % (path, parsed[1:])
+        if config.TEXT_VERBOSE :
+            print "Reading data from %s with ranges %s" % (path, parsed[1:])
         # read the data
         full_data = bump_load(path)
         if len(parsed) == 1 :
@@ -133,10 +147,8 @@ def bump_load_analyze_tweaked(tweak_file, zpGain=config.zpGain,
                 df.extend(full_data['Deflection input'][starti:stopi])
             data = {'Z piezo output': numpy.array(zp),
                     'Deflection input': numpy.array(df)}
-        pSi = bump_analyze(data, zpGain, zpSensitivity,
-                           Vzp_out2V, Vphoto_in2V, plotVerbose)
+        pSi = bump_analyze(data, **bump_analyze_kwargs)
         photoSensitivity.append(pSi)
-        bump_plot(data, plotVerbose)
     return numpy.array(photoSensitivity, dtype=numpy.float)
 
 # commandline interface functions
@@ -216,10 +228,6 @@ if __name__ == '__main__' :
         ofile = file(options.ofilename, 'w')
     else :
         ofile = sys.stdout
-    if options.verbose == True :
-        vfile = sys.stderr
-    else :
-        vfile = None
     config.TEXT_VERBOSE = options.verbose
     config.PYLAB_INTERACTIVE = False
     config.PYLAB_VERBOSE = options.pylab
@@ -238,11 +246,11 @@ if __name__ == '__main__' :
                 # data not very bilinear, so don't cut anything off.
                 print >> sys.stderr, "use everything"
                 
-        photoSensitivity = bump_analyze(data, textVerboseFile=vfile)
+        photoSensitivity = bump_analyze(data)
         
         print >> ofile, photoSensitivity
     else : # tweak file mode
-        slopes = bump_load_analyze_tweaked(ifilename, textVerboseFile=vfile)
+        slopes = bump_load_analyze_tweaked(ifilename)
         if options.comma_out :
             sep = ','
         else :