Parsing and models¶
thermoml_io.parser
¶
Safe, namespace-aware parsing of ThermoML XML documents.
The implementation follows the public IUPAC ThermoML schema directly. It is independent of third-party ThermoML Python implementations.
validate_xml_schema(source: XMLSource, schema: str | Path) -> None
¶
Validate a ThermoML XML source against an explicit local XSD.
The package intentionally does not fetch a mutable schema URL implicitly. Callers should pin and checksum the schema used by their workflow.
Source code in src/thermoml_io/parser.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | |
parse_thermoml(source: XMLSource, *, source_label: str | None = None, retrieved_at: datetime | None = None, schema: str | Path | None = None) -> ThermoMLDocument
¶
Parse a ThermoML XML document into immutable scientific objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
XMLSource
|
XML bytes, XML text, a local path, or a binary file object. |
required |
source_label
|
str | None
|
Provenance locator overriding an inferred local path. |
None
|
retrieved_at
|
datetime | None
|
Retrieval timestamp for network-originated bytes. |
None
|
schema
|
str | Path | None
|
Optional explicit XSD path. The parser never downloads a mutable schema implicitly. |
None
|
Returns:
| Type | Description |
|---|---|
ThermoMLDocument
|
Parsed document with SHA-256 provenance and resolved local references. |
Raises:
| Type | Description |
|---|---|
ThermoMLParseError
|
If required XML structure or numeric fields are malformed. |
ThermoMLValidationError
|
If XSD or semantic reference validation fails. |
Notes
This parser is an original implementation of the IUPAC ThermoML schema.
It currently decodes PureOrMixtureData fully. ReactionData entries
are counted and reported as unsupported warnings rather than silently
represented as mixture data.
References
M. Frenkel et al., "XML-based IUPAC standard for experimental, predicted, and critically evaluated thermodynamic property data storage and capture", Pure Appl. Chem. 78 (2006) 541-612. DOI: 10.1351/pac200678030541.
Source code in src/thermoml_io/parser.py
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 | |
parse_thermoml_json(source: JSONSource, *, source_label: str | None = None, retrieved_at: datetime | None = None, recovery: SourceRecovery | None = None) -> ThermoMLDocument
¶
Parse the official NIST JSON representation of a ThermoML document.
NIST JSON objects carry tml_elements lists that preserve XML element
ordering. The parser reconstructs an in-memory ThermoML tree and sends it
through the same semantic decoder and reference validation used for XML.
The source SHA-256 always describes the exact JSON bytes parsed.
Notes
JSON numbers are loaded directly as :class:~decimal.Decimal, without a
binary floating-point round trip. Nevertheless, the JSON representation
may not preserve the exact lexical decimal spelling used in related XML.
This limitation is also recorded in ThermoMLDocument.warnings.
Source code in src/thermoml_io/parser.py
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 | |
load_thermoml_url(url: str, *, timeout: float = 30.0, max_bytes: int = DEFAULT_MAX_DOWNLOAD_BYTES, schema: str | Path | None = None, json_fallback: Literal['never', 'on_xml_error'] = 'on_xml_error') -> ThermoMLDocument
¶
Download and parse one HTTPS ThermoML document with size limits.
Remote bytes are held in memory and are not persisted by this function. The source URL, checksum, and UTC retrieval time are stored as provenance.
Source code in src/thermoml_io/parser.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 | |
load_thermoml_json_url(url: str, *, timeout: float = 30.0, max_bytes: int = DEFAULT_MAX_DOWNLOAD_BYTES, recovery: SourceRecovery | None = None) -> ThermoMLDocument
¶
Download and parse one official NIST ThermoML JSON document safely.
Source code in src/thermoml_io/parser.py
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 | |
thermoml_io.models
¶
Immutable scientific data model for ThermoML documents.
The model preserves publication, sample, experimental, phase, and uncertainty metadata without forcing heterogeneous ThermoML properties into a single rectangular representation. Tabular views are constructed separately.
SourceRecovery
dataclass
¶
Audit record for recovery from an unreadable primary serialization.
The successful replacement remains the source described by
:class:SourceProvenance. These fields retain the failed source so a
downstream table never hides that recovery occurred.
Source code in src/thermoml_io/models.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
SourceProvenance
dataclass
¶
Provenance of the exact serialized source parsed by the library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locator
|
str | None
|
Local path, URL, persistent identifier, or user-supplied label. It may be absent for in-memory documents. |
required |
sha256
|
str
|
SHA-256 digest of the original source bytes. |
required |
retrieved_at
|
datetime | None
|
UTC timestamp recorded by the network loader, when applicable. |
None
|
media_type
|
str
|
Media type of the original serialization. |
'application/xml'
|
related_xml_md5
|
str | None
|
NIST-provided MD5 checksum of the related XML representation, when reported by an official JSON document. This is a relationship field, not the integrity digest used for the parsed JSON bytes. |
None
|
recovery
|
SourceRecovery | None
|
Explicit audit record when this source replaced an unreadable primary serialization. |
None
|
Source code in src/thermoml_io/models.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
Citation
dataclass
¶
Bibliographic metadata reported in a ThermoML document.
Source code in src/thermoml_io/models.py
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 | |
normalized_doi: str | None
property
¶
Return a lower-case DOI without a resolver URL prefix.
PurityAssessment
dataclass
¶
One reported purification or purity-assessment step for a sample.
Source code in src/thermoml_io/models.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
Sample
dataclass
¶
Metadata for one material sample used by an experiment.
Source code in src/thermoml_io/models.py
118 119 120 121 122 123 124 125 | |
Compound
dataclass
¶
Chemical identity and associated sample metadata.
The local_id is scoped to one ThermoML document and must never be used
as a global chemical identifier.
Source code in src/thermoml_io/models.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
preferred_name: str
property
¶
Return the best available human-readable component label.
stable_identifier: str
property
¶
Return the most stable reported identifier available.
matches(query: str) -> bool
¶
Return whether query identifies this compound.
Matching is case-insensitive and considers names, formula, CAS, standard InChI, and InChIKey. It is intentionally exact after trimming whitespace to avoid accidental chemical matches.
Source code in src/thermoml_io/models.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
Uncertainty
dataclass
¶
One uncertainty assessment associated with a reported quantity.
Values are retained in the same units as the corresponding ThermoML
quantity. coverage_factor and confidence_level are dimensionless.
Source code in src/thermoml_io/models.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
Repeatability
dataclass
¶
Repeatability metadata attached to a quantity definition or value.
Source code in src/thermoml_io/models.py
217 218 219 220 221 222 223 | |
DeviceSpecification
dataclass
¶
Instrument or device specification reported by the source.
Source code in src/thermoml_io/models.py
226 227 228 229 230 231 232 233 234 | |
QuantityDefinition
dataclass
¶
Definition shared by properties, variables, and constraints.
Source code in src/thermoml_io/models.py
237 238 239 240 241 242 243 244 245 246 247 248 | |
PropertyDefinition
dataclass
¶
Bases: QuantityDefinition
Definition of one experimentally reported property.
Source code in src/thermoml_io/models.py
251 252 253 254 255 256 257 258 259 | |
VariableDefinition
dataclass
¶
Bases: QuantityDefinition
Definition of one independent variable varied between data points.
Source code in src/thermoml_io/models.py
262 263 264 | |
ConstraintDefinition
dataclass
¶
Bases: QuantityDefinition
Definition and fixed value of one experimental constraint.
Source code in src/thermoml_io/models.py
267 268 269 270 271 272 | |
MeasuredValue
dataclass
¶
Reported numeric value linked to a quantity definition.
Source code in src/thermoml_io/models.py
275 276 277 278 279 280 281 282 283 284 | |
DataPoint
dataclass
¶
One ThermoML NumValues record.
Source code in src/thermoml_io/models.py
287 288 289 290 291 292 293 | |
DataSet
dataclass
¶
One pure-compound or mixture experimental dataset.
Source code in src/thermoml_io/models.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
ThermoMLDocument
dataclass
¶
Complete parsed ThermoML document and its source provenance.
Source code in src/thermoml_io/models.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
schema_version: str
property
¶
Return the document-declared ThermoML version.
compound(local_id: int) -> Compound
¶
Resolve a document-local component identifier.
Source code in src/thermoml_io/models.py
354 355 356 | |
system_compounds(dataset: DataSet) -> tuple[Compound, ...]
¶
Resolve all components in dataset in document order.
Source code in src/thermoml_io/models.py
358 359 360 | |
system_key(dataset: DataSet) -> str
¶
Return an order-independent, chemically stable system key.
Source code in src/thermoml_io/models.py
362 363 364 365 366 367 | |
dataset_key(dataset: DataSet) -> str
¶
Return a stable key for a dataset within a source publication.
Source code in src/thermoml_io/models.py
369 370 371 372 | |
thermoml_io.identity
¶
Chemical-identity aggregation and explicit component resolution.
ThermoML compound metadata remain immutable. This module builds a separate index that connects reported aliases through shared structural or registry identifiers and refuses to choose silently when a query remains ambiguous.
ComponentIdentity
dataclass
¶
One resolved chemical identity with all known exact aliases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preferred_name
|
str
|
Human-readable label selected from the reported names or formula. |
required |
common_names
|
tuple[str, ...]
|
Exact names observed in ThermoML or returned by an explicit resolver. |
()
|
iupac_names
|
tuple[str, ...]
|
Exact names observed in ThermoML or returned by an explicit resolver. |
()
|
cas_names
|
tuple[str, ...]
|
Exact names observed in ThermoML or returned by an explicit resolver. |
()
|
formulas
|
tuple[str, ...]
|
Reported molecular formulas. Formulas are aliases, not assumed unique. |
()
|
standard_inchis
|
tuple[str, ...]
|
Strong identifiers used to connect aliases across documents. |
()
|
standard_inchi_keys
|
tuple[str, ...]
|
Strong identifiers used to connect aliases across documents. |
()
|
cas_registry_numbers
|
tuple[str, ...]
|
Strong identifiers used to connect aliases across documents. |
()
|
pubchem_cids
|
tuple[int, ...]
|
Optional PubChem compound identifiers supplied only by explicit PubChem resolution. |
()
|
Notes
This object is separate from :class:~thermoml_io.models.Compound; it does
not mutate or replace source-reported ThermoML metadata.
Source code in src/thermoml_io/identity.py
99 100 101 102 103 104 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
stable_identifier: str
property
¶
Return the strongest deterministic identifier available.
from_compound(compound: Compound) -> ComponentIdentity
classmethod
¶
Create a detached identity from one source-reported compound.
Source code in src/thermoml_io/identity.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
values(kind: IdentifierKind = 'auto') -> tuple[str, ...]
¶
Return exact identifier values considered for one query kind.
Source code in src/thermoml_io/identity.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
matches(other: ComponentIdentity) -> bool
¶
Return whether other is compatible with this resolved identity.
Shared strong identifiers decide first. If both sides report strong identifiers and none agree, aliases are not allowed to override that chemical conflict. Exact aliases are used only when at least one side lacks strong identity metadata.
Source code in src/thermoml_io/identity.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
ComponentIndex
dataclass
¶
Immutable alias index for resolving components without guessing.
Source code in src/thermoml_io/identity.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
from_documents(documents: Iterable[ThermoMLDocument]) -> ComponentIndex
classmethod
¶
Build an index from source compounds across several documents.
Source code in src/thermoml_io/identity.py
290 291 292 293 294 295 296 297 | |
from_identities(identities: Iterable[ComponentIdentity]) -> ComponentIndex
classmethod
¶
Connect aliases only through shared strong identifiers.
Records without InChIKey, InChI, or CAS number may join a strong group when their aliases identify exactly one such group. If two strong identities share a name or formula, they remain separate so resolution reports the ambiguity.
Source code in src/thermoml_io/identity.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
resolve(query: ComponentQuery) -> ComponentIdentity
¶
Resolve one query or raise an explicit not-found/ambiguity error.
Source code in src/thermoml_io/identity.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
resolve_many(queries: Iterable[ComponentQuery]) -> tuple[ComponentIdentity, ...]
¶
Resolve several queries and reject duplicate chemical identities.
Source code in src/thermoml_io/identity.py
394 395 396 397 398 399 400 | |
explicit_component_identity(query: str) -> ComponentIdentity | None
¶
Return a detached identity for a self-identifying strong query.
This helper avoids an archive-wide alias scan for explicit CAS, InChI, or
InChIKey queries. Names and formulas return None because they require
ambiguity checks against the indexed source.
Source code in src/thermoml_io/identity.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
component_query_label(query: ComponentQuery) -> str
¶
Return a stable user-facing representation of a component query.
Source code in src/thermoml_io/identity.py
403 404 405 | |
thermoml_io.pubchem
¶
Explicit, optional PubChem PUG REST component resolution.
resolve_pubchem_component(query: str, *, namespace: PubChemNamespace = 'name', timeout: float = 30.0, max_bytes: int = 2 * 1024 * 1024) -> ComponentIdentity
¶
Resolve one component through the official PubChem PUG REST service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Exact PubChem input in the selected namespace, commonly a familiar
chemical name such as |
required |
namespace
|
PubChemNamespace
|
PubChem input namespace. Network resolution is always explicit; this function is never called implicitly by ThermoML search operations. |
'name'
|
timeout
|
float
|
Network timeout in seconds. |
30.0
|
max_bytes
|
int
|
Maximum accepted JSON response size. |
2 * 1024 * 1024
|
Returns:
| Type | Description |
|---|---|
ComponentIdentity
|
Detached identity metadata suitable for a collection or archive query. |
Raises:
| Type | Description |
|---|---|
ComponentNotFoundError
|
If PubChem reports no matching compound. |
AmbiguousComponentError
|
If the input maps to more than one distinct chemical identity. |
PubChemResolutionError
|
If the request or response cannot be processed safely. |
Source code in src/thermoml_io/pubchem.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |