compare-unfold.py: Add --contact-slope and --output options
authorW. Trevor King <wking@tremily.us>
Wed, 1 May 2013 16:59:24 +0000 (12:59 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 1 May 2013 16:59:24 +0000 (12:59 -0400)
For more quantitative comparisons and easier automation respectively.

posts/Comparing_velocity_clamp_experiments/compare-unfold.py

index f27127f3a245ca101fe7ab2ee39ff4004528cea8..a83dd2b9160c7e0392f7da5609b57daacf5ef20a 100755 (executable)
 """Compare unfolding pulls between Prof. Yang's software and my own.
 """
 
+import collections as _collections
 import os as _os
 
 import crunch as _crunch
 import gyang_curve as _gyang_curve
 from matplotlib import pylab as _pylab
+import numpy as _numpy
 from pypiezo import surface as _pypiezo_surface
 
 
 def load_yang_curves(basedir, single=False):
+    if _os.path.isfile(basedir):
+        for curve in _gyang_curve.load_all_traces(basedir):
+            yield(curve)
+        return
     for series in sorted(_os.listdir(basedir)):
         if series.endswith('.ibw'):
             path = _os.path.join(basedir, series)
@@ -36,6 +42,9 @@ def load_yang_curves(basedir, single=False):
                     return
 
 def load_king_curves(basedir, single=False):
+    if _os.path.isfile(basedir):
+        yield _crunch.load_pull(filename=basedir)
+        return
     for path in sorted(_os.listdir(basedir)):
         if path.endswith('.h5'):
             curve = _os.path.join(basedir, path)
@@ -43,14 +52,19 @@ def load_king_curves(basedir, single=False):
             if single:
                 return
 
-def plot_comparison(yang_dir, king_dir, single=False):
+def plot_comparison(yang_dir, king_dir, single=False, contact_slope=False):
     yang = load_yang_curves(yang_dir, single=single)
     king = load_king_curves(king_dir, single=single)
     figure = _pylab.figure()
     axes = figure.add_subplot(1, 1, 1)
     axes.set_title('velocity clamp unfolding')
-    axes.set_xlabel('z piezo (bits)')
-    axes.set_ylabel('deflection (bits)')
+    if contact_slope:
+        axes.set_xlabel('pull index')
+        axes.set_ylabel('contact slope (bits/bit)')
+        hist_data = {'b': [], 'r': []}
+    else:
+        axes.set_xlabel('z piezo (bits)')
+        axes.set_ylabel('deflection (bits)')
     axes.hold(True)
     xyc = []
     for curve in yang:
@@ -71,9 +85,27 @@ def plot_comparison(yang_dir, king_dir, single=False):
             ddict, return_all_parameters=True)
         y0 = non_contact_offset + non_contact_slope*(kink-x.min())
         x0 = x.max() - (y.max()-y0)/alpha_dp
-        axes.plot(
-            x - x0, y - y0,
-            linestyle='none', marker=',', color=c, markeredgecolor=c)
+        if contact_slope:
+            hist_data[c].append(alpha_dp)
+        else:
+            axes.plot(
+                x - x0, y - y0,
+                linestyle='none', marker=',', color=c, markeredgecolor=c)
+    if contact_slope:
+        for c,data in hist_data.items():
+            data = [d for d in data if d > 5.5]  # HACK!!
+            axes.plot(
+                data,
+                linestyle='none', marker='o', color=c, markeredgecolor=c)
+            #axes.hist(data, color=c, alpha=0.5)
+            data = _numpy.array(data)
+            print('\n'.join(
+                    '{}: {}'.format(key,value)
+                    for key,value in {
+                        'color': c,
+                        'mean': data.mean(),
+                        'std': data.std(),
+                        }.items()))
     return figure
 
 
@@ -84,6 +116,12 @@ if __name__ == '__main__':
     parser.add_argument(
         '--single', action='store_const', const=True,
         help='compare only a single curve of each type')
+    parser.add_argument(
+        '--contact-slope', action='store_const', const=True,
+        help='compare contact slopes')
+    parser.add_argument(
+        '-o', '--output', default=None,
+        help='save figure instead of showing it')
     parser.add_argument(
         'yang', metavar='DIR',
         help="directory for data taken with Prof. Yang's LabVIEW program")
@@ -93,8 +131,12 @@ if __name__ == '__main__':
 
     args = parser.parse_args()
 
-    plot_comparison(
+    figure = plot_comparison(
         yang_dir=_os.path.expanduser(args.yang),
         king_dir=_os.path.expanduser(args.king),
-        single=args.single)
-    _pylab.show()
+        single=args.single,
+        contact_slope=args.contact_slope)
+    if args.output:
+        figure.savefig(args.output)
+    else:
+        _pylab.show()