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

Adds unit tests #41

Merged
merged 29 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b055f93
created few unit tests for bp_utils
Jul 30, 2021
faef39b
Merge branch 'vicariousinc:master' into unit_tests
NishanthJKumar Jul 30, 2021
7b25f33
Merge branch 'vicariousinc:master' into unit_tests
NishanthJKumar Aug 2, 2021
f12001e
includes first E2E test for pgmax
Aug 2, 2021
e17f697
adds new tests
Aug 2, 2021
0835ef8
Merge branch 'master' of github.com:vicariousinc/PGMax into vicarious…
Aug 13, 2021
407ccee
Merge branch 'vicariousinc-master' into unit_tests
Aug 13, 2021
2c7ab3c
updates sanity check and E2E test, but test currently fails
Aug 13, 2021
99501b4
adds coverage dependency to poetry
NishanthJKumar Aug 18, 2021
e0460e7
Merge branch 'unit_tests' into master
NishanthJKumar Aug 18, 2021
acaf801
Merge pull request #7 from NishanthJKumar/master
NishanthJKumar Aug 18, 2021
a6a32ff
small update to test_pgmax
NishanthJKumar Aug 18, 2021
f1811aa
adds codecov to CI
NishanthJKumar Aug 19, 2021
bf78603
test codecov upload with github secret
NishanthJKumar Aug 19, 2021
944297d
updates ci and poetry deps
NishanthJKumar Aug 19, 2021
e2afc5f
fixes command error in ci
NishanthJKumar Aug 19, 2021
46188df
fixes codecov yaml to be parseable
NishanthJKumar Aug 19, 2021
c5aa087
Merge branch 'master' of github.com:vicariousinc/PGMax into vicarious…
NishanthJKumar Aug 19, 2021
889479c
Merge branch 'vicariousinc-master' into unit_tests
NishanthJKumar Aug 19, 2021
cbac54a
updates ci to only run build on PR's again!
NishanthJKumar Aug 19, 2021
db8f120
testing codecov from personal fork
NishanthJKumar Aug 20, 2021
8472fd4
updates tests
NishanthJKumar Aug 21, 2021
43ccf99
adds simple e2e test inspired by the heretic model
NishanthJKumar Aug 22, 2021
2ab9fbe
includes tests reaching 95% coverage of pgmax
NishanthJKumar Aug 22, 2021
d75050e
adds more tests to get to 97% overall coverage
NishanthJKumar Aug 24, 2021
ed293af
updates tests to 100% coverage
NishanthJKumar Aug 24, 2021
401f629
addresses first major round of comments on #41
NishanthJKumar Aug 25, 2021
e894d9d
Update tests/fg/test_groups.py
NishanthJKumar Aug 25, 2021
52fdfa0
addresses second round of comments on #41
NishanthJKumar Aug 25, 2021
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
Prev Previous commit
Next Next commit
addresses first major round of comments on #41
  • Loading branch information
NishanthJKumar committed Aug 25, 2021
commit 401f629c8adc83c41e4b1131de964610485e8575
7 changes: 7 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[report]
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain if tests don't hit defensive assertion code:
raise NotImplementedError
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ jobs:
- name: Upload Coverage to Codecov
NishanthJKumar marked this conversation as resolved.
Show resolved Hide resolved
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
verbose: true # optional (default = false)
#----------------------------------------------
# test docs build only on PR
Expand Down
28 changes: 13 additions & 15 deletions pgmax/fg/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@dataclass(frozen=True, eq=False)
class VariableGroup:
"""Base class to represent a group of variables.
"""Class to represent a group of variables.

All variables in the group are assumed to have the same size. Additionally, the
variables are indexed by a "key", and can be retrieved by direct indexing (even indexing
Expand All @@ -35,12 +35,12 @@ def __post_init__(self) -> None:
)

@typing.overload
def __getitem__(self, key: Hashable) -> nodes.Variable: # pragma: no cover
pass
def __getitem__(self, key: Hashable) -> nodes.Variable:
"""This function must be overriden by a subclass; does nothing"""
NishanthJKumar marked this conversation as resolved.
Show resolved Hide resolved

@typing.overload
def __getitem__(self, key: List) -> List[nodes.Variable]: # pragma: no cover
pass
def __getitem__(self, key: List) -> List[nodes.Variable]:
"""This function must be overriden by a subclass; does nothing"""
NishanthJKumar marked this conversation as resolved.
Show resolved Hide resolved

def __getitem__(self, key):
"""Given a key, retrieve the associated Variable.
Expand Down Expand Up @@ -84,9 +84,7 @@ def _set_keys_to_vars(self) -> Dict[Any, nodes.Variable]:
"Please subclass the VariableGroup class and override this method"
)

def get_vars_to_evidence(
self, evidence: Any
) -> Dict[nodes.Variable, np.ndarray]: # pragma: no cover
def get_vars_to_evidence(self, evidence: Any) -> Dict[nodes.Variable, np.ndarray]:
"""Function that turns input evidence into a dictionary mapping variables to evidence.

Returns:
Expand Down Expand Up @@ -143,13 +141,13 @@ def __post_init__(self):
self, "_keys_to_vars", MappingProxyType(self._set_keys_to_vars())
)

@typing.overload # pragma: no cover
@typing.overload
def __getitem__(self, key: Hashable) -> nodes.Variable:
pass
"""This function must be overriden by a subclass; does nothing"""
NishanthJKumar marked this conversation as resolved.
Show resolved Hide resolved

@typing.overload # pragma: no cover
@typing.overload
def __getitem__(self, key: List) -> List[nodes.Variable]:
pass
"""This function must be overriden by a subclass; does nothing"""
NishanthJKumar marked this conversation as resolved.
Show resolved Hide resolved

def __getitem__(self, key):
"""Given a key, retrieve the associated Variable from the associated VariableGroup.
Expand Down Expand Up @@ -355,7 +353,7 @@ def get_vars_to_evidence(

@dataclass(frozen=True, eq=False)
class FactorGroup:
"""Base class to represent a group of factors.
"""Class to represent a group of factors.

Args:
variable_group: either a VariableGroup or - if the elements of more than one VariableGroup
Expand Down Expand Up @@ -384,7 +382,7 @@ def factors(self) -> Tuple[nodes.EnumerationFactor, ...]:

@dataclass(frozen=True, eq=False)
class EnumerationFactorGroup(FactorGroup):
"""Base class to represent a group of EnumerationFactors.
"""Class to represent a group of EnumerationFactors.

All factors in the group are assumed to have the same set of valid configurations and
the same potential function. Note that the log potential function is assumed to be
Expand Down Expand Up @@ -430,7 +428,7 @@ def factors(self) -> Tuple[nodes.EnumerationFactor, ...]:

@dataclass(frozen=True, eq=False)
class PairwiseFactorGroup(FactorGroup):
"""Base class to represent a group of EnumerationFactors where each factor connects to
"""Class to represent a group of EnumerationFactors where each factor connects to
two different variables.

All factors in the group are assumed to be such that all possible configuration of the two
Expand Down
2 changes: 1 addition & 1 deletion pgmax/fg/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EnumerationFactor:

Args:
variables: List of involved variables
configs: 2D array of shape (num_val_configs, num_variables)
configs: Array of shape (num_val_configs, num_variables)
An array containing an explicit enumeration of all valid configurations
factor_configs_log_potentials: Array of shape (num_val_configs,). An array containing
the log of the potential value for every possible configuration
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
filterwarnings =
ignore::DeprecationWarning
ignore::DeprecationWarning:flatbuffers
12 changes: 0 additions & 12 deletions tests/fg/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import pytest

from pgmax.fg import graph, groups


def test_onevar_graph():
v_group = groups.GenericVariableGroup(15, tuple([0]))
fg = graph.FactorGraph(v_group)
assert fg._composite_variable_group[0, 0].num_states == 15


def test_set_evidence_Error():
v_group = groups.GenericVariableGroup(15, tuple([0]))
fg = graph.FactorGraph(v_group)
fg.evidence_default_mode = "test"
with pytest.raises(NotImplementedError) as nerror:
fg.evidence

assert "evidence_default_mode" in str(nerror.value)
17 changes: 5 additions & 12 deletions tests/fg/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def test_vargroup_list_idx():
assert v_group[[0, 1, 2]][0].num_states == 15


def test_vargroup_set_keys_to_vars():
with pytest.raises(NotImplementedError) as nerror:
groups.VariableGroup()
assert "Please subclass" in str(nerror.value)


def test_composite_vargroup_valueerror():
v_group1 = groups.GenericVariableGroup(15, tuple([0, 1, 2]))
v_group2 = groups.GenericVariableGroup(15, tuple([0, 1, 2]))
Expand All @@ -32,10 +26,14 @@ def test_composite_vargroup_evidence():
len(
comp_var_group.get_vars_to_evidence(
[{0: np.zeros(3)}, {0: np.zeros(3)}]
).keys()
).values()
)
== 2
)
for arr in comp_var_group.get_vars_to_evidence(
[{0: np.zeros(3)}, {0: np.zeros(3)}]
).values():
assert (arr == np.zeros(3, dtype=float)).all()


def test_ndvararray_evidence_error():
Expand All @@ -51,11 +49,6 @@ def test_facgroup_errors():
groups.FactorGroup(v_group, [])
assert "self.connected_var_keys is empty" == str(verror.value)

fac_group = groups.FactorGroup(v_group, [[(0, 0), (1, 1)]])
with pytest.raises(NotImplementedError) as nerror:
fac_group.factors
assert "Needs to be overriden by subclass" == str(nerror.value)


def test_pairwisefacgroup_errors():
v_group = groups.NDVariableArray(3, (2, 2))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pgmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def create_valid_suppression_config_arr(suppression_diameter):
)

# Run BP
one_step_msgs = fg.run_bp(100, 0.5)
one_step_msgs = fg.run_bp(1, 0.5)
final_msgs = fg.run_bp(99, 0.5, one_step_msgs)

# Test that the output messages are close to the true messages
Expand Down