Skip to content

Commit

Permalink
fix issue in Constraints.add_constraint (#255)
Browse files Browse the repository at this point in the history
* fix issue in Constraints.add_constraint

* rename add_constraint argument
  • Loading branch information
peterstangl authored Jul 16, 2024
1 parent 188120d commit aa153bb
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
13 changes: 8 additions & 5 deletions flavio/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,20 @@ def all_parameters(self):
"""Returns a list of all parameters/observables constrained."""
return list(self._parameters.keys())

def add_constraint(self, parameters, constraint):
def add_constraint(self, parameters, constraint, is_parameter_constraint=None):
"""Set the constraint on one or several parameters/observables.
`constraint` must be an instance of a child of ProbabilityDistribution.
Note that if there already exists a constraint, it will be removed."""
for num, parameter in enumerate(parameters):
try: # check if parameter object already exists
p = Parameter[parameter]
except: # otherwise, create a new one
p = Parameter(parameter)
if is_parameter_constraint:
try: # check if parameter object already exists
p = Parameter[parameter]
except: # otherwise, create a new one
p = Parameter(parameter)
else: # if parameter exists, remove existing constraints
self.remove_constraint(parameter)
# remove constraint if there is one
if parameter in self._parameters:
self.remove_constraint(parameter)
Expand Down
2 changes: 1 addition & 1 deletion flavio/physics/bdecays/formfactors/b_p/bcl_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def load_parameters(filename, constraints):
ff_dict = yaml.safe_load(f)
covariance = np.outer(ff_dict['uncertainties'], ff_dict['uncertainties'])*ff_dict['correlation']
constraints.add_constraint(ff_dict['parameters'],
MultivariateNormalDistribution(central_value=ff_dict['central_values'], covariance=covariance) )
MultivariateNormalDistribution(central_value=ff_dict['central_values'], covariance=covariance), is_parameter_constraint=True)
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def load_parameters(filename, constraints):
raise ValueError('No f or b in observable name')
observables_renamed.append(o[:index]+ ' BCL ' + o[index:])
constraints.add_constraint(observables_renamed,
MultivariateNormalDistribution(central_value=central_values, covariance=covariance) )
MultivariateNormalDistribution(central_value=central_values, covariance=covariance), is_parameter_constraint=True)
2 changes: 2 additions & 0 deletions flavio/physics/bdecays/formfactors/b_p/bsz_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def load_parameters(filename, process, constraints):
_tex_ff = tex_ff[parameter_name.split(' ')[-1].split('_')[-1]]
p.tex = r'$' + _tex_a + r'^{' + _tex_ff + r'}$'
p.description = r'BSZ form factor parametrization coefficient $' + _tex_a + r'$ of $' + _tex_ff + r'$'
else: # if parameter exists, remove existing constraints
constraints.remove_constraint(parameter_name)
[central, unc, corr] = get_ffpar(filename)
constraints.add_constraint(parameter_names,
MultivariateNormalDistribution(central_value=central, covariance=np.outer(unc, unc)*corr))
Expand Down
2 changes: 2 additions & 0 deletions flavio/physics/bdecays/formfactors/b_v/bsz_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def load_parameters(filename, process, constraints):
_tex_ff = tex_ff[parameter_name.split(' ')[-1].split('_')[-1]]
p.tex = r'$' + _tex_a + r'^{' + _tex_ff + r'}$'
p.description = r'BSZ form factor parametrization coefficient $' + _tex_a + r'$ of $' + _tex_ff + r'$'
else: # if parameter exists, remove existing constraints
constraints.remove_constraint(parameter_name)
[central, unc, corr] = get_ffpar(filename)
constraints.add_constraint(parameter_names,
MultivariateNormalDistribution(central_value=central, covariance=np.outer(unc, unc)*corr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def load_parameters(file_res, file_cov, process, constraints):
- np.diag([ cov_dict[(k,k)] for k in keys_sorted]) )
parameter_names = [implementation_name + ' ' + coeff_name for coeff_name in keys_sorted]
constraints.add_constraint(parameter_names,
MultivariateNormalDistribution(central_value=res, covariance=cov ))
MultivariateNormalDistribution(central_value=res, covariance=cov ), is_parameter_constraint=True)


def lattice_load(constraints):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def load_parameters(file_res, file_cov, process, constraints):
_tex_ff = tex_ff[parameter_name.split(' ')[-1].split('_')[-1]]
p.tex = r'$' + _tex_a + r'^{' + _tex_ff + r'}$'
p.description = r'SSE form factor parametrization coefficient $' + _tex_a + r'$ of $' + _tex_ff + r'$'
else: # if parameter exists, remove existing constraints
constraints.remove_constraint(parameter_name)
constraints.add_constraint(parameter_names,
MultivariateNormalDistribution(central_value=res, covariance=cov ))

Expand Down

0 comments on commit aa153bb

Please sign in to comment.