/usr/lib/python3/dist-packages/csb/statistics/samplers/__init__.py is in python3-csb 1.2.3+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | """
Defines abstract samplers.
"""
import numpy as np
import csb.core
from abc import ABCMeta, abstractmethod, abstractproperty
class DimensionError(TypeError):
pass
class AbstractSampler(object):
"""
Abstract interface for sampling algorithms.
"""
__metaclass__ = ABCMeta
@abstractmethod
def sample(self):
"""
Draw a sample.
@rtype: L{AbstractState}
"""
pass
class AbstractState(object):
"""
Represents a point in phase-space.
"""
__metaclass__ = ABCMeta
@abstractproperty
def position(self):
pass
@abstractproperty
def momentum(self):
pass
class State(AbstractState):
"""
Represents a point in phase-space.
"""
@staticmethod
def check_flat_array(*args):
"""
Check whether arguments are flat, one-dimensional numpy arrays.
"""
for q in args:
if not isinstance(q, np.ndarray):
raise TypeError(q, 'numpy.ndarray expected!')
if not len(q.squeeze().shape) <= 1:
raise DimensionError(q, '1d numpy.ndarray expected!')
@staticmethod
def check_equal_length(q, p):
"""
Check whether arguments have equal length.
"""
if len(q) != len(p):
raise DimensionError(p, 'momentum needs to have the same dimension as coordinates!')
def __init__(self, position, momentum=None):
self._position = None
self._momentum = None
self.position = position
self.momentum = momentum
def __eq__(self, other):
return self.position == other.position and self.momentum == other.momentum
@property
def position(self):
return self._position.copy()
@position.setter
def position(self, value):
State.check_flat_array(value)
self._position = np.array(value)
@property
def momentum(self):
if self._momentum is None:
return None
else:
return self._momentum.copy()
@momentum.setter
def momentum(self, value):
if not value is None:
State.check_flat_array(value)
State.check_equal_length(value, self.position)
self._momentum = np.array(value)
else:
self._momentum = None
def clone(self):
if self.momentum is not None:
return self.__class__(self.position.copy(), self.momentum.copy())
else:
return self.__class__(self.position.copy())
class EnsembleState(csb.core.BaseCollectionContainer, AbstractState):
"""
Defines an Ensemble Monte Carlo state; it is a read-only collection
of State objects.
@param items: initialization list of states
@type items: list of L{States}
"""
def __init__(self, items):
super(EnsembleState, self).__init__(items, type=State)
@property
def position(self):
return np.array([s.position for s in self])
@property
def momentum(self):
return np.array([s.momentum for s in self])
|