Skip to content

_utils

Utility and helper functions.

parse_assemblies(instance)

Parse assemblies properties from the OPTIMADEStructureResource entity into "proper" OPT StructureResource 'Assembly's.

Source code in oteapi_optimade/_utils.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def parse_assemblies(instance: dlite.Instance | dict[str, Any]) -> list[dict[str, Any]]:
    """Parse assemblies properties from the OPTIMADEStructureResource entity into "proper" OPT
    StructureResource 'Assembly's."""
    if not _check_correct_entity(instance, "OPTIMADEStructureResource"):
        raise ValueError(
            "Entity for the instance is not an OPTIMADEStructureResource entity."
        )

    if isinstance(instance, dlite.Instance):
        instance = instance.asdict(single=True)

    if not isinstance(instance, dict):
        raise TypeError("Entity instance is not a dictionary.")

    assemblies: list[dict[str, Any]] = []

    for index in range(instance["dimensions"]["nassemblies"]):
        assemblies.append(
            {
                "sites_in_groups": [
                    [int(_) for _ in group.split(",")]
                    for group in instance["properties"]["assembly_sites_in_groups"][
                        index
                    ].split(";")
                ],
                "group_probabilities": [
                    float(_)
                    for _ in instance["properties"]["assembly_group_probabilities"][
                        index
                    ].split(",")
                ],
            }
        )

    return assemblies

parse_species(instance)

Parse species properties from the OPTIMADEStructureResource entity into "proper" OPT StructureResource Species.

Source code in oteapi_optimade/_utils.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def parse_species(instance: dlite.Instance | dict[str, Any]) -> list[dict[str, Any]]:
    """Parse species properties from the OPTIMADEStructureResource entity into "proper" OPT
    StructureResource Species."""
    if not _check_correct_entity(instance, "OPTIMADEStructureResource"):
        raise ValueError(
            "Entity for the instance is not an OPTIMADEStructureResource entity."
        )

    if isinstance(instance, dlite.Instance):
        instance = instance.asdict(single=True)

    if not isinstance(instance, dict):
        raise TypeError("Entity instance is not a dictionary.")

    species: list[dict[str, Any]] = []

    for index in range(instance["dimensions"]["nspecies"]):
        # Required fields
        new_species = {
            "name": instance["properties"]["species_name"][index],
            "chemical_symbols": instance["properties"]["species_chemical_symbols"][
                index
            ].split(","),
            "concentration": [
                float(_)
                for _ in instance["properties"]["species_concentration"][index].split(
                    ","
                )
            ],
        }

        # Optional fields
        if mass := instance["properties"]["species_mass"][index]:
            new_species["mass"] = [float(_) for _ in mass.split(",")]

        if original_name := instance["properties"]["species_original_name"][index]:
            new_species["original_name"] = original_name

        if attached := instance["properties"]["species_attached"][index]:
            new_species["attached"] = attached.split(",")

            if "species_nattached" not in instance["properties"]:
                raise ValueError(
                    "species_attached is present, but species_nattached is missing."
                )

            new_species["nattached"] = [
                int(_)
                for _ in instance["properties"]["species_nattached"][index].split(",")
            ]

        species.append(new_species)

    return species