A codelist (also called a value set or controlled terminology) lists the values that a variable may take and what each one means. When import_metadata() reads a file, the codelists it finds become Codelist objects in MetadataImport.codelists: the CodeList elements of a Define-XML document, the codelists of a CDISC Controlled Terminology file, the enum constraint of a Frictionless field (as a codelist named "<field>_enum"), and the value labels of an SPSS variable (as "<variable>_values"). Variables that use a codelist name it in their codelist_ref and have its values in allowed_values.
A codelist’s values are what a col_vals_in_set() check needs, and to_set() gives them as a list. to_dict() maps each value to its label, and extended_values() lists the sponsor extensions of an extensible codelist.
Parameters
name:str
The name of the codelist, which is also its key in MetadataImport.codelists.
codes:list[CodelistEntry]=list()
The entries of the codelist, as CodelistEntry objects. The default is an empty list.
label:str | None=None
A human-readable name for the codelist. The default is None.
version:str | None=None
The version of the terminology, such as a CDISC Controlled Terminology release date. The default is None.
source:str | None=None
Where the codelist came from, such as "CDISC Define-XML" or "SPSS .sav". The default is None.
extensible:bool=False
Whether values that aren’t in the codelist may be added, as with CDISC codelists marked as extensible. A non-extensible codelist is a closed set. The default is False.
Examples
The bundled Define-XML document defines a Sex codelist for the Demographics dataset. Let’s get it from the imported metadata:
len() counts all four entries, but to_set() leaves out the deprecated "IV", and extended_values() gives the one value that was added to the extensible codelist.
The values are given in the order of the codelist’s entries, leaving out any entry marked as deprecated. The result is ready to be used as the set= of col_vals_in_set().
Returns
list
The values of all entries that aren’t deprecated.
Examples
import pointblank as pbdm = pb.import_metadata(pb.load_metadata_example("define.xml"), dataset="DM")dm.get_codelist("Race").to_set()
['WHITE',
'BLACK OR AFRICAN AMERICAN',
'ASIAN',
'AMERICAN INDIAN OR ALASKA NATIVE',
'NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER']
These are the five values that the RACE variable may take in the bundled Define-XML document. A table can be checked against them with col_vals_in_set(columns="RACE", set=dm.get_codelist("Race").to_set()).
The mapping gives the meaning of each severity code used in the Adverse Events dataset. It can be used to add a column of labels to a table, for example with Polars’ pl.col("AESEV").replace_strict(mapping).
extended_values()
Get the values that were added to an extensible codelist.
An extensible codelist, such as many in CDISC Controlled Terminology, allows a study to add values of its own. In Define-XML these sponsor extensions are marked with def:ExtendedValue="Yes", and they’re read into entries with is_extended=True. This method lists those values, so they can be reviewed separately from the published ones.
Returns
list
The values of the entries marked as extended. The list is empty if there are none.
Only "NASOGASTRIC TUBE" was added to the codelist, so it’s the only extended value. Extended values are still permitted values, and to_set() includes them.