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: edge_index.dtype is float #31

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 12 additions & 8 deletions adbpyg_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,24 +904,28 @@ def __process_adb_edge_df(
]

# 5. Map each ArangoDB from/to _key to the corresponding PyG node id
from_nodes = et_df["from_key"].map(adb_map[from_col]).tolist()
to_nodes = et_df["to_key"].map(adb_map[to_col]).tolist()
# NOTE: map() is somehow converting int values to float...
# So we rely on astype(int) to convert the float back to int,
# but we also fill NaN values with -1 so that we can convert
# the entire column to int without any issues. Need to revisit...
from_n = et_df["from_key"].map(adb_map[from_col]).fillna(-1).astype(int)
to_n = et_df["to_key"].map(adb_map[to_col]).fillna(-1).astype(int)

# 6. Set/Update the PyG Edge Index
edge_index = tensor([from_nodes, to_nodes])
edge_data.edge_index = torch.cat(
(edge_data.get("edge_index", tensor([])), edge_index), dim=1
)
edge_index = tensor([from_n.tolist(), to_n.tolist()], dtype=torch.int64)
aMahanna marked this conversation as resolved.
Show resolved Hide resolved
empty_tensor = torch.tensor([], dtype=torch.int64)
existing_edge_index = edge_data.get("edge_index", empty_tensor)
edge_data.edge_index = torch.cat((existing_edge_index, edge_index), dim=1)

# 7. Deal with invalid edges
if torch.any(torch.isnan(edge_data.edge_index)):
if torch.any(edge_data.edge_index == -1):
if strict:
m = f"Invalid edges found in Edge Collection {e_col}, {from_col} -> {to_col}." # noqa: E501
raise InvalidADBEdgesError(m)
else:
# Remove the invalid edges
edge_data.edge_index = edge_data.edge_index[
:, ~torch.any(edge_data.edge_index.isnan(), dim=0)
:, ~torch.any(edge_data.edge_index == -1, dim=0)
]

# 8. Set the PyG Edge Data
Expand Down
10 changes: 9 additions & 1 deletion tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Dict, List, Optional, Set, Union

import pytest
import torch
from pandas import DataFrame
from torch import Tensor, cat, long, tensor
from torch_geometric.data import Data, HeteroData
Expand Down Expand Up @@ -554,6 +555,7 @@ def test_adb_partial_to_pyg() -> None:
assert type(pyg_g_new) is Data
assert pyg_g["v0"].x.tolist() == pyg_g_new.x.tolist()
assert pyg_g["v0"].y.tolist() == pyg_g_new.y.tolist()
assert pyg_g[e_t].edge_index.dtype == torch.int64
assert pyg_g[e_t].edge_index.tolist() == pyg_g_new.edge_index.tolist()
assert pyg_g[e_t].edge_attr.tolist() == pyg_g_new.edge_attr.tolist()

Expand Down Expand Up @@ -714,13 +716,17 @@ def test_adb_graph_to_pyg_to_arangodb_with_missing_document_and_permissive(

graph = db.graph(name)
v_cols: Set[str] = graph.vertex_collections()
assert len(v_cols) == 1
edge_definitions: List[Json] = graph.edge_definitions()
e_cols: Set[str] = {c["edge_collection"] for c in edge_definitions}
assert len(e_cols) == 1

for v_col in v_cols:
vertex_collection = db.collection(v_col)
vertex_collection.delete("0")

number_of_missing_edges = 32 # (i.e node 0 has 32 edges)

metagraph: ADBMetagraph = {
"vertexCollections": {col: {} for col in v_cols},
"edgeCollections": {col: {} for col in e_cols},
Expand All @@ -729,7 +735,8 @@ def test_adb_graph_to_pyg_to_arangodb_with_missing_document_and_permissive(
data = adapter.arangodb_to_pyg(name, metagraph=metagraph, strict=False)

collection_count: int = db.collection(list(e_cols)[0]).count()
assert len(data.edge_index[0]) < collection_count
assert data.edge_index.dtype == torch.int64
assert data.num_edges + number_of_missing_edges == collection_count

db.delete_graph(name, drop_collections=True)

Expand Down Expand Up @@ -1076,6 +1083,7 @@ def assert_adb_to_pyg(
from_nodes = et_df["from_key"].map(adb_map[from_col]).tolist()
to_nodes = et_df["to_key"].map(adb_map[to_col]).tolist()

assert edge_data.edge_index.dtype == torch.int64
assert from_nodes == edge_data.edge_index[0].tolist()
assert to_nodes == edge_data.edge_index[1].tolist()

Expand Down
Loading