Skip to content

Demos: one question, every backend

The demo/ folder has one script per backend. Each script asks the same question of the course's sample Labour Force Survey: which codes and categories does the variable ILOSTAT (labour force status) have?

The survey comes in three files, one per DDI flavor, in tests/fixtures/. Each flavor keeps a variable's codes its own way, so each script has one query per flavor. The answer is the same in all 18 cases:

  1  Employed
  2  Unemployed
  3  Not in the labour force
Flavor File Path from the variable to its codes
DDI-Codebook lfs_codebook.xml Variable -USES_CATEGORY-> Category. The code is on the category.
DDI-Lifecycle lfs_lifecycle.xml Variable -USES_CODELIST-> CodeList -CONTAINS-> Code -HAS_CATEGORY-> Category. The value is on the code.
DDI-CDI lfs_cdi.xml InstanceVariable -TAKES_SUBSTANTIVE_VALUES_FROM-> ValueDomain -TAKES_VALUES_FROM-> CodeList -HAS_CODE-> Code. The code DENOTES its category and USES a notation, which holds the value.

Run each script from the root of a clone, since it reads the files in tests/fixtures/. Add --flavor codebook, lifecycle or cdi for one file, and --variable SEX for another variable. Each picture below opens full size when you click it.

NetworkX

No server is needed. The script walks the edges of the graph in memory.

uv run --extra networkx python demo/backend_networkx.py

The pictures draw the same path as a tree: the variable at the top, its codes and categories below it.

NetworkX drawing of the DDI-Codebook path: the variable ILOSTAT points to the categories Employed, Unemployed and Not in the labour force

NetworkX drawing of the DDI-Lifecycle path: ILOSTAT uses a code list, which contains three codes, each with its category

NetworkX drawing of the DDI-CDI path: ILOSTAT, its value domain and code list, then three codes, each with its notation and category

pandas

No server is needed. Each edge on the path is one merge on the relationships table.

uv run --extra pandas python demo/backend_pandas.py

The pandas code for DDI-Codebook and the table it prints: codes 1 to 3 with their labels

The pandas code for DDI-Lifecycle and the same table of codes and labels

The pandas code for DDI-CDI and the same table of codes and labels

RDF and SPARQL

No server is needed: the graph is an rdflib graph in memory. A category is a skos:Concept with a skos:prefLabel in all three flavors.

uv run --extra rdf python demo/backend_rdf.py

The SPARQL query for DDI-Codebook and its result: codes 1 to 3 with their labels

The SPARQL query for DDI-Lifecycle and the same result

The SPARQL query for DDI-CDI and the same result

PostgreSQL and DuckDB

In ddigraph's tables, each edge is a join on ddigraph_relationships and each property a join on ddigraph_properties. --duckdb needs no server. PostgreSQL needs one; never point the script at a database you care about, since it replaces its three demo datasets.

uv run --extra duckdb python demo/backend_postgresql.py --duckdb
uv run --extra postgres python demo/backend_postgresql.py --postgres postgresql://postgres:postgres@localhost:5432/postgres

The SQL query for DDI-Codebook and its result in PostgreSQL: codes 1 to 3 with their labels

The SQL query for DDI-Lifecycle and the same result

The SQL query for DDI-CDI and the same result

Neo4j

The script loads each file as its own dataset (demo-lfs-codebook, demo-lfs-lifecycle, demo-lfs-cdi), then runs one Cypher query per flavor. It needs Neo4j 5 and the settings in demo/demo.md. Never point it at a database you care about.

uv run python demo/backend_neo4j.py

The pictures are from Neo4j Browser. They show the path each query follows, and the chips on the right are the legend.

Neo4j Browser showing ILOSTAT and its three categories in the DDI-Codebook dataset

Neo4j Browser showing ILOSTAT, its code list, three codes and their categories in the DDI-Lifecycle dataset

Neo4j Browser showing ILOSTAT, its value domain, code list, codes, notations and categories in the DDI-CDI dataset

Gremlin

The script writes each file to a Gremlin server as its own dataset, then runs one traversal per flavor. A node's label property is ddi_label in Gremlin, since some servers keep the name label for themselves.

uv run --extra gremlin python demo/backend_gremlin.py

Gremlin Server has no viewer of its own. These pictures are from the open-source gremlin-visualizer, which draws the same path in colours of its own. Its physics is turned off, and each node sits where it is in the NetworkX pictures. You could do the same by dragging the nodes.

gremlin-visualizer showing ILOSTAT and its three categories

gremlin-visualizer showing ILOSTAT, its code list, codes and categories

gremlin-visualizer showing ILOSTAT, its value domain, code list, codes, notations and categories

Remaking the pictures

scripts/docs_images.py --set demos takes all 18 pictures again from the running servers. The course's pictures come from the same script.