1 """
2 Defines abstract samplers.
3 """
4
5 import numpy as np
6 import csb.core
7
8 from abc import ABCMeta, abstractmethod, abstractproperty
13
15 """
16 Abstract interface for sampling algorithms.
17 """
18
19 __metaclass__ = ABCMeta
20
21 @abstractmethod
23 """
24 Draw a sample.
25 @rtype: L{AbstractState}
26 """
27 pass
28
30 """
31 Represents a point in phase-space.
32 """
33
34 __metaclass__ = ABCMeta
35
36 @abstractproperty
39
40 @abstractproperty
43
44 -class State(AbstractState):
45 """
46 Represents a point in phase-space.
47 """
48
49 @staticmethod
51 """
52 Check whether arguments are flat, one-dimensional numpy arrays.
53 """
54
55 for q in args:
56 if not isinstance(q, np.ndarray):
57 raise TypeError(q, 'numpy.ndarray expected!')
58
59 if not len(q.squeeze().shape) <= 1:
60 raise DimensionError(q, '1d numpy.ndarray expected!')
61
62 @staticmethod
64 """
65 Check whether arguments have equal length.
66 """
67
68 if len(q) != len(p):
69 raise DimensionError(p, 'momentum needs to have the same dimension as coordinates!')
70
71 - def __init__(self, position, momentum=None):
78
82
83 @property
85 return self._position.copy()
86 @position.setter
88 State.check_flat_array(value)
89 self._position = np.array(value)
90
91 @property
93 if self._momentum is None:
94 return None
95 else:
96 return self._momentum.copy()
97 @momentum.setter
105
111
112
113 -class EnsembleState(csb.core.BaseCollectionContainer, AbstractState):
114 """
115 Defines an Ensemble Monte Carlo state; it is a read-only collection
116 of State objects.
117
118 @param items: initialization list of states
119 @type items: list of L{States}
120 """
121
123 super(EnsembleState, self).__init__(items, type=State)
124
125 @property
127 return np.array([s.position for s in self])
128
129 @property
132