Oops, use numpy arrays instead of lists for the last commit.
[calibcant.git] / calibcant / analyze.py
index d32e7faf72ee8fbb0cdc8a4067a0a097b8b52385..1033dcc3b692865f9b87f56d7018fad882dc7ad5 100644 (file)
@@ -1,22 +1,20 @@
 # 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/>.
 
 """Calculate `k` from arrays of bumps, temperatures, and vibrations.
 
@@ -44,13 +42,14 @@ Which are related by the parameters:
 >>> import os
 >>> import tempfile
 >>> import numpy
->>> from h5config.hdf5 import pprint_HDF5
->>> from .config import HDF5_CalibrationConfig
+>>> from h5config.storage.hdf5 import HDF5_Storage, pprint_HDF5
+>>> from .config import CalibrationConfig
 
 >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='calibcant-')
 >>> os.close(fd)
 
->>> calibration_config = HDF5_CalibrationConfig(filename, '/calib/config/')
+>>> calibration_config = CalibrationConfig(storage=HDF5_Storage(
+...         filename=filename, group='/calib/config/'))
 >>> 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))
@@ -79,15 +78,21 @@ photodiode sensitivity (bumps).
 /
   /calib
     /calib/config
-      <HDF5 dataset "num-bumps": shape (), type "|S2">
+      <HDF5 dataset "bump": shape (), type "|S1">
+<BLANKLINE>
+      <HDF5 dataset "num-bumps": shape (), type "<i4">
         10
-      <HDF5 dataset "num-temperatures": shape (), type "|S2">
+      <HDF5 dataset "num-temperatures": shape (), type "<i4">
         10
-      <HDF5 dataset "num-vibrations": shape (), type "|S2">
+      <HDF5 dataset "num-vibrations": shape (), type "<i4">
         20
-      <HDF5 dataset "temperature-sleep": shape (), type "|S1">
+      <HDF5 dataset "temperature": shape (), type "|S1">
+<BLANKLINE>
+      <HDF5 dataset "temperature-sleep": shape (), type "<i4">
         1
-      <HDF5 dataset "vibration-spacing": shape (), type "|S5">
+      <HDF5 dataset "vibration": shape (), type "|S1">
+<BLANKLINE>
+      <HDF5 dataset "vibration-spacing": shape (), type "<f8">
         5e-05
     /calib/processed
       /calib/processed/spring-constant
@@ -140,14 +145,15 @@ except (ImportError, RuntimeError), e:
     _matplotlib = None
     _matplotlib_import_error = e
 
-from h5config.hdf5 import h5_create_group as _h5_create_group
+from h5config.storage.hdf5 import HDF5_Storage as _HDF5_Storage
+from h5config.storage.hdf5 import h5_create_group as _h5_create_group
 
 from . import LOG as _LOG
 from . import package_config as _package_config
 from .bump_analyze import bump_analyze as _bump_analyze
 from .bump_analyze import bump_load as _bump_load
 from .bump_analyze import bump_save as _bump_save
-from .config import HDF5_CalibrationConfig as _HDF5_CalibrationConfig
+from .config import CalibrationConfig as _CalibrationConfig
 from .T_analyze import T_analyze as _temperature_analyze
 from .T_analyze import T_load as _temperature_load
 from .T_analyze import T_save as _temperature_save
@@ -226,7 +232,8 @@ def calib_save(filename, group='/', bumps=None, temperatures=None,
         cwg = _h5_create_group(f, group)
         if calibration_config is not None:
             config_cwg = _h5_create_group(cwg, 'config')
-            calibration_config.save(group=config_cwg)
+            storage = _HDF5_Storage()
+            storage.save(config=calibration_config, group=config_cwg)
         if bumps is not None:
             try:
                 del cwg['raw/photodiode-sensitivity/data']
@@ -296,15 +303,16 @@ def calib_load(filename, group='/'):
         except KeyError:
             pass
         try:
-            k = f[group+'processed/spring-constant/data'][...]
+            k = float(f[group+'processed/spring-constant/data'][...])
         except KeyError:
             pass
         try:
-            k_s = f[group+'processed/spring-constant/standard-deviation'][...]
+            k_s = float(
+                f[group+'processed/spring-constant/standard-deviation'][...])
         except KeyError:
             pass
-    calibration_config = _HDF5_CalibrationConfig(
-        filename=filename, group=group+'config/')
+    calibration_config = _CalibrationConfig(storage=_HDF5_Storage(
+            filename=filename, group=group+'config/'))
     calibration_config.load()
     return (bumps, temperatures, vibrations, calibration_config, k, k_s)
 
@@ -327,7 +335,8 @@ def calib_plot(bumps, temperatures, vibrations):
     vib_axes.plot(vibrations, 'b.-')
     vib_axes.set_ylabel('thermal deflection variance (V^2)')
 
-    figure.show()
+    if hasattr(figure, 'show'):
+        figure.show()
 
 
 def calib_load_all(filename, group='/'):
@@ -337,13 +346,12 @@ def calib_load_all(filename, group='/'):
         filename, group+'calibration/')
     bump_details = []
     for i in range(calibration_config['num-bumps']):
-        (raw_bump,bump_config,z_channel_config,z_axis_config,
-         deflection_channel_config,processed_bump) = _bump_load(
+        (raw_bump,bump_config,z_axis_config,deflection_channel_config,
+         processed_bump) = _bump_load(
             filename=filename, group='%sbump/%d/' % (group, i))
         bump_details.append({
                 'raw_bump': raw_bump,
                 'bump_config': bump_config,
-                'z_channel_config': z_channel_config,
                 'z_axis_config': z_axis_config,
                 'deflection_channel_config': deflection_channel_config,
                 'processed_bump': processed_bump,
@@ -387,15 +395,23 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5,
     assert group.endswith('/'), group
     bumps,temperatures,vibrations,calibration_config,k,k_s = calib_load(
         filename, group+'calibration/')
+    if bumps is None:
+        bumps = _numpy.zeros(
+            (calibration_config['num-bumps'],), dtype=float)
+    if temperatures is None:
+        temperatures = _numpy.zeros(
+            (calibration_config['num-temperatures'],), dtype=float)
+    if vibrations is None:
+        vibrations = _numpy.zeros(
+            (calibration_config['num-vibrations'],), dtype=float)
     changed_bump = changed_temperature = changed_vibration = False
     for i in range(calibration_config['num-bumps']):
         bump_group = '%sbump/%d/' % (group, i)
-        (raw_bump,bump_config,z_channel_config,z_axis_config,
+        (raw_bump,bump_config,z_axis_config,
          deflection_channel_config,processed_bump) = _bump_load(
             filename=filename, group=bump_group)
         sensitivity = _bump_analyze(
             data=raw_bump, bump_config=bump_config,
-            z_channel_config=z_channel_config,
             z_axis_config=z_axis_config,
             deflection_channel_config=deflection_channel_config)
         bumps[i] = sensitivity
@@ -482,7 +498,6 @@ def calib_plot_all(bumps, bump_details, temperatures, temperature_details,
     for i,bump in enumerate(bump_details):
         sensitivity = _bump_analyze(
             data=bump['raw_bump'], bump_config=bump['bump_config'],
-            z_channel_config=bump['z_channel_config'],
             z_axis_config=bump['z_axis_config'],
             deflection_channel_config=bump['deflection_channel_config'],
             plot=True)