Skip to main content

Temporal Negative Edge Sampler

PyPI Latest Release PyPI Downloads

The Problem:

Poursafaei et al. in NeurIPS 2022 described that random negative edges make temporal link prediction task easy. This is because random negatives are often between unrelated node pairs that have never interacted, making them trivially easy for models to distinguish from true positive edges. So they introduced the concept of historical negatives—edges that existed in the past but are inactive at the current time—making the prediction task more realistic and challenging.

Huang et al. in NeurIPS 2023 created a benchmark based on this called TGB (Temporal Graph Benchmark) where in the validation and test sets they included both historical and random negatives.

  • Historical negatives: Edges that existed in the past but are inactive at the current time between a pair of nodes.
  • Random negatives: Edges that never existed in the past between a pair of nodes.

TGB generated 10-20 negative edges per positive edge where 50% is historical and 50% is random.

However, they only released pre-generated pickle files with negatives for their benchmark datasets, and did not publish code to generate such negatives for new datasets or for training-time augmentation.

Negative edges are generated using the following steps:

  1. Group all edges by timestamp, sorted in chronological order.

  2. Iterate through each timestamp group (i.e., a batch of edges sharing the same timestamp):

    • Add the current batch to a streaming graph structure, which keeps track of all previously seen edges (the historical graph).
  3. For each source node in the current batch:

    • Sample n × p historical negative edges:
      • These are destination nodes that have been connected to the source in the past, but are not connected to the source in the current timestamp.
    • Sample n × (1 - p) random negative edges:
      • These are destination nodes that have never been connected to the source in the historical graph (including the current batch).

While the algorithm described above is correct and benchmark-aligned, it is computationally expensive if implemented naively in Python. A naive Python implementation will not scale to large datasets and may run indefinitely for even moderate-sized graphs.

Temporal Negative Edge Sampler is a high-performance library for generating negative edges in temporal link prediction tasks, supporting both historical and random negative sampling. It mirrors the evaluation strategy used in the Temporal Graph Benchmark (TGB), but extends it to training and custom datasets. The core implementation is written in parallelized C++ with Python bindings, making it scalable to large graphs with millions of edges.

API

collect_all_negatives_by_timestamp

Collects negative edges for temporal graph sampling, processing edges grouped by timestamp.

Signature

from temporal_negative_edge_sampler import collect_all_negatives_by_timestamp

collect_all_negatives_by_timestamp(
    sources: np.ndarray,
    targets: np.ndarray,
    timestamps: np.ndarray,
    is_directed: bool,
    num_negatives_per_positive: int,
    historical_negative_percentage: float = 0.5
) -> Tuple[np.ndarray, np.ndarray]

Parameters

  • sources (np.ndarray): Array of source node IDs
  • targets (np.ndarray): Array of target node IDs
  • timestamps (np.ndarray): Array of timestamps for each edge
  • is_directed (bool): Whether the graph is directed
  • num_negatives_per_positive (int): Number of negative edges to sample per positive edge
  • historical_negative_percentage (float, optional): Ratio of historical vs random negatives (default: 0.5)

References:

Poursafaei et al. (2022)
Towards Better Evaluation for Dynamic Link Prediction
Farimah Poursafaei, Shenyang Huang, Kris Pelrine, Reihaneh Rabbany
In NeurIPS 2022 Datasets and Benchmarks Track.
https://arxiv.org/pdf/2207.10128

Huang et al. (2023)
Temporal Graph Benchmark for Machine Learning on Temporal Graphs
Shenyang Huang, Farimah Poursafaei, Jacob Danovitch, Matthias Fey, Weihua Hu, Emanuele Rossi, Jure Leskovec, Michael Bronstein, Guillaume Rabusseau, Reihaneh Rabbany
In NeurIPS 2023 Datasets and Benchmarks Track.
https://arxiv.org/pdf/2307.01026

Metadata

Release files for temporal-negative-edge-sampler 0.1.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for temporal-negative-edge-sampler 0.1.16
File
temporal_negative_edge_sampler-0.1.16-cp311-cp311-manylinux_2_34_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.34+ x86-64 Details
temporal_negative_edge_sampler-0.1.16-cp310-cp310-manylinux_2_34_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ x86-64 Details
temporal_negative_edge_sampler-0.1.16-cp310-cp310-macosx_15_0_arm64.whl CPython 3.10 CPython 3.10 macOS 15.0+ ARM64 Details
temporal_negative_edge_sampler-0.1.16-cp39-cp39-manylinux_2_34_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.34+ x86-64 Details
temporal_negative_edge_sampler-0.1.16-cp38-cp38-manylinux_2_34_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.34+ x86-64 Details

Total release size: 1.1 MB

Release files / temporal_negative_edge_sampler-0.1.16-cp311-cp311-manylinux_2_34_x86_64.whl

Download URL temporal_negative_edge_sampler-0.1.16-cp311-cp311-manylinux_2_34_x86_64.whl
Size 245.0 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
5be3c795b1feb6238382110da0f9abca8f0fc09b96b2b7e0e1d34665f672cc77
BLAKE2b-256 checksum
How to use checksums
dcffae4df5adf97b371c55a2bae7b234c07d3ab074200a434021f5563b5bc823
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release files / temporal_negative_edge_sampler-0.1.16-cp310-cp310-manylinux_2_34_x86_64.whl

Download URL temporal_negative_edge_sampler-0.1.16-cp310-cp310-manylinux_2_34_x86_64.whl
Size 243.9 kB
Tags CPython 3.10 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
1f4940d65adb9d621e51af186ad485b6bdadaad33e12e8056bdd1da3a48b9b9c
BLAKE2b-256 checksum
How to use checksums
517f1ccd051bfa7749058a6db15a0535e738318c4a03280d15422898c4fb5971
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release files / temporal_negative_edge_sampler-0.1.16-cp310-cp310-macosx_15_0_arm64.whl

Download URL temporal_negative_edge_sampler-0.1.16-cp310-cp310-macosx_15_0_arm64.whl
Size 76.3 kB
Tags CPython 3.10 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
d3d68c2b64670c53ceb69c8d0caa94b62189e5209d73f0a434dc0d76ef40e3aa
BLAKE2b-256 checksum
How to use checksums
3ce9a5fb4cb96dceeff684d54d422a801f55d7c43c0aa2c1f8160195529f0957
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.14

Release files / temporal_negative_edge_sampler-0.1.16-cp39-cp39-manylinux_2_34_x86_64.whl

Download URL temporal_negative_edge_sampler-0.1.16-cp39-cp39-manylinux_2_34_x86_64.whl
Size 243.3 kB
Tags CPython 3.9 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
6167bd87d74dac095bc5365a186f358f2e97d8487253458cc1f34e62ce0a7f44
BLAKE2b-256 checksum
How to use checksums
2400c003ea57fb2a534a4f19dadcc1f67c3a96cf1d10caa6d7596da2f69a506d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release files / temporal_negative_edge_sampler-0.1.16-cp38-cp38-manylinux_2_34_x86_64.whl

Download URL temporal_negative_edge_sampler-0.1.16-cp38-cp38-manylinux_2_34_x86_64.whl
Size 242.8 kB
Tags CPython 3.8 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
78449c6f30f7c92f4d0e1cc527613c2da2bf61e22225faed4bc959d0e2a8308c
BLAKE2b-256 checksum
How to use checksums
a25775b21a8efc24bf5299653029f6df2cac2d25ec789b1ab51b427d56a496fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.15

Release history Release notifications | RSS feed

This release

0.1.16 This release

5 release files

0.1.15

5 release files

0.1.14

5 release files

0.1.13

4 release files

0.1.12

3 release files

0.1.10

1 release file

0.1.9

1 release file

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

1 release file

0.1.3

3 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page