Skip to content
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
34 changes: 18 additions & 16 deletions src/pathpyG/core/temporal_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,20 @@ def __init__(self, data: Data, mapping: IndexMap | None = None) -> None:
print(t)
```
"""
if not isinstance(data.edge_index, EdgeIndex):
data.edge_index = data.edge_index = EdgeIndex(
data=data.edge_index.contiguous(), sparse_size=(data.num_nodes, data.num_nodes)
self.data = data
if not isinstance(self.data.edge_index, EdgeIndex):
self.data.edge_index = EdgeIndex(
data=self.data.edge_index.contiguous(), sparse_size=(self.data.num_nodes, self.data.num_nodes)
)

# reorder temporal data
# TODO: Fix in PyG
if data.num_nodes != data.num_edges:
self.data = data.sort_by_time()
else:
sorted_idx = torch.argsort(data.time)
data.time = data.time[sorted_idx]
for edge_attr in data.edge_attrs():
if edge_attr == "edge_index":
data.edge_index = data.edge_index[:, sorted_idx]
else:
data[edge_attr] = data[edge_attr][sorted_idx]
self.data = data
# Note that we do not use `torch_geometric.self.data.Data.sort_by_time` because it cannot sort numpy arrays`
sorted_idx = torch.argsort(self.data.time)
for edge_attr in set(self.data.edge_attrs()).union(set(["time"])):
if edge_attr == "edge_index":
self.data.edge_index = self.data.edge_index[:, sorted_idx]
else:
data[edge_attr] = data[edge_attr][sorted_idx]

if mapping is not None:
self.mapping = mapping
Expand All @@ -68,6 +64,12 @@ def from_edge_list(edge_list, num_nodes: Optional[int] = None) -> TemporalGraph:
edge_array = np.array(edge_list)
ts = edge_array[:, 2].astype(np.number)

# Convert timestamps to tensor
if np.issubdtype(ts.dtype, np.integer):
ts = torch.tensor(ts, dtype=torch.long)
else:
ts = torch.tensor(ts, dtype=torch.float32)

index_map = IndexMap(np.unique(edge_array[:, :2]))
edge_index = index_map.to_idxs(edge_array[:, :2].T)

Expand All @@ -77,7 +79,7 @@ def from_edge_list(edge_list, num_nodes: Optional[int] = None) -> TemporalGraph:
return TemporalGraph(
data=Data(
edge_index=edge_index,
time=torch.Tensor(ts),
time=ts,
num_nodes=num_nodes,
),
mapping=index_map,
Expand Down
4 changes: 1 addition & 3 deletions src/pathpyG/io/netzschleuder.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ def read_netzschleuder_graph(
if timestamps:
g = df_to_temporal_graph(df=edges, is_undirected=not is_directed, num_nodes=num_nodes)
else:
g = df_to_graph(df=edges, multiedges=True, num_nodes=num_nodes)
if not is_directed:
g = g.to_undirected()
g = df_to_graph(df=edges, multiedges=multiedges, is_undirected=not is_directed, num_nodes=num_nodes)

node_attrs = pd.read_csv(
f"{temp_dir}/nodes.csv", header=0, sep=",", skip_blank_lines=True, skipinitialspace=True
Expand Down
Loading
Loading