Download tasks/bibliographic_archive/solution/reference/graph.py from Tokenwave-AI/REE: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/bibliographic_archive/solution/reference/graph.py
- Command line
-
hf download hf://datasets/Tokenwave-AI/REE/tasks/bibliographic_archive/solution/reference/graph.py
-
curl -L -o graph.py https://huggingface.co/datasets/Tokenwave-AI/REE/resolve/main/tasks/bibliographic_archive/solution/reference/graph.py
2.84 kB
| """SciPy adaptation of the pinned DGL SIGN MAG preprocessing. | |
| Based on DGL, Apache-2.0; see vendor/dgl_sign/LICENSE and SOURCES.json. | |
| The previous port's feature means and five-hop computation are retained. | |
| """ | |
| from __future__ import annotations | |
| import numpy as np | |
| import scipy.sparse as sp | |
| def _mean_into(dst: np.ndarray, ids: np.ndarray, values: np.ndarray, n: int) -> None: | |
| sums = np.zeros((n, values.shape[1]), dtype=np.float32) | |
| np.add.at(sums, ids, values) | |
| counts = np.bincount(ids, minlength=n).astype(np.float32) | |
| counts[counts == 0] = 1.0 | |
| dst[:] = sums / counts[:, None] | |
| def sign_features(meta: dict, arrays: dict, hops: int = 5) -> np.ndarray: | |
| """The SIGN MAG conversion and R-hop neighbor-mean features.""" | |
| paper, author, field, institution = map(int, meta["counts"]) | |
| total = paper + author + field + institution | |
| width = int(arrays["features"].shape[1]) | |
| node_features = np.zeros((total, width), dtype=np.float32) | |
| paper_features = np.asarray(arrays["features"], dtype=np.float32) | |
| node_features[:paper] = paper_features | |
| r1 = np.asarray(arrays["r1"], dtype=np.int64) # author -> paper | |
| _mean_into(node_features[paper : paper + author], r1[:, 0], paper_features[r1[:, 1]], author) | |
| r2 = np.asarray(arrays["r2"], dtype=np.int64) # paper -> field | |
| _mean_into(node_features[paper + author : paper + author + field], r2[:, 1], paper_features[r2[:, 0]], field) | |
| r3 = np.asarray(arrays["r3"], dtype=np.int64) # author -> institution | |
| _mean_into(node_features[paper + author + field :], r3[:, 1], node_features[paper + r3[:, 0]], institution) | |
| edge_blocks = [] | |
| for name, source_offset, dest_offset in ( | |
| ("r0", 0, 0), ("r1", paper, 0), | |
| ("r2", 0, paper + author), ("r3", paper, paper + author + field), | |
| ): | |
| e = np.asarray(arrays[name], dtype=np.int64) | |
| src = e[:, 0] + source_offset | |
| dst = e[:, 1] + dest_offset | |
| edge_blocks.append(np.column_stack((src, dst))) | |
| # DGL add_reverse_edges defaults to exclude_self=True. Preserve | |
| # parallel edges, but do not add a second copy of an existing loop. | |
| nonself = src != dst | |
| edge_blocks.append(np.column_stack((dst[nonself], src[nonself]))) | |
| edges = np.concatenate(edge_blocks, axis=0) | |
| adjacency = sp.coo_matrix( | |
| (np.ones(edges.shape[0], dtype=np.float32), (edges[:, 0], edges[:, 1])), | |
| shape=(total, total), | |
| ).tocsr() | |
| degree = np.asarray(adjacency.sum(axis=1)).ravel().astype(np.float32) | |
| degree[degree == 0] = 1.0 | |
| result = [node_features[:paper].copy()] | |
| for _ in range(hops): | |
| node_features = adjacency.dot(node_features).astype(np.float32, copy=False) | |
| node_features /= degree[:, None] | |
| result.append(node_features[:paper].copy()) | |
| return np.concatenate(result, axis=1) | |