httk.io.cif.cif_parser¶
Classes¶
Functions¶
Parse a CIF numeric field. |
|
|
Parse a CIF numeric token exactly, preserving finite decimals and fractions. |
|
Convert a CIF numeric token to an integer using its central value. |
|
The numeric part of a CIF value, as text, with any uncertainty estimate removed. |
|
Parse a CIF block into its asymmetric-unit cell data. |
|
Extract structural superspace modulation information from a standard CIF. |
|
Convert one normalized CIF block to a neutral asymmetric-unit payload. |
|
Read a CIF and return its asymmetric units as a neutral, tagged payload. |
|
Return the first structural CIF block from |
Module Contents¶
- class httk.io.cif.cif_parser.CifMeta[source]¶
Bases:
TypedDictWhat a CIF number claims about its own precision, beyond its value.
Both are exact rationals, or
Nonewhen the token makes no such claim.Noneis not the same as claiming exactness: a value written1/3states an exact number and reportsNonefor both, while?states nothing at all and also reportsNone.precisionis implied by the digits written (0.3333is good to1/10000);esdis the standard uncertainty a file states explicitly, so0.3333(7)reports a precision of1/10000and an esd of7/10000. They measure different things and the coarser of the two is the honest claim.- Parameters:
esd – Standard uncertainty explicitly stated by the CIF token.
precision – Precision implied by the digits written in the CIF token.
- esd: fractions.Fraction | None[source]¶
- precision: fractions.Fraction | None[source]¶
- httk.io.cif.cif_parser.parse_cif_float(token: str, *, meta: Literal[False] = ..., pragmatic: bool = ...) float | None[source]¶
- httk.io.cif.cif_parser.parse_cif_float(token: str, *, meta: Literal[True], pragmatic: bool = ...) tuple[float | None, CifMeta]
Parse a CIF numeric field.
- If meta=False:
return float_value or None.
- If meta=True:
return
(float_value_or_None, CifMeta)— the value plus what the token claims about its own precision. SeeCifMeta; both of its fields are exact rationals orNone.
- Parameters:
token – Numeric text to parse, including CIF uncertainty notation when present.
meta – Include the value’s claimed precision and standard uncertainty.
pragmatic – Salvage selected non-standard numeric spellings instead of rejecting them.
- Returns:
The parsed value, optionally paired with its precision metadata.
- Raises:
ValueError – If the token is missing or cannot be interpreted as a CIF number.
- httk.io.cif.cif_parser.parse_cif_fraction(token)[source]¶
Parse a CIF numeric token exactly, preserving finite decimals and fractions.
- Parameters:
token (str) – Numeric text whose exact central value should be preserved.
- Returns:
The exact value, or
Nonefor an unknown CIF value.- Raises:
ValueError – If the token does not contain a valid exact numeric value.
- Return type:
fractions.Fraction | None
- httk.io.cif.cif_parser.parse_cif_int(token, *, strict=True, allow_round=False)[source]¶
Convert a CIF numeric token to an integer using its central value.
The accepted forms include
'123(4)','3E2', and'1.0E3'. - strict=True: require the value to be exactly integral; otherwise raise ValueError. - allow_round=True (only if strict=False): round half-even to the nearest int.- Parameters:
- Returns:
The converted integer.
- Raises:
ValueError – If the token is missing or its central value cannot be converted under the selected rules.
decimal.InvalidOperation – If an unmatched token has malformed decimal syntax.
- Return type:
- httk.io.cif.cif_parser.cif_exact_token(token)[source]¶
The numeric part of a CIF value, as text, with any uncertainty estimate removed.
"0.3333(5)"becomes"0.3333"and"3(1)e-1"becomes"3e-1";"?"and"."becomeNone. The point of keeping the text rather than a float is fidelity: a consumer that wants an exact value can read0.3333as the rational 3333/10000, which is what the file says, whereasfloat("0.3333")is a binary approximation whose exact rational value is 6004199023210345/18014398509481984 and says something the file did not.
- class httk.io.cif.cif_parser.AsuCell[source]¶
Bases:
NamedTupleHold the parsed cell, atom sites, exact tokens, and precision claims.
- Parameters:
basis – Conventional Cartesian basis vectors for the unit cell.
positions – Fractional atom positions.
positions_exact – Original central coordinate tokens for each atom.
occupancies – Parsed atom occupancies, when supplied.
occupancies_exact – Original central occupancy tokens, when supplied.
occupancy_precisions – Coarsest claimed precision for each occupancy, when supplied.
coordinate_precision – Coarsest claimed precision across the coordinates.
basis_precision – Coarsest claimed precision across the cell lengths.
symbols – Atom-site symbols in input order.
labels – Atom-site labels in input order.
equivalent_atoms – One-based identifiers grouping equal atom-site labels.
- coordinate_precision: fractions.Fraction | None[source]¶
- basis_precision: fractions.Fraction | None[source]¶
- httk.io.cif.cif_parser.parse_asu_cell(cifblock)[source]¶
Parse a CIF block into its asymmetric-unit cell data.
- Parameters:
cifblock (collections.abc.Mapping[str, Any]) – Normalized CIF data for one data block.
- Returns:
The parsed unit cell and atom-site data.
- Raises:
ValueError – If required cell or atom-site data is missing or invalid.
- Return type:
- httk.io.cif.cif_parser.parse_structural_modulation(cifblock)[source]¶
Extract structural superspace modulation information from a standard CIF.
- Returns a tuple
(structural_q, mod_dim, has_struct_mod, struct_mod_atoms)where structural_qis a list of q-vectors orNone,mod_dimis the modulation dimension (0 if absent),has_struct_modis a bool, andstruct_mod_atomsis a sorted list of atom-site labels.
- Parameters:
cifblock (collections.abc.Mapping[str, Any]) – Normalized CIF data for one data block.
- Returns:
Structural q-vectors, modulation dimension, presence flag, and affected labels.
- Raises:
ValueError – If a structural wave vector contains an unknown component.
- Return type:
tuple[list[list[fractions.Fraction]] | None, int, bool, list[str]]
- Returns a tuple
- httk.io.cif.cif_parser.cifblock_to_asu(cifblock)[source]¶
Convert one normalized CIF block to a neutral asymmetric-unit payload.
The payload keeps
cell_parameters_exactandpositions_exactas the central numeric text written by the file, and keepssymops_xyzas the raw operation strings. The dual numeric channel gives_httk_*_exactcompanion tags precedence over standard numeric tags on read, preserving central text after a lossy display write.- Parameters:
cifblock (collections.abc.Mapping[str, Any]) – Normalized CIF data for one data block.
- Returns:
A neutral mapping containing cell, atom, symmetry, and metadata channels.
- Raises:
ValueError – If the block lacks required cell, atom-site, or symmetry data.
- Return type:
- httk.io.cif.cif_parser.read_cif_asus(source, *, autocorrect=False)[source]¶
Read a CIF and return its asymmetric units as a neutral, tagged payload.
This is what
httk.core.loadreturns for a.ciffile: a mapping withformatset to"cif",blocksholding one asymmetric-unit mapping per data block that describes a structure, andheaderthe file’s leading comment. The tag lets a consumer dispatch on the file type without knowing which reader produced the payload. Whenautocorrect=True, the top-level payload also containsautocorrect=Trueso downstream adapters can apply compatible repairs.Loading never fails on account of a block that is not a structure. CIF is a general-purpose format and a file may hold bibliographic entries, powder patterns, or an incomplete draft alongside — or instead of — anything crystallographic. Blocks without atom sites are simply not structures and are passed over; blocks that have atom sites but cannot be interpreted are collected in
unparsed, each with the reason, so that nothing is dropped silently and the failure surfaces when a structure is actually asked for.The mapping stays neutral — plain lists, strings, exact numeric text channels, and raw symmetry-operation strings, with no domain objects — so httk-io need not know about httk-atomistic. Turning it into a structure is
httk.core.load(which returns anASUStructurewhen atomistic support is installed).- Parameters:
source (str | os.PathLike[str] | collections.abc.Iterable[str]) – A filename, open text stream, or iterable of CIF lines.
autocorrect (bool) – Drop malformed auxiliary loops and warn about each repair.
- Returns:
A neutral CIF payload containing structural blocks, unparsed block reasons, and the header.
- Raises:
ValueError – If the CIF stream contains malformed data that prevents parsing.
- Return type:
- httk.io.cif.cif_parser.single_asu_from_cif_file(source)[source]¶
Return the first structural CIF block from
read_cif_asus().- Parameters:
source (str | os.PathLike[str] | collections.abc.Iterable[str]) – A filename, open text stream, or iterable of CIF lines.
- Returns:
The first parsed asymmetric-unit mapping.
- Raises:
ValueError – If no structural block is available.
- Return type: