Module 3: Look before you load¶
Learning objectives
By the end of this module, you can:
- O1. Run
ddigraph previewon a file you have never seen, and read its node and relationship counts. - O2. Tell a file's flavor from the shape of its preview.
- O3. Print sample identities with
--limit, and check that the right things were read. - O4. Draw the shape as a Mermaid diagram with
--format mermaid, and write a page you can share with--format html -o.
Prerequisites: Module 2
Time: 30 min self-paced / 45 min instructor-led.
Needs: No server.
New in this module: ddigraph preview, --limit, --dataset-id, --format mermaid, --format html, ddigraph.preview()
1. A file you have never seen¶
Someone sends you a 65 MB DDI file. What is in it?
Opening it in an editor tells you very little: you see the first screen of a hundred thousand lines. Loading it into a database tells you a lot, but you must set up the database first. If the file turns out to be the wrong one, that work was for nothing.
ddigraph preview fills that gap. It reads the file into a graph in
memory, then tells you what it found. It needs no database and no
optional extra.
2. Your first preview¶
Preview the sample survey's Codebook file:
ddigraph preview tests/fixtures/lfs_codebook.xml
Expected output (your path will differ):
Preview: .../tests/fixtures/lfs_codebook.xml
Nodes: 80 Relationships: 48
Node types
DDIGenericIdentifiable 26
Category 21
Concept 7
Variable 7
Question 5
Citation 2
DataCollectionEvent 2
Organization 2
Universe 2
Contributor 1
Coverage 1
DataFile 1
Dataset 1
DocumentDescription 1
Study 1
Relationships
(Variable)-[:USES_CATEGORY]->(Category) 21
(Variable)-[:IN_FILE]->(DataFile) 7
(Variable)-[:USES_CONCEPT]->(Concept) 7
(Variable)-[:ASKED_AS]->(Question) 5
(Citation)-[:DESCRIBES]->(Dataset) 2
(Organization)-[:ASSOCIATED_WITH]->(Dataset) 2
(Contributor)-[:CONTRIBUTES_TO]->(Dataset) 1
(Coverage)-[:COVERS]->(Dataset) 1
(Study)-[:DESCRIBES]->(Dataset) 1
(Variable)-[:IN_UNIVERSE]->(Universe) 1
Read it in three parts:
- The totals. The file gives 80 nodes and 48 relationships (edges).
- Node types. Each node has a type, also called its label, such
as
Variable. The list counts the nodes of each type, largest first. - Relationships. Each line is one shape of edge: its start type,
its edge type and its end type, then how many edges have that shape.
(Variable)-[:IN_FILE]->(DataFile) 7means seven variables point at a data file. You wrote those seven edges by hand in Module 1's Exercise 3.
Read this as a summary of the shape, not of the contents. Notice what the preview does not do: it does not draw 80 boxes. A real survey has tens of thousands of nodes, and a box for each one is a picture of nothing. Grouping by type is what makes the answer fit on a screen.
3. What the shape tells you¶
Three things stand out in that output.
DDIGenericIdentifiable is the largest type. It is the catch-all for
DDI elements that ddigraph has no special type for. Seeing it at the top
means the file uses many such elements. That is not an error, but it tells
you where the detail is thin.
There is one Dataset node. Every file gives exactly one. It stands
for the file itself, and the study and its citations DESCRIBES it. When
you load the file into a database, every other node also gets an
IN_DATASET edge to it, which records where the node came from. That
edge belongs to the load, not to the file, so the preview does not count
it. That is why many of the 80 nodes have no edge in this list.
The edges start at variables. A variable uses categories and a
concept, comes from a question, and sits in a data file. The 21 categories
are the answers of five coded variables, such as "Male" and "Female" for
SEX. Only 5 of the 7 variables were asked as a question: AGEGRP and
ILOSTAT are worked out from other answers. Only HOURS names its own
universe, employed persons, because it is narrower than the study's. The
variables are the hub. That is the mark of a Codebook file. Compare the
same survey as a Lifecycle file:
ddigraph preview tests/fixtures/lfs_lifecycle.xml
Expected output:
Preview: .../tests/fixtures/lfs_lifecycle.xml
Nodes: 87 Relationships: 108
Node types
Category 23
Code 23
QuestionConstruct 8
QuestionItem 8
Variable 7
CodeList 6
Concept 5
Sequence 2
Universe 2
Dataset 1
IfThenElse 1
Instrument 1
Relationships
(Code)-[:HAS_CATEGORY]->(Category) 23
(CodeList)-[:CONTAINS]->(Code) 23
(Variable)-[:REFERENCES_QUESTION]->(QuestionItem) 9
(QuestionConstruct)-[:REFERENCES_QUESTION]->(QuestionItem) 8
(QuestionItem)-[:USES_CONCEPT]->(Concept) 8
(Sequence)-[:HAS_CONSTRUCT]->(QuestionConstruct) 7
(Variable)-[:IN_UNIVERSE]->(Universe) 7
(Variable)-[:USES_CONCEPT]->(Concept) 7
(QuestionItem)-[:USES_CODELIST]->(CodeList) 6
(Variable)-[:USES_CODELIST]->(CodeList) 5
(Dataset)-[:HAS_ENTRY_POINT]->(Instrument) 1
(IfThenElse)-[:ELSE]->(Sequence) 1
(IfThenElse)-[:THEN]->(QuestionConstruct) 1
(Instrument)-[:HAS_CONSTRUCT]->(Sequence) 1
(Sequence)-[:HAS_CONSTRUCT]->(IfThenElse) 1
The Dataset node points at the instrument with HAS_ENTRY_POINT: that
is where a reader of the questionnaire starts. From there the edges form a
chain:
Instrument → Sequence → QuestionConstruct → QuestionItem → CodeList → Code → Category.
That chain is the flow of the questionnaire, written as edges. The
IfThenElse is a choice in that flow: its THEN edge leads to the
HOURS question, for people who worked, and its ELSE edge to the job
search sequence, for everyone else.
Two more things differ from the Codebook file. Variables point back at
questions: 9 edges for 7 variables, because ILOSTAT comes from three
questions. And there are 5 Concept nodes, not 7. The Codebook file
repeats "Demographic characteristics" in each of three variables, so it
gives three nodes. DDI-L writes the concept once, and the variables point
at it. This is the "obviously a graph" flavor from Module 1, in fifteen
lines of output.
4. Counts are not proof¶
Counts tell you the shape. They do not tell you that the right things were
read. For that, ask for examples with --limit:
ddigraph preview tests/fixtures/lfs_lifecycle.xml --limit 1
Expected output:
Preview: .../tests/fixtures/lfs_lifecycle.xml
Nodes: 87 Relationships: 108
...
Sample Category
urn:ddi:example.nso:cat-sex-1:1
Sample Code
urn:ddi:example.nso:code-sex-1:1
Sample CodeList
urn:ddi:example.nso:cl-sex:1
Sample Concept
urn:ddi:example.nso:concept-demography:1
Sample Dataset
urn:ddigraph:scoped:lfs_lifecycle:%3Adataset
Sample IfThenElse
urn:ddi:example.nso:if-worked:1
Sample Instrument
urn:ddi:example.nso:lfs-instrument:1
Sample QuestionConstruct
urn:ddi:example.nso:qc-sex:1
Sample QuestionItem
urn:ddi:example.nso:q-sex:1
Sample Sequence
urn:ddi:example.nso:lfs-main:1
Sample Universe
urn:ddi:example.nso:universe-persons15:1
Sample Variable
urn:ddi:example.nso:var-sex:1
The counts come first, as before. Then each type gets up to one example
(--limit 2 would give two). Each example is the node's identity,
written as text: the name that tells this node apart from every other.
urn:ddi:example.nso:q-sex:1is the DDI URN of the questionSEX: agency, ID and version, as in Module 1.urn:ddigraph:scoped:lfs_lifecycle:%3Adatasetis the Dataset node. It is scoped to a dataset, herelfs_lifecycle, which by default is the file name.%3Ais how a colon is written inside one part of an identity, so the node's own ID is:dataset.
The samples look right: the first code list is "Sex", its first category
is "Male", and the first variable is SEX. If the samples look wrong
(empty, repeated, or cut short), you have found a problem before you spent
anything on a load. Module 4
explains identities in full.
5. Draw it as a diagram¶
The default output is text, because you are in a terminal. Two other formats serve two other readers. The first is Mermaid, a text format for diagrams that GitHub, many wikis and this site can draw:
ddigraph preview tests/fixtures/lfs_lifecycle.xml --format mermaid
The command prints the diagram's source. This site draws it:
graph LR
Category["Category<br/>23"]
Code["Code<br/>23"]
QuestionConstruct["QuestionConstruct<br/>8"]
QuestionItem["QuestionItem<br/>8"]
Variable["Variable<br/>7"]
CodeList["CodeList<br/>6"]
Concept["Concept<br/>5"]
Sequence["Sequence<br/>2"]
Universe["Universe<br/>2"]
Dataset["Dataset<br/>1"]
IfThenElse["IfThenElse<br/>1"]
Instrument["Instrument<br/>1"]
Code -->|HAS_CATEGORY 23| Category
CodeList -->|CONTAINS 23| Code
Variable -->|REFERENCES_QUESTION 9| QuestionItem
QuestionConstruct -->|REFERENCES_QUESTION 8| QuestionItem
QuestionItem -->|USES_CONCEPT 8| Concept
Sequence -->|HAS_CONSTRUCT 7| QuestionConstruct
Variable -->|IN_UNIVERSE 7| Universe
Variable -->|USES_CONCEPT 7| Concept
QuestionItem -->|USES_CODELIST 6| CodeList
Variable -->|USES_CODELIST 5| CodeList
Dataset -->|HAS_ENTRY_POINT 1| Instrument
IfThenElse -->|ELSE 1| Sequence
IfThenElse -->|THEN 1| QuestionConstruct
Instrument -->|HAS_CONSTRUCT 1| Sequence
Sequence -->|HAS_CONSTRUCT 1| IfThenElse
Each box is a node type with its count, and each arrow an edge shape with its count. Paste the text into a pull request or an issue, and it shows as a picture there too.
6. Write a page to share¶
The second format is a single HTML page. It needs no internet connection and no JavaScript, so it still opens after it is sent by email:
ddigraph preview tests/fixtures/lfs_lifecycle.xml --format html -o preview.html
Expected output:
Wrote preview.html
Open preview.html in a web browser. It shows the counts as bars, the
edge shapes, and the same diagram: the questionnaire's 12 node types and
15 edge shapes, with Category and Code as the longest bars.
-o works with the other formats too: --format mermaid -o shape.mmd
writes the diagram to a file. From Python, ddigraph.preview() returns
the same text as a string. The CLI reference
lists every option.
Exercises¶
Scenario
A partner agency sends two files and asks you to load them next week. Before you plan the load, you preview each one, check what kind of file it is, and share a picture of it with your team.
Exercise 1 (O1, O2). Preview tests/fixtures/lfs_cdi.xml. How many
nodes and edges does it give? Every type name but one shares something
that the Codebook and Lifecycle names did not. What is it, and why would a
tool do that?
Expected output: the preview, then your answer in a sentence or two.
Answer
ddigraph preview tests/fixtures/lfs_cdi.xml
Preview: .../tests/fixtures/lfs_cdi.xml
Nodes: 91 Relationships: 92
Node types
CDICategory 21
CDICode 21
CDIGenericEntity 21
CDIInstanceVariable 7
CDIValueDomain 7
CDICodeList 5
CDIConcept 5
CDIAgent 1
CDIConceptSystem 1
CDIUnitType 1
Dataset 1
Relationships
(CDICode)-[:DENOTES]->(CDICategory) 21
(CDICode)-[:USES]->(CDIGenericEntity) 21
(CDICodeList)-[:HAS_CODE]->(CDICode) 21
(CDIInstanceVariable)-[:MEASURES]->(CDIUnitType) 7
(CDIInstanceVariable)-[:TAKES_SUBSTANTIVE_VALUES_FROM]->(CDIValueDomain) 7
(CDICodeList)-[:IS_DEFINED_BY]->(CDIConcept) 5
(CDIConceptSystem)-[:HAS_CONCEPT]->(CDIConcept) 5
(CDIValueDomain)-[:TAKES_VALUES_FROM]->(CDICodeList) 5
Ninety-one nodes and 92 edges. Every type but Dataset starts with
CDI. Names such as Category and CodeList exist in all three
flavors, with different properties. The prefix keeps the CDI ones
apart when files of several flavors share one database. Dataset is
the one type every flavor shares.
Exercise 2 (O3). A colleague will load both lfs_lifecycle.xml and
lfs_codebook.xml as the dataset labour. Preview each with
--limit 1 --dataset-id labour, and keep only the identity lines. Which
identities change with the dataset ID, and which do not?
Expected output: the identities of both files.
Answer
ddigraph preview tests/fixtures/lfs_lifecycle.xml --limit 1 --dataset-id labour | grep "urn:"
echo ---
ddigraph preview tests/fixtures/lfs_codebook.xml --limit 1 --dataset-id labour | grep "urn:"
urn:ddi:example.nso:cat-sex-1:1
urn:ddi:example.nso:code-sex-1:1
urn:ddi:example.nso:cl-sex:1
urn:ddi:example.nso:concept-demography:1
urn:ddigraph:scoped:labour:%3Adataset
urn:ddi:example.nso:if-worked:1
urn:ddi:example.nso:lfs-instrument:1
urn:ddi:example.nso:qc-sex:1
urn:ddi:example.nso:q-sex:1
urn:ddi:example.nso:lfs-main:1
urn:ddi:example.nso:universe-persons15:1
urn:ddi:example.nso:var-sex:1
---
urn:ddigraph:derived:cfd6b2969cc9f54d953e837240eeec3d:category:c04b7030b05ced156755d5f7e375b022:0
urn:ddigraph:derived:0aa2a9376f4048929fe16e931eaa2f04:citation:9949242b7f8e236a740cdd4b36ae63b9:0
urn:ddigraph:derived:cfd6b2969cc9f54d953e837240eeec3d:concept:1972aebb3b80351bec83fc6df89fbb8a:0
urn:ddigraph:derived:f000f1dbd579a3510e520b5dd8d6bbcb:contributor:a1258669715688a503cf56c4274deeda:0
urn:ddigraph:derived:ac5d626b8d530c78737d23e437d01867:coverage:b007c5636b03dee51f45ac605a92494b:0
urn:ddigraph:derived:510c526a246f6f31ecdc069865e430ba:generic_identifiable:8a4ce13234bd7a06b8ea56c369c7d090:0
urn:ddigraph:derived:ac5d626b8d530c78737d23e437d01867:data_collection_event:48d30e45f186bb9d98c5dce67e73a195:0
urn:ddigraph:scoped:labour:F1
urn:ddigraph:scoped:labour:%3Adataset
urn:ddigraph:derived:6851a35bb4a06b58440f4f458741fc01:document_description:963cb4fdd0eca98bfc997d4dd858ca62:0
urn:ddigraph:derived:510c526a246f6f31ecdc069865e430ba:organization:02eb7cc011d19a938711c5fd105ec8c5:0
urn:ddigraph:scoped:labour:Q-SEX
urn:ddigraph:derived:6851a35bb4a06b58440f4f458741fc01:study:b895ca2be27fdbf6f1c5180e3dbccb81:0
urn:ddigraph:scoped:labour:UNIV-PERSONS15
urn:ddigraph:scoped:labour:V1
The DDI-L nodes keep their DDI URNs, which are the same in every
dataset. Only their Dataset node takes the new ID. The Codebook file
has no DDI URNs. An element with an ID, such as the variable V1 or
the data file F1, gets a scoped identity, which takes labour. An
element with no ID, such as a category, gets a derived identity,
built from the key of the node that holds it. That key takes the
dataset ID too, so every identity in the Codebook file changes.
Exercise 3 (O4). Write the shape of tests/fixtures/lfs_correspondence.xml
as a Mermaid file, correspondence.mmd, then print its first three lines.
Expected output:
Wrote correspondence.mmd
graph LR
CDIGenericEntity["CDIGenericEntity<br/>19"]
CDICategory["CDICategory<br/>17"]
Answer
ddigraph preview tests/fixtures/lfs_correspondence.xml --format mermaid -o correspondence.mmd
head -n 3 correspondence.mmd
Wrote correspondence.mmd
graph LR
CDIGenericEntity["CDIGenericEntity<br/>19"]
CDICategory["CDICategory<br/>17"]
-o sends any format to a file. The boxes come largest first, as in
the text preview: this file has 19 generic entities. Seventeen hold the
code of an education level, such as 5A in ISCED 1997 or 6 in
ISCED 2011. The other two are the two sets of levels themselves.
Exercise 4 (O1, O4). From Python, print the Mermaid text for
tests/fixtures/lfs_cdi.xml, with ddigraph.preview().
Expected output:
graph LR
CDICategory["CDICategory<br/>21"]
...
Answer
import ddigraph
print(ddigraph.preview("tests/fixtures/lfs_cdi.xml", format="mermaid"))
graph LR
CDICategory["CDICategory<br/>21"]
CDICode["CDICode<br/>21"]
CDIGenericEntity["CDIGenericEntity<br/>21"]
CDIInstanceVariable["CDIInstanceVariable<br/>7"]
CDIValueDomain["CDIValueDomain<br/>7"]
CDICodeList["CDICodeList<br/>5"]
CDIConcept["CDIConcept<br/>5"]
CDIAgent["CDIAgent<br/>1"]
CDIConceptSystem["CDIConceptSystem<br/>1"]
CDIUnitType["CDIUnitType<br/>1"]
Dataset["Dataset<br/>1"]
CDICode -->|DENOTES 21| CDICategory
CDICode -->|USES 21| CDIGenericEntity
CDICodeList -->|HAS_CODE 21| CDICode
CDIInstanceVariable -->|MEASURES 7| CDIUnitType
CDIInstanceVariable -->|TAKES_SUBSTANTIVE_VALUES_FROM 7| CDIValueDomain
CDICodeList -->|IS_DEFINED_BY 5| CDIConcept
CDIConceptSystem -->|HAS_CONCEPT 5| CDIConcept
CDIValueDomain -->|TAKES_VALUES_FROM 5| CDICodeList
ddigraph.preview() takes the same format and limit as the
command, and returns the text instead of printing it, so a script can
save it or put it in a report.
Quiz¶
Question 1 (O1): Why does ddigraph preview count nodes by type instead of drawing every node?
A. Drawing nodes needs a database.
B. A real file has thousands of nodes, and a box for each would hide the shape.
C. The preview cannot read each node's identity.
D. Counting is faster than reading the file.
Answer
B. Grouping by type keeps the answer on one screen, whatever
the file's size. C is wrong: --limit prints identities, so the
preview reads them. And the preview reads the whole file either
way (D).
Question 2 (O2): A preview shows (Instrument)-[:HAS_CONSTRUCT]->(Sequence) and (QuestionItem)-[:USES_CODELIST]->(CodeList). Which flavor is the file?
A. Codebook
B. Lifecycle
C. CDI
D. You cannot tell from a preview.
Answer
B. An instrument whose constructs lead to questions and code
lists is the questionnaire flow of a DDI-L file. A CDI file's type
names would start with CDI, and in a Codebook file the edges
start at variables.
Question 3 (O1): You load a file, and Neo4j holds an IN_DATASET edge for every node. The preview of the same file counts none. Why?
A. The preview has a bug.
B. IN_DATASET records which load a node came from, so the load writes it, not the file.
C. The preview hides edges that point at the Dataset node.
D. IN_DATASET edges exist only for Codebook files.
Answer
B. The edge belongs to the load. C is tempting, but the
preview does show edges into the Dataset node, such as
(Study)-[:DESCRIBES]->(Dataset).
Question 4 (O3): ddigraph preview tests/fixtures/lfs_codebook.xml --limit 2 prints urn:ddigraph:scoped:lfs_codebook:V1. What is lfs_codebook?
A. The agency that maintains the variable.
B. The dataset ID, which defaults to the file name.
C. The variable's label.
D. The flavor of the file.
Answer
B. A Codebook ID such as V1 is unique only inside its file,
so its identity is scoped to a dataset. --dataset-id sets
another one. An agency (A) belongs to a DDI URN, which starts with
urn:ddi:.
Question 5 (O4): You want to email a preview to a manager who works offline and has no ddigraph. Which command fits best?
A. ddigraph preview survey.xml
B. ddigraph preview survey.xml --format mermaid
C. ddigraph preview survey.xml --format html -o preview.html
D. ddigraph preview survey.xml --limit 5
Answer
C. The HTML page is one file that opens in any browser, with no internet and no JavaScript. Mermaid (B) needs a viewer that can draw it, such as GitHub.
Check yourself¶
- I can run
ddigraph previewon a new file and read its node and relationship counts. (O1) - I can tell a file's flavor from the shape of its preview. (O2)
- I can print sample identities with
--limitand check that the right things were read. (O3) - I can draw the shape as Mermaid and write an HTML page with
--format html -o. (O4)
