Get calibcant working with the new load_from_config-based pyafm.
[calibcant.git] / calibcant / vibration.py
similarity index 64%
rename from calibcant/vib.py
rename to calibcant/vibration.py
index 05a9dea4f01df3e075f68e609d2561c6623e8353..1313dad2d00f5df97742bfd2f152b06583c39880 100644 (file)
@@ -31,24 +31,24 @@ from pycomedi.utility import inttrig_insn as _inttrig_insn
 from pycomedi.utility import Reader as _Reader
 
 from . import LOG as _LOG
-from .vib_analyze import vib_analyze as _vib_analyze
-from .vib_analyze import vib_save as _vib_save
+from .vibration_analyze import analyze as _analyze
+from .vibration_analyze import save as _save
 
 
-def vib_acquire(piezo, vibration_config):
+def acquire(piezo, config):
     """Record thermal vibration data for `piezo` at its current position.
 
     Inputs:
       piezo             a pypiezo.afm.AFMPiezo instance
-      vibration_config  a .config._VibrationConfig instance
+      config  a .config.Config instance
     """
     _LOG.debug('prepare vibration aquisition command')
 
     # round up to the nearest power of two, for efficient FFT-ing
     n_samps = _ceil_pow_of_two(
-        vibration_config['sample-time']*vibration_config['frequency'])
-    time = n_samps / vibration_config['frequency']
-    scan_period_ns = int(1e9 / vibration_config['frequency'])
+        config['sample-time']*config['frequency'])
+    time = n_samps / config['frequency']
+    scan_period_ns = int(1e9 / config['frequency'])
 
     input_channel = piezo.input_channel_by_name('deflection')
     channel = input_channel.channel
@@ -72,8 +72,8 @@ def vib_acquire(piezo, vibration_config):
         _LOG.debug('command test %d: %s' % (i,rc))
 
     _LOG.info('get %g seconds of vibration data at %g Hz'
-              % (vibration_config['sample-time'],
-                 vibration_config['frequency']))
+              % (config['sample-time'],
+                 config['frequency']))
     channel.subdevice.command()
     reader = _Reader(channel.subdevice, data)
     reader.start()
@@ -82,54 +82,37 @@ def vib_acquire(piezo, vibration_config):
     data = data.reshape((data.size,))
     return data
 
-def vib(piezo, vibration_config, filename, group='/'):
-    """Wrapper around vib_acquire(), vib_analyze(), vib_save().
+def run(piezo, config, filename, group='/'):
+    """Wrapper around acquire(), analyze(), save().
 
     >>> import os
     >>> import tempfile
     >>> from h5config.storage.hdf5 import pprint_HDF5
-    >>> from pycomedi.device import Device
-    >>> from pycomedi.subdevice import StreamingSubdevice
-    >>> from pycomedi.channel import AnalogChannel
-    >>> from pycomedi.constant import AREF, SUBDEVICE_TYPE, UNIT
-    >>> from pypiezo.afm import AFMPiezo
-    >>> from pypiezo.base import InputChannel
-    >>> from pypiezo.config import ChannelConfig
+    >>> from pyafm.storage import load_afm
     >>> from .config import VibrationConfig
 
-    Setup an `AFMPiezo` instance.
-
     >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='calibcant-')
     >>> os.close(fd)
 
-    >>> d = Device('/dev/comedi0')
-    >>> d.open()
-
-    >>> s_in = d.find_subdevice_by_type(SUBDEVICE_TYPE.ai,
-    ...     factory=StreamingSubdevice)
-
-    >>> channel = s_in.channel(0, factory=AnalogChannel, aref=AREF.diff)
-    >>> channel.range = channel.find_range(
-    ...     unit=UNIT.volt, min=-10, max=10)
-    >>> channel_config = ChannelConfig()
-    >>> channel_config['name'] = 'deflection'
-
-    >>> c = InputChannel(config=channel_config, channel=channel)
-    >>> c.setup_config()
-
-    >>> piezo = AFMPiezo(axes=[], inputs=[c])
+    >>> devices = []
+    >>> afm = load_afm(devices=devices)
+    >>> afm.load_from_config()
 
     Test a vibration:
 
-    >>> vibration_config = VibrationConfig()
-    >>> vib(piezo, vibration_config, filename, group='/vibration')
-    TODO: replace skipped example data with real-world values
+    >>> config = VibrationConfig()
+    >>> output = run(piezo=afm.piezo, config=config, filename=filename,
+    ...     group='/vibration')
+    >>> output  # doctest: +SKIP
+    4.1589771694838657e-05
     >>> pprint_HDF5(filename)  # doctest: +ELLIPSIS, +REPORT_UDIFF
     /
       /vibration
         /vibration/config
           /vibration/config/deflection
-            <HDF5 dataset "channel": shape (), type "<i4">
+            <HDF5 dataset "analog-reference": shape (), type "|S4">
+              diff
+            <HDF5 dataset "channel": shape (), type "<i...">
               0
             <HDF5 dataset "conversion-coefficients": shape (2,), type "<f8">
               [ -1.00000000e+01   3.05180438e-04]
@@ -141,16 +124,16 @@ def vib(piezo, vibration_config, filename, group='/'):
               [    0.    3276.75]
             <HDF5 dataset "inverse-conversion-origin": shape (), type "<f8">
               -10.0
-            <HDF5 dataset "maxdata": shape (), type "<i8">
+            <HDF5 dataset "maxdata": shape (), type "<i...">
               65535
             <HDF5 dataset "name": shape (), type "|S10">
               deflection
-            <HDF5 dataset "range": shape (), type "<i4">
+            <HDF5 dataset "range": shape (), type "<i...">
               0
-            <HDF5 dataset "subdevice": shape (), type "<i4">
+            <HDF5 dataset "subdevice": shape (), type "<i...">
               0
           /vibration/config/vibration
-            <HDF5 dataset "chunk-size": shape (), type "<i4">
+            <HDF5 dataset "chunk-size": shape (), type "<i...">
               2048
             <HDF5 dataset "frequency": shape (), type "<f8">
               50000.0
@@ -162,19 +145,25 @@ def vib(piezo, vibration_config, filename, group='/'):
               Breit-Wigner
             <HDF5 dataset "overlap": shape (), type "|b1">
               False
-            <HDF5 dataset "sample-time": shape (), type "<i4">
+            <HDF5 dataset "sample-time": shape (), type "<i...">
               1
             <HDF5 dataset "window": shape (), type "|S4">
               Hann
-        <HDF5 dataset "processed": shape (), type "<f8">
-          ...
+        /vibration/processed
+          <HDF5 dataset "data": shape (), type "<f8">
+            ...
+          <HDF5 dataset "units": shape (), type "|S6">
+            V^2/Hz
         /vibration/raw
-          <HDF5 dataset "deflection": shape (65536,), type "<u2">
+          <HDF5 dataset "data": shape (65536,), type "<u2">
             [...]
+          <HDF5 dataset "units": shape (), type "|S4">
+            bits
 
-    Close the Comedi device.
+    Close the Comedi devices.
 
-    >>> d.close()
+    >>> for device in devices:
+    ...     device.close()
 
     Cleanup our temporary config file.
 
@@ -184,12 +173,11 @@ def vib(piezo, vibration_config, filename, group='/'):
     
     deflection_channel_config = deflection_input_channel.config
 
-    deflection = vib_acquire(piezo, vibration_config)
-    variance = _vib_analyze(
-        deflection, vibration_config, deflection_channel_config)
-    _vib_save(
-        filename, group, raw_vibration=deflection,
-        vibration_config=vibration_config,
+    deflection = acquire(piezo, config)
+    variance = _analyze(
+        deflection, config, deflection_channel_config)
+    _save(
+        filename=filename, group=group, raw=deflection, config=config,
         deflection_channel_config=deflection_channel_config,
-        processed_vibration=variance)
+        processed=variance)
     return variance