-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
#
# This file is part of Hooke.
#
storing force curves.
"""
+from copy_reg import dispatch_table
+from copy import _reconstruct, _copy_dispatch
import logging
import os.path
import numpy
from .command_stack import CommandStack
-from . import experiment
class NotRecognized (ValueError):
The data-type is also YAMLable (see :mod:`hooke.util.yaml`).
>>> import yaml
- >>> print yaml.dump(d)
- null
- ...
+ >>> s = yaml.dump(d)
+ >>> print s
+ !hooke.curve.DataInfo
+ columns: [distance (m), force (N)]
<BLANKLINE>
+ >>> z = yaml.load(s)
+ >>> z
+ Data([], shape=(0, 0), dtype=float32)
"""
def __new__(subtype, shape, dtype=numpy.float, buffer=None, offset=0,
strides=None, order=None, info=None):
would consist of the approach data and the retract data. Metadata
would be the temperature, cantilever spring constant, etc.
- Two important :attr:`info` settings are `filetype` and
- `experiment`. These are two strings that can be used by Hooke
- commands/plugins to understand what they are looking at.
-
- * :attr:`info['filetype']` should contain the name of the exact
- filetype defined by the driver (so that filetype-speciofic
- commands can know if they're dealing with the correct filetype).
- * :attr:`info['experiment']` should contain an instance of a
- :class:`hooke.experiment.Experiment` subclass to identify the
- experiment type. For example, various
- :class:`hooke.driver.Driver`\s can read in force-clamp data, but
- Hooke commands could like to know if they're looking at force
- clamp data, regardless of their origin.
+ Each :class:`Data` block in :attr:`data` must contain an
+ :attr:`info['name']` setting with a unique (for the parent
+ curve) name identifying the data block. This allows plugins
+ and commands to access individual blocks.
- Another important attribute is :attr:`command_stack`, which holds
- a :class:`~hooke.command_stack.CommandStack` listing the commands
- that have been applied to the `Curve` since loading.
+ Each curve maintiains a :class:`~hooke.command_stack.CommandStack`
+ (:attr:`command_stack`) listing the commands that have been
+ applied to the `Curve` since loading.
The data-type is pickleable, to ensure we can move it between
processes with :class:`multiprocessing.Queue`\s.
>>> z = pickle.loads(s)
>>> z
<Curve path>
+ >>> z.command_stack
+ [<CommandMessage curve info {curve: <Curve path>}>]
>>> z.command_stack[-1].arguments['curve'] == z
True
>>> print yaml.dump(c) # doctest: +REPORT_UDIFF
arguments:
curve: *id001
command: curve info
- data: null
- driver: null
- info: {}
+ explicit_user_call: true
name: path
path: some/path
<BLANKLINE>
arguments:
curve: !!python/object:hooke.curve.Curve
command_stack: *id001
- data: null
- driver: null
- info: {}
name: path
path: some/path
command: curve info
+ explicit_user_call: true
<BLANKLINE>
"""
def __init__(self, path, info=None):
return self.__str__()
def set_path(self, path):
+ if path != None:
+ path = os.path.expanduser(path)
self.path = path
if self.name == None and path != None:
self.name = os.path.basename(path)
+ def _setup_default_attrs(self):
+ # .data contains: {name of data: list of data sets [{[x], [y]}]
+ # ._hooke contains a Hooke instance for Curve.load()
+ self._default_attrs = {
+ '_hooke': None,
+ 'command_stack': [],
+ 'data': None,
+ 'driver': None,
+ 'info': {},
+ 'name': None,
+ 'path': None,
+ }
+
def __getstate__(self):
- state = dict(self.__dict__) # make a copy of the attribute dict.
+ state = dict(self.__dict__) # make a copy of the attribute dict.
del(state['_hooke'])
return state
def __setstate__(self, state):
- # .data contains: {name of data: list of data sets [{[x], [y]}]
- # ._hooke contains a Hooke instance for Curve.load()
- self.name = self.driver = self.data = self._hooke = None
- self.info = {}
- self.command_stack = CommandStack()
- for key,value in state.items():
- setattr(self, key, value)
- if self.info == None:
+ self._setup_default_attrs()
+ self.__dict__.update(self._default_attrs)
+ if state == True:
+ return
+ self.__dict__.update(state)
+ self.set_path(getattr(self, 'path', None))
+ if self.info in [None, {}]:
self.info = {}
- self.set_path(state.get('path', None))
+ if type(self.command_stack) == list:
+ self.command_stack = CommandStack()
+
+ def __copy__(self):
+ """Set copy to preserve :attr:`_hooke`.
+
+ :meth:`getstate` drops :attr:`_hooke` for :mod:`pickle` and
+ :mod:`yaml` output, but it should be preserved (but not
+ duplicated) during copies.
+
+ >>> import copy
+ >>> class Hooke (object):
+ ... pass
+ >>> h = Hooke()
+ >>> d = Data(shape=(3,2), info={'columns':['distance (m)', 'force (N)']})
+ >>> for i in range(3): # initialize d
+ ... for j in range(2):
+ ... d[i,j] = i*10 + j
+ >>> c = Curve(None)
+ >>> c.data = [d]
+ >>> c.set_hooke(h)
+ >>> c._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c._hooke == h
+ True
+ >>> c2 = copy.copy(c)
+ >>> c2._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c2._hooke == h
+ True
+ >>> c2.data
+ [Data([[ 0., 1.],
+ [ 10., 11.],
+ [ 20., 21.]])]
+ >>> d.info
+ {'columns': ['distance (m)', 'force (N)']}
+ >>> id(c2.data[0]) == id(d)
+ True
+ """
+ copier = _copy_dispatch.get(type(self))
+ if copier:
+ return copier(self)
+ reductor = dispatch_table.get(type(self))
+ if reductor:
+ rv = reductor(self)
+ else:
+ # :class:`object` implements __reduce_ex__, see :pep:`307`.
+ rv = self.__reduce_ex__(2)
+ y = _reconstruct(self, rv, 0)
+ y.set_hooke(self._hooke)
+ return y
+
+ def __deepcopy__(self, memo):
+ """Set deepcopy to preserve :attr:`_hooke`.
+
+ :meth:`getstate` drops :attr:`_hooke` for :mod:`pickle` and
+ :mod:`yaml` output, but it should be preserved (but not
+ duplicated) during copies.
+
+ >>> import copy
+ >>> class Hooke (object):
+ ... pass
+ >>> h = Hooke()
+ >>> d = Data(shape=(3,2), info={'columns':['distance (m)', 'force (N)']})
+ >>> for i in range(3): # initialize d
+ ... for j in range(2):
+ ... d[i,j] = i*10 + j
+ >>> c = Curve(None)
+ >>> c.data = [d]
+ >>> c.set_hooke(h)
+ >>> c._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c._hooke == h
+ True
+ >>> c2 = copy.deepcopy(c)
+ >>> c2._hooke # doctest: +ELLIPSIS
+ <hooke.curve.Hooke object at 0x...>
+ >>> c2._hooke == h
+ True
+ >>> c2.data
+ [Data([[ 0., 1.],
+ [ 10., 11.],
+ [ 20., 21.]])]
+ >>> d.info
+ {'columns': ['distance (m)', 'force (N)']}
+ >>> id(c2.data[0]) == id(d)
+ False
+ """
+ reductor = dispatch_table.get(type(self))
+ if reductor:
+ rv = reductor(self)
+ else:
+ # :class:`object` implements __reduce_ex__, see :pep:`307`.
+ rv = self.__reduce_ex__(2)
+ y = _reconstruct(self, rv, 1, memo)
+ y.set_hooke(self._hooke)
+ return y
def set_hooke(self, hooke=None):
if hooke != None: