Buckets:

download
raw
4.63 kB
"""
Built-in serializer for the XAS Data Interchange (XDI) format.
Spec: https://github.com/XraySpectroscopy/XAS-Data-Interchange/blob/master/specification/spec.md
This serializer is intended to be used with tables tagged with Spec("xdi", ...), which
carry XDI-structured metadata, but will produce valid (if sparse) output for any table.
The expected metadata structure mirrors that produced by tiled.examples.xdi.XDIAdapter:
metadata = {
"xdi_version": "1.0",
"extra_version": "GSE/1.0", # optional
"fields": {
"Element": {"symbol": "Cu", "edge": "K"},
"Mono": {"d_spacing": "3.13553"},
...
},
"comments": "# optional free-form text\\n", # optional
}
The MIME type is "application/x-xdi" and the file extension alias ".xdi" is registered.
"""
import io
import mimetypes
from ..media_type_registration import default_serialization_registry
# Register ".xdi" extension -> "application/x-xdi"
mimetypes.types_map.setdefault(".xdi", "application/x-xdi")
# XDI-defined namespaces (from the specification dictionary).
# Used as a lookup table to prioritise known fields when writing.
XDI_DEFINED_NAMESPACES = {
"Facility": {"name", "energy", "current", "xray_source"},
"Beamline": {"name", "collimation", "focusing", "harmonic_rejection"},
"Mono": {"name", "d_spacing"},
"Detector": {"i0", "it", "if", "ir"},
"Sample": {
"name",
"id",
"stoichiometry",
"prep",
"experimenters",
"temperature",
},
"Scan": {"start_time", "end_time", "edge_energy"},
"Element": {"symbol", "edge", "reference", "ref_edge"},
"Column": set(), # Column tags are integers; all are valid
}
# Required fields per the XDI spec
XDI_REQUIRED_FIELDS = {
"Element": {"symbol", "edge"},
"Mono": {"d_spacing"},
}
@default_serialization_registry.register("xdi", "application/x-xdi")
def serialize_xdi(mimetype, df, metadata):
"""Serialize a pandas DataFrame and its XDI metadata to XDI format.
Parameters
----------
mimetype : str
The requested MIME type (accepted but not used).
df : pandas.DataFrame
The data table. The first column should be the energy/abscissa array.
metadata : dict
May contain:
- "fields": dict of {namespace: {tag: value}} XDI header fields
- "xdi_version": str (defaults to "1.0")
- "extra_version": str (e.g. "GSE/1.0")
- "comments": str (free-form user comment text)
Returns
-------
bytes
UTF-8 encoded XDI file content.
"""
output = io.StringIO()
# --- Version line (required, must be first) ---
xdi_version = metadata.get("xdi_version") or "1.0"
extra_version = metadata.get("extra_version") or ""
version_line = f"# XDI/{xdi_version}"
if extra_version:
version_line += f" {extra_version}"
output.write(version_line + "\n")
# --- Header fields ---
# Write defined namespaces first (in spec-recommended order), then extensions.
fields = metadata.get("fields", {})
def _write_namespace(namespace, tags):
for tag, value in tags.items():
output.write(f"# {namespace}.{tag}: {value}\n")
defined_order = list(XDI_DEFINED_NAMESPACES.keys())
written_namespaces = set()
for namespace in defined_order:
if namespace in fields:
_write_namespace(namespace, fields[namespace])
written_namespaces.add(namespace)
# Write any extension (non-defined) namespaces afterwards
for namespace, tags in fields.items():
if namespace not in written_namespaces:
_write_namespace(namespace, tags)
# --- Field-end separator (always present, per spec) ---
output.write("# /////////////\n")
# --- Comments block ---
from .. import __version__
comments = metadata.get("comments", "")
if comments:
for line in comments.splitlines():
stripped = line.strip()
if stripped and not stripped.startswith("#"):
output.write(f"# {line}\n")
else:
output.write(f"{line}\n")
output.write(f"# generated by tiled {__version__}\n")
# --- Header-end separator (required) ---
output.write("# -------------\n")
# --- Column labels line ---
columns = list(df.columns)
output.write("# " + " ".join(str(c) for c in columns) + "\n")
# --- Data section (whitespace-delimited, no index, no header row) ---
df.to_csv(output, header=False, index=False, sep=" ")
return output.getvalue().encode()

Xet Storage Details

Size:
4.63 kB
·
Xet hash:
908607b134ba5171ca970d9f7d8b45e7c4418b94890deb36fbc2f5a0d9c64aca

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.