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

fix: Umap duplicate index #489

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions graphistry/tests/test_umap_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@
node_numeric = node_ints + node_floats
node_target = triangleNodes[["y"]]

node_graph_with_index = pd.DataFrame(
{
"index": range(1, 13),
"a": ["a", "b", "c", "d"] * 3,
"b": ["w", "x", "y", "z"] * 3,
}
)

edge_graph_with_index = pd.DataFrame(
{
"index": range(1, 13),
"a": ["a", "b", "c", "d"] * 3,
"b": ["w", "x", "y", "z"] * 3,
"src": [1, 2, 3, 4] * 3,
"dst": [4, 3, 1, 2] * 3,
}
)

def _eq(df1, df2):
try:
df1 = df1.to_pandas()
Expand Down Expand Up @@ -150,6 +168,15 @@ def setUp(self):
)
self.g2e = g2

# graph with index
self.g_index_nodes = graphistry.nodes(node_graph_with_index)
self.g_index_nodes_umaped = self.g_index_nodes.umap(engine="umap_learn")
assert "_n" == self.g_index_nodes_umaped._node

self.g_index_edges = graphistry.nodes(edge_graph_with_index)
self.g_index_edges_umaped = self.g_index_edges.umap(engine="umap_learn")
assert "_n" == self.g_index_edges_umaped._node


@pytest.mark.skipif(not has_umap, reason="requires umap feature dependencies")
def test_columns_match(self):
Expand Down Expand Up @@ -816,6 +843,15 @@ def test_base(self):
graphistry.nodes(self.df).umap('auto')._node_embedding.shape == (self.samples, 2)
graphistry.nodes(self.df).umap('engine')._node_embedding.shape == (self.samples, 2)

# graph with index
self.g_index_nodes = graphistry.nodes(node_graph_with_index)
self.g_index_nodes_umaped = self.g_index_nodes.umap(engine="cuml")
assert "_n" == self.g_index_nodes_umaped._node

self.g_index_edges = graphistry.nodes(edge_graph_with_index)
self.g_index_edges_umaped = self.g_index_edges.umap(engine="cuml")
assert "_n" == self.g_index_edges_umaped._node


if __name__ == "__main__":
unittest.main()
7 changes: 4 additions & 3 deletions graphistry/umap_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,13 @@ def umap(

if kind == "nodes":
index = res._nodes.index

if res._node is None:
logger.debug("-Writing new node name")
res._nodes[config.IMPLICIT_NODE_ID] = range(len(res._nodes))

Copy link
Contributor

Choose a reason for hiding this comment

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

Clever

Will this work if cudf?

Will this break somehow downstream if .edges is bound and user was using string name IDs for src dst?

Copy link
Contributor

Choose a reason for hiding this comment

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

Strange, I thought this was already done this way...

res = res.nodes( # type: ignore
res._nodes.reset_index(drop=True)
.reset_index()
.rename(columns={"index": config.IMPLICIT_NODE_ID}),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just call it __index_umap__ or something that has low prob of collision? then we can keep it safe for cudf?

res._nodes,
config.IMPLICIT_NODE_ID,
)
res._nodes.index = index
Expand Down
Loading