| 1 |
""" |
| 2 |
Defines abstract samplers. |
| 3 |
""" |
| 4 |
|
| 5 |
import numpy as np |
| 6 |
import csb.core |
| 7 |
|
| 8 |
from abc import ABCMeta, abstractmethod, abstractproperty |
| 9 |
|
| 10 |
|
| 11 |
class DimensionError(TypeError): |
| 12 |
pass |
| 13 |
|
| 14 |
class AbstractSampler(object): |
| 15 |
""" |
| 16 |
Abstract interface for sampling algorithms. |
| 17 |
""" |
| 18 |
|
| 19 |
__metaclass__ = ABCMeta |
| 20 |
|
| 21 |
@abstractmethod |
| 22 |
def sample(self): |
| 23 |
""" |
| 24 |
Draw a sample. |
| 25 |
@rtype: L{AbstractState} |
| 26 |
""" |
| 27 |
pass |
| 28 |
|
| 29 |
class AbstractState(object): |
| 30 |
""" |
| 31 |
Represents a point in phase-space. |
| 32 |
""" |
| 33 |
|
| 34 |
__metaclass__ = ABCMeta |
| 35 |
|
| 36 |
@abstractproperty |
| 37 |
def position(self): |
| 38 |
pass |
| 39 |
|
| 40 |
@abstractproperty |
| 41 |
def momentum(self): |
| 42 |
pass |
| 43 |
|
| 44 |
class State(AbstractState): |
| 45 |
""" |
| 46 |
Represents a point in phase-space. |
| 47 |
""" |
| 48 |
|
| 49 |
@staticmethod |
| 50 |
def check_flat_array(*args): |
| 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 |
| 63 |
def check_equal_length(q, p): |
| 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): |
| 72 |
|
| 73 |
self._position = None |
| 74 |
self._momentum = None |
| 75 |
|
| 76 |
self.position = position |
| 77 |
self.momentum = momentum |
| 78 |
|
| 79 |
def __eq__(self, other): |
| 80 |
|
| 81 |
return self.position == other.position and self.momentum == other.momentum |
| 82 |
|
| 83 |
@property |
| 84 |
def position(self): |
| 85 |
return self._position.copy() |
| 86 |
@position.setter |
| 87 |
def position(self, value): |
| 88 |
State.check_flat_array(value) |
| 89 |
self._position = np.array(value) |
| 90 |
|
| 91 |
@property |
| 92 |
def momentum(self): |
| 93 |
if self._momentum is None: |
| 94 |
return None |
| 95 |
else: |
| 96 |
return self._momentum.copy() |
| 97 |
@momentum.setter |
| 98 |
def momentum(self, value): |
| 99 |
if not value is None: |
| 100 |
State.check_flat_array(value) |
| 101 |
State.check_equal_length(value, self.position) |
| 102 |
self._momentum = np.array(value) |
| 103 |
else: |
| 104 |
self._momentum = None |
| 105 |
|
| 106 |
def clone(self): |
| 107 |
if self.momentum is not None: |
| 108 |
return self.__class__(self.position.copy(), self.momentum.copy()) |
| 109 |
else: |
| 110 |
return self.__class__(self.position.copy()) |
| 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 |
|
| 122 |
def __init__(self, items): |
| 123 |
super(EnsembleState, self).__init__(items, type=State) |
| 124 |
|
| 125 |
@property |
| 126 |
def position(self): |
| 127 |
return np.array([s.position for s in self]) |
| 128 |
|
| 129 |
@property |
| 130 |
def momentum(self): |
| 131 |
return np.array([s.momentum for s in self]) |