Plotting commands now return the plots they generate.
[calibcant.git] / calibcant / analyze.py
index 073d7b4a1a4a6e1487d477c21482cb12d98cd9bb..67d6908418321369350094e39bf225af04d10b29 100644 (file)
 # calibcant - tools for thermally calibrating AFM cantilevers
 #
-# Copyright (C) 2008-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2008-2012 W. Trevor King <wking@drexel.edu>
 #
 # This file is part of calibcant.
 #
-# calibcant is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
+# calibcant is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
 #
-# calibcant is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Lesser General Public License for more details.
+# calibcant is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU Lesser General Public
-# License along with calibcant.  If not, see
-# <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU General Public License along with
+# calibcant.  If not, see <http://www.gnu.org/licenses/>.
 
-"""
-Separate the more general calib_analyze() from the other calib_*()
-functions in calibcant.  Also provide a command line interface
-for analyzing data acquired through other workflows.
-
-The relevent physical quantities are : 
- Vzp_out  Output z-piezo voltage (what we generate)
- Vzp      Applied z-piezo voltage (after external ZPGAIN)
- Zp       The z-piezo position
- Zcant    The cantilever vertical deflection
- Vphoto   The photodiode vertical deflection voltage (what we measure)
- Fcant    The force on the cantilever
- T        The temperature of the cantilever and surrounding solution
-          (another thing we measure)
- k_b      Boltzmann's constant
-
-Which are related by the parameters :
- zpGain           Vzp_out / Vzp
- zpSensitivity    Zp / Vzp
- photoSensitivity Vphoto / Zcant
- k_cant           Fcant / Zcant
+"""Calculate `k` from arrays of bumps, temperatures, and vibrations.
+
+Separate the more general `analyze()` from the other calibration
+functions in calibcant.
+
+The relevent physical quantities are :
+  Vzp_out  Output z-piezo voltage (what we generate)
+  Vzp      Applied z-piezo voltage (after external ZPGAIN)
+  Zp       The z-piezo position
+  Zcant    The cantilever vertical deflection
+  Vphoto   The photodiode vertical deflection voltage (what we measure)
+  Fcant    The force on the cantilever
+  T        The temperature of the cantilever and surrounding solution
+           (another thing we measure)
+  k_b      Boltzmann's constant
+
+Which are related by the parameters:
+  zp_gain           Vzp_out / Vzp
+  zp_sensitivity    Zp / Vzp
+  photo_sensitivity Vphoto / Zcant
+  k_cant            Fcant / Zcant
+
+
+>>> import numpy
+>>> from .config import CalibrateConfig
+
+>>> config = CalibrateConfig()
+>>> bumps = numpy.array((15.9e6, 16.9e6, 16.3e6))
+>>> temperatures = numpy.array((295, 295.2, 294.8))
+>>> vibrations = numpy.array((2.20e-5, 2.22e-5, 2.21e-5))
+
+>>> k,k_s = analyze(bumps=bumps, temperatures=temperatures,
+...     vibrations=vibrations)
+>>> (k, k_s)  # doctest: +ELLIPSIS
+(0.0493..., 0.00248...)
+
+Most of the error in this example comes from uncertainty in the
+photodiode sensitivity (bumps).
+
+>>> k_s/k  # doctest: +ELLIPSIS
+0.0503...
+>>> bumps.std()/bumps.mean()  # doctest: +ELLIPSIS
+0.0251...
+>>> temperatures.std()/temperatures.mean()  # doctest: +ELLIPSIS
+0.000553...
+>>> vibrations.std()/vibrations.mean()  # doctest: +ELLIPSIS
+0.00369...
 """
 
-import numpy
+import h5py as _h5py
+import numpy as _numpy
+try:
+    from scipy.constants import Boltzmann as _kB  # in J/K
+except ImportError:
+    from scipy.constants import Bolzmann as _kB  # in J/K
+# Bolzmann -> Boltzmann patch submitted:
+#   http://projects.scipy.org/scipy/ticket/1417
+# Fixed in scipy commit 4716d91, Apr 2, 2011, during work after v0.9.0rc5.
 
-import data_logger
-from splittable_kwargs import splittableKwargsFunction, \
-    make_splittable_kwargs_function
+try:
+    import matplotlib as _matplotlib
+    import matplotlib.pyplot as _matplotlib_pyplot
+    import time as _time  # for timestamping lines on plots
+except (ImportError, RuntimeError), e:
+    _matplotlib = None
+    _matplotlib_import_error = e
 
-from . import common
-from . import config
-from . import T_analyze
+from h5config.storage.hdf5 import h5_create_group as _h5_create_group
+from pypiezo.base import get_axis_name as _get_axis_name
 
+from . import LOG as _LOG
+from . import package_config as _package_config
 
-kb = 1.3806504e-23 # Boltzmann's constant in J/K
+from .bump_analyze import analyze as _bump_analyze
+from .bump_analyze import save as _bump_save
+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
 
-#@splittableKwargsFunction((calib_plot, 'bumps', 'Ts', 'vibs'))
-# Some of the child functions aren't yet defined, so postpone
-# make-splittable until later in the module.
-def calib_analyze(bumps, Ts, vibs, **kwargs) :
-    """
-    Analyze data from get_calibration_data()
-    return (k, k_s, !!!a2_r, T_r, one_o_Vp2_r)
-    Inputs (all are arrays of recorded data) :
-     bumps measured (V_photodiode / nm_tip) proportionality constant
-     Ts    measured temperature (K)
-     vibs  measured V_photodiode variance in free solution (V**2)
-    Outputs :
-     k    cantilever spring constant (in N/m, or equivalently nN/nm)
-     k_s  standard deviation in our estimate of k
-     !!!a2_r relative error in a**2
-     !!!T_r  relative error in T
-     !!!one_o_Vp2_r relative error in 1/Vphotodiode_variance
-    Notes :
-     We're assuming vib is mostly from thermal cantilever vibrations
-    (and then only from vibrations in the single vertical degree of freedom),
-    and not from other noise sources.
-     The various relative errors are returned to help you gauge the
-    largest source of random error in your measurement of k.
-    If one of them is small, don't bother repeating that measurment too often.
-    If one is large, try repeating that measurement more.
-    Remember that you need enough samples to have a valid error estimate in
-    the first place, and that none of this addresses any systematic errors.
+
+def analyze(bumps, temperatures, vibrations):
+    """Analyze data from `get_calibration_data()`
+
+    Inputs (all are arrays of recorded data):
+      bumps measured (V_photodiode / nm_tip) proportionality constant
+      temperatures    measured temperature (K)
+      vibrations  measured V_photodiode variance in free solution (V**2)
+    Outputs:
+      k    cantilever spring constant (in N/m, or equivalently nN/nm)
+      k_s  standard deviation in our estimate of k
+
+    Notes:
+
+    We're assuming vib is mostly from thermal cantilever vibrations
+    (and then only from vibrations in the single vertical degree of
+    freedom), and not from other noise sources.
+
+    If the error is large, check the relative errors
+    (`x.std()/x.mean()`)of your input arrays.  If one of them is
+    small, don't bother repeating that measurment too often.  If one
+    is large, try repeating that measurement more.  Remember that you
+    need enough samples to have a valid error estimate in the first
+    place, and that none of this addresses any systematic errors.
     """
-    calib_plot_kwargs, = calib_analyze._splitargs(calib_analyze, kwargs)
-    photoSensitivity2 = bumps**2
-    one_o_Vphoto2 = 1/vibs
-
-    photoSensitivity2_m = photoSensitivity2.mean()
-    T_m = Ts.mean()
-    one_o_Vphoto2_m = one_o_Vphoto2.mean()
-    # Vphoto / photoSensitivity = x
-    # k = kb T / <x**2> = kb T photoSensitiviy**2 * (1e9nm/m)**2 / 
-    #                                                       <Vphoto_std**2>
+    ps_m = bumps.mean()  # ps for photo-sensitivity
+    ps_s = bumps.std()
+    T_m = temperatures.mean()
+    T_s = temperatures.std()
+    v2_m = vibrations.mean()  # average voltage variance
+    v2_s = vibrations.std()
+
+    # Vphoto / photo_sensitivity = x
+    # k = kB T / <x**2> = kB T photo_sensitivity**2 / Vphoto_var
     #
-    # units,  photoSensitivity =  Vphoto/(Zcant in nm),
-    # so Vphoto/photoSensitivity = Zcant in nm
-    # so k = J/K * K / nm^2 * (1e9nm/m)**2 = N/m
-    k  = kb * T_m * photoSensitivity2_m * one_o_Vphoto2_m * 1e18
-
-    # propogation of errors !!!
-    # first, get standard deviations
-    photoSensitivity2_s = photoSensitivity2.std()
-    T_s = Ts.std()
-    one_o_Vphoto2_s = one_o_Vphoto2.std()
-    # !!!!now, get relative errors
-    photoSensitivity2_r = photoSensitivity2_s / photoSensitivity2_m
-    T_r = T_s / T_m
-    one_o_Vphoto2_r = one_o_Vphoto2_s / one_o_Vphoto2_m
-
-    k_s = k*(photoSensitivity2_r**2 + T_r**2 + one_o_Vphoto2_r**2)**0.5
-
-    calib_plot(bumps, Ts, vibs, **calib_plot_kwargs)
-    
-    return (k, k_s,
-            photoSensitivity2_m, photoSensitivity2_s,
-            T_m, T_s, one_o_Vphoto2_m, one_o_Vphoto2_s)
-
-@splittableKwargsFunction()
-def string_errors(k_m, k_s,
-                  photoSensitivity2_m, photoSensitivity2_s,
-                  T_m, T_s,
-                  one_o_Vphoto2_m, one_o_Vphoto2_s) :
-    k_r = k_s / k_m
-    photoSensitivity2_r = photoSensitivity2_s / photoSensitivity2_m
-    T_r = T_s / T_m
-    one_o_Vphoto2_r = one_o_Vphoto2_s / one_o_Vphoto2_m
-    string  = "Variable (units)              : mean +/- std. dev. (relative error)\n"
-    string += "Cantilever k (N/m)            : %g +/- %g (%g)\n" \
-              % (k_m, k_s, k_r)
-    string += "photoSensitivity**2 (V/nm)**2 : %g +/- %g (%g)\n" \
-              % (photoSensitivity2_m, photoSensitivity2_s, photoSensitivity2_r)
-    string += "T (K)                         : %g +/- %g (%g)\n" \
-              % (T_m, T_s, T_r)
-    string += "1/Vphoto**2 (1/V)**2          : %g +/- %g (%g)" \
-              % (one_o_Vphoto2_m, one_o_Vphoto2_s, one_o_Vphoto2_r)
-    return string
-
-@splittableKwargsFunction()
-def calib_save(bumps, Ts, vibs, log_dir=None) :
-    """
-    Save a dictonary with the bump, T, and vib data.
-    """
-    if log_dir != None :
-        data = {'bump':bumps, 'T':Ts, 'vib':vibs}
-        log = data_logger.data_log(log_dir, noclobber_logsubdir=False,
-                                   log_name="calib")
-        log.write_dict_of_arrays(data)
-
-def calib_load(datafile) :
-    "Load the dictionary data, using data_logger.date_load()"
-    dl = data_logger.data_load()
-    data = dl.read_dict_of_arrays(path)
-    return (data['bump'], data['T'], data['vib'])
-
-def calib_save_analysis(k, k_s,
-                        photoSensitivity2_m, photoSensitivity2_s,
-                        T_m, T_s, one_o_Vphoto2_m, one_o_Vphoto2_s,
-                        log_dir=None) :
-    if log_dir != None :
-        log = data_logger.data_log(log_dir, noclobber_logsubdir=False,
-                                   log_name="calib_analysis_text")
-        log.write_binary(string_errors(k, k_s,
-                                       photoSensitivity2_m, photoSensitivity2_s,
-                                       T_m, T_s,
-                                       one_o_Vphoto2_m, one_o_Vphoto2_s)
-                         +'\n')
-
-@splittableKwargsFunction()
-def calib_plot(bumps, Ts, vibs, plotVerbose=False) :
-    if plotVerbose or config.PYLAB_VERBOSE :
-        common._import_pylab()
-        common._pylab.figure(config.BASE_FIGNUM+4)
-        common._pylab.subplot(311)
-        common._pylab.plot(bumps, 'g.-')
-        common._pylab.title('Photodiode sensitivity (V/nm)')
-        common._pylab.subplot(312)
-        common._pylab.plot(Ts, 'r.-')
-        common._pylab.title('Temperature (K)')
-        common._pylab.subplot(313)
-        common._pylab.plot(vibs, 'b.-')
-        common._pylab.title('Thermal deflection variance (Volts**2)')
-        common._flush_plot()
-
-make_splittable_kwargs_function(calib_analyze,
-                                (calib_plot, 'bumps', 'Ts', 'vibs'))
-
-@splittableKwargsFunction((calib_analyze, 'bumps', 'Ts', 'vibs'))
-def calib_load_analyze_tweaks(bump_tweaks, vib_tweaks, T_tweaks=None) :
-    raise NotImplementedError
-    a = read_tweaked_bumps(bump_tweaks)
-    vib = V_photo_variance_from_file(vib_tweaks)
-    if T_tweaks == None:
-        pass
-    return analyze_calibration_data(a, T, vib, log_dir=log_dir)
-
-# commandline interface functions
-import scipy.io, sys
-
-def array_from_string(string):
-    ret = []
-    for num in string.split(',') :
-        ret.append(float(num))
-    assert len(ret) > 0
-    return numpy.array(ret)
-
-def read_data(ifile):
-    "ifile can be a filename string or open (seekable) file object"
-    unlabeled_data=scipy.io.read_array(file)
-    return unlabeled_data
-
-def get_array(string, filename, name) :
-    "get an array from supplied command line options"
-    if string != None :
-        array = array_from_string(string)
-    elif filename != None :
-        array = read_data(filename)
-    else :
-        raise Exception, "no %s information given" % (name)
-    return array
-
-if __name__ == '__main__' :
-    # command line interface
-    from optparse import OptionParser
-    
-    usage_string = ('%prog <bumps> <temps> <vibs>\n'
-                    '2008, W. Trevor King.\n'
-                    '\n'
-                    'Takes arrays of Vphotodiode sensitivity (V/nm), Temperature (K), \n'
-                    'and Vibration variance (V**2) as comma seperated lists.\n'
-                    'Returns the cantilever spring constant (pN/nm).\n'
-                    'for example:\n'
-                    ' $ %prog -b 0.02,0.03,0.025 -t 298.2,300.1 -v 6e-9,5.5e-9\n'
-                    )
-    parser = OptionParser(usage=usage_string, version='%prog '+common.VERSION)
-    parser.add_option('-b', '--bump-string', dest='bump_string',
-                      help='comma seperated photodiode sensitivities (V/nm)',
-                      type='string', metavar='BUMPS')
-    parser.add_option('-t', '--temp-string', dest='temp_string',
-                      help='comma seperated temperatures (K)',
-                      type='string', metavar='TEMPS')
-    parser.add_option('-v', '--vib-string', dest='vib_string',
-                      help='comma seperated vibration variances (V**2)',
-                      type='string', metavar='VIBS')
-    parser.add_option('-B', '--bump-file', dest='bump_file',
-                      help='comma seperated photodiode sensitivities (V/nm)',
-                      type='string', metavar='BUMPFILE')
-    parser.add_option('-T', '--temp-file', dest='temp_file',
-                      help='comma seperated temperatures (K)',
-                      type='string', metavar='TEMPFILE')
-    parser.add_option('-V', '--vib-file', dest='vib_file',
-                      help='comma seperated vibration variances (V**2)',
-                      type='string', metavar='VIBFILE')
-    parser.add_option('-C', '--celsius', dest='celsius',
-                      help='Use Celsius input temperatures instead of Kelvin (defaul %default)\n',
-                      action='store_true', default=False)
-    parser.add_option('-o', '--output-file', dest='ofilename',
-                      help='write output to FILE (default stdout)',
-                      type='string', metavar='FILE')
-    parser.add_option('-p', '--plot-inputs', dest='plot',
-                      help='plot the input arrays to check their distribution',
-                      action='store_true', default=False)
-    parser.add_option('', '--verbose', dest='verbose', action='store_true',
-                      help='print lots of debugging information',
-                      default=False)
-
-    options,args = parser.parse_args()
-    parser.destroy()
-    
-    config.TEXT_VERBOSE = options.verbose
-    config.PYLAB_INTERACTIVE = False
-    config.PYLAB_VERBOSE = options.plot
-    
-    bumps = get_array(options.bump_string, options.bump_file, "bump")
-    temps = get_array(options.temp_string, options.temp_file, "temp")
-    vibs = get_array(options.vib_string, options.vib_file, "vib")
-
-    #if options.plot :
-    #    calib_plot(bumps, temps, vibs)
-
-    if options.celsius :
-        for i in range(len(temps)) :
-            temps[i] = T_analyze.C_to_K(temps[i])
-
-    km,ks,ps2m,ps2s,Tm,Ts,ooVp2m,ooVp2s = \
-        calib_analyze(bumps, temps, vibs, plotVerbose=options.plot)
-
-    if options.verbose :
-        print >> sys.stderr, \
-            string_errors(km,ks,ps2m,ps2s,Tm,Ts,ooVp2m,ooVp2s)
-
-    if options.ofilename != None :
-        print >> file(options.ofilename, 'w'), km
-    else :
-        print km
+    # units,  photo_sensitivity =  Vphoto/(Zcant in m),
+    # so Vphoto/photo_sensitivity = Zcant in m
+    # so k = J/K * K / m^2 = J / m^2 = N/m
+    k  = _kB * T_m * ps_m**2 / v2_m
+
+    # propogation of errors
+    # dk/dT = k/T
+    dk_T = k/T_m * T_s
+    # dk/dps = 2k/ps
+    dk_ps = 2*k/ps_m * ps_s
+    # dk/dv2 = -k/v2
+    dk_v = -k/v2_m * v2_s
+
+    k_s = _numpy.sqrt(dk_T**2 + dk_ps**2 + dk_v**2)
+
+    _LOG.info('variable (units)         : '
+              'mean +/- std. dev. (relative error)')
+    _LOG.info('cantilever k (N/m)       : %g +/- %g (%g)' % (k, k_s, k_s/k))
+    _LOG.info('photo sensitivity (V/m)  : %g +/- %g (%g)'
+              % (ps_m, ps_s, ps_s/ps_m))
+    _LOG.info('T (K)                    : %g +/- %g (%g)'
+              % (T_m, T_s, T_s/T_m))
+    _LOG.info('vibration variance (V^2) : %g +/- %g (%g)'
+              % (v2_m, v2_s, v2_s/v2_m))
+
+    if _package_config['matplotlib']:
+        plot(bumps, temperatures, vibrations)
+
+    return (k, k_s)
+
+
+def plot(bumps, temperatures, vibrations):
+    if not _matplotlib:
+        raise _matplotlib_import_error
+    figure = _matplotlib_pyplot.figure()
+
+    bump_axes = figure.add_subplot(3, 1, 1)
+    T_axes = figure.add_subplot(3, 1, 2)
+    vib_axes = figure.add_subplot(3, 1, 3)
+
+    timestamp = _time.strftime('%H%M%S')
+    bump_axes.set_title('cantilever calibration %s' % timestamp)
+
+    bump_axes.plot(bumps, 'g.-')
+    bump_axes.set_ylabel('photodiode sensitivity (V/m)')
+    T_axes.plot(temperatures, 'r.-')
+    T_axes.set_ylabel('temperature (K)')
+    vib_axes.plot(vibrations, 'b.-')
+    vib_axes.set_ylabel('thermal deflection variance (V^2)')
+
+    if hasattr(figure, 'show'):
+        figure.show()
+    return figure
+_plot = plot  # alternative name for use inside analyze_all()
+
+
+def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
+                filename=None, group=None, plot=False, dry_run=False):
+    "(Re)analyze (and possibly plot) all data from a `calib()` run."
+    if not data.get('bump', None):
+        data['bump'] = _numpy.zeros((config['num-bumps'],), dtype=float)
+    if not data.get('temperature', None):
+        data['temperature'] = _numpy.zeros(
+            (config['num-temperatures'],), dtype=float)
+    if not data.get('vibrations', None):
+        data['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
+    if not isinstance(group, _h5py.Group) and not dry_run:
+        f = _h5py.File(filename, mode)
+        group = _h5_create_group(f, group)
+    else:
+        f = None
+    try:
+        for i,bump in enumerate(raw_data['bump']):        
+            data['bump'][i],changed = check_bump(
+                index=i, bump=bump, 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])
+        for i,temperature in enumerate(raw_data['temperature']):
+            data['temperature'][i],changed = check_temperature(
+                index=i, temperature=temperature,
+                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])
+        for i,vibration in enumerate(raw_data['vibration']):
+            data['vibration'][i],changed = check_vibration(
+                    index=i, vibration=vibration,
+                    deflection_channel_config=input_config, plot=plot,
+                    maximum_relative_error=maximum_relative_error)
+            if changed and not dry_run:
+                vibrations_changed = True
+                vibration_group = _h5_create_group(
+                    group, 'vibration/{}'.format(i))
+                _vibration_save(
+                    group=vibration_group, processed=data['vibration'])
+        k,k_s,changed = check_calibration(
+            k=data['processed']['spring_constant'],
+            k_s=data['processed']['spring_constant_deviation'],
+            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:
+            calibration_group = _h5_create_group(group, 'calibration')
+            if bumps_changed:
+                calib_save(group=calibration_group, bump=data['bump'])
+            if temperatures_changed:
+                calib_save(
+                    group=calibration_group, temperature=data['temperature'])
+            if vibrations_changed:
+                calib_save(
+                    group=calibration_group, vibration=data['vibration'])
+            if changed:
+                calib_save(group=calibration_group, k=k, k_s=k_s)
+    finally:
+        if f:
+            f.close()
+    if plot:
+        _plot(bumps=data['raw']['bump'],
+             temperatures=data['raw']['temperature'],
+             vibrations=data['raw']['vibration'])
+    return (k, k_s)
+
+def check_bump(index, bump, maximum_relative_error, **kwargs):
+    changed = False
+    sensitivity = _bump_analyze(
+        config=bump['config']['bump'], data=bump['raw'], **kwargs)
+    if bump.get('processed', None) is None:
+        changed = True            
+        _LOG.warn('new analysis for bump {}: {}'.format(index, sensitivity))
+    else:
+        rel_error = abs(sensitivity - bump['processed'])/bump['processed']
+        if rel_error > maximum_relative_error:
+            changed = True
+            _LOG.warn(("new analysis doesn't match for bump {}: {} -> {} "
+                       "(difference: {}, relative error: {})").format(
+                    index, bump['processed'], sensitivity,
+                    sensitivity-bump['processed'], rel_error))
+    return (sensitivity, changed)
+
+def check_temperature(index, temperature, maximum_relative_error, **kwargs):
+    changed = False
+    temp = _temperature_analyze(
+        config=temperature['config']['temperature'],
+        temperature=temperature['raw'], **kwargs)
+    if temperature.get('processed', None) is None:
+        changed = True            
+        _LOG.warn('new analysis for temperature {}: {}'.format(index, temp))
+    else:
+        rel_error = abs(temp - temperature['processed']
+                        )/temperature['processed']
+        if rel_error > maximum_relative_error:
+            changed = True
+            _LOG.warn(("new analysis doesn't match for temperature "
+                       "{} -> {} (difference: {}, relative error: {})"
+                       ).format(
+                    index, temperature['processed'], temp,
+                    temp-temperature['processed'], rel_error))
+    return (temp, changed)
+
+def check_vibration(index, vibration, maximum_relative_error, **kwargs):
+    changed = False
+    variance = _vibration_analyze(
+        config=vibration['config']['vibration'],
+        deflection=vibration['raw'], **kwargs)
+    if vibration.get('processed', None) is None:
+        changed = True
+        _LOG.warn('new analysis for temperature {}: {}'.format(
+                index, variance))
+    else:
+        rel_error = abs(variance-vibration['processed'])/vibration['processed']
+        if rel_error > maximum_relative_error:
+            _LOG.warn(("new analysis doesn't match for vibration {}: {} != {} "
+                       "(difference: {}, relative error: {})").format(
+                    index, variance, vibration['processed'],
+                    variance-vibration['processed'], rel_error))
+    return (variance, changed)
+
+def check_calibration(k, k_s, maximum_relative_error, **kwargs):
+    changed = False
+    new_k,new_k_s = analyze(**kwargs)
+    if k is None:
+        changed = True
+        _LOG.warn('new analysis for the spring constant: {}'.format(new_k))
+    else:
+        rel_error = abs(new_k-k)/k
+        if rel_error > maximum_relative_error:
+            _LOG.warn(("new analysis doesn't match for the spring constant: "
+                       "{} != {} (difference: {}, relative error: {})").format(
+                    new_k, k, new_k-k, rel_error))
+    if k_s is None:
+        changed = True
+        _LOG.warn('new analysis for the spring constant deviation: {}'.format(
+                new_k_s))
+    else:
+        rel_error = abs(new_k-k)/k
+        if rel_error > maximum_relative_error:
+            _LOG.warn(
+                ("new analysis doesn't match for the spring constant deviation"
+                 ": {} != {} (difference: {}, relative error: {})").format(
+                    new_k_s, k_s, new_k_s-k_s, rel_error))
+    return (new_k, new_k_s, changed)