Package glue :: Module offsetvector :: Class offsetvector
[hide private]
[frames] | no frames]

Class offsetvector

source code

object --+    
         |    
      dict --+
             |
            offsetvector

Subclass of the dict built-in type for storing mappings of instrument to time offset.

Examples:

>>> x = offsetvector({"H1": 0, "L1": 10, "V1": 20})
>>> x["H1"]
0
>>> not any(x.values()) # check for "zero-lag"
False

The motivation for introducing this class, instead of using dictionaries, is that it provides a number of tools for comparing offset vectors besides strict value-for-value equality. For example the Python cmp() operation compares two offset vectors by the relative offsets between instruments rather than their absolute offsets, whereas the == operation compares two offset vectors by demanding strict equality. There is also the ability to check if one offset vector is a subset of another one.

Instance Methods [hide private]
 
__abs__(self)
Returns max(offset) - min(offset).
source code
 
__cmp__(self, other)
Compare two offset vectors by their relative offsets.
source code
 
__repr__(self)
Return a string representation of the offset vector.
source code
 
__str__(self, compact=False)
Return a human-readable string representation of an offset vector.
source code
 
contains(self, other)
Returns True if offset vector @other can be found in @self, False otherwise.
source code
 
normalize(self, **kwargs)
Adjust the offsetvector so that a particular instrument has the desired offset.
source code

Inherited from dict: __contains__, __delitem__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __init__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __setitem__, __sizeof__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values, viewitems, viewkeys, viewvalues

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __subclasshook__

Class Methods [hide private]
 
fromdeltas(cls, deltas)
Construct an offsetvector from a dictionary of offset deltas as returned by the .deltas attribute.
source code
Class Variables [hide private]

Inherited from dict: __hash__

Properties [hide private]
  deltas
Dictionary of relative offsets.
  refkey
= min(self)

Inherited from object: __class__

Method Details [hide private]

__abs__(self)

source code 

Returns max(offset) - min(offset).

Example:

>>> abs(offsetvector({"H1": 0.0, "H2": 0.0, "L1": 0.0}))
0.0
>>> abs(offsetvector({"H1": 10.0, "H2": 10.0, "L1": 10.0}))
0.0
>>> abs(offsetvector({'H1': 10.0, 'L1': 0.0, 'V1': -10.0}))
20.0

__cmp__(self, other)
(Comparison operator)

source code 

Compare two offset vectors by their relative offsets. The return value is 0 if the relative offsets are all equal, nonzero otherwise.

Example:

>>> a = offsetvector({"H1": 0.0, "H2": 0.0, "L1": 0.0})
>>> b = offsetvector({"H1": 10.0, "H2": 10.0, "L1": 10.0})
>>> cmp(a, b)
0
>>> a == b
False

Note that cmp() and testing for equality are different tests! The equality test returns False because the offset vectors are not identical, however the cmp() function returns 0 because the relative offsets are all equal.

Overrides: dict.__cmp__

__repr__(self)
(Representation operator)

source code 

Return a string representation of the offset vector. Running eval() on the result reconstructs the offsetvector.

Example:

>>> a = offsetvector({"H1": -10.1234567, "L1": 0.1})
>>> repr(a)
"offsetvector({'H1': -10.1234567, 'L1': 0.1})"
>>> b = eval(repr(a))
>>> b
offsetvector({'H1': -10.1234567, 'L1': 0.1})
>>> b == a
True
>>> b is a
False
Overrides: object.__repr__

__str__(self, compact=False)
(Informal representation operator)

source code 

Return a human-readable string representation of an offset vector.

Example:

>>> a = offsetvector({"H1": -10.1234567, "L1": 0.125})
>>> str(a)
'H1 = -10.1234567 s, L1 = +0.125 s'
>>> a.__str__(compact = True)
'H1=-10.123,L1=0.125'
Overrides: object.__str__

contains(self, other)

source code 

Returns True if offset vector @other can be found in @self, False otherwise. An offset vector is "found in" another offset vector if the latter contains all of the former's instruments and the relative offsets among those instruments are equal (the absolute offsets need not be).

Example:

>>> a = offsetvector({"H1": 10, "L1": 20, "V1": 30})
>>> b = offsetvector({"H1": 20, "V1": 40})
>>> a.contains(b)
True

Note the distinction between this and the "in" operator:

>>> "H1" in a
True

fromdeltas(cls, deltas)
Class Method

source code 

Construct an offsetvector from a dictionary of offset deltas as returned by the .deltas attribute.

Example:

>>> x = offsetvector({"H1": 0, "L1": 10, "V1": 20})
>>> y = offsetvector.fromdeltas(x.deltas)
>>> y
offsetvector({'V1': 20, 'H1': 0, 'L1': 10})
>>> y == x
True

See also .deltas, .fromkeys()

normalize(self, **kwargs)

source code 

Adjust the offsetvector so that a particular instrument has the desired offset. All other instruments have their offsets adjusted so that the relative offsets are preserved. The instrument to noramlize, and the offset one wishes it to have, are provided as a key-word argument. The return value is the time slide dictionary, which is modified in place.

If more than one key-word argument is provided the keys are sorted and considered in order until a key is found that is in the offset vector. The offset vector is normalized to that value. This function is a no-op if no key-word argument is found that applies.

Example:

>>> a = offsetvector({"H1": -10, "H2": -10, "L1": -10})
>>> a.normalize(L1 = 0)
offsetvector({'H2': 0, 'H1': 0, 'L1': 0})
>>> a = offsetvector({"H1": -10, "H2": -10})
>>> a.normalize(L1 = 0, H2 = 5)
offsetvector({'H2': 5, 'H1': 5})

Property Details [hide private]

deltas

Dictionary of relative offsets. The keys in the result are pairs of keys from the offset vector, (a, b), and the values are the relative offsets, (offset[b] - offset[a]). Raises ValueError if the offsetvector is empty (WARNING: this behaviour might change in the future).

Example:

>>> x = offsetvector({"H1": 0, "L1": 10, "V1": 20})
>>> x.deltas
{('H1', 'L1'): 10, ('H1', 'V1'): 20, ('H1', 'H1'): 0}
>>> y = offsetvector({'H1': 100, 'L1': 110, 'V1': 120})
>>> y.deltas == x.deltas
True

Note that the result always includes a "dummy" entry, giving the relative offset of self.refkey with respect to itself, which is always 0.

See also .fromdeltas().

BUGS: I think the keys in each tuple should be reversed. I can't remember why I put them in the way they are. Expect them to change in the future.

Get Method:
unreachable.deltas(self) - Dictionary of relative offsets.

refkey

= min(self)

Raises ValueError if the offsetvector is empty.

Get Method:
unreachable.refkey(self) - = min(self)