Module 1: What DDI metadata is, and why a graph¶
Learning objectives
By the end of this module, you can:
- O1. Say what survey metadata records, and why data is hard to use without it.
- O2. Name the three DDI flavors, Codebook, Lifecycle and CDI, and the root element of each.
- O3. Rewrite the links in a piece of DDI metadata as nodes and edges, and say why a question about paths suits a graph.
- O4. Find the reference that joins two DDI-L fragments, and write the URN it names.
- O5. Tell a file's flavor with
ddigraph.detect()andddigraph detect.
Prerequisites: None
Time: 30 min self-paced / 45 min instructor-led.
Needs: No server. You can read this module before you install anything; its code runs once you finish Module 2.
New in this module: DDI, Codebook, Lifecycle, CDI, node, edge, URN, ddigraph.detect(), ddigraph detect
1. Data needs metadata¶
A survey produces two things. The first is the data: rows of answers, one row per person. The second is everything you need to make sense of those rows. What was asked, and of whom? In what order? What does each code mean? Who ran the survey, and when?
That second part is the metadata: data about the data. Lose it and the
data is a sheet of numbers that nobody can read. A column called EDUC
that holds the value 3 says little on its own. Is 3 a number of years
at school, or a level? The metadata says that EDUC asked "What is the
highest level of education you have completed?", that 3 means "Upper
secondary (ISCED 3)", and that only persons aged 15 and over living in
private households were asked.
This course uses one sample survey all the way through: a Labour Force Survey for the third quarter of 2025. It is made up, but it has the shape of a real one. It asks people about their sex, age, region, education and work, and it groups them as employed, unemployed or not in the labour force.
DDI, the Data Documentation Initiative, is a standard way to write that metadata down. A DDI file is XML. An international alliance of data archives maintains the standard, and most social science archives use it.
ddigraph reads DDI files and turns them into graphs. It does not write DDI. A tool such as an archive's catalog or a questionnaire designer writes the file, and ddigraph helps you explore what is in it.
2. Three flavors of DDI¶
DDI comes in three shapes, called flavors. You will meet all three in this course.
| Flavor | What it is | Root element |
|---|---|---|
| Codebook (DDI-C 2.x) | The older, simpler one. Everything hangs off one study. | <codeBook> |
| Lifecycle (DDI-L 3.x) | Metadata split into reusable fragments that refer to each other. | <FragmentInstance> |
| CDI (DDI-CDI 1.0) | The newest. It describes data across domains, not only surveys. | An element in the CDI namespace, such as <Wrapper> |
The root element is the outermost tag of an XML file: the first one that opens and the last one that closes.
There are three flavors because they were designed at different times for different jobs. Archives hold files in all three, so a tool that reads only one of them will soon fall short.
3. Metadata is already a graph¶
Look at what survey metadata says:
- A question uses a code list.
- A code list holds categories, such as "Yes" and "No".
- A variable is about a concept, such as "Labour market status".
- A variable covers a universe: the people it applies to, such as "Employed persons aged 15 and over".
- A variable comes from a question, or from several.
- A sequence holds questions, and sequences nest inside each other.
Each line links two things. A set of things and the links between them is a graph. In a graph, each thing is a node, and each link is an edge (also called a relationship). An edge has a direction and a type, such as "uses code list".
You can store this in tables, and many people do. But the questions people want to ask are often about paths: chains of links.
Which variables trace back to questions that use this code list?
In SQL, that is a chain of joins, and the chain's length depends on how deeply the sequences nest. You have to know the depth before you write the query. A graph database writes it as one pattern. In Cypher, the query language of Neo4j, it looks like this:
MATCH (v:Variable)-[*1..5]->(c:CodeList {ddi_id: 'cl-yesno'})
RETURN v.name
[*1..5] means "follow one to five edges, of any type". In the sample
survey, one variable matches: ILOSTAT, the labour force status. Nobody
answers it directly. It is worked out from three questions:
WORK: "Last week, did you do any work for pay or profit, even for one hour?"LOOK: "In the last four weeks, did you look for work?"AVAIL: "If you found a job, could you start within the next two weeks?"
A person who worked is employed. A person who did not work, but looked for work and could start, is unemployed. Anyone else is not in the labour force. All three questions use the same "Yes or no" code list:
graph LR
ILOSTAT["Variable<br/>ILOSTAT"] -->|REFERENCES_QUESTION| WORK["Question<br/>WORK"]
ILOSTAT -->|REFERENCES_QUESTION| LOOK["Question<br/>LOOK"]
ILOSTAT -->|REFERENCES_QUESTION| AVAIL["Question<br/>AVAIL"]
WORK -->|USES_CODELIST| YESNO["Code list<br/>Yes or no"]
LOOK -->|USES_CODELIST| YESNO
AVAIL -->|USES_CODELIST| YESNO
YESNO -->|code 1| YES["Category<br/>Yes"]
YESNO -->|code 2| NO["Category<br/>No"]
Following a path of any length is the real difference. Graphs are not always faster than tables. The point is that the question becomes easy to write. You will run Cypher yourself in Module 7.
When a table is the right answer
If all you ever ask is "give me every variable in this study", a table is simpler, and you should use one. A graph earns its place when the interesting questions are about connections. In survey metadata, they usually are.
4. How a DDI-L file writes a link¶
Lifecycle is the flavor that most clearly is a graph. Each fragment names
the fragments it points at. Look at the question WORK in the sample file
tests/fixtures/lfs_lifecycle.xml, the survey written as DDI-L. Run this
from the folder you will clone in Module 2:
grep -A 2 "did you do any work" tests/fixtures/lfs_lifecycle.xml
Expected output:
<d:QuestionText><d:LiteralText><d:Text xml:lang="en">Last week, did you do any work for pay or profit, even for one hour?</d:Text></d:LiteralText></d:QuestionText>
<d:CodeDomain>
<r:CodeListReference><r:Agency>example.nso</r:Agency><r:ID>cl-yesno</r:ID><r:Version>1</r:Version><r:TypeOfObject>CodeList</r:TypeOfObject></r:CodeListReference>
The first line is the question's text. The last line is a reference: it points at the code list that holds the answers. The file writes it on one line. Spread over several lines, it reads:
<r:CodeListReference>
<r:Agency>example.nso</r:Agency>
<r:ID>cl-yesno</r:ID>
<r:Version>1</r:Version>
<r:TypeOfObject>CodeList</r:TypeOfObject>
</r:CodeListReference>
It names three things:
- the agency, the organization that maintains the object:
example.nso, the survey's National Statistics Office; - the object's ID within that agency:
cl-yesno; - its version:
1.
The code list declares the same three values:
grep -B 1 -A 1 "<r:URN>urn:ddi:example.nso:cl-yesno:1</r:URN>" tests/fixtures/lfs_lifecycle.xml
Expected output:
<l:CodeList isUniversallyUnique="true">
<r:URN>urn:ddi:example.nso:cl-yesno:1</r:URN><r:Agency>example.nso</r:Agency><r:ID>cl-yesno</r:ID><r:Version>1</r:Version>
<l:CodeListName><r:String xml:lang="en">Yes or no</r:String></l:CodeListName>
Agency, ID and version together form a URN (a Uniform Resource Name):
urn:ddi:example.nso:cl-yesno:1. The code list writes it out in full, in
<r:URN>. Below these lines it holds two codes: 1 for "Yes" and 2 for
"No". A URN is unique across the world, so DDI-L fragments can live in
separate files and still link up. The fourth part of the reference,
TypeOfObject, says what kind of object it expects.
The questions LOOK and AVAIL hold the same reference. The file writes
the code list once, and three questions point at it. That is reuse,
and it is what makes DDI-L a graph rather than a list.
The other two flavors link things in their own ways. A Codebook file uses
IDs that are unique only inside the file: a variable's <location
fileid="F1"/> points at the data file whose ID is F1. A CDI file names
its objects with identifiers, and links them with association elements.
ddigraph reads all three ways into one kind of graph.
5. Ask ddigraph which flavor a file is¶
You can tell a file's flavor from its root element. ddigraph does it for
you with ddigraph.detect():
import ddigraph
for path in (
"tests/fixtures/lfs_lifecycle.xml",
"tests/fixtures/lfs_codebook.xml",
"tests/fixtures/lfs_cdi.xml",
):
print(path, ddigraph.detect(path))
Expected output:
tests/fixtures/lfs_lifecycle.xml lifecycle
tests/fixtures/lfs_codebook.xml codebook
tests/fixtures/lfs_cdi.xml cdi
The three files describe the same survey, one in each flavor. The command line gives the same answer, with the file's full path:
ddigraph detect tests/fixtures/lfs_lifecycle.xml
Expected output (your path will differ):
Format: lifecycle
File: .../tests/fixtures/lfs_lifecycle.xml
detect reads only the root element and its namespace. It does not load
the file, and it does not look at the file's name. You need no
configuration, and you do not have to guess.
A file that is not DDI XML, such as a Turtle file, makes detect fail with
an error that says the file has no XML root element.
Exercises¶
Scenario
A colleague leaves three DDI files in a shared folder, with no notes. Before anyone loads them, you are asked what each one is and how its parts link together.
Exercise 1 (O2, O5). Print the flavor of lfs_correspondence.xml,
reusable_fragments.xml and cdi_valid_minimal.xml, all in
tests/fixtures/. Print each file's name, then its flavor.
Expected output:
lfs_correspondence.xml cdi
reusable_fragments.xml codebook
cdi_valid_minimal.xml cdi
Answer
from pathlib import Path
import ddigraph
for name in ("lfs_correspondence.xml", "reusable_fragments.xml", "cdi_valid_minimal.xml"):
path = Path("tests/fixtures") / name
print(name, ddigraph.detect(path))
lfs_correspondence.xml cdi
reusable_fragments.xml codebook
cdi_valid_minimal.xml cdi
lfs_correspondence.xml holds no questions at all. It maps the
survey's age groups and education levels to other groupings. Its root
is a CDI <Wrapper>, so it is CDI. The other two are small test
files, each made odd on purpose. reusable_fragments.xml sounds like a
Lifecycle file, but its root element is <codeBook>.
cdi_valid_minimal.xml holds nothing but its root, <DDICDIModels>.
That is not <Wrapper>, but it is in the CDI namespace, and that is
enough. detect reads the root element, never the name.
Exercise 2 (O4). In tests/fixtures/lfs_lifecycle.xml, the
Instrument (the questionnaire) points at the first thing a respondent
meets, through a <d:ControlConstructReference>. Find that reference, and
write the URN of the object it names.
Expected output: one URN, such as urn:ddi:agency:id:version.
Answer
grep -n -A 4 "<d:Instrument " tests/fixtures/lfs_lifecycle.xml
9: <d:Instrument isUniversallyUnique="true">
10- <r:URN>urn:ddi:example.nso:lfs-instrument:1</r:URN><r:Agency>example.nso</r:Agency><r:ID>lfs-instrument</r:ID><r:Version>1</r:Version>
11- <d:InstrumentName><r:String xml:lang="en">LFS questionnaire</r:String></d:InstrumentName>
12- <r:Label><r:Content xml:lang="en">Labour Force Survey questionnaire, 2025</r:Content></r:Label>
13- <d:ControlConstructReference><r:Agency>example.nso</r:Agency><r:ID>lfs-main</r:ID><r:Version>1</r:Version><r:TypeOfObject>Sequence</r:TypeOfObject></d:ControlConstructReference>
The reference on line 13 sits inside the Instrument, so the answer
is urn:ddi:example.nso:lfs-main:1, a Sequence: the main sequence.
It holds six more references. The first five lead to the questions
SEX, AGE, REGION, EDUC and WORK. The sixth leads to a choice,
"Worked last week?". A person who worked is asked HOURS, and anyone
else the job search questions, LOOK and AVAIL. Following one
reference after another is walking a path in the graph.
Exercise 3 (O3). In tests/fixtures/lfs_codebook.xml, the survey
written as DDI-Codebook, find which variables sit in the data file F1.
Write each link as an edge, in the form (V1)-[:IN_FILE]->(F1).
Expected output: one edge per variable that points at F1.
Answer
grep -n -e 'fileDscr ID' -e 'varQnty' -e '<var ID' -e 'fileid' tests/fixtures/lfs_codebook.xml
34: <fileDscr ID="F1">
38: <dimensns><varQnty>7</varQnty></dimensns>
43: <var ID="V1" name="SEX" intrvl="discrete">
44: <location fileid="F1"/>
52: <var ID="V2" name="AGE" intrvl="contin">
53: <location fileid="F1"/>
60: <var ID="V3" name="AGEGRP" intrvl="discrete">
61: <location fileid="F1"/>
73: <var ID="V4" name="REGION" intrvl="discrete">
74: <location fileid="F1"/>
85: <var ID="V5" name="EDUC" intrvl="discrete">
86: <location fileid="F1"/>
97: <var ID="V6" name="ILOSTAT" intrvl="discrete">
98: <location fileid="F1"/>
107: <var ID="V7" name="HOURS" intrvl="contin">
108: <location fileid="F1"/>
Seven edges, from (V1)-[:IN_FILE]->(F1) to (V7)-[:IN_FILE]->(F1).
The data file's own description agrees: <varQnty>7</varQnty> says it
holds seven variables. In a table you would keep a file column and join
on it. In a graph the link is an edge you can follow, in either
direction.
Quiz¶
Question 1 (O1): A data file has a column EDUC that holds the values 1 to 5. What do you need before you can use it?
A. More rows, so that the numbers mean something.
B. The metadata: the question that was asked, what each code means, and who was asked.
C. A faster database.
D. Only the column's data type, such as "integer".
Answer
B. The numbers mean nothing until the metadata says what was asked and what each code stands for. The data type (D) tells you the values are whole numbers, not what they mean.
Question 2 (O2): A file's root element is <FragmentInstance>. Which flavor is it?
A. Codebook
B. CDI
C. Lifecycle
D. You cannot tell without loading the file.
Answer
C. <FragmentInstance> is the root of a DDI-L file. You do not
need to load it: the root element is enough, which is all
ddigraph detect reads.
Question 3 (O3): Why does a question such as 'which variables trace back to this code list?' suit a graph better than tables?
A. Graph databases are always faster than SQL databases.
B. The number of links between a variable and a code list varies, and a graph pattern can follow any number of them.
C. Tables cannot store links between things.
D. DDI files can only be read into graphs.
Answer
B. The path's length depends on how deeply things nest, so a
fixed chain of joins does not fit. A pattern such as [*1..5]
follows as many edges as it takes. A is tempting, but speed is not
the point: the question becomes easy to write.
Question 4 (O4): A reference holds Agency example.nso, ID cl-sex and Version 1. Which URN does it name?
A. urn:ddi:cl-sex:example.nso:1
B. urn:ddi:example.nso:1:cl-sex
C. urn:ddi:example.nso:cl-sex:1
D. urn:example.nso:cl-sex
Answer
C. A DDI URN is urn:ddi: then the agency, the ID and the
version, in that order. D drops the version, but two versions of
one object are two different objects.
Question 5 (O5): You rename lfs_lifecycle.xml to codebook.xml, then run ddigraph detect codebook.xml. What does it print?
A. Format: codebook, because of the new name.
B. Format: lifecycle, because the root element did not change.
C. An error, because the name and the content disagree.
D. Format: unknown.
Answer
B. detect reads the root element, <FragmentInstance>, and
ignores the file's name. Renaming a file does not change what is
inside it.
Question 6 (O2, O3): Which flavor writes its links as explicit reference elements, each with an agency, an ID and a version?
A. Codebook
B. Lifecycle
C. CDI
D. None of them: DDI has no links.
Answer
B. A DDI-L reference, such as <r:CodeListReference>, names its
target by agency, ID and version. Codebook links things with IDs
that are unique only inside the file, and CDI uses its own
identifiers and association elements.
Check yourself¶
- I can say what survey metadata records, and why data is hard to use without it. (O1)
- I can name the three DDI flavors and the root element of each. (O2)
- I can rewrite the links in a piece of DDI metadata as nodes and edges, and say why paths suit a graph. (O3)
- I can find the reference that joins two DDI-L fragments and write its URN. (O4)
- I can tell a file's flavor with
ddigraph.detect()andddigraph detect. (O5)