khaeru / sdmx

SDMX information model and client in Python
https://sdmx1.readthedocs.io
Apache License 2.0
26 stars 18 forks source link

Get codelist from a DSD dimension #28

Closed brockfanning closed 3 years ago

brockfanning commented 3 years ago

As with all my newbie questions - please feel free to point me to some documentation or code if that is easier.

If I have a DataStructureDefinition object, I can loop through the dimensions. What I'm wondering is, how would get the CodeLists associated with a particular dimension. Here is the basic code:

def get_codelists_from_dimension(dimension):
    # How would I do this?
    return []

msg = sdmx.read_sdmx('my_dsd.xml')
dsd = msg.structure[0]
for dimension in dsd.dimensions:
    codelists = get_codelists_from_dimension(dimension)
khaeru commented 3 years ago

The key reference is the SDMX Information Model (IM). There's a link from the front page of the documentation. You can also find it by going to https://sdmx.org/?page_id=5008 (“Standards”) and clicking on “Section 2 – Information Model”.

To answer this particular question, you would find “Figure 25: Representation of DSD Components” on p.60. The contents of this diagram and the accompanying text is exactly reflected in the classes and attributes of the sdmx package and thus in the API documentation. Like so:

So this means you need to go from the Dimension (Component) via its local_representation attribute to a Representation object; and from that via its enumerated attribute to the Codelist (ItemScheme):


def get_codelists_from_dimension(dimension):
    return dimension.local_representation.enumerated

Note that "get codelists" plural seems a misnomer given the 1-to-1 relationships shown in the IM and reflected in the code.


In general: the IM document is 170 pages long, and the front page of the sdmx docs tries to be clear:

This documentation does not repeat full descriptions of SDMX, the IM, or SDMX web services.

I personally feel the IM is a bit dense, and think it would be wonderful if there were some learning materials to help new users understand it. But those would take many hours to write, and that is not in scope for this project given current resources (i.e. none).

I've written this comment as one general example of how to read the IM and the package docs together, in case other users are trying to do the same thing. When similar questions are asked in the future, I will answer by linking to this example. I hope it helps! Please close the issue if the question is answered.

brockfanning commented 3 years ago

Thank you very much for the detail!