Fix problems with the transition to the new nested-Config h5config package.
[calibcant.git] / calibcant / vib_analyze.py
index d226d358504a819847a84ddabd6661649f10167c..bf7455839e3226a6187a8da5f948a9461a80184d 100644 (file)
@@ -31,23 +31,17 @@ The relevent physical quantities are :
 >>> import random
 >>> import tempfile
 >>> import numpy
->>> from .config import HDF5_VibrationConfig
->>> from pypiezo.config import pprint_HDF5, HDF5_ChannelConfig
+>>> from .config import VibrationConfig
+>>> from h5config.storage.hdf5 import pprint_HDF5
+>>> from pypiezo.test import get_piezo_config
 >>> from pypiezo.base import convert_volts_to_bits
 
 >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='calibcant-')
 >>> os.close(fd)
 
->>> vibration_config = HDF5_VibrationConfig(
-...     filename=filename, group='/vibration/config/vibration')
+>>> piezo_config = get_piezo_config()
+>>> vibration_config = VibrationConfig()
 >>> vibration_config['frequency'] = 50e3
->>> vibration_config.save()
->>> deflection_channel_config = HDF5_ChannelConfig(filename=None)
->>> deflection_channel_config['maxdata'] = 200
->>> deflection_channel_config['conversion-coefficients'] = (0,1)
->>> deflection_channel_config['conversion-origin'] = 0
->>> deflection_channel_config['inverse-conversion-coefficients'] = (0,1)
->>> deflection_channel_config['inverse-conversion-origin'] = 0
 
 We'll be generating a test vibration time series with the following
 parameters.  Make sure these are all floats to avoid accidental
@@ -58,7 +52,7 @@ integer division in later steps.
 >>> k = 0.05        # N/m
 >>> T = 1/vibration_config['frequency']
 >>> T  # doctest: +ELLIPSIS
-2.00...e-05
+2...e-05
 >>> N = int(2**15)  # count
 >>> F_sigma = 1e3   # N
 
@@ -134,7 +128,8 @@ Rearranging and shifting to `j=i-1`
 Convert the simulated data to bits.
 
 >>> deflection = x
->>> deflection_bits = convert_volts_to_bits(deflection_channel_config, x)
+>>> deflection_bits = convert_volts_to_bits(
+...     piezo_config.select_config('inputs', 'deflection'), x)
 
 Analyze the simulated data.
 
@@ -145,14 +140,16 @@ Analyze the simulated data.
 True
 
 >>> processed_vibration = vib_analyze(
-...     deflection_bits, vibration_config, deflection_channel_config)
+...     deflection_bits, vibration_config,
+...     piezo_config.select_config('inputs', 'deflection'))
 >>> processed_vibration  # doctest: +SKIP
 136457906.25574699
 
 >>> vib_plot(deflection=deflection_bits, vibration_config=vibration_config)
 >>> vib_save(filename=filename, group='/vibration/',
 ...     raw_vibration=deflection_bits, vibration_config=vibration_config,
-...     deflection_channel_config=deflection_channel_config,
+...     deflection_channel_config=piezo_config.select_config(
+...         'inputs', 'deflection'),
 ...     processed_vibration=processed_vibration)
 
 >>> pprint_HDF5(filename)  # doctest: +ELLIPSIS, +REPORT_UDIFF
@@ -160,38 +157,40 @@ True
   /vibration
     /vibration/config
       /vibration/config/deflection
-        <HDF5 dataset "channel": shape (), type "|S1">
+        <HDF5 dataset "channel": shape (), type "<i4">
           0
-        <HDF5 dataset "conversion-coefficients": shape (), type "|S4">
-          0, 1
-        <HDF5 dataset "conversion-origin": shape (), type "|S1">
+        <HDF5 dataset "conversion-coefficients": shape (2,), type "<i4">
+          [0 1]
+        <HDF5 dataset "conversion-origin": shape (), type "<i4">
           0
         <HDF5 dataset "device": shape (), type "|S12">
           /dev/comedi0
-        <HDF5 dataset "inverse-conversion-coefficients": shape (), type "|S4">
-          0, 1
-        <HDF5 dataset "inverse-conversion-origin": shape (), type "|S1">
+        <HDF5 dataset "inverse-conversion-coefficients": shape (2,), type "<i4">
+          [0 1]
+        <HDF5 dataset "inverse-conversion-origin": shape (), type "<i4">
           0
-        <HDF5 dataset "maxdata": shape (), type "|S3">
-          200
-        <HDF5 dataset "range": shape (), type "|S1">
+        <HDF5 dataset "maxdata": shape (), type "<i4">
+          100
+        <HDF5 dataset "name": shape (), type "|S10">
+          deflection
+        <HDF5 dataset "range": shape (), type "<i4">
           1
-        <HDF5 dataset "subdevice": shape (), type "|S2">
+        <HDF5 dataset "subdevice": shape (), type "<i4">
           -1
       /vibration/config/vibration
-        <HDF5 dataset "chunk-size": shape (), type "|S4">
+        <HDF5 dataset "chunk-size": shape (), type "<i4">
           2048
-        <HDF5 dataset "frequency": shape (), type "|S7">
+        <HDF5 dataset "frequency": shape (), type "<f8">
           50000.0
-        <HDF5 dataset "maximum-fit-frequency": shape (), type "|S7">
+        <HDF5 dataset "maximum-fit-frequency": shape (), type "<f8">
           25000.0
-        <HDF5 dataset "minimum-fit-frequency": shape (), type "|S5">
+        <HDF5 dataset "minimum-fit-frequency": shape (), type "<f8">
           500.0
         <HDF5 dataset "model": shape (), type "|S12">
           Breit-Wigner
-        <HDF5 dataset "overlap": shape (), type "|S2">
-          no
-        <HDF5 dataset "sample-time": shape (), type "|S1">
+        <HDF5 dataset "overlap": shape (), type "|b1">
+          False
+        <HDF5 dataset "sample-time": shape (), type "<i4">
           1
         <HDF5 dataset "window": shape (), type "|S4">
           Hann
@@ -228,17 +227,18 @@ except (ImportError, RuntimeError), e:
     _matplotlib = None
     _matplotlib_import_error = e
 
+from h5config.storage.hdf5 import HDF5_Storage as _HDF5_Storage
+from h5config.storage.hdf5 import h5_create_group as _h5_create_group
 import FFT_tools as _FFT_tools
 from pypiezo.base import convert_bits_to_volts as _convert_bits_to_volts
-from pypiezo.config import HDF5_ChannelConfig as _HDF5_ChannelConfig
-from pypiezo.config import h5_create_group as _h5_create_group
+from pypiezo.config import ChannelConfig as _ChannelConfig
 
 from . import LOG as _LOG
-from . import base_config as _base_config
+from . import package_config as _package_config
 from .config import Variance as _Variance
 from .config import BreitWigner as _BreitWigner
 from .config import OffsetBreitWigner as _OffsetBreitWigner
-from .config import HDF5_VibrationConfig as _HDF5_VibrationConfig
+from .config import VibrationConfig as _VibrationConfig
 
 
 def vib_analyze_naive(deflection):
@@ -271,7 +271,7 @@ def vib_analyze(deflection, vibration_config, deflection_channel_config,
       deflection        Vphoto deflection input in bits.
       vibration_config  `.config._VibrationConfig` instance
       deflection_channel_config
-                        deflection `pypiezo.config._ChannelConfig` instance
+                        deflection `pypiezo.config.ChannelConfig` instance
       plot              boolean overriding matplotlib config setting.
 
     The conversion to frequency space generates an average power
@@ -305,7 +305,7 @@ def vib_analyze(deflection, vibration_config, deflection_channel_config,
     _LOG.debug('fit PSD(f) = C / ((A**2-f**2)**2 + (f*B)**2) with '
                'A = %g, B = %g, C = %g, D = %g' % (A, B, C, D))
 
-    if plot or _base_config['matplotlib']:
+    if plot or _package_config['matplotlib']:
         vib_plot(deflection, freq_axis, power, A, B, C, D,
                  vibration_config=vibration_config)
 
@@ -314,7 +314,7 @@ def vib_analyze(deflection, vibration_config, deflection_channel_config,
 
     _LOG.debug('fitted deflection variance: %g V**2' % fitted_variance)
 
-    if _base_config['matplotlib']:
+    if _package_config['matplotlib']:
         vib_plot(deflection, freq_axis, power, A, B, C, D,
                  vibration_config=vibration_config)
 
@@ -436,7 +436,8 @@ def fit_psd(freq_axis, psd_data, min_frequency=500, max_frequency=25000,
         A,B,C = p
         D = 0
     A=abs(A) # A and B only show up as squares in f(x)
-    B=abs(B) # so ensure we get positive values
+    B=abs(B) # so ensure we get positive values.
+    C=abs(C) # Only abs(C) is used in breit_wigner().
     return (A, B, C, D)
 
 def breit_wigner_area(A, B, C):
@@ -458,13 +459,14 @@ def vib_save(filename, group='/', raw_vibration=None, vibration_config=None,
             except KeyError:
                 pass
             cwg['raw/deflection'] = raw_vibration
+        storage = _HDF5_Storage()
         for config,key in [(vibration_config, 'config/vibration'),
                            (deflection_channel_config,
                             'config/deflection')]:
             if config is None:
                 continue
             config_cwg = _h5_create_group(cwg, key)
-            config.save(group=config_cwg)
+            storage.save(config=config, group=config_cwg)
         if processed_vibration is not None:
             try:
                 del cwg['processed']
@@ -481,12 +483,13 @@ def vib_load(filename, group='/'):
             raw_vibration = f[group+'raw/deflection'][...]
         except KeyError:
             pass
-        for Config,key in [(_HDF5_VibrationConfig, 'config/vibration'),
-                           (_HDF5_ChannelConfig, 'config/deflection')]:
-            config = Config(filename=filename, group=group+key)
+        for Config,key in [(_VibrationConfig, 'config/vibration'),
+                           (_ChannelConfig, 'config/deflection')]:
+            config = Config(storage=_HDF5_Storage(
+                    filename=filename, group=group+key))
             configs.append(config)
         try:
-            processed_vibration = f[group+'processed'][...]
+            processed_vibration = float(f[group+'processed'][...])
         except KeyError:
             pass
     ret = [raw_vibration]