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

Add integration test and runner #14

Merged
merged 6 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for networkx adapter
  • Loading branch information
gkirgizov committed Dec 26, 2022
commit 96a15ccdc64fd21486fe3c02570c4dd2ea4227e6
61 changes: 61 additions & 0 deletions test/unit/adapter/graph_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from golem.core.adapter.nx_adapter import DumbNetworkxAdapter, BaseNetworkxAdapter
from test.unit.mocks.common_mocks import MockNode, MockDomainStructure, MockAdapter


def get_adapters():
return [MockAdapter(),
BaseNetworkxAdapter(),
DumbNetworkxAdapter()]


def get_graphs():
node_a = MockNode('operation_a')
node_b = MockNode('operation_a', nodes_from=[node_a])
node_c = MockNode('operation_a', nodes_from=[node_b, node_a])
skip_connection_structure = MockDomainStructure([node_c])

one_node_graph = MockDomainStructure([node_a])

node_d = MockNode('operation_a', nodes_from=[node_b])
linear_graph = MockDomainStructure([node_d])

node_f = MockNode('operation_f')
node_g = MockNode('operation_g', nodes_from=[node_b, node_f])
branching_structure = MockDomainStructure([node_g])

node_k = MockNode('operation_k', nodes_from=[node_f])
node_m = MockNode('operation_m', nodes_from=[node_b, node_k])
node_r = MockNode('operation_r', nodes_from=[node_m])
branching_structure2 = MockDomainStructure([node_r])

return [one_node_graph, linear_graph,
branching_structure, branching_structure2,
skip_connection_structure]


def get_optgraphs():
adapter = MockAdapter()
return [adapter.adapt(g) for g in get_graphs()]


def graph_with_custom_parameters(alpha_value):
node_a = MockNode('a')
node_b = MockNode('b')
node_c = MockNode('c', nodes_from=[node_a])
node_d = MockNode('d', nodes_from=[node_b])
node_final = MockNode('e', nodes_from=[node_c, node_d])
node_final.content['params'] = {'alpha': alpha_value}
graph = MockDomainStructure([node_final])

return graph


def get_complex_graph():
node_a = MockNode('a')
node_b = MockNode('b', nodes_from=[node_a])
node_c = MockNode('c', nodes_from=[node_b])
node_d = MockNode('d', nodes_from=[node_b, node_c])
node_e = MockNode('e', nodes_from=[node_d])
node_final = MockNode('a', nodes_from=[node_c, node_e])
graph = MockDomainStructure([node_final])
return graph
83 changes: 23 additions & 60 deletions test/unit/adapter/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,12 @@
from golem.core.dag.graph_verifier import GraphVerifier
from golem.core.dag.verification_rules import DEFAULT_DAG_RULES
from golem.core.optimisers.graph import OptNode
from test.unit.mocks.common_mocks import MockNode, MockDomainStructure, MockAdapter
from test.unit.adapter.graph_data import get_graphs, graph_with_custom_parameters, get_complex_graph, get_adapters, \
get_optgraphs
from test.unit.mocks.common_mocks import MockNode, MockAdapter
from test.unit.utils import find_first


def get_graphs():
node_a = MockNode('operation_a')
node_b = MockNode('operation_a', nodes_from=[node_a])
node_c = MockNode('operation_a', nodes_from=[node_b, node_a])
skip_connection_structure = MockDomainStructure([node_c])

one_node_graph = MockDomainStructure([node_a])

node_d = MockNode('operation_a', nodes_from=[node_b])
linear_graph = MockDomainStructure([node_d])

node_f = MockNode('operation_f')
node_g = MockNode('operation_g', nodes_from=[node_b, node_f])
branching_structure = MockDomainStructure([node_g])

node_k = MockNode('operation_k', nodes_from=[node_f])
node_m = MockNode('operation_m', nodes_from=[node_b, node_k])
node_r = MockNode('operation_r', nodes_from=[node_m])
branching_structure2 = MockDomainStructure([node_r])

return [one_node_graph, linear_graph,
branching_structure, branching_structure2,
skip_connection_structure]


def graph_with_custom_parameters(alpha_value):
node_a = MockNode('a')
node_b = MockNode('b')
node_c = MockNode('c', nodes_from=[node_a])
node_d = MockNode('d', nodes_from=[node_b])
node_final = MockNode('e', nodes_from=[node_c, node_d])
node_final.content['params'] = {'alpha': alpha_value}
graph = MockDomainStructure([node_final])

return graph


def get_complex_graph():
node_a = MockNode('a')
node_b = MockNode('b', nodes_from=[node_a])
node_c = MockNode('c', nodes_from=[node_b])
node_d = MockNode('d', nodes_from=[node_b, node_c])
node_e = MockNode('e', nodes_from=[node_d])
node_final = MockNode('a', nodes_from=[node_c, node_e])
graph = MockDomainStructure([node_final])
return graph


def test_adapters_params_correct():
""" Checking the correct conversion of hyperparameters in nodes when nodes
are passing through adapter
Expand All @@ -77,9 +31,21 @@ def test_adapters_params_correct():
assert np.isclose(init_alpha, restored_alpha)


@pytest.mark.parametrize('adapter', get_adapters())
@pytest.mark.parametrize('optgraph', get_optgraphs())
def test_restored_and_adapted_are_equal(adapter, optgraph):
graph = adapter.restore(optgraph)
retransformed_optgraph = adapter.adapt(graph)

# assert 2-way mapping doesn't change the structure
assert retransformed_optgraph.descriptive_id == optgraph.descriptive_id
# assert that new graph is a different object
assert id(optgraph) != id(retransformed_optgraph)


@pytest.mark.parametrize('adapter', [MockAdapter()])
@pytest.mark.parametrize('graph', get_graphs())
def test_graph_adapt_properly(graph):
adapter = MockAdapter()
def test_graph_adapt_properly(adapter, graph):
verifier = GraphVerifier(DEFAULT_DAG_RULES)

assert all(isinstance(node, MockNode) for node in graph.nodes)
Expand All @@ -93,20 +59,18 @@ def test_graph_adapt_properly(graph):
assert verifier(opt_graph)


@pytest.mark.parametrize('adapter', [MockAdapter()])
@pytest.mark.parametrize('graph', get_graphs())
def test_adapted_has_same_structure(graph):
adapter = MockAdapter()

def test_adapted_has_same_structure(adapter, graph):
opt_graph = adapter.adapt(graph)

# assert graph structures are same
assert graph.descriptive_id == opt_graph.descriptive_id


@pytest.mark.parametrize('adapter', [MockAdapter()])
@pytest.mark.parametrize('graph', get_graphs())
def test_adapted_and_restored_are_equal(graph):
adapter = MockAdapter()

def test_adapted_and_restored_are_equal(adapter, graph):
opt_graph = adapter.adapt(graph)
restored_graph = adapter.restore(opt_graph)

Expand All @@ -116,10 +80,9 @@ def test_adapted_and_restored_are_equal(graph):
assert id(graph) != id(restored_graph)


@pytest.mark.parametrize('adapter', [MockAdapter()])
@pytest.mark.parametrize('graph', get_graphs())
def test_changes_to_transformed_dont_affect_origin(graph):
adapter = MockAdapter()

def test_changes_to_transformed_dont_affect_origin(adapter, graph):
original_graph = deepcopy(graph)
opt_graph = adapter.adapt(graph)

Expand Down