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

Inconsistent number of nodes with graph-tool for disconnected graphs #61

Closed
RMeli opened this issue Jan 27, 2022 · 0 comments · Fixed by #62
Closed

Inconsistent number of nodes with graph-tool for disconnected graphs #61

RMeli opened this issue Jan 27, 2022 · 0 comments · Fixed by #62
Assignees

Comments

@RMeli
Copy link
Owner

RMeli commented Jan 27, 2022

Describe the bug

Disconnected graphs are usually not within the use case of spyrmsd, however they might appear with distance-based atom selections in some applications (for example selecting protein atoms within a certain distance of the ligand in the binding site).

When there is a disconnected node in the graph, using graph-tool for symmetry corrections results in the following error:

self = <VertexPropertyMap object with value type 'int16_t', for Graph 0x7fb9705cdee0, at 0x7fb9705cd1f0>
v = array([0, 1, 4])

    def __set_array(self, v):
        a = self.get_array()
        if a is None:
            raise TypeError("cannot set property map values from array for" +
                            " property map of type: " + self.value_type())
>       a[:] = v
E       ValueError: could not broadcast input array from shape (3,) into shape (2,)

To Reproduce

c = np.array([[0.0, 1.0, 2.0], [1.0, 2.0, 3.0], [2.0, 3.0, 4.0]])
a = np.array([0, 1, 4])

# Adjacency matrix with disconnected node
A = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 0]])

assert rmsd.symmrmsd(c, c, a, a, A, A) == pytest.approx(0.0, abs=1e-5)

This only happens when using graph-tool.

Expected behavior

One should get the same behavior with networkx and graph-tool.

  • Support disconnected nodes with graph-tool as well
  • Add warning when there are disconnected nodes?

Environment

  • OS: macOS 11.6.2
  • Python: 3.8.10
  • Package Manager: conda

Additional Context

With graph-tool, a graph is built from the adjacency matrix as follows:

# Get upper triangular adjacency matrix
adj = np.triu(adjacency_matrix)

G = gt.Graph(directed=False)
G.add_edge_list(np.transpose(adj.nonzero()))

The adj.nonzero() implies that if there are disconnected nodes, they are discarded (they have no non-zero entries in the adjacency matrix) and therefore the number of nodes will be inconsistent with the number of node properties.

In order to fix the issue, it is possible to explicitly define the number of vertices, instead of relying on the edges:

num_vertices = adjacency_matrix.shape[0]

# Get upper triangular adjacency matrix
adj = np.triu(adjacency_matrix)

G.add_vertex(n=num_vertices)
G = gt.Graph(directed=False)
G.add_edge_list(np.transpose(adj.nonzero()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant