graph-seeder 1.0.0.dev0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graph_seeder-1.0.0.dev0/.gitignore +218 -0
- graph_seeder-1.0.0.dev0/PKG-INFO +191 -0
- graph_seeder-1.0.0.dev0/README.md +176 -0
- graph_seeder-1.0.0.dev0/pyproject.toml +26 -0
- graph_seeder-1.0.0.dev0/requirements.txt +65 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/GraphSeeder.py +47 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/SubgraphExtractor.py +377 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/configs/dbpedia_default.json +59 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/configs/default.json +47 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/configs/europeana_default.json +50 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/configs/pgxlod_default.json +47 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/configs/wikidata_default.json +70 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/densification/GraphConnector.py +113 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/extraction/BFS/BFS.py +192 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/extraction/ExtractionStrategy.py +70 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/extraction/Hop/HopExpansion.py +92 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/utils/ConsoleUI.py +273 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/utils/Factory.py +64 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/utils/GraphExporter.py +84 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/utils/GraphStatistics.py +32 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/utils/URIManager.py +95 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/utils/utils.py +217 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/NeighborhoodWrapper.py +47 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/hashmap/HashMapWrapper.py +124 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/sparql/BaseClient.py +23 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/sparql/GraphWrapper.py +269 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/sparql/SparqlQueryBuilder.py +175 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/sparql/client/SparqlClient.py +118 -0
- graph_seeder-1.0.0.dev0/src/graph_seeder/wrapper/sparql/client/TurtleClient.py +47 -0
- graph_seeder-1.0.0.dev0/uv.lock +786 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
*.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
|
|
209
|
+
.vscode
|
|
210
|
+
|
|
211
|
+
stat
|
|
212
|
+
|
|
213
|
+
data/
|
|
214
|
+
|
|
215
|
+
*.json
|
|
216
|
+
!src/graph_seeder/configs/*.json
|
|
217
|
+
|
|
218
|
+
src/test.py
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: graph-seeder
|
|
3
|
+
Version: 1.0.0.dev0
|
|
4
|
+
Summary: A powerful tool to extract and densify subgraphs from Knowledge Graphs via SPARQL or LMDB, with different extraction strategies.
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Requires-Dist: lmdb>=2.2.0
|
|
7
|
+
Requires-Dist: networkx<4.0.0,>=3.2.1
|
|
8
|
+
Requires-Dist: pandas<3.0.0,>=2.3.3
|
|
9
|
+
Requires-Dist: rdflib>=7.6.0
|
|
10
|
+
Requires-Dist: requests>=2.32.5
|
|
11
|
+
Requires-Dist: rich>=15.0.0
|
|
12
|
+
Requires-Dist: sparqlwrapper>=2.0.0
|
|
13
|
+
Requires-Dist: urllib3>=2.6.3
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# Graph densifier
|
|
17
|
+
|
|
18
|
+
Graph Densifier is a collection of tools that can be used for enriching, analyzing, and extracting subgraphs from
|
|
19
|
+
knowledge graphs represented as triplet datasets.
|
|
20
|
+
|
|
21
|
+
It offers the following functions:
|
|
22
|
+
|
|
23
|
+
- **Densify graphs** by enriching an existing knowledge graph with additional Wikidata triplets between known entities.
|
|
24
|
+
- **Compute statistics** to analyze the graph's composition and connectivity.
|
|
25
|
+
- **Extract paths from Wikidata** dynamically by finding connections between pairs of entities.
|
|
26
|
+
- **Extract local subgraphs** from an existing dataset using shortest paths or neighborhood expansion around seed
|
|
27
|
+
entities.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
Follow these steps to set up the project locally:
|
|
32
|
+
|
|
33
|
+
### 1. Clone the repository
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
git clone https://github.com/Wimmics/graph-densifier.git
|
|
37
|
+
cd graph-densifier
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. Install dependencies
|
|
41
|
+
|
|
42
|
+
#### Option A (recommended): using uv
|
|
43
|
+
|
|
44
|
+
We recommend using uv for fast and reliable dependency management.
|
|
45
|
+
|
|
46
|
+
- [Install uv](https://docs.astral.sh/uv/#installation) by following the official guide
|
|
47
|
+
- Then run:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
uv sync
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### Option B: using pip
|
|
54
|
+
|
|
55
|
+
If you prefer not to use uv, you can install dependencies with pip:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install -r requirements.txt
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 3. Environment configuration
|
|
62
|
+
|
|
63
|
+
> [!note]
|
|
64
|
+
> To avoid being rate-limited or blocked when querying Wikidata, you should configure a user identity.
|
|
65
|
+
> - Create a `.env` file at the root of the project
|
|
66
|
+
> - Add the following line to the file with your Wikidata username:
|
|
67
|
+
> ```bash
|
|
68
|
+
> USER_AGENT="graph_densify/1.0 (contact: wikidata_username)"
|
|
69
|
+
> ```
|
|
70
|
+
> While this step is not strictly required to run the project, it is **recommended**. Without it, requests to Wikidata
|
|
71
|
+
> may be throttled or blocked during large runs, which can interrupt the graph densification and path extraction
|
|
72
|
+
> processes.
|
|
73
|
+
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
The project provides four main scripts:
|
|
77
|
+
|
|
78
|
+
1. **graph_densify.py** – enrich a local graph with additional Wikidata triplets.
|
|
79
|
+
2. **statistics.py** – compute statistics for a triplet dataset.
|
|
80
|
+
3. **subgraph_extract.py** – query Wikidata to find paths between entity pairs.
|
|
81
|
+
4. **hashmap_extract_subgraph.py** – extract relevant subgraphs from a local CSV graph.
|
|
82
|
+
|
|
83
|
+
## 1. Graph Densification (`graph_densify.py`)
|
|
84
|
+
|
|
85
|
+
This script enriches the input graph by querying Wikidata for additional relationships between entities already present
|
|
86
|
+
in the graph. It identifies all unique entities in the `subject` and `object` columns and adds any newly discovered
|
|
87
|
+
direct relations to the dataset.
|
|
88
|
+
|
|
89
|
+
### Command
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
python src/graph_densify.py --input path/to/input.csv --output path/to/output.csv
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 2. Graph Statistics (`statistics.py`)
|
|
96
|
+
|
|
97
|
+
This script computes descriptive statistics for a triplet dataset and generates a summary CSV file in the `stat/`
|
|
98
|
+
directory.
|
|
99
|
+
|
|
100
|
+
### Command
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
python src/statistics.py --input path/to/graph.csv
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Computed Statistics
|
|
107
|
+
|
|
108
|
+
| Metric | Description |
|
|
109
|
+
|-------------------------------|----------------------------------------------------|
|
|
110
|
+
| `total_triplets` | Total number of triplets |
|
|
111
|
+
| `unique_subjects` | Number of unique subjects |
|
|
112
|
+
| `unique_predicates` | Number of unique predicates |
|
|
113
|
+
| `unique_objects` | Number of unique objects |
|
|
114
|
+
| `unique_entities` | Unique entities across subjects and objects |
|
|
115
|
+
| `unique_subject_object_pairs` | Distinct `(subject, object)` pairs |
|
|
116
|
+
| `connected_components` | Number of weakly connected components in the graph |
|
|
117
|
+
|
|
118
|
+
## 3. Wikidata Path Extraction (`subgraph_extract.py`)
|
|
119
|
+
|
|
120
|
+
This script takes a list of entity pairs and dynamically queries Wikidata to find a short path (not necessarily the
|
|
121
|
+
shortest) between them. It outputs the discovered path triplets as a CSV and saves the explored network as a `.gpickle`
|
|
122
|
+
graph file.
|
|
123
|
+
|
|
124
|
+
### Command
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
python src/subgraph_extract.py --input path/to/pairs.csv --output path/to/extracted_paths.csv
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## 4. Local Subgraph Extraction (`hashmap_extract_subgraph.py`)
|
|
133
|
+
|
|
134
|
+
This script extracts subgraphs from a **local** graph dataset (CSV) using one of the two modes:
|
|
135
|
+
|
|
136
|
+
### Mode A — Shortest paths between seed/target pairs
|
|
137
|
+
|
|
138
|
+
Extracts all shortest paths between specified source-target entity pairs.
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
python src/hashmap_extract_subgraph.py \
|
|
142
|
+
--sub_graph path/to/main_graph.csv \
|
|
143
|
+
--seed_target_pairs path/to/pairs.csv
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Mode B — Radius around seed nodes
|
|
147
|
+
|
|
148
|
+
Extracts all nodes within a specified number of hops (default: 2) from a list of seed entities.
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
python src/hashmap_extract_subgraph.py \
|
|
152
|
+
--sub_graph path/to/main_graph.csv \
|
|
153
|
+
--seeds_only path/to/seeds.csv \
|
|
154
|
+
--max_length 2
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Output:** The extracted subgraph is saved by default to `data/extracted_subgraph.csv`.
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Dataset Structure
|
|
162
|
+
|
|
163
|
+
All datasets are expected to be provided as CSV files.
|
|
164
|
+
|
|
165
|
+
### Main Graph Dataset
|
|
166
|
+
|
|
167
|
+
Must contain three columns representing a knowledge graph triplet:
|
|
168
|
+
|
|
169
|
+
| subject | predicate | object |
|
|
170
|
+
|---------|-----------|--------|
|
|
171
|
+
| Q937 | P36 | Q90 |
|
|
172
|
+
| Q90 | P17 | Q142 |
|
|
173
|
+
|
|
174
|
+
### Seed-Target Pair Dataset
|
|
175
|
+
|
|
176
|
+
Used for finding paths between specific entities. Must contain two columns:
|
|
177
|
+
|
|
178
|
+
| subject | object |
|
|
179
|
+
|---------|--------|
|
|
180
|
+
| Q937 | Q304 |
|
|
181
|
+
| Q90 | Q183 |
|
|
182
|
+
|
|
183
|
+
### Seed-Only Dataset
|
|
184
|
+
|
|
185
|
+
Used for neighborhood expansion. Must contain one column representing the seed entity (the column can be named `seed` or
|
|
186
|
+
be the first column):
|
|
187
|
+
|
|
188
|
+
| seed |
|
|
189
|
+
|------|
|
|
190
|
+
| Q937 |
|
|
191
|
+
| Q90 |
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Graph densifier
|
|
2
|
+
|
|
3
|
+
Graph Densifier is a collection of tools that can be used for enriching, analyzing, and extracting subgraphs from
|
|
4
|
+
knowledge graphs represented as triplet datasets.
|
|
5
|
+
|
|
6
|
+
It offers the following functions:
|
|
7
|
+
|
|
8
|
+
- **Densify graphs** by enriching an existing knowledge graph with additional Wikidata triplets between known entities.
|
|
9
|
+
- **Compute statistics** to analyze the graph's composition and connectivity.
|
|
10
|
+
- **Extract paths from Wikidata** dynamically by finding connections between pairs of entities.
|
|
11
|
+
- **Extract local subgraphs** from an existing dataset using shortest paths or neighborhood expansion around seed
|
|
12
|
+
entities.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Follow these steps to set up the project locally:
|
|
17
|
+
|
|
18
|
+
### 1. Clone the repository
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
git clone https://github.com/Wimmics/graph-densifier.git
|
|
22
|
+
cd graph-densifier
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. Install dependencies
|
|
26
|
+
|
|
27
|
+
#### Option A (recommended): using uv
|
|
28
|
+
|
|
29
|
+
We recommend using uv for fast and reliable dependency management.
|
|
30
|
+
|
|
31
|
+
- [Install uv](https://docs.astral.sh/uv/#installation) by following the official guide
|
|
32
|
+
- Then run:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
uv sync
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### Option B: using pip
|
|
39
|
+
|
|
40
|
+
If you prefer not to use uv, you can install dependencies with pip:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install -r requirements.txt
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 3. Environment configuration
|
|
47
|
+
|
|
48
|
+
> [!note]
|
|
49
|
+
> To avoid being rate-limited or blocked when querying Wikidata, you should configure a user identity.
|
|
50
|
+
> - Create a `.env` file at the root of the project
|
|
51
|
+
> - Add the following line to the file with your Wikidata username:
|
|
52
|
+
> ```bash
|
|
53
|
+
> USER_AGENT="graph_densify/1.0 (contact: wikidata_username)"
|
|
54
|
+
> ```
|
|
55
|
+
> While this step is not strictly required to run the project, it is **recommended**. Without it, requests to Wikidata
|
|
56
|
+
> may be throttled or blocked during large runs, which can interrupt the graph densification and path extraction
|
|
57
|
+
> processes.
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
The project provides four main scripts:
|
|
62
|
+
|
|
63
|
+
1. **graph_densify.py** – enrich a local graph with additional Wikidata triplets.
|
|
64
|
+
2. **statistics.py** – compute statistics for a triplet dataset.
|
|
65
|
+
3. **subgraph_extract.py** – query Wikidata to find paths between entity pairs.
|
|
66
|
+
4. **hashmap_extract_subgraph.py** – extract relevant subgraphs from a local CSV graph.
|
|
67
|
+
|
|
68
|
+
## 1. Graph Densification (`graph_densify.py`)
|
|
69
|
+
|
|
70
|
+
This script enriches the input graph by querying Wikidata for additional relationships between entities already present
|
|
71
|
+
in the graph. It identifies all unique entities in the `subject` and `object` columns and adds any newly discovered
|
|
72
|
+
direct relations to the dataset.
|
|
73
|
+
|
|
74
|
+
### Command
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
python src/graph_densify.py --input path/to/input.csv --output path/to/output.csv
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 2. Graph Statistics (`statistics.py`)
|
|
81
|
+
|
|
82
|
+
This script computes descriptive statistics for a triplet dataset and generates a summary CSV file in the `stat/`
|
|
83
|
+
directory.
|
|
84
|
+
|
|
85
|
+
### Command
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
python src/statistics.py --input path/to/graph.csv
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Computed Statistics
|
|
92
|
+
|
|
93
|
+
| Metric | Description |
|
|
94
|
+
|-------------------------------|----------------------------------------------------|
|
|
95
|
+
| `total_triplets` | Total number of triplets |
|
|
96
|
+
| `unique_subjects` | Number of unique subjects |
|
|
97
|
+
| `unique_predicates` | Number of unique predicates |
|
|
98
|
+
| `unique_objects` | Number of unique objects |
|
|
99
|
+
| `unique_entities` | Unique entities across subjects and objects |
|
|
100
|
+
| `unique_subject_object_pairs` | Distinct `(subject, object)` pairs |
|
|
101
|
+
| `connected_components` | Number of weakly connected components in the graph |
|
|
102
|
+
|
|
103
|
+
## 3. Wikidata Path Extraction (`subgraph_extract.py`)
|
|
104
|
+
|
|
105
|
+
This script takes a list of entity pairs and dynamically queries Wikidata to find a short path (not necessarily the
|
|
106
|
+
shortest) between them. It outputs the discovered path triplets as a CSV and saves the explored network as a `.gpickle`
|
|
107
|
+
graph file.
|
|
108
|
+
|
|
109
|
+
### Command
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
python src/subgraph_extract.py --input path/to/pairs.csv --output path/to/extracted_paths.csv
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 4. Local Subgraph Extraction (`hashmap_extract_subgraph.py`)
|
|
118
|
+
|
|
119
|
+
This script extracts subgraphs from a **local** graph dataset (CSV) using one of the two modes:
|
|
120
|
+
|
|
121
|
+
### Mode A — Shortest paths between seed/target pairs
|
|
122
|
+
|
|
123
|
+
Extracts all shortest paths between specified source-target entity pairs.
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
python src/hashmap_extract_subgraph.py \
|
|
127
|
+
--sub_graph path/to/main_graph.csv \
|
|
128
|
+
--seed_target_pairs path/to/pairs.csv
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Mode B — Radius around seed nodes
|
|
132
|
+
|
|
133
|
+
Extracts all nodes within a specified number of hops (default: 2) from a list of seed entities.
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
python src/hashmap_extract_subgraph.py \
|
|
137
|
+
--sub_graph path/to/main_graph.csv \
|
|
138
|
+
--seeds_only path/to/seeds.csv \
|
|
139
|
+
--max_length 2
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**Output:** The extracted subgraph is saved by default to `data/extracted_subgraph.csv`.
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Dataset Structure
|
|
147
|
+
|
|
148
|
+
All datasets are expected to be provided as CSV files.
|
|
149
|
+
|
|
150
|
+
### Main Graph Dataset
|
|
151
|
+
|
|
152
|
+
Must contain three columns representing a knowledge graph triplet:
|
|
153
|
+
|
|
154
|
+
| subject | predicate | object |
|
|
155
|
+
|---------|-----------|--------|
|
|
156
|
+
| Q937 | P36 | Q90 |
|
|
157
|
+
| Q90 | P17 | Q142 |
|
|
158
|
+
|
|
159
|
+
### Seed-Target Pair Dataset
|
|
160
|
+
|
|
161
|
+
Used for finding paths between specific entities. Must contain two columns:
|
|
162
|
+
|
|
163
|
+
| subject | object |
|
|
164
|
+
|---------|--------|
|
|
165
|
+
| Q937 | Q304 |
|
|
166
|
+
| Q90 | Q183 |
|
|
167
|
+
|
|
168
|
+
### Seed-Only Dataset
|
|
169
|
+
|
|
170
|
+
Used for neighborhood expansion. Must contain one column representing the seed entity (the column can be named `seed` or
|
|
171
|
+
be the first column):
|
|
172
|
+
|
|
173
|
+
| seed |
|
|
174
|
+
|------|
|
|
175
|
+
| Q937 |
|
|
176
|
+
| Q90 |
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[tool.hatch.build.targets.wheel]
|
|
6
|
+
packages = ["src/graph_seeder"]
|
|
7
|
+
|
|
8
|
+
[project]
|
|
9
|
+
name = "graph-seeder"
|
|
10
|
+
version = "1.0.0.dev0"
|
|
11
|
+
description = "A powerful tool to extract and densify subgraphs from Knowledge Graphs via SPARQL or LMDB, with different extraction strategies."
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.9"
|
|
14
|
+
dependencies = [
|
|
15
|
+
"lmdb>=2.2.0",
|
|
16
|
+
"networkx>=3.2.1,<4.0.0",
|
|
17
|
+
"pandas>=2.3.3,<3.0.0",
|
|
18
|
+
"rdflib>=7.6.0",
|
|
19
|
+
"requests>=2.32.5",
|
|
20
|
+
"rich>=15.0.0",
|
|
21
|
+
"sparqlwrapper>=2.0.0",
|
|
22
|
+
"urllib3>=2.6.3",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.scripts]
|
|
26
|
+
graph-seeder = "graph_seeder.GraphSeeder:main"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# This file was autogenerated by uv via the following command:
|
|
2
|
+
# uv export --no-hashes --format requirements.txt
|
|
3
|
+
-e .
|
|
4
|
+
certifi==2026.4.22
|
|
5
|
+
# via requests
|
|
6
|
+
charset-normalizer==3.4.7
|
|
7
|
+
# via requests
|
|
8
|
+
idna==3.15
|
|
9
|
+
# via requests
|
|
10
|
+
isodate==0.7.2 ; python_full_version < '3.11'
|
|
11
|
+
# via rdflib
|
|
12
|
+
lmdb==2.2.0
|
|
13
|
+
# via graph-seeder
|
|
14
|
+
markdown-it-py==3.0.0 ; python_full_version < '3.10'
|
|
15
|
+
# via rich
|
|
16
|
+
markdown-it-py==4.2.0 ; python_full_version >= '3.10'
|
|
17
|
+
# via rich
|
|
18
|
+
mdurl==0.1.2
|
|
19
|
+
# via markdown-it-py
|
|
20
|
+
networkx==3.2.1 ; python_full_version < '3.10'
|
|
21
|
+
# via graph-seeder
|
|
22
|
+
networkx==3.4.2 ; python_full_version == '3.10.*'
|
|
23
|
+
# via graph-seeder
|
|
24
|
+
networkx==3.6.1 ; python_full_version >= '3.11'
|
|
25
|
+
# via graph-seeder
|
|
26
|
+
numpy==2.0.2 ; python_full_version < '3.10'
|
|
27
|
+
# via pandas
|
|
28
|
+
numpy==2.2.6 ; python_full_version == '3.10.*'
|
|
29
|
+
# via pandas
|
|
30
|
+
numpy==2.4.4 ; python_full_version >= '3.11'
|
|
31
|
+
# via pandas
|
|
32
|
+
pandas==2.3.3
|
|
33
|
+
# via graph-seeder
|
|
34
|
+
pygments==2.20.0
|
|
35
|
+
# via rich
|
|
36
|
+
pyparsing==3.3.2
|
|
37
|
+
# via rdflib
|
|
38
|
+
python-dateutil==2.9.0.post0
|
|
39
|
+
# via pandas
|
|
40
|
+
pytz==2026.2
|
|
41
|
+
# via pandas
|
|
42
|
+
rdflib==7.6.0
|
|
43
|
+
# via
|
|
44
|
+
# graph-seeder
|
|
45
|
+
# sparqlwrapper
|
|
46
|
+
requests==2.32.5 ; python_full_version < '3.10'
|
|
47
|
+
# via graph-seeder
|
|
48
|
+
requests==2.34.0 ; python_full_version >= '3.10'
|
|
49
|
+
# via graph-seeder
|
|
50
|
+
rich==15.0.0
|
|
51
|
+
# via graph-seeder
|
|
52
|
+
six==1.17.0
|
|
53
|
+
# via python-dateutil
|
|
54
|
+
sparqlwrapper==2.0.0
|
|
55
|
+
# via graph-seeder
|
|
56
|
+
tzdata==2025.3
|
|
57
|
+
# via pandas
|
|
58
|
+
urllib3==2.6.3 ; python_full_version < '3.10'
|
|
59
|
+
# via
|
|
60
|
+
# graph-seeder
|
|
61
|
+
# requests
|
|
62
|
+
urllib3==2.7.0 ; python_full_version >= '3.10'
|
|
63
|
+
# via
|
|
64
|
+
# graph-seeder
|
|
65
|
+
# requests
|