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.dev2.tar.gz (72.2 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.dev2-py3-none-any.whl (38.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: graph_seeder-1.0.0.dev2.tar.gz
  • Upload date:
  • Size: 72.2 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.dev2.tar.gz
Algorithm Hash digest
SHA256 0335c90ef02347f66c2a17cf3032f61b57741fe205e5e22415b188a4e21b3fbe
MD5 6d658cb7a8ec30c9d5fae449cada498e
BLAKE2b-256 f44856345ec9a97c23c076c1e6545258ba7647a43a484eaade653c0dfc60e347

See more details on using hashes here.

File details

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

File metadata

  • Download URL: graph_seeder-1.0.0.dev2-py3-none-any.whl
  • Upload date:
  • Size: 38.0 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.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 bb61eb43b492adb2c76d77b1205b9c8ff1ce0b570e49ae062266d1e4f2f5ae83
MD5 98a92f4e82c40d79ea2e3e6075436d47
BLAKE2b-256 06263f01de5808aa36f7751498ec2387a6b67ffac9831756ad58865fe7637c46

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