Only calculate relative errors if a previous value exists.
authorW. Trevor King <wking@drexel.edu>
Fri, 2 Mar 2012 19:17:19 +0000 (14:17 -0500)
committerW. Trevor King <wking@drexel.edu>
Fri, 2 Mar 2012 19:17:19 +0000 (14:17 -0500)
This avoids:

  TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

calibcant/analyze.py

index 1033dcc3b692865f9b87f56d7018fad882dc7ad5..6a840004a6aee3426c9f5a9af0e8adb1cbe4403f 100644 (file)
@@ -406,6 +406,7 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5,
             (calibration_config['num-vibrations'],), dtype=float)
     changed_bump = changed_temperature = changed_vibration = False
     for i in range(calibration_config['num-bumps']):
+        _changed_bump = False
         bump_group = '%sbump/%d/' % (group, i)
         (raw_bump,bump_config,z_axis_config,
          deflection_channel_config,processed_bump) = _bump_load(
@@ -415,16 +416,22 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5,
             z_axis_config=z_axis_config,
             deflection_channel_config=deflection_channel_config)
         bumps[i] = sensitivity
-        rel_error = abs(sensitivity - processed_bump)/processed_bump
-        if rel_error > maximum_relative_error:
-            _LOG.warn(("new analysis doesn't match for bump %d: %g -> %g "
-                       "(difference: %g, relative error: %g)")
-                      % (i, processed_bump, sensitivity,
-                         sensitivity-processed_bump, rel_error))
+        if processed_bump is None:
+            _changed_bump = True            
+            _LOG.warn('new analysis for bump %d: %g' % (i, sensitivity))
+        else:
+            rel_error = abs(sensitivity - processed_bump)/processed_bump
+            if rel_error > maximum_relative_error:
+                _changed_bump = True
+                _LOG.warn(("new analysis doesn't match for bump %d: %g -> %g "
+                           "(difference: %g, relative error: %g)")
+                          % (i, processed_bump, sensitivity,
+                             sensitivity-processed_bump, rel_error))
+        if _changed_bump and not dry_run:
             changed_bump = True
-            if not dry_run:
-                _bump_save(filename, bump_group, processed_bump=sensitivity)
+            _bump_save(filename, bump_group, processed_bump=sensitivity)
     for i in range(calibration_config['num-temperatures']):
+        _changed_temperature = False
         temperature_group = '%stemperature/%d/' % (group, i)
         (raw_temperature,temperature_config,processed_temperature
          ) = _temperature_load(
@@ -432,19 +439,25 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5,
         temperature = _temperature_analyze(
             raw_temperature, temperature_config)
         temperatures[i] = temperature
-        rel_error = abs(temperature - processed_temperature
-                        )/processed_temperature
-        if rel_error > maximum_relative_error:
-            _LOG.warn(("new analysis doesn't match for temperature %d: "
-                       "%g -> %g (difference: %g, relative error: %g)")
-                      % (i, processed_temperature, temperature,
-                         temperature-processed_temperature, rel_error))
+        if processed_temperature is None:
+            _changed_temperature = True            
+            _LOG.warn('new analysis for temperature %d: %g' % (i, temperature))
+        else:
+            rel_error = abs(temperature - processed_temperature
+                            )/processed_temperature
+            if rel_error > maximum_relative_error:
+                _changed_temperature = True
+                _LOG.warn(("new analysis doesn't match for temperature %d: "
+                           "%g -> %g (difference: %g, relative error: %g)")
+                          % (i, processed_temperature, temperature,
+                             temperature-processed_temperature, rel_error))
+        if _changed_temperature and not dry_run:
             changed_temperature = True
-            if not dry_run:
-                _temperature_save(
-                    filename, temperature_group,
-                    processed_T=temperature)
+            _temperature_save(
+                filename, temperature_group,
+                processed_T=temperature)
     for i in range(calibration_config['num-vibrations']):
+        _changed_vibration = False
         vibration_group = '%svibration/%d/' % (group, i)
         (raw_vibration,vibration_config,deflection_channel_config,
          processed_vibration) = _vibration_load(
@@ -453,16 +466,21 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5,
             deflection=raw_vibration, vibration_config=vibration_config,
             deflection_channel_config=deflection_channel_config)
         vibrations[i] = variance
-        rel_error = abs(variance - processed_vibration)/processed_vibration
-        if rel_error > maximum_relative_error:
-            _LOG.warn(("new analysis doesn't match for vibration %d: %g -> %g "
-                       "(difference: %g, relative error: %g)")
-                      % (i, processed_vibration, variance,
-                         variance-processed_vibration, rel_error))
+        if processed_vibration is None:
+            _changed_vibration = True
+            _LOG.warn('new analysis for vibration %d: %g' % (i, variance))
+        else:
+            rel_error = abs(variance - processed_vibration)/processed_vibration
+            if rel_error > maximum_relative_error:
+                _changed_vibration = True
+                _LOG.warn(("new analysis doesn't match for vibration %d: "
+                           "%g -> %g (difference: %g, relative error: %g)")
+                          % (i, processed_vibration, variance,
+                             variance-processed_vibration, rel_error))
+        if _changed_vibration and not dry_run:
             changed_vibration = True
-            if not dry_run:
-                _vibration_save(
-                    filename, vibration_group, processed_vibration=variance)
+            _vibration_save(
+                filename, vibration_group, processed_vibration=variance)
 
     calib_group = '%scalibration/' % group
 
@@ -475,20 +493,32 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5,
 
     new_k,new_k_s = calib_analyze(
         bumps=bumps, temperatures=temperatures, vibrations=vibrations)
-    rel_error = abs(new_k-k)/k
-    if rel_error > maximum_relative_error:
-        _LOG.warn(("new analysis doesn't match for k: %g -> %g "
-                   "(difference: %g, relative error: %g)")
-                  % (k, new_k, new_k-k, rel_error))
-        if not dry_run:
-            calib_save(filename, calib_group, k=new_k)
-    rel_error = abs(new_k_s-k_s)/k_s
-    if rel_error > maximum_relative_error:
-        _LOG.warn(("new analysis doesn't match for k_s: %g -> %g "
-                   "(difference: %g, relative error: %g)")
-                  % (k_s, new_k_s, new_k_s-k_s, rel_error))
-        if not dry_run:
-            calib_save(filename, calib_group, k_s=new_k_s)
+    new_calib_k = False
+    if k is None:
+        new_calib_k = True
+        _LOG.warn('new analysis for k: %g' % new_k)
+    else:
+        rel_error = abs(new_k-k)/k
+        if rel_error > maximum_relative_error:
+            new_calib_k = True
+            _LOG.warn(("new analysis doesn't match for k: %g -> %g "
+                       "(difference: %g, relative error: %g)")
+                      % (k, new_k, new_k-k, rel_error)) 
+    if new_calib_k and not dry_run:
+        calib_save(filename, calib_group, k=new_k)
+    new_calib_k_s = False
+    if k_s is None:
+        new_calib_k_s = True
+        _LOG.warn('new analysis for k_s: %g' % new_k_s)
+    else:
+        rel_error = abs(new_k_s-k_s)/k_s
+        if rel_error > maximum_relative_error:
+            new_calib_k_s = True
+            _LOG.warn(("new analysis doesn't match for k_s: %g -> %g "
+                       "(difference: %g, relative error: %g)")
+                      % (k_s, new_k_s, new_k_s-k_s, rel_error))
+    if new_calib_k_s and not dry_run:
+        calib_save(filename, calib_group, k_s=new_k_s)
     return (new_k, new_k_s)
 
 def calib_plot_all(bumps, bump_details, temperatures, temperature_details,