When a file describes more than one dataset, import_metadata() returns a MetadataPackage with a MetadataImport for each. This happens with a Define-XML document for a whole study (one entry per dataset, such as "DM" and "AE"), a Frictionless Data Package or CSVW table group with several tables, and a CDISC Controlled Terminology file (one entry per codelist).
The package works much like a read-only dictionary: package["DM"] gets an entry, "DM" in package checks for one, len() counts them, iterating gives their names, and keys() and values() give the names and entries. get_domain() also gets an entry, with an error message that lists the available names, and summary() describes the package in text.
Parameters
name:str | None=None
The name of the package, such as the study OID of a Define-XML document. The default is None.
items:dict[str, MetadataImport]=dict()
The entries, as a dictionary from dataset name to MetadataImport. The default is an empty dictionary.
description:str | None=None
A description of the package. The default is None.
version:str | None=None
The version of the package or of the standard it follows. The default is None.
Examples
The bundled Define-XML document describes two datasets of a clinical study:
import pointblank as pbpackage = pb.import_metadata(pb.load_metadata_example("define.xml"))print(len(package))print("DM"in package)for name in package:print(name, "-", package[name].dataset_label)
2
True
DM - Demographics
AE - Adverse Events
Each entry is a MetadataImport, so it can be used like one. Here the Demographics metadata is used to check a small table:
The table has all the variables of the DM dataset with the declared types, and the required ones have values. The last step fails for one row, since "BLACK" isn’t one of the values in the Race codelist (which uses "BLACK OR AFRICAN AMERICAN").
Asking for a dataset that isn’t in the package, such as package.get_domain("LB"), raises a KeyError with the message No domain/dataset named 'LB'. Available: ['DM', 'AE'].
The MetadataImport entries, in the order of the source file, as a dictionary values view.
Examples
import pointblank as pbpackage = pb.import_metadata(pb.load_metadata_example("define.xml"))for meta in package.values():print(meta.dataset_name, len(meta), list(meta.codelists))
This is the easiest way to do something with every dataset, such as counting variables or listing codelists as here. Use package.items.items() to get the names and entries together.
The summary gives the package’s name and description (when known), the number of datasets, and the number of variables in each. Printing a MetadataPackage with print() shows the same summary. For the details of a dataset, use the summary() of its entry.
Returns
str
The summary, as a string with one line per item.
Examples
import pointblank as pbpackage = pb.import_metadata(pb.load_metadata_example("define.xml"))print(package.summary())