How MaiML export works
This page explains how the sample and measurement records you keep in eLabFTW are written out as MaiML (JIS K 0200) files. For the details of each function, see 10. MaiML export. This page focuses on the overall flow and on what goes where. To understand where data lives in eLabFTW (fields, structured metadata, attachments), read Understanding metadata first.
What is MaiML?
MaiML is an XML format, standardized as JIS K 0200, for recording measurement and analysis data independently of instruments and vendors. A MaiML file has four parts.
| Part | What it records |
|---|---|
| document | Who created the document, at which facility or instrument |
| protocol | How the measurement is done (the step flow, plus templates for samples and results) |
| data | What was actually measured and what came out (samples, conditions, results) |
| eventLog | A record of the steps actually performed |
The idea: eLabFTW is the source of truth, MaiML is for exchange
- eLabFTW is the source of truth: keep sample information and measurement records in eLabFTW fields (extra_fields) and attachments, as usual.
- Create MaiML when you need it: when you hand data to instruments, facilities, or databases, create MaiML from what's in eLabFTW. If you fix something in eLabFTW, just create the MaiML again.
- You decide the mapping: which field becomes which MaiML key differs by facility and field of research. The toolkit provides only facility-independent building blocks; you write the mapping in your script.
What goes where
| In eLabFTW | Where it goes in MaiML | Building block |
|---|---|---|
| Facility, instrument, creator (set in your script) | document | b.document() |
| Measurement steps (sample → measure → spectrum) | protocol | b.flow(), b.method() |
| Sample fields (formula, mass, and so on) | properties of a material in data | FieldMap, props_from_fields(), b.material() |
| Attached files (spectra and so on) | insertions of a result in data (URI and SHA-256) | insertions_from_uploads(), b.result() |
| The fact that the measurement was done | an event in eventLog | b.event() |
| Entry IDs | the uuid of each element | stable_uuid() |
b is the builder you create with maiml.MaimlBuilder. Calling it from top to bottom builds the four parts of a MaiML file.
Steps
1. Decide the mapping
Use FieldMap to map each eLabFTW field to a MaiML key, type, unit, and description.
from elabftw_toolkit import maiml
from elabftw_toolkit.maiml import FieldMap as F
FMAP = [
F("chemical_formula", "ex:chemical_formula", description="Chemical formula"),
F("calculated_mass", "ex:calculated_mass", "float", "mg", "Calculated mass"),
]| Argument | Meaning |
|---|---|
| 1st | The eLabFTW field name |
| 2nd | The MaiML key (with a prefix such as ex:) |
| 3rd | Type (string, float, int, uri, or datetime; defaults to string) |
| 4th | Unit (if left out, the eLabFTW field units passed to props_from_fields() are used) |
| 5th | Description |
Units are only added for float and int fields. Fields with empty values aren't included in the MaiML.
2. Read from eLabFTW
SITE = "https://eln.example.org"
sample = elab.items.entry(34).raw() # the sample (a Resource)
measurement = elab.experiments.entry(12).raw() # the measurement (an Experiment)
fields = maiml.extra_fields(sample) # {field name: value}
units = maiml.extra_field_units(sample) # {field name: unit}3. Build the MaiML
b = maiml.MaimlBuilder(namespaces={"ex": "https://example.org/ns#"})
# document: who, with which instrument
b.document("exp_12", "Measurement 12",
uuid=maiml.stable_uuid(SITE, "experiments", 12, "maiml"),
owner=("ORG", "Example Org"), vendor=("FAC", "Example Facility"),
instrument=("BL1", "Beamline 1"))
# protocol: sample → (measure) → spectrum
b.flow("xas", places=[("sample", "Sample"), ("spec", "Spectrum")],
transitions=[("meas", "Measure")],
arcs=[("place-sample", "tr-meas"), ("tr-meas", "place-spec")])
b.method("XAS", "X-ray absorption spectroscopy",
materials=[("sample", "Sample")], results=[("spec", "Spectrum")])
# data: the sample fields and the files from the measurement
b.material("mat-sample", "sample", sample["title"],
props=maiml.props_from_fields(fields, FMAP, units),
uuid=maiml.stable_uuid(SITE, "items", 34))
b.result("res-spec", "spec", "spectrum",
insertions=maiml.insertions_from_uploads(measurement, "ex:data/", base=SITE))
# eventLog: the record that the measurement was done
b.event("meas", with_results=True)4. Write, validate, and keep it in eLabFTW
result = maiml.dump_and_validate(b.build(), "exp12.maiml", namespaces=b.namespaces)
assert result.ok, result
elab.experiments.entry(12).files.upload("exp12.maiml")insertions_from_uploads() only includes attachments for which eLabFTW has recorded a SHA-256 hash.
dump_and_validate() writes the file and then checks it against the MaiML schema. If you attach the file to the original Experiment, you can always tell which record a MaiML file came from.
Common questions
How is the uuid decided?
stable_uuid(SITE, "items", 34) builds the same uuid every time from the eLabFTW URL and the entry's type and ID. The same sample gets the same uuid in every file, however many times you export it, so separate MaiML files can be linked automatically.
Can I use Japanese names, spaces, and so on?
Some characters aren't allowed in MaiML names. The toolkit converts names with qname() and keeps the original name in the description.
I want to use external vocabularies like PaNET
Embed them with a key prefix and a URI value. For example, pass b.uri("ex:technique", "http://purl.org/pan-science/PaNET/PaNET01196") to b.method(..., properties=[...]).
I want to link to another MaiML file
Point to the parent document (uuid and SHA-256) with b.parent(uuid, path) and pass it to b.document(..., parents=[...]).
Installation
MaiML export needs an extra package (pymaiml).
python -m pip install -e ".[maiml]"What to read next
- 10. MaiML export: how to use each building block, and caveats
- 11. Recipes: exporting many entries at once