From: W. Trevor King Date: Thu, 15 Mar 2012 20:44:30 +0000 (-0400) Subject: Add pyafm.storage module with save_afm and load_afm. X-Git-Tag: v0.4~27 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b249631b679989160119845ecbbb3dfb1725957d;p=pyafm.git Add pyafm.storage module with save_afm and load_afm. Also remove some underscore prefixes from the AFM doctests. I'm too used to trying to keep the modules themselves clean, but that's no reason to go confusing newbies ;). --- diff --git a/pyafm/afm.py b/pyafm/afm.py index 2681b56..a68b772 100644 --- a/pyafm/afm.py +++ b/pyafm/afm.py @@ -50,30 +50,37 @@ class AFM (object): temperature | temperature.Controller instance or None Optional temperature monitoring and control. + >>> import os + >>> import tempfile >>> from pycomedi.device import Device - >>> from pycomedi import constant as _constant - >>> import pypiezo.config as _pypiezo_config - >>> import pyafm.config as _config + >>> from pycomedi import constant + >>> import pypiezo.config + >>> import pyafm.config + >>> import pyafm.storage + >>> from h5config.storage.hdf5 import pprint_HDF5 + + >>> fd,filename = tempfile.mkstemp(suffix='.h5', prefix='pyafm-') + >>> os.close(fd) >>> device = Device('/dev/comedi0') >>> device.open() - >>> config = _config.AFMConfig() - >>> config['piezo'] = _pypiezo_config.PiezoConfig() + >>> config = pyafm.config.AFMConfig() + >>> config['piezo'] = pypiezo.config.PiezoConfig() >>> config['piezo']['name'] = 'test piezo' - >>> config['piezo']['axes'] = [_pypiezo_config.AxisConfig()] + >>> config['piezo']['axes'] = [pypiezo.config.AxisConfig()] >>> config['piezo']['axes'][0]['channel'] = ( - ... _pypiezo_config.OutputChannelConfig()) + ... pypiezo.config.OutputChannelConfig()) >>> config['piezo']['axes'][0]['channel']['name'] = 'z' - >>> config['piezo']['inputs'] = [_pypiezo_config.InputChannelConfig()] + >>> config['piezo']['inputs'] = [pypiezo.config.InputChannelConfig()] >>> config['piezo']['inputs'][0]['name'] = 'deflection' - >>> config['stepper'] = _config.StepperConfig() - >>> config['stepper']['port'] = _config.DigitalPortConfig() + >>> config['stepper'] = pyafm.config.StepperConfig() + >>> config['stepper']['port'] = pyafm.config.DigitalPortConfig() >>> config['stepper']['port']['channels'] = [1, 2, 3, 4] - >>> config['stepper']['port']['direction'] = _constant.IO_DIRECTION.output + >>> config['stepper']['port']['direction'] = constant.IO_DIRECTION.output >>> config['stepper']['port']['name'] = 'stepper port' >>> config['stepper']['name'] = 'test stepper' - >>> config['temperature'] = _config.TemperatureConfig() + >>> config['temperature'] = pyafm.config.TemperatureConfig() >>> config['temperature']['name'] = 'test temperature' >>> afm = AFM(config=config, devices=[device]) @@ -142,9 +149,128 @@ class AFM (object): max-current: 0.0 far: 3e-05 + >>> pyafm.storage.save_afm(afm=afm, filename=filename) + >>> pprint_HDF5(filename=filename) # doctest: +REPORT_UDIFF + / + + 3e-05 + + + + + /piezo + /piezo/axes + /piezo/axes/0 + /piezo/axes/0/channel + + ground + + 0 + + [ -1.00000000e+01 3.05180438e-04] + + 0.0 + + /dev/comedi0 + + [ 0. 3276.75] + + -10.0 + + 65535 + + z + + 0 + + 1 + + 1.0 + + 10.0 + + -10.0 + + + + 1.0 + /piezo/inputs + /piezo/inputs/0 + + ground + + 0 + + [ -1.00000000e+01 3.05180438e-04] + + 0.0 + + /dev/comedi0 + + [ 0. 3276.75] + + -10.0 + + 65535 + + deflection + + 0 + + 0 + + test piezo + /stepper + + 100 + + 0.01 + + True + + True + + test stepper + /stepper/port + + [1 2 3 4] + + /dev/comedi0 + + output + + stepper port + + 2 + + dio + + 1.7e-07 + /temperature + + 9600 + + 1 + + /dev/ttyS0 + + 0.0 + + test temperature + + Celsius + >>> afm2 = pyafm.storage.load_afm(filename=filename, devices=[device]) + + >>> afm2.get_temperature() # doctest: +SKIP + 297.37 + It's hard to test anything else without pugging into an actual AFM. >>> device.close() + + Cleanup our temporary config file. + + >>> os.remove(filename) """ def __init__(self, config, piezo=None, stepper=None, temperature=None, devices=None): diff --git a/pyafm/storage.py b/pyafm/storage.py new file mode 100644 index 0000000..562df94 --- /dev/null +++ b/pyafm/storage.py @@ -0,0 +1,38 @@ +# Copyright + +import os.path as _os_path + +import h5py as _h5py +from h5config.storage.hdf5 import HDF5_Storage as _HDF5_Storage + +from . import LOG as _LOG +from .afm import AFM as AFM +from .config import AFMConfig as _AFMConfig + + +DEFAULT_FILENAME = _os_path.expanduser(_os_path.join( + '~', '.config', 'pyafm-default.h5')) +DEFAULT_GROUP = '/' + + +def save_afm(afm, filename=None, group=None): + if filename is None: + filename = DEFAULT_FILENAME + if group is None: + group = DEFAULT_GROUP + assert group.endswith('/'), group + _LOG.info('saving AFM config to {} {}'.format(filename, group)) + storage = _HDF5_Storage(filename=filename, group=group) + storage.save(config=afm.config) + +def load_afm(filename=None, group=None, devices=None): + if filename is None: + filename = DEFAULT_FILENAME + if group is None: + group = DEFAULT_GROUP + assert group.endswith('/'), group + _LOG.info('loading AFM config from {} {}'.format(filename, group)) + config = _AFMConfig(storage=_HDF5_Storage(filename=filename, group=group)) + _LOG.debug( + 'constructing AFM from configuration:\n{}'.format(config.dump())) + return AFM(config=config, devices=devices)