Page MenuHomec4science

generic_sampler.py
No OneTemporary

File Metadata

Created
Sun, Apr 28, 11:49

generic_sampler.py

# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
from abc import abstractmethod
from rrompy.utilities.base.types import List, paramList
from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert
from rrompy.parameter import checkParameterList
__all__ = ['GenericSampler']
class GenericSampler:
"""ABSTRACT. Generic generator of sample points."""
def __init__(self, lims:paramList, scaling : List[callable] = None,
scalingInv : List[callable] = None):
self.lims = lims
self.scaling = scaling
self.scalingInv = scalingInv
def name(self) -> str:
return self.__class__.__name__
def __str__(self) -> str:
return "{}[{}_{}]".format(self.name(), self.lims[0], self.lims[1])
def __repr__(self) -> str:
return self.__str__() + " at " + hex(id(self))
def __eq__(self, other) -> bool:
return self.__dict__ == other.__dict__
@property
def npar(self):
"""Number of parameters."""
return self._lims.shape[1]
@property
def lims(self):
"""Value of lims."""
return self._lims
@lims.setter
def lims(self, lims):
lims = checkParameterList(lims)[0]
if len(lims) != 2:
raise RROMPyException("2 limits must be specified.")
self._lims = lims
@property
def scaling(self):
"""Value of scaling."""
return self._scaling
@scaling.setter
def scaling(self, scaling):
if scaling is not None:
if not hasattr(scaling, "__len__"): scaling = [scaling]
RROMPyAssert(self.npar, len(scaling), "Number of scaling terms")
if not all([callable(s) for s in scaling]):
raise RROMPyException(("Each value of scaling must be a "
"callable."))
self._scaling = scaling
@property
def scalingInv(self):
"""Value of scalingInv."""
return self._scalingInv
@scalingInv.setter
def scalingInv(self, scalingInv):
if scalingInv is not None:
if not hasattr(scalingInv, "__len__"): scalingInv = [scalingInv]
RROMPyAssert(self.npar, len(scalingInv),
"Number of scalingInv terms")
if not all([callable(sInv) for sInv in scalingInv]):
raise RROMPyException(("Each value of scalingInv must be a "
"callable."))
self._scalingInv = scalingInv
@abstractmethod
def generatePoints(self, n:List[int]) -> paramList:
"""Array of points."""
if not hasattr(n, "__len__"): n = [n]
RROMPyAssert(self.npar, len(n), "Point number")
pass

Event Timeline