calibrate.py should now work.
[calibcant.git] / calibcant / analyze.py
index 797a78a5a91e4e2785cc37b31573ebe7526853b4..2e535efac835df4974fcdcee406cfd4a7c414e1f 100755 (executable)
@@ -47,13 +47,18 @@ Which are related by the parameters :
 """
 
 import numpy
+from splittable_kwargs import splittableKwargsFunction, \
+    make_splittable_kwargs_function
 import common # common module for the calibcant package
 import config # config module for the calibcant package
 import T_analyze # T_analyze module for the calibcant package
 
 kb = 1.3806504e-23 # Boltzmann's constant in J/K
 
-def calib_analyze(bumps, Ts, vibs) :
+#@splittableKwargsFunction((calib_plot, 'bumps', 'Ts', 'vibs'))
+# Some of the child functions aren't yet defined, so postpone
+# make-splittable until later in the module.
+def calib_analyze(bumps, Ts, vibs, **kwargs) :
     """
     Analyze data from get_calibration_data()
     return (k, k_s, !!!a2_r, T_r, one_o_Vp2_r)
@@ -78,6 +83,7 @@ def calib_analyze(bumps, Ts, vibs) :
     Remember that you need enough samples to have a valid error estimate in
     the first place, and that none of this addresses any systematic errors.
     """
+    calib_plot_kwargs, = calib_analyze._splitargs(calib_analyze, kwargs)
     photoSensitivity2 = bumps**2
     one_o_Vphoto2 = 1/vibs
 
@@ -105,10 +111,13 @@ def calib_analyze(bumps, Ts, vibs) :
 
     k_s = k*(photoSensitivity2_r**2 + T_r**2 + one_o_Vphoto2_r**2)**0.5
 
+    calib_plot(bumps, Ts, vibs, **calib_plot_kwargs)
+    
     return (k, k_s,
             photoSensitivity2_m, photoSensitivity2_s,
             T_m, T_s, one_o_Vphoto2_m, one_o_Vphoto2_s)
 
+@splittableKwargsFunction()
 def string_errors(k_m, k_s,
                   photoSensitivity2_m, photoSensitivity2_s,
                   T_m, T_s,
@@ -128,19 +137,66 @@ def string_errors(k_m, k_s,
               % (one_o_Vphoto2_m, one_o_Vphoto2_s, one_o_Vphoto2_r)
     return string
 
-def calib_plot(bumps, Ts, vibs) :
-    from pylab import figure, subplot, plot, title, show
-    figure()
-    subplot(311)
-    plot(bumps, 'g.-')
-    title('Photodiode sensitivity (V/nm)')
-    subplot(312)
-    plot(Ts, 'r.-')
-    title('Temperature (K)')
-    subplot(313)
-    plot(vibs, 'b.-')
-    title('Thermal deflection variance (Volts**2)')
-    show()
+@splittableKwargsFunction()
+def calib_save(bumps, Ts, vibs, log_dir=None) :
+    """
+    Save a dictonary with the bump, T, and vib data.
+    """
+    if log_dir != None :
+        data = {'bump':bumps, 'T':Ts, 'vib':vibs}
+        log = data_logger.data_log(log_dir, noclobber_logsubdir=False,
+                                   log_name="calib")
+        log.write_dict_of_arrays(data)
+
+def calib_load(datafile) :
+    "Load the dictionary data, using data_logger.date_load()"
+    dl = data_logger.data_load()
+    data = dl.read_dict_of_arrays(path)
+    return (data['bump'], data['T'], data['vib'])
+
+def calib_save_analysis(k, k_s,
+                        photoSensitivity2_m, photoSensitivity2_s,
+                        T_m, T_s, one_o_Vphoto2_m, one_o_Vphoto2_s,
+                        log_dir=None) :
+    if log_dir != None :
+        log = data_logger.data_log(log_dir, noclobber_logsubdir=False,
+                                   log_name="calib_analysis_text")
+        log.write_binary((
+            "k (N/m) : %g +/- %g\n" % (k, k_s)
+            + "photoSensitivity**2 (V/nm)**2 : %g +/- %g\n" % \
+                (photoSensitivity2_m, photoSensitivity2_s)
+            + "T (K) : %g +/- %g\n" % (T_m, T_s)
+            + "1/Vp**2 (1/V)**2 : %g +/- %g\n" % \
+                (one_o_Vphoto2_m, one_o_Vphoto2_s)
+                         ))
+
+@splittableKwargsFunction()
+def calib_plot(bumps, Ts, vibs, plotVerbose=False) :
+    if plotVerbose or config.PYLAB_VERBOSE :
+        common.import_pylab()
+        common._pylab.figure(config.BASE_FIGNUM+4)
+        common._pylab.subplot(311)
+        common._pylab.plot(bumps, 'g.')
+        common._pylab.title('Photodiode sensitivity (V/nm)')
+        common._pylab.subplot(312)
+        common._pylab.plot(Ts, 'r.')
+        common._pylab.title('Temperature (K)')
+        common._pylab.subplot(313)
+        common._pylab.plot(vibs, 'b.')
+        common._pylab.title('Thermal deflection variance (Volts**2)')
+        common._flush_plot()
+
+make_splittable_kwargs_function(calib_analyze,
+                                (calib_plot, 'bumps', 'Ts', 'vibs'))
+
+@splittableKwargsFunction((calib_analyze, 'bumps', 'Ts', 'vibs'))
+def calib_load_analyze_tweaks(bump_tweaks, vib_tweaks, T_tweaks=None) :
+    raise NotImplementedError
+    a = read_tweaked_bumps(bump_tweaks)
+    vib = V_photo_variance_from_file(vib_tweaks)
+    if T_tweaks == None:
+        pass
+    return analyze_calibration_data(a, T, vib, log_dir=log_dir)
 
 # commandline interface functions
 import scipy.io, sys