def _split_repr(cls, string): subclass_args = infer_args_from_method(cls.__init__) args = string.split(',') remove = list() for ii, key in enumerate(args): if '(' in key: jj = ii while ')' not in args[jj]: jj += 1 args[ii] = ','.join([args[ii], args[jj]]).strip() remove.append(jj) remove.reverse() for ii in remove: del args[ii] kwargs = dict() for ii, arg in enumerate(args): if '=' not in arg: logger.debug( 'Reading priors with non-keyword arguments is dangerous!') key = subclass_args[ii] val = arg else: split_arg = arg.split('=') key = split_arg[0] val = '='.join(split_arg[1:]) kwargs[key] = val return kwargs
def __init__(self, condition_func, name=None, latex_label=None, unit=None, boundary=None, **reference_params): """ Parameters ---------- condition_func: func Functional form of the condition for this prior. The first function argument has to be a dictionary for the `reference_params` (see below). The following arguments are the required variables that are required before we can draw this prior. It needs to return a dictionary with the modified values for the `reference_params` that are being used in the next draw. For example if we have a Uniform prior for `x` depending on a different variable `y` `p(x|y)` with the boundaries linearly depending on y, then this could have the following form: ``` def condition_func(reference_params, y): return dict(minimum=reference_params['minimum'] + y, maximum=reference_params['maximum'] + y) ``` name: str, optional See superclass latex_label: str, optional See superclass unit: str, optional See superclass boundary: str, optional See superclass reference_params: Initial values for attributes such as `minimum`, `maximum`. This differs on the `prior_class`, for example for the Gaussian prior this is `mu` and `sigma`. """ if 'boundary' in infer_args_from_method( super(ConditionalPrior, self).__init__): super(ConditionalPrior, self).__init__(name=name, latex_label=latex_label, unit=unit, boundary=boundary, **reference_params) else: super(ConditionalPrior, self).__init__(name=name, latex_label=latex_label, unit=unit, **reference_params) self._required_variables = None self.condition_func = condition_func self._reference_params = reference_params self.__class__.__name__ = 'Conditional{}'.format( prior_class.__name__) self.__class__.__qualname__ = 'Conditional{}'.format( prior_class.__qualname__)
def _repr_dict(self): """ Get a dictionary containing the arguments needed to reproduce this object. """ property_names = {p for p in dir(self.__class__) if isinstance(getattr(self.__class__, p), property)} subclass_args = infer_args_from_method(self.__init__) dict_with_properties = self.__dict__.copy() for key in property_names.intersection(subclass_args): dict_with_properties[key] = getattr(self, key) return {key: dict_with_properties[key] for key in subclass_args}
def get_instantiation_dict(self): subclass_args = infer_args_from_method(self.__init__) property_names = [p for p in dir(self.__class__) if isinstance(getattr(self.__class__, p), property)] dict_with_properties = self.__dict__.copy() for key in property_names: dict_with_properties[key] = getattr(self, key) instantiation_dict = dict() for key in subclass_args: instantiation_dict[key] = dict_with_properties[key] return instantiation_dict
def get_instantiation_dict(self): subclass_args = infer_args_from_method(self.__init__) dict_with_properties = get_dict_with_properties(self) instantiation_dict = dict() for key in subclass_args: if isinstance(dict_with_properties[key], list): value = np.asarray(dict_with_properties[key]).tolist() else: value = dict_with_properties[key] instantiation_dict[key] = value return instantiation_dict
def get_instantiation_dict(self): subclass_args = infer_args_from_method(self.__init__) property_names = [p for p in dir(self.__class__) if isinstance(getattr(self.__class__, p), property)] dict_with_properties = self.__dict__.copy() for key in property_names: dict_with_properties[key] = getattr(self, key) instantiation_dict = dict() for key in subclass_args: if isinstance(dict_with_properties[key], list): value = np.asarray(dict_with_properties[key]).tolist() else: value = dict_with_properties[key] instantiation_dict[key] = value return instantiation_dict
def _from_repr(cls, string): subclass_args = infer_args_from_method(cls.__init__) string = string.replace(' ', '') kwargs = cls._split_repr(string) for key in kwargs: val = kwargs[key] if key not in subclass_args and not hasattr(cls, "reference_params"): raise AttributeError('Unknown argument {} for class {}'.format( key, cls.__name__)) else: kwargs[key] = cls._parse_argument_string(val) if key in ["condition_func", "conversion_function"] and isinstance(kwargs[key], str): if "." in kwargs[key]: module = '.'.join(kwargs[key].split('.')[:-1]) name = kwargs[key].split('.')[-1] else: module = __name__ name = kwargs[key] kwargs[key] = getattr(import_module(module), name) return cls(**kwargs)
def test_self_handling_method_as_function(self): expected = ['a', 'b'] actual = utils.infer_args_from_method(self.source5) self.assertListEqual(expected, actual)
def get_instantiation_dict(self): subclass_args = infer_args_from_method(self.__init__) dict_with_properties = get_dict_with_properties(self) return {key: dict_with_properties[key] for key in subclass_args}
def test_self_handling(self): expected = ["a", "b"] actual = utils.infer_args_from_method(self.source2) self.assertListEqual(expected, actual)