Candidate Links¶
Two datasets often describe the same things. Wave 1 and wave 2 of a survey both have an age variable. But their DDI files give each one its own ID, so the graph holds two nodes, with no link between them.
ddigraph match finds such pairs. It compares the nodes of one type in two
datasets of the store, and scores each pair with
Splink. A pair with its
score is a candidate.
A candidate is not a fact¶
A candidate is a guess. The score says how likely the guess is, not that it is true. So ddigraph never merges two nodes because of a candidate, and never changes a node:
- A node's identity comes from the DDI file (see Node identity). Two nodes are the same node only when their files say so.
- The candidates live beside the graph: in a file, and in Neo4j as edges of a type of their own. Delete them, and the graph is as it was.
- A lens's read never returns a candidate edge, and a repair never touches one. So the graph you read back is the graph the files describe.
A person, or a rule you trust, decides which candidates are true. Then you record that decision in DDI, such as with a concept both variables share.
The command¶
Matching comes with the match extra, which installs Splink and DuckDB:
pip install "ddigraph[match]"
Add both datasets to the store, then match them. Here the same Codebook file plays both waves:
ddigraph store add codebook.xml --dataset-id wave1
ddigraph store add codebook.xml --dataset-id wave2
ddigraph match wave1 wave2 --out candidates.jsonl
Generation 1 of dataset 'wave1': 52 nodes, 20 relationships
Generation 1 of dataset 'wave2': 52 nodes, 20 relationships
3 candidates between 'wave1' and 'wave2' in candidates.jsonl
ddigraph match LEFT RIGHT --out FILE [--label LABEL] [--seed N] [--threshold T]
[--neo4j] [--store DIR] [connection options]
| Option | What it does |
|---|---|
--out FILE |
The file to write, one candidate per line (needed) |
--label LABEL |
The type of the nodes to compare (default: Variable) |
--seed N |
The seed of Splink's random sampling (default: 42) |
--threshold T |
The lowest score to keep, from 0 to less than 1 (default: 0.5) |
--neo4j |
Also write the candidates to Neo4j, as below |
--store DIR |
The store's folder (default: ddigraph-store, or DDIGRAPH_STORE_DIR) |
It compares the HEAD of each dataset. A stub, which a dataset only refers to, is left out. A node both datasets hold is never paired with itself: it is already one node.
The file¶
Each line of the file is one candidate, as JSON:
{"start":"8b12a3a8e7b5b111c17da58af332368b","end":"392d18b0768991189be300a0fca55b3b","score":0.6539792387543253,"model":"splink-5.0.0+f679c27947b6931f","generation":[1,1]}
| Field | What it holds |
|---|---|
start |
The key of the node in the left dataset |
end |
The key of the node in the right dataset |
score |
Splink's match probability, from 0 to 1 |
model |
Splink's version, then a hash of the settings and the seed |
generation |
The HEADs of the left and right datasets it was computed from |
The lines are sorted by start, then end. Two runs with the same seed, on
the same generations, write the same file, byte for byte. When model or
generation changes, so may the scores: run it again after you add a new
generation.
In Neo4j¶
With --neo4j, match also writes each candidate as an edge between the
two nodes. The edge has the same score, model and generation, and
datasets: the IDs of the left and right datasets.
(:DDINode)-[:CANDIDATE_SAME_AS {score, model, generation, datasets}]->(:DDINode)
It takes the connection options
of load. It first deletes the candidates of an earlier
run: every CANDIDATE_SAME_AS edge from a node of that type whose
datasets names the same two datasets, in the same order. So a new run
replaces the old one, even for a node that has left one of the datasets
since. A run of another type, or of the two datasets the other way round,
keeps them. A candidate whose nodes Neo4j does not hold, such as one of a
generation it has not loaded, is not written. Each transaction deletes or
writes at most 10,000 edges, so a large run does not fill the server's
memory. If a run stops halfway, the next run of the same two datasets
deletes what it left.
Then list the best ones:
MATCH (a:Variable)-[c:CANDIDATE_SAME_AS]->(b:Variable)
WHERE c.score >= 0.9
RETURN a.name, a.label, b.name, b.label, c.score
ORDER BY c.score DESC
A purge or a delta load that deletes a node deletes its candidates too.
How the score is made¶
The model is small and fixed, so that a run can be repeated:
- Each node is its
nameand the text of itslabel, trimmed and in lower case. - To keep the work small, Splink compares only some pairs: those whose
names share their first three letters, or whose labels share their first
five. This is blocking. A pair it never compares is never a candidate.
If blocking would still give more than ten million pairs,
matchstops with an error, since Splink needs about 1.4 GB of memory to score ten million. This happens when most names start alike, such asVAR00001toVAR05000, or most labels do. - For each field, a pair is in one of four levels: equal, very close (a Jaro-Winkler similarity of 0.9 or more), close (0.7 or more), or not close. A missing value counts for nothing.
- How often true matches fall in each level is fixed (the m probabilities). How often other pairs do is learnt from the data, by random sampling with the seed (the u probabilities).
- Before it looks at a pair, Splink assumes each node has one match on the other side.
- DuckDB runs on one thread, so its sums always add up in the same order.
Reading the scores¶
The score is a probability. At 0.5, a match is as likely as not. Near 1, both fields agree where other pairs rarely do. Near 0, they disagree.
Read the scores of one run against each other, not as a promise:
- The u probabilities come from the data. In a small dataset, many of the pairs Splink samples are true matches. So it learns that agreement is common, and even a perfect pair scores lower. In the example above, the three variables match their twins exactly, yet score 0.65.
- A pair that agrees on one field, while the other field is missing, scores
below 0.5 once each side has four nodes or more, and about 0.4 in a large
dataset. So if your nodes have names but no labels, or labels but no
names, lower
--threshold, such as to 0.3. - Blocking can miss a pair.
incomeandhh_incomeshare no first three letters, so if their labels differ too, Splink never compares them. - A name and a label say little. Two variables named
q1in two surveys may ask different questions.
So check the candidates by hand, best first. A --threshold of 0 keeps
every pair Splink compared, which helps to see what it missed. Each
candidate kept takes memory, about half a gigabyte per million, so try it
on small datasets first.
In Python¶
from ddigraph import ir
from ddigraph.match import candidates
from ddigraph.store import Store
store = Store("ddigraph-store")
for wave in ("wave1", "wave2"):
store.write(wave, ir.parse("codebook.xml", dataset_id=wave), memory_limit=2**26)
for candidate in candidates(store, "wave1", "wave2", label="Variable", seed=42):
print(candidate.start, candidate.end, round(candidate.score, 2))
8b12a3a8e7b5b111c17da58af332368b 392d18b0768991189be300a0fca55b3b 0.65
95f0a0f55d14b07a776adbfef2412ec3 c4e764fae1fb1c7518641ea3803996f3 0.65
c9a44a16fd8e1dfa93b53abafe147a3c 8f684b53f68ed602f32a4836cd84ed2a 0.65
candidates returns a list of Candidate, with the fields of the file.
ddigraph.match.write_candidates(driver, found, left=..., right=...) writes
them to Neo4j, as --neo4j does.