lbl-anp / becquerel

Becquerel is a Python package for analyzing nuclear spectroscopic measurements.
Other
43 stars 16 forks source link

Latest Compendium materials JSON causes errors #324

Closed markbandstra closed 2 years ago

markbandstra commented 2 years ago

The latest version of the materials data from the PNNL Compendium cannot be parsed by the current version of becquerel.

Downloading the latest JSON and placing it into becquerel/tools and calling bq.tools.force_load_and_write_materials_csv() causes the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../becquerel/tools/materials.py", line 196, in force_load_and_write_materials_csv
    materials = _load_and_compile_materials()
  File ".../becquerel/tools/materials.py", line 26, in _load_and_compile_materials
    data_comp = fetch_compendium_data()
  File ".../becquerel/tools/materials_compendium.py", line 63, in fetch_compendium_data
    names = [datum["Name"] for datum in data]
  File ".../becquerel/tools/materials_compendium.py", line 63, in <listcomp>
    names = [datum["Name"] for datum in data]
TypeError: string indices must be integers

It seems from looking at a JSON from a few months ago and a JSON downloaded today that the difference is that the older JSON has a structure that parses into a list of dicts:

[
    {
        "Comment": "Material 1 data",
    },
    {
        "Comment": "Material 2 data",
    }
]

whereas the new format parses as a dict and the list of material data are inside of it:

{
    "siteVersion": "0.1.1",
    "data": [
        {
            "Comment": "Material 1 data",
        },
        {
            "Comment": "Material 2 data",
        }
    ]
}

So the solution is easy: to check for the data that get loaded and use data["data"] if it is the new format.

Both versions appear to contain 411 materials, but I have not checked whether any of the data for individual materials differ.