Moved hooke.libhookecurve -> hooke.curve and rewrote HookeCurve -> Curve class.
[hooke.git] / hooke / curve.py
1 import os.path
2 import numpy
3
4 from .driver import NotRecognized
5
6 class Data (numpy.ndarray):
7     """Stores a single, continuous data set.
8
9     Adds :attr:`info` :class:`dict` to the standard :class:`numpy.ndarray`.
10
11     See :mod:`numpy.doc.subclassing` for the peculiarities of
12     subclassing :class:`numpy.ndarray`.
13     """
14     def __new__(self, subtype, shape, dtype=numpy.float, buffer=None, offset=0,
15                 strides=None, order=None, info=None):
16         """Create the ndarray instance of our type, given the usual
17         input arguments.  This will call the standard ndarray
18         constructor, but return an object of our type.
19         """
20         obj = np.ndarray.__new__(subtype=subtype, shape=shape, dtype=dtype,
21                                  buffer=buffer, offset=offset, strides=strides,
22                                  order=order)
23         # add the new attribute to the created instance
24         if info == None:
25             info = {}
26         obj.info = info
27         # Finally, we must return the newly created object:
28         return obj
29
30     def __array_finalize__(self, obj):
31         """Set any extra attributes from the original object when
32         creating a new view object."""
33         # reset the attribute from passed original object
34         self.info = getattr(obj, 'info', {})
35         # We do not need to return anything
36
37
38 class Curve (object):
39     """A grouped set of :class:`Data` runs from the same file with metadata.
40
41     For an approach/retract force spectroscopy experiment, the group
42     would consist of the approach data and the retract data.  Metadata
43     would be the temperature, cantilever spring constant, etc.
44     """
45     def __init__(self, path):
46         #the data dictionary contains: {name of data: list of data sets [{[x], [y]}]
47         self.path = path
48         self.driver = None
49         self.data = []
50         self.info = None
51         self.name = os.path.basename(path)
52         self.notes = ''
53
54     def identify(self, drivers):
55         """Identify the appropriate :class:`hooke.driver.Driver` for
56         the curve file (`.path`).
57         """
58         for driver in drivers:
59             current_driver = driver(self.path)
60             if current_driver.is_me():
61                 self.driver = current_driver # remember the working driver
62                 return
63         raise NotRecognized(self.path)
64
65     def load(self):
66         """Use the driver to read the curve into memory.
67         """
68         pass