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

Better assertion in Python utils #3149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions share/lib/python/neuron/tests/utils/checkresult.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import math
import os

from neuron import h, hoc


Expand All @@ -17,7 +19,6 @@
A temporary must_exist=False provides a simple way for the test
author to bootstrap the file.
"""
import os

self.fname = fname
self.d = {}
Expand Down Expand Up @@ -48,33 +49,29 @@
assert type(a) == type(b)
if type(a) in (float, int):
match = math.isclose(a, b, rel_tol=tol)
if not match:
print(a, b, "diff", abs(a - b) / max(abs(a), abs(b)), ">", tol)
assert (
match
), f"{a}, {b}, diff = {abs(a - b) / max(abs(a), abs(b))} > {tol}"
return match
elif type(a) == str:
match = a == b
if not match:
print("strdiff", a, b)
assert match, f"strdiff: {a} != {b}"
return match
elif type(a) == list:
# List comprehension avoids short-circuit, so the "diff"
# message just above is printed for all elements
return all([equal(aa, bb) for aa, bb in zip(a, b)])
return all(equal(aa, bb) for aa, bb in zip(a, b))
elif type(a) == dict:
assert a.keys() == b.keys()
return all([equal(a[k], b[k]) for k in a.keys()])
return all(equal(a[k], b[k]) for k in a.keys())

Check warning on line 66 in share/lib/python/neuron/tests/utils/checkresult.py

View check run for this annotation

Codecov / codecov/patch

share/lib/python/neuron/tests/utils/checkresult.py#L66

Added line #L66 was not covered by tests
raise Exception(
"Don't know how to compare objects of type " + str(type(a))
)

match = equal(value, self.d[key])
if not match:
print(key, "difference")
print("std = ", self.d[key])
print("val = ", value)
assert match
assert match, f"{key=}, difference:\nstd = {self.d[key]}\n{value=}"
else:
print("{} added {}".format(self, key))
print(f"{self} added {key}")

Check warning on line 74 in share/lib/python/neuron/tests/utils/checkresult.py

View check run for this annotation

Codecov / codecov/patch

share/lib/python/neuron/tests/utils/checkresult.py#L74

Added line #L74 was not covered by tests
if isinstance(value, hoc.Vector):
self.d[key] = value.to_python()
else:
Expand Down
Loading