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

Improve code coverage #43

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
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
18 changes: 5 additions & 13 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,13 @@ def test_multi_matching_result():
num_graphs = 5
pygm.BACKEND = 'numpy'
As, X = pygm.utils.generate_isomorphic_graphs(num_nodes, num_graphs)
As_1, As_2, Fs_1, Fs_2 = [], [], [], []
for i in range(num_graphs):
for j in range(num_graphs):
As_1.append(pygm.utils.to_numpy(As[i]))
As_2.append(pygm.utils.to_numpy(As[j]))
As_1 = pygm.utils.from_numpy(np.stack(As_1, axis=0))
As_2 = pygm.utils.from_numpy(np.stack(As_2, axis=0))
conn1, edge1, ne1 = pygm.utils.dense_to_sparse(As_1)
conn2, edge2, ne2 = pygm.utils.dense_to_sparse(As_2)
K = pygm.utils.build_aff_mat(None, edge1, conn1, None, edge2, conn2, None, ne1, None, ne2)
K = K.reshape((num_graphs, num_graphs, num_nodes ** 2, num_nodes ** 2))
X = pygm.cao(K) # X is multi-matching result
mmX = pygm.utils.MultiMatchingResult()
for i in range(X.shape[0]):
for j in range(X.shape[1]):
mmX[i, j] = X[i, j]

for backend in ['numpy', 'pytorch', 'paddle', 'jittor']:
newX = pygm.utils.from_numpy(X, backend=backend)
newX = pygm.utils.from_numpy(mmX, backend=backend)
newX.__repr__()
newX.__str__()

Expand Down
31 changes: 27 additions & 4 deletions tests/test_multi_graph_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def _test_mgm_solver_on_isomorphic_graphs(num_graph, num_node, node_feat_dim, so
pygm.BACKEND = working_backend
if 'x0' in solver_param_dict and solver_param_dict['x0'] is not None:
solver_param_dict['x0'] = pygm.utils.from_numpy(solver_param_dict['x0'])
if 'ns' in solver_param_dict and solver_param_dict['ns'] is not None:
solver_param_dict['ns'] = pygm.utils.from_numpy(solver_param_dict['ns'])
if mode == 'lawler-qap':
_As_1, _As_2, _Fs_1, _Fs_2, _X_gt = data_from_numpy(As_1, As_2, Fs_1, Fs_2, X_gt)
_conn1, _edge1, _ne1 = pygm.utils.dense_to_sparse(_As_1)
Expand Down Expand Up @@ -133,17 +135,26 @@ def _test_mgm_solver_on_isomorphic_graphs(num_graph, num_node, node_feat_dim, so
last_X = pygm.utils.to_numpy(_X)

matched = 0
total = 0
for i, j in itertools.product(range(num_graph), repeat=2):
matched += (pygm.utils.to_numpy(_X[i, j]) * X_gt[i, j]).sum()
accuracy = matched / X_gt.sum()
if 'ns' in solver_param_dict and solver_param_dict['ns'] is not None:
nsi = pygm.utils.to_numpy(solver_param_dict['ns'][i]).item()
nsj = pygm.utils.to_numpy(solver_param_dict['ns'][j]).item()
matched += (pygm.utils.to_numpy(_X[i, j]) * X_gt[i, j, :nsi, :nsj]).sum()
total += X_gt[i, j, :nsi, :nsj].sum()
else:
matched += (pygm.utils.to_numpy(_X[i, j]) * X_gt[i, j]).sum()
total += X_gt[i, j].sum()
accuracy = matched / total
assert accuracy == 1, f"GM is inaccurate for {working_backend}, accuracy={accuracy}, " \
f"params: {';'.join([k + '=' + str(v) for k, v in aff_param_dict.items()])};" \
f"{';'.join([k + '=' + str(v) for k, v in solver_param_dict.items()])}"
else:
raise ValueError(f'Unknown mode: {mode}')
if 'x0' in solver_param_dict and solver_param_dict['x0'] is not None:
solver_param_dict['x0'] = pygm.utils.to_numpy(solver_param_dict['x0'])

if 'ns' in solver_param_dict and solver_param_dict['ns'] is not None:
solver_param_dict['ns'] = pygm.utils.to_numpy(solver_param_dict['ns'])

def test_cao():
num_nodes = 5
Expand Down Expand Up @@ -174,6 +185,7 @@ def test_mgm_floyd():
def test_gamgm():
num_nodes = 5
num_graphs = 10
# test without outliers
_test_mgm_solver_on_isomorphic_graphs(num_graphs, num_nodes, 10, pygm.gamgm, 'kb-qap', {
'sk_init_tau': [0.5, 0.1],
'sk_min_tau': [0.1, 0.05],
Expand All @@ -182,6 +194,18 @@ def test_gamgm():
'verbose': [True]
}, ['pytorch', 'numpy', 'paddle', 'jittor'])

# test with outliers
_test_mgm_solver_on_isomorphic_graphs(num_graphs, num_nodes, 10, pygm.gamgm, 'kb-qap', {
'sk_init_tau': [0.5],
'sk_gamma': [0.8],
'sk_min_tau': [0.1],
'param_lambda': [0.1],
'node_aff_fn': [functools.partial(pygm.utils.gaussian_aff_fn, sigma=.1)],
'verbose': [True],
'n_univ': [10],
'ns': [np.array([num_nodes] * (num_graphs // 2) + [num_nodes-1] * (num_graphs - num_graphs // 2))],
}, ['pytorch', 'numpy', 'paddle', 'jittor'])


def test_gamgm_backward():
# Pytorch
Expand All @@ -208,7 +232,6 @@ def test_gamgm_backward():
acc.backward()
assert torch.sum(W.grad != 0) > 0


# Jittor
pygm.BACKEND = 'jittor'
jt.set_global_seed(2)
Expand Down