analyze.py: Set `changed = True` for tweaked vibration variance
[calibcant.git] / calibcant / analyze.py
index 625ae4caec931ac0578faa0d1069e821fb776ee6..c765387a7534d05373f229287e1c75ec95d1cc53 100644 (file)
@@ -1,6 +1,6 @@
 # calibcant - tools for thermally calibrating AFM cantilevers
 #
-# Copyright (C) 2008-2012 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2008-2013 W. Trevor King <wking@tremily.us>
 #
 # This file is part of calibcant.
 #
@@ -224,13 +224,22 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
     if not data.get('vibrations', None):
         data['vibration'] = _numpy.zeros(
                 (config['num-vibrations'],), dtype=float)
+    if 'raw' not in data:
+        data['raw'] = {}
+    if 'bump' not in data['raw']:
+        data['raw']['bump'] = _numpy.zeros((config['num-bumps'],), dtype=float)
+    if 'temperature' not in data['raw']:
+        data['raw']['temperature'] = _numpy.zeros(
+        (config['num-temperatures'],), dtype=float)
+    if 'vibration' not in data['raw']:
+        data['raw']['vibration'] = _numpy.zeros(
+        (config['num-vibrations'],), dtype=float)
     axis_config = config['afm']['piezo'].select_config(
         setting_name='axes',
         attribute_value=config['afm']['main-axis'],
         get_attribute=_get_axis_name)
     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')
@@ -238,36 +247,33 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
     else:
         f = None
     try:
-        if len(data.get('raw', {}).get('bump', [])) != len(data['bump']):
-            bumps_changed = True
-        for i,bump in enumerate(raw_data['bump']):
+        bumps_changed = len(data['raw']['bump']) != len(data['bump'])
+        for i,bump in enumerate(raw_data.get('bump', [])):  # compare values
             data['bump'][i],changed = check_bump(
-                index=i, bump=bump, z_axis_config=axis_config,
+                index=i, bump=bump, config=config, z_axis_config=axis_config,
                 deflection_channel_config=input_config, plot=plot,
                 maximum_relative_error=maximum_relative_error)
             if changed and not dry_run:
                 bumps_changed = True
                 bump_group = _h5_create_group(group, 'bump/{}'.format(i))
                 _bump_save(group=bump_group, processed=data['bump'][i])
-        if len(data.get('raw', {}).get('temperature', [])
-               ) != len(data['temperature']):
-            temperatures_changed = True
-        for i,temperature in enumerate(raw_data['temperature']):
+        temperatures_changed = len(data['raw']['temperature']) != len(
+            data['temperature'])
+        for i,temperature in enumerate(raw_data.get('temperature', [])):
             data['temperature'][i],changed = check_temperature(
-                index=i, temperature=temperature,
+                index=i, temperature=temperature, config=config,
                 maximum_relative_error=maximum_relative_error)
             if changed and not dry_run:
                 temperatures_changed = True
                 temperature_group = _h5_create_group(
                     group, 'temperature/{}'.format(i))
                 _temperature_save(
-                    group=temerature_group, processed=data['temperature'][i])
-        if len(data.get('raw', {}).get('vibration', [])
-               ) != len(data['vibration']):
-            vibrations_changed = True
-        for i,vibration in enumerate(raw_data['vibration']):
+                    group=temperature_group, processed=data['temperature'][i])
+        vibrations_changed = len(data['raw']['vibration']) != len(
+            data['vibration'])
+        for i,vibration in enumerate(raw_data.get('vibration', [])):
             data['vibration'][i],changed = check_vibration(
-                    index=i, vibration=vibration,
+                    index=i, vibration=vibration, config=config,
                     deflection_channel_config=input_config, plot=plot,
                     maximum_relative_error=maximum_relative_error)
             if changed and not dry_run:
@@ -275,7 +281,7 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
                 vibration_group = _h5_create_group(
                     group, 'vibration/{}'.format(i))
                 _vibration_save(
-                    group=vibration_group, processed=data['vibration'])
+                    group=vibration_group, processed=data['vibration'][i])
         if (bumps_changed or temperatures_changed or vibrations_changed
             ) and not dry_run:
             calibration_group = _h5_create_group(group, 'calibration')
@@ -288,18 +294,20 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
             if vibrations_changed:
                 save_results(
                     group=calibration_group, vibration=data['vibration'])
-        if len(raw_data['bump']) != len(data['bump']):
+        if len(raw_data.get('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']):
+                    len(raw_data.get('bump', [])), len(data['bump'])))
+        if len(raw_data.get('temperature', [])) != len(data['temperature']):
             raise ValueError(
                 'not enough raw temperature data: {} of {}'.format(
-                    len(raw_data['temperature']), len(data['temperature'])))
+                    len(raw_data.get('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'])))
+                    len(raw_data.get('vibration', [])),
+                    len(data['vibration'])))
         k,k_s,changed = check_calibration(
             k=data.get('processed', {}).get('spring_constant', None),
             k_s=data.get('processed', {}).get(
@@ -317,15 +325,19 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
         if f:
             f.close()
     if plot:
-        _plot(bumps=data['raw']['bump'],
-             temperatures=data['raw']['temperature'],
-             vibrations=data['raw']['vibration'])
+        _plot(bumps=data['bump'],
+              temperatures=data['temperature'],
+              vibrations=data['vibration'])
     return (k, k_s)
 
-def check_bump(index, bump, maximum_relative_error, **kwargs):
+def check_bump(index, bump, config=None, maximum_relative_error=0, **kwargs):
     changed = False
+    try:
+        bump_config = bump['config']['bump']
+    except KeyError:
+        bump_config = config['bump']
     sensitivity = _bump_analyze(
-        config=bump['config']['bump'], data=bump['raw'], **kwargs)
+        config=bump_config, data=bump['raw'], **kwargs)
     if bump.get('processed', None) is None:
         changed = True            
         _LOG.warn('new analysis for bump {}: {}'.format(index, sensitivity))
@@ -339,10 +351,15 @@ def check_bump(index, bump, maximum_relative_error, **kwargs):
                     sensitivity-bump['processed'], rel_error))
     return (sensitivity, changed)
 
-def check_temperature(index, temperature, maximum_relative_error, **kwargs):
+def check_temperature(index, temperature, config=None,
+                      maximum_relative_error=0, **kwargs):
     changed = False
+    try:
+        temp_config = temperature['config']['temperature']
+    except KeyError:
+        temp_config = config['temperature']
     temp = _temperature_analyze(
-        config=temperature['config']['temperature'],
+        config=temp_config,
         temperature=temperature['raw'], **kwargs)
     if temperature.get('processed', None) is None:
         changed = True            
@@ -359,18 +376,23 @@ def check_temperature(index, temperature, maximum_relative_error, **kwargs):
                     temp-temperature['processed'], rel_error))
     return (temp, changed)
 
-def check_vibration(index, vibration, maximum_relative_error, **kwargs):
+def check_vibration(index, vibration, config=None, maximum_relative_error=0,
+                    **kwargs):
     changed = False
+    try:
+        vib_config = vibration['config']['vibration']
+    except KeyError:
+        vib_config = config['vibration']
     variance = _vibration_analyze(
-        config=vibration['config']['vibration'],
-        deflection=vibration['raw'], **kwargs)
+        config=vib_config, deflection=vibration['raw'], **kwargs)
     if vibration.get('processed', None) is None:
         changed = True
-        _LOG.warn('new analysis for temperature {}: {}'.format(
+        _LOG.warn('new analysis for vibration {}: {}'.format(
                 index, variance))
     else:
         rel_error = abs(variance-vibration['processed'])/vibration['processed']
         if rel_error > maximum_relative_error:
+            changed = True
             _LOG.warn(("new analysis doesn't match for vibration {}: {} != {} "
                        "(difference: {}, relative error: {})").format(
                     index, variance, vibration['processed'],