Skip to main content

A powerful tool to extract and densify subgraphs from Knowledge Graphs via SPARQL or LMDB, with different extraction strategies.

Project description

Graph densifier

Graph Densifier is a collection of tools that can be used for enriching, analyzing, and extracting subgraphs from knowledge graphs represented as triplet datasets.

It offers the following functions:

  • Densify graphs by enriching an existing knowledge graph with additional Wikidata triplets between known entities.
  • Compute statistics to analyze the graph's composition and connectivity.
  • Extract paths from Wikidata dynamically by finding connections between pairs of entities.
  • Extract local subgraphs from an existing dataset using shortest paths or neighborhood expansion around seed entities.

Installation

Follow these steps to set up the project locally:

1. Clone the repository

git clone https://github.com/Wimmics/graph-densifier.git
cd graph-densifier

2. Install dependencies

Option A (recommended): using uv

We recommend using uv for fast and reliable dependency management.

  • Install uv by following the official guide
  • Then run:
uv sync

Option B: using pip

If you prefer not to use uv, you can install dependencies with pip:

pip install -r requirements.txt

3. Environment configuration

[!note] To avoid being rate-limited or blocked when querying Wikidata, you should configure a user identity.

  • Create a .env file at the root of the project
  • Add the following line to the file with your Wikidata username:
USER_AGENT="graph_densify/1.0 (contact: wikidata_username)"

While this step is not strictly required to run the project, it is recommended. Without it, requests to Wikidata may be throttled or blocked during large runs, which can interrupt the graph densification and path extraction processes.

Usage

The project provides four main scripts:

  1. graph_densify.py – enrich a local graph with additional Wikidata triplets.
  2. statistics.py – compute statistics for a triplet dataset.
  3. subgraph_extract.py – query Wikidata to find paths between entity pairs.
  4. hashmap_extract_subgraph.py – extract relevant subgraphs from a local CSV graph.

1. Graph Densification (graph_densify.py)

This script enriches the input graph by querying Wikidata for additional relationships between entities already present in the graph. It identifies all unique entities in the subject and object columns and adds any newly discovered direct relations to the dataset.

Command

python src/graph_densify.py --input path/to/input.csv --output path/to/output.csv

2. Graph Statistics (statistics.py)

This script computes descriptive statistics for a triplet dataset and generates a summary CSV file in the stat/ directory.

Command

python src/statistics.py --input path/to/graph.csv

Computed Statistics

Metric Description
total_triplets Total number of triplets
unique_subjects Number of unique subjects
unique_predicates Number of unique predicates
unique_objects Number of unique objects
unique_entities Unique entities across subjects and objects
unique_subject_object_pairs Distinct (subject, object) pairs
connected_components Number of weakly connected components in the graph

3. Wikidata Path Extraction (subgraph_extract.py)

This script takes a list of entity pairs and dynamically queries Wikidata to find a short path (not necessarily the shortest) between them. It outputs the discovered path triplets as a CSV and saves the explored network as a .gpickle graph file.

Command

python src/subgraph_extract.py --input path/to/pairs.csv --output path/to/extracted_paths.csv

4. Local Subgraph Extraction (hashmap_extract_subgraph.py)

This script extracts subgraphs from a local graph dataset (CSV) using one of the two modes:

Mode A — Shortest paths between seed/target pairs

Extracts all shortest paths between specified source-target entity pairs.

python src/hashmap_extract_subgraph.py \
    --sub_graph path/to/main_graph.csv \
    --seed_target_pairs path/to/pairs.csv

Mode B — Radius around seed nodes

Extracts all nodes within a specified number of hops (default: 2) from a list of seed entities.

python src/hashmap_extract_subgraph.py \
    --sub_graph path/to/main_graph.csv \
    --seeds_only path/to/seeds.csv \
    --max_length 2

Output: The extracted subgraph is saved by default to data/extracted_subgraph.csv.


Dataset Structure

All datasets are expected to be provided as CSV files.

Main Graph Dataset

Must contain three columns representing a knowledge graph triplet:

subject predicate object
Q937 P36 Q90
Q90 P17 Q142

Seed-Target Pair Dataset

Used for finding paths between specific entities. Must contain two columns:

subject object
Q937 Q304
Q90 Q183

Seed-Only Dataset

Used for neighborhood expansion. Must contain one column representing the seed entity (the column can be named seed or be the first column):

seed
Q937
Q90

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graph_seeder-1.0.0.dev3.tar.gz (72.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graph_seeder-1.0.0.dev3-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

File details

Details for the file graph_seeder-1.0.0.dev3.tar.gz.

File metadata

  • Download URL: graph_seeder-1.0.0.dev3.tar.gz
  • Upload date:
  • Size: 72.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for graph_seeder-1.0.0.dev3.tar.gz
Algorithm Hash digest
SHA256 a87d1b52529a48e3b6eebed4c7a0ddb0c90d2948e7c4cf3138f1123d9f11d124
MD5 78e0c0e473a66c72de8cf7c238a3d0a9
BLAKE2b-256 1e7e190c1cbea0ddc3fb5c6918cb44728cb5e63aec4126cafc87e79c57b1709c

See more details on using hashes here.

File details

Details for the file graph_seeder-1.0.0.dev3-py3-none-any.whl.

File metadata

  • Download URL: graph_seeder-1.0.0.dev3-py3-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for graph_seeder-1.0.0.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 0503b66d9bdaecdbce7f0508aed72cfccd53c3f6f8dfaf1bf0c66315617d969c
MD5 13bc5eb343c349ba082270b6d3d3c203
BLAKE2b-256 6fb4989a669622b75ec07da5ef8bdac7f7410adb3038ff36050067b513765b57

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page