Adding custom metadata¶
It is possible to attach and store arbitrary metadata to WelDX objects as python attributes. The WelDX API reserves the attribute names wx_metadata
and wx_user
for these use cases.
The wx_metadata
field is intended for additional information that might be required as part of a custom Quality Standard. The wx_user
attribute is intended to add any user data and should only be used for additional information that will not get accessed by the API.
A small example¶
Let’s say we want to use metadata to add a custom information to a current-sensor used during a welding experiment. As an example we want to add a timestamp to indicate when the sensor was last calibrated.
We start by creating the sensor object as a GenericEquipment
:
[2]:
from weldx.measurement import GenericEquipment
HKS_sensor = GenericEquipment(name="HKS P1000-S3")
Now we want to add the timestamp of the last time the sensor was calibrated as additional metadata. To do this we simple assign a new .wx_metadata
attribute to the sensor. We will also add a small personal note to wx_user
.
Note: By convention the metadata fields should be python dictionaries.
[3]:
import pandas as pd
HKS_sensor.wx_metadata = {"calibration": pd.Timestamp("2020-06-01")}
HKS_sensor.wx_user = {"notes": ["wallmounted", "The cable seems to be a bit loose."]}
Now let’s look at the ASDF file contents with our metadata:
[4]:
from weldx.asdf.util import _write_buffer, notebook_fileprinter
buffer = _write_buffer({"sensor": HKS_sensor})
notebook_fileprinter(buffer)
/home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/asdf/schema.py:302: AsdfWarning: Unable to locate schema file for 'tag:weldx.bam.de:weldx/equipment/generic_equipment-1.0.0': 'file:///home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/weldx/asdf/schemas/weldx.bam.de/weldx/equipment/generic_equipment-1.0.0.yaml'
warnings.warn(msg.format(tag, schema_path), AsdfWarning)
/home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/asdf/schema.py:302: AsdfWarning: Unable to locate schema file for 'tag:weldx.bam.de:weldx/time/timestamp-1.0.0': 'file:///home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/weldx/asdf/schemas/weldx.bam.de/weldx/time/timestamp-1.0.0.yaml'
warnings.warn(msg.format(tag, schema_path), AsdfWarning)
/home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/asdf/schema.py:302: AsdfWarning: Unable to locate schema file for 'tag:weldx.bam.de:weldx/equipment/generic_equipment-1.0.0': 'file:///home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/weldx/asdf/schemas/weldx.bam.de/weldx/equipment/generic_equipment-1.0.0.yaml'
warnings.warn(msg.format(tag, schema_path), AsdfWarning)
/home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/asdf/schema.py:302: AsdfWarning: Unable to locate schema file for 'tag:weldx.bam.de:weldx/time/timestamp-1.0.0': 'file:///home/docs/checkouts/readthedocs.org/user_builds/weldx/conda/v0.3.2/lib/python3.8/site-packages/weldx/asdf/schemas/weldx.bam.de/weldx/time/timestamp-1.0.0.yaml'
warnings.warn(msg.format(tag, schema_path), AsdfWarning)
[4]:
#ASDF 1.0.0
#ASDF_STANDARD 1.5.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {author: Space Telescope Science Institute, homepage: 'http://github.com/spacetelescope/asdf',
name: asdf, version: 2.7.3}
history:
extensions:
- !core/extension_metadata-1.0.0
extension_class: weldx.asdf.extension.WeldxExtension
software: !core/software-1.0.0 {name: weldx, version: 0.3.2}
- !core/extension_metadata-1.0.0
extension_class: asdf.extension.BuiltinExtension
software: !core/software-1.0.0 {name: asdf, version: 2.7.3}
sensor: !<tag:weldx.bam.de:weldx/equipment/generic_equipment-1.0.0>
data_transformations: []
name: HKS P1000-S3
sources: []
wx_metadata:
calibration: !<tag:weldx.bam.de:weldx/time/timestamp-1.0.0> {value: '2020-06-01T00:00:00'}
wx_user:
notes: [wallmounted, The cable seems to be a bit loose.]
...
As we can see, our custom metadata gets serialized into the ASDF tree:
wx_metadata:
calibration: !<tag:weldx.bam.de:weldx/time/timestamp-1.0.0> {value: '2020-06-01T00:00:00'}
wx_user:
notes: [wallmounted, The cable seems to be a bit loose.]
Let’s check if we keep the information upon reading the file:
[5]:
from weldx.asdf.util import _read_buffer
data = _read_buffer(buffer)
display(data["sensor"].wx_metadata)
display(data["sensor"].wx_user)
{'calibration': Timestamp('2020-06-01 00:00:00')}
{'notes': ['wallmounted', 'The cable seems to be a bit loose.']}