Better errors from analyze_all when we're missing raw data.
authorW. Trevor King <wking@tremily.us>
Fri, 11 May 2012 16:21:56 +0000 (12:21 -0400)
committerW. Trevor King <wking@tremily.us>
Fri, 11 May 2012 16:21:56 +0000 (12:21 -0400)
calibcant/analyze.py

index 0cfab7c4da2b9ebdcdd936c5185b5e2ef0fe185d..a8dda107074df8552f25d6a62c6d39d783156ed2 100644 (file)
@@ -130,6 +130,13 @@ def analyze(bumps, temperatures, vibrations):
     v2_m = vibrations.mean()  # average voltage variance
     v2_s = vibrations.std()
 
+    if ps_m == 0:
+        raise ValueError('invalid bumps: {}'.format(bumps))
+    if T_m == 0:
+        raise ValueError('invalid temperatures: {}'.format(temperatures))
+    if v2_m == 0:
+        raise ValueError('invalid vibrations: {}'.format(vibrations))
+
     # Vphoto / photo_sensitivity = x
     # k = kB T / <x**2> = kB T photo_sensitivity**2 / Vphoto_var
     #
@@ -224,6 +231,7 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
     input_config = config['afm']['piezo'].select_config(
         setting_name='inputs', attribute_value='deflection')
     bumps_changed = temperatures_changed = vibrations_changed = False
+    calibration_group = None
     if not isinstance(group, _h5py.Group) and not dry_run:
         f = _h5py.File(filename, mode='a')
         group = _h5_create_group(f, group)
@@ -266,14 +274,8 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
                     group, 'vibration/{}'.format(i))
                 _vibration_save(
                     group=vibration_group, processed=data['vibration'])
-        k,k_s,changed = check_calibration(
-            k=data.get('processed/spring_constant', None),
-            k_s=data.get('processed/spring_constant_deviation', None),
-            bumps=data['bump'],
-            temperatures=data['temperature'], vibrations=data['vibration'],
-            maximum_relative_error=maximum_relative_error)
-        if (changed or bumps_changed or temperatures_changed or
-            vibrations_changed) and not dry_run:
+        if (bumps_changed or temperatures_changed or vibrations_changed
+            ) and not dry_run:
             calibration_group = _h5_create_group(group, 'calibration')
             if bumps_changed:
                 save_results(
@@ -284,10 +286,30 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
             if vibrations_changed:
                 save_results(
                     group=calibration_group, vibration=data['vibration'])
-            if changed:
-                save_results(
-                    group=calibration_group,
-                    spring_constant=k, spring_constant_deviation=k_s)
+        if len(raw_data['bump']) != len(data['bump']):
+            raise ValueError(
+                'not enough raw bump data: {} of {}'.format(
+                    len(raw_data['bump']), len(data['bump'])))
+        if len(raw_data['temperature']) != len(data['temperature']):
+            raise ValueError(
+                'not enough raw temperature data: {} of {}'.format(
+                    len(raw_data['temperature']), len(data['temperature'])))
+        if len(raw_data['vibration']) != len(data['vibration']):
+            raise ValueError(
+                'not enough raw vibration data: {} of {}'.format(
+                    len(raw_data['vibration']), len(data['vibration'])))
+        k,k_s,changed = check_calibration(
+            k=data.get('processed/spring_constant', None),
+            k_s=data.get('processed/spring_constant_deviation', None),
+            bumps=data['bump'],
+            temperatures=data['temperature'], vibrations=data['vibration'],
+            maximum_relative_error=maximum_relative_error)
+        if changed and not dry_run:
+            if calibration_group is None:
+                calibration_group = _h5_create_group(group, 'calibration')
+            save_results(
+                group=calibration_group,
+                spring_constant=k, spring_constant_deviation=k_s)
     finally:
         if f:
             f.close()