Skip to content

Commit

Permalink
Use boolean flag, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tjburch committed Jun 26, 2023
1 parent f94c5a8 commit 03c74ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
34 changes: 17 additions & 17 deletions bambi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,31 +506,32 @@ def set_alias(self, aliases):
if len(self.distributional_components) == 1: # pylint: disable=too-many-nested-blocks
for name, alias in aliases.items():
assert isinstance(alias, str)

# Monitor if this particular alias is used
is_used = False

if name in self.response_component.terms:
self.response_component.terms[name].alias = alias
is_used = True

if name in self.constant_components:
self.constant_components[name].alias = alias
is_used = True

if name == response_name:
self.response_component.response_term.alias = alias
is_used = True

# Now add aliases for hyperpriors in group specific terms
for term in self.response_component.group_specific_terms.values():
if name in term.prior.args:
term.hyperprior_alias = {name: alias}
is_used = True

# Add any aliases not used in prior logic to unused alias list
if (
not any(
name in term.prior.args
for term in self.response_component.group_specific_terms.values()
)
and name not in self.response_component.terms
and name not in self.constant_components
and name != response_name
):
if is_used is False:
missing_names.append(name)

else:
for component_name, component_aliases in aliases.items():
if component_name in self.constant_components:
Expand All @@ -541,26 +542,25 @@ def set_alias(self, aliases):
assert component_name in self.distributional_components
component = self.distributional_components[component_name]
for name, alias in component_aliases.items():

is_used = False

if name in component.terms:
component.terms[name].alias = alias
is_used = True

# Useful for non-response distributional components
if name == component.response_name:
component.alias = alias
is_used = True

for term in component.group_specific_terms.values():
if name in term.prior.args:
term.hyperprior_alias = {name: alias}
is_used = True

# Add any aliases not used in prior logic to unused alias list
if (
not any(
name in term.prior.args
for term in component.group_specific_terms.values()
)
and name not in component.terms
and name != component.response_name
):
if is_used is False:
missing_names.append(name)

# Report unused aliases
Expand Down
29 changes: 29 additions & 0 deletions tests/test_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,32 @@ def test_alias_equal_to_name(my_data):
model.set_alias({"sigma": "sigma"})
idata = model.fit(tune=100, draws=100)
set(idata.posterior.data_vars) == {"Intercept", "y_mean", "x", "sigma"}


def test_set_alias_warnings(my_data):
# Create a model to use aliases on
formula = bmb.Formula("y ~ x")
model = bmb.Model(formula, my_data)

# Define cases that throw the various warnings
test_cases = [
# Only one unused alias, explicitly tell user the name
(
{"unused_alias": "ua"},
"The following names do not match any terms, "
"their aliases were not assigned: unused_alias",
),
# Many unused aliases, generic response
(
{f"unused_alias{i}": f"ua{i}" for i in range(6)},
"There are 6 names that do not match any terms, so their aliases were not assigned.",
),
]

# Evaluate each case
for alias_dict, expected_warning in test_cases:
with pytest.warns(UserWarning) as record:
model.set_alias(alias_dict)
print(model.constant_components)
assert len(record) == 1
assert str(record[0].message) == expected_warning

0 comments on commit 03c74ff

Please sign in to comment.