Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Adding non-default loggables explicitly. #1331

Merged
merged 11 commits into from May 31, 2022
12 changes: 8 additions & 4 deletions hoomd/logging.py
Expand Up @@ -624,12 +624,16 @@ def only_default(self):
quantities."""
return self._only_default

def _filter_quantities(self, quantities):
def _filter_quantities(self, quantities, force_quantities=False):
for quantity in quantities:
if self._only_default and not quantity.default:
if quantity.category not in self._categories:
continue
elif quantity.category in self._categories:
# Must be before default check to overwrite _only_default
if force_quantities:
yield quantity
if self._only_default and not quantity.default:
continue
yield quantity

def _get_loggables_by_name(self, obj, quantities):
if quantities is None:
Expand All @@ -643,7 +647,7 @@ def _get_loggables_by_name(self, obj, quantities):
"object {} has not loggable quantities {}.".format(
obj, bad_keys))
yield from self._filter_quantities(
map(lambda q: obj._export_dict[q], quantities))
map(lambda q: obj._export_dict[q], quantities), True)

def add(self, obj, quantities=None, user_name=None):
"""Add loggables from obj to logger.
Expand Down
13 changes: 8 additions & 5 deletions hoomd/pytest/test_logging.py
Expand Up @@ -56,6 +56,10 @@ def prop(self):
def proplist(self):
return [1, 2, 3]

@log(category="string", default=False)
def prop_nondefault(self):
return "foo"

def __eq__(self, other):
return isinstance(other, type(self))

Expand Down Expand Up @@ -242,7 +246,7 @@ def test_get_loggables_by_names(self, blank_logger, logged_obj):
])

# Check when quantities is given
accepted_quantities = ['prop', 'proplist']
accepted_quantities = ['proplist', "prop_nondefault"]
log_quanities = blank_logger._get_loggables_by_name(
logged_obj, accepted_quantities)
assert all([
Expand Down Expand Up @@ -275,10 +279,9 @@ def test_add(self, blank_logger, logged_obj, base_namespace):

b-butler marked this conversation as resolved.
Show resolved Hide resolved
# Test multiple quantities
blank_logger._dict = dict()
blank_logger.add(logged_obj, ['prop', 'proplist'])
expected_namespaces = [
base_namespace + ('prop',), base_namespace + ('proplist',)
]
loggables = ['prop', 'proplist', "prop_nondefault"]
blank_logger.add(logged_obj, loggables)
expected_namespaces = [base_namespace + (name,) for name in loggables]
assert all([ns in blank_logger for ns in expected_namespaces])
assert len(blank_logger) == 2

Expand Down