Further fixups to get calibcant-analyze.py working.
authorW. Trevor King <wking@drexel.edu>
Fri, 20 Apr 2012 18:17:30 +0000 (14:17 -0400)
committerW. Trevor King <wking@drexel.edu>
Fri, 20 Apr 2012 18:17:30 +0000 (14:17 -0400)
calibcant/analyze.py
calibcant/calibrate.py
calibcant/util.py

index b6807f882970c4b1c68247188c309f251ac5c5f2..ab3fbbb7c771c5e0da230b878cfa17af38d9fefb 100644 (file)
@@ -95,6 +95,8 @@ from .temperature_analyze import analyze as _temperature_analyze
 from .temperature_analyze import save as _temperature_save
 from .vibration_analyze import analyze as _vibration_analyze
 from .vibration_analyze import save as _vibration_save
+from .util import SaveSpec as _SaveSpec
+from .util import save as _save
 
 
 def analyze(bumps, temperatures, vibrations):
@@ -189,6 +191,20 @@ def plot(bumps, temperatures, vibrations):
     return figure
 _plot = plot  # alternative name for use inside analyze_all()
 
+def save_results(filename=None, group='/', bump=None,
+                 temperature=None, vibration=None, spring_constant=None,
+                 spring_constant_deviation=None):
+    specs = [
+        _SaveSpec(item=bump, relpath='raw/photodiode-sensitivity',
+                  array=True, units='V/m'),
+        _SaveSpec(item=temperature, relpath='raw/temperature',
+                  array=True, units='K'),
+        _SaveSpec(item=vibration, relpath='raw/vibration',
+                  array=True, units='V^2/Hz'),
+        _SaveSpec(item=spring_constant, relpath='processed/spring-constant',
+                  units='N/m', deviation=spring_constant_deviation),
+        ]
+    _save(filename=filename, group=group, specs=specs)
 
 def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
                 filename=None, group=None, plot=False, dry_run=False):
@@ -260,15 +276,18 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
             vibrations_changed) and not dry_run:
             calibration_group = _h5_create_group(group, 'calibration')
             if bumps_changed:
-                calib_save(group=calibration_group, bump=data['bump'])
+                save_results(
+                    group=calibration_group, bump=data['bump'])
             if temperatures_changed:
-                calib_save(
+                save_results(
                     group=calibration_group, temperature=data['temperature'])
             if vibrations_changed:
-                calib_save(
+                save_results(
                     group=calibration_group, vibration=data['vibration'])
             if changed:
-                calib_save(group=calibration_group, k=k, k_s=k_s)
+                save_results(
+                    group=calibration_group,
+                    spring_constant=k, spring_constant_deviation=k_s)
     finally:
         if f:
             f.close()
@@ -342,6 +361,7 @@ def check_calibration(k, k_s, maximum_relative_error, **kwargs):
     else:
         rel_error = abs(new_k-k)/k
         if rel_error > maximum_relative_error:
+            changed = True
             _LOG.warn(("new analysis doesn't match for the spring constant: "
                        "{} != {} (difference: {}, relative error: {})").format(
                     new_k, k, new_k-k, rel_error))
@@ -350,8 +370,9 @@ def check_calibration(k, k_s, maximum_relative_error, **kwargs):
         _LOG.warn('new analysis for the spring constant deviation: {}'.format(
                 new_k_s))
     else:
-        rel_error = abs(new_k-k)/k
+        rel_error = abs(new_k_s-k_s)/k_s
         if rel_error > maximum_relative_error:
+            changed = True
             _LOG.warn(
                 ("new analysis doesn't match for the spring constant deviation"
                  ": {} != {} (difference: {}, relative error: {})").format(
index 8a68cbe01dbf8694b49f8c5a0738ea29b5a7bf52..964ee24f2976a03b496efe92855ebe92cf777df9 100644 (file)
@@ -96,8 +96,8 @@ from .temperature_analyze import load as _temperature_load
 from .vibration import run as _vibration
 from .vibration_analyze import load as _vibration_load
 from .analyze import analyze as _analyze
+from .analyze import save_results as _save_results
 from .util import SaveSpec as _SaveSpec
-from .util import save as _save
 from .util import load as _load
 
 
@@ -556,22 +556,6 @@ class Calibrator (object):
         storage = _HDF5_Storage(filename=filename, group=group)
         storage.save(config=self.config)
 
-    @staticmethod
-    def save_results(filename=None, group='/', bump=None,
-                     temperature=None, vibration=None, spring_constant=None,
-                     spring_constant_deviation=None):
-        specs = [
-            _SaveSpec(item=bump, relpath='raw/photodiode-sensitivity',
-                      array=True, units='V/m'),
-            _SaveSpec(item=temperature, relpath='raw/temperature',
-                      array=True, units='K'),
-            _SaveSpec(item=vibration, relpath='raw/vibration',
-                      array=True, units='V^2/Hz'),
-            _SaveSpec(item=spring_constant, relpath='processed/spring-constant',
-                      units='N/m', deviation=spring_constant_deviation),
-            ]
-        _save(filename=filename, group=group, specs=specs)
-
     @staticmethod
     def load_results(filename, group='/'):
         """Load results saved with `.save_results()`."""
@@ -609,3 +593,5 @@ class Calibrator (object):
                         else:
                             data[name].append(loader(group=cwg))
         return data
+
+Calibrator.save_results = _save_results
index 8d24a35e495a1f727731ec0cd53b68eafa644dfe..8f4c43998722486867e06de6cb90686778ec292a 100644 (file)
@@ -41,14 +41,11 @@ def save(filename=None, group='/', specs=tuple()):
                 storage.save(config=spec.item, group=cwg)
                 continue
             assert spec.units, spec.item
-            try:
-                del cwg['data']
-            except KeyError:
-                pass
-            try:
-                del cwg['units']
-            except KeyError:
-                pass
+            for k in ['data', 'units', 'standard-deviation']:
+                try:
+                    del cwg[k]
+                except KeyError:
+                    pass
             cwg['data'] = spec.item
             cwg['units'] = spec.units
             if spec.deviation is not None: