def __init__(self, name, definition): # NOTE: this is triggered only for parameters outside of functions if 'prior' in definition: # Need the create a function for the prior first try: function_name = list(definition['prior'].keys())[0] parameters_definition = definition['prior'][function_name] except KeyError: # pragma: no cover raise ModelSyntaxError( "The prior for parameter %s is malformed" % name) # parse the function shape_parser = ShapeParser(name) prior_instance = shape_parser.parse(name, function_name, parameters_definition) # Substitute the definition with the instance, so that the following constructor will work definition['prior'] = prior_instance self._variable = parameter.Parameter(name, **definition)
def __init__(self, name, definition): self._links = [] # NOTE: this is triggered only for parameters outside of functions if "prior" in definition: # Need the create a function for the prior first try: function_name = list(definition["prior"].keys())[0] parameters_definition = definition["prior"][function_name] except KeyError: # pragma: no cover raise ModelSyntaxError( "The prior for parameter %s is malformed" % name) # parse the function shape_parser = ShapeParser(name) prior_instance = shape_parser.parse(name, function_name, parameters_definition) # Substitute the definition with the instance, so that the following constructor will work definition["prior"] = prior_instance # Check if this is a linked parameter, i.e., if 'value' is something like f(source.spectrum.powerlaw.index) matches = re.findall("""f\((.+)\)""", str(definition["value"])) if matches: # This is an expression which marks a parameter # with a link to another parameter (or an IndependentVariable such as time) # Get the variable linked_variable = matches[0] # Now get the law if "law" not in definition: # pragma: no cover raise ModelSyntaxError( "The parameter %s in function %s " " is linked to %s but lacks a 'law' attribute" % (name, function_name, linked_variable)) link_function_name = list(definition["law"].keys())[0] # ok, now we parse the linked parameter function_parser = ShapeParser(name) link_function_instance = function_parser.parse( name, link_function_name, definition["law"][link_function_name]) self._links.append({ "parameter_path": name, "law": link_function_instance, "variable": linked_variable, }) # get rid of the 'law' entry definition.pop("law", None) # this parameter's value will be replaced later. # for now we just need to get rid of the f(param) entry definition["value"] = 1.0 self._variable = parameter.Parameter(name, **definition)