Source code for pycbc.distributions.constraints

# Copyright (C) 2017 Christopher M. Biwer
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program 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 General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""
This modules provides classes for evaluating multi-dimensional constraints.
"""

from pycbc import transforms
from pycbc.io import record

[docs]class Constraint(object): """Creates a constraint that evaluates to True if parameters obey the constraint and False if they do not. """ name = "custom" def __init__(self, constraint_arg, transforms=None, **kwargs): self.constraint_arg = constraint_arg self.transforms = transforms for kwarg in kwargs.keys(): setattr(self, kwarg, kwargs[kwarg]) def __call__(self, params): """Evaluates constraint. """ # cast to FieldArray if isinstance(params, dict): params = record.FieldArray.from_kwargs(**params) elif not isinstance(params, record.FieldArray): raise ValueError("params must be dict or FieldArray instance") # try to evaluate; this will assume that all of the needed parameters # for the constraint exists in params try: out = self._constraint(params) except NameError: # one or more needed parameters don't exist; try applying the # transforms params = transforms.apply_transforms(params, self.transforms) \ if self.transforms else params out = self._constraint(params) if isinstance(out, record.FieldArray): out = out.item() if params.size == 1 else out return out def _constraint(self, params): """ Evaluates constraint function. """ return params[self.constraint_arg]
# list of all constraints constraints = { Constraint.name : Constraint, }