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

Use proper normalization in Hamming #250

Merged
merged 4 commits into from
Sep 23, 2019
Merged
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
31 changes: 28 additions & 3 deletions netrd/distance/hamming.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,34 @@ def dist(self, G1, G2):
.. [1] https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.hamming.html#scipy.spatial.distance.hamming

"""

if G1.number_of_nodes() == G2.number_of_nodes():
N = G1.number_of_nodes()
else:
raise ValueError("Graphs have the same number of nodes")

adj1 = nx.to_numpy_array(G1)
adj2 = nx.to_numpy_array(G2)
dist = scipy.spatial.distance.hamming(adj1.flatten(), adj2.flatten())
self.results['dist'] = dist
self.results['adjacency_matrices'] = adj1, adj2

# undirected case: consider only upper triangular
mask = np.triu_indices(N, k=1)

# directed case: consider all but the diagonal
if nx.is_directed(G1) or nx.is_directed(G2):
new_mask = np.tril_indices(N, k=-1)
mask = (np.append(mask[0], new_mask[0]), np.append(mask[1], new_mask[1]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it can be achieved in a single append or hstack call? No need to change, just thinking out loud here..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be? It's a tuple of np.arrays, so (i) they're immutable, and (ii) they're two separate objects. I think what I wrote is pretty ugly and I don't like it, but I wasn't sure about a better way to do it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh you're right it returns a tuple because it's stupid. What about

mask = np.array(np.triu_indices(N, k=1))

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem to work.

In [12]: m = np.triu_indices(10, k=1)                                                                                                                                   

In [13]: M = np.array(m)                                                                                                                                                

In [14]: A = np.zeros((10,10))                                                                                                                                          

In [15]: len(A[M])                                                                                                                                                      
Out[15]: 2

In [16]: len(A[m])                                                                                                                                                      
Out[16]: 45

In [17]: A[M].shape                                                                                                                                                     
Out[17]: (2, 45, 10)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ok. There must be a way to do it nicely, but I don't care enough right now...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. There's some DRY here so I thought about wrapping it in a function, but the idea of needing a function to do that bothered me...


# only if there are self-loops include the diagonal
# this corrects the implicit denominator of Hamming, which
# should be N^2 for networks with self-loops and N(N-1) for
# those without
if next(nx.selfloop_edges(G1), False) or next(nx.selfloop_edges(G2), False):
new_mask = np.diag_indices(N)
mask = (np.append(mask[0], new_mask[0]), np.append(mask[1], new_mask[1]))

dist = scipy.spatial.distance.hamming(
adj1[mask].flatten(), adj2[mask].flatten()
)
self.results["dist"] = dist
self.results["adjacency_matrices"] = adj1, adj2
return dist