2. 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.

2.1. 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 MeasurementEquipment:

from weldx.measurement import MeasurementEquipment

HKS_sensor = MeasurementEquipment(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.

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. We will find our data at the very end after the ASDF header.

from weldx import WeldxFile

file = WeldxFile(tree={"sensor": HKS_sensor}, mode="rw")
file.header()
#ASDF 1.0.0
#ASDF_STANDARD 1.5.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
%TAG !weldx! asdf://weldx.bam.de/weldx/tags/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {author: The ASDF Developers, homepage: 'http://github.com/asdf-format/asdf',
  name: asdf, version: 2.15.1}
history:
  extensions:
  - !core/extension_metadata-1.0.0
    extension_class: asdf.extension.BuiltinExtension
    software: !core/software-1.0.0 {name: asdf, version: 2.15.1}
  - !core/extension_metadata-1.0.0
    extension_class: weldx.asdf.extension.WeldxExtension
    extension_uri: asdf://weldx.bam.de/weldx/extensions/weldx-0.1.2
    software: !core/software-1.0.0 {name: weldx, version: 0.6.8.dev29+g0e0a9ea.d20240414}
sensor: !weldx!equipment/measurement_equipment-0.1.0
  name: HKS P1000-S3
  sources: []
  transformations: []
  wx_metadata: {calibration: !weldx!time/timestamp-0.1.0 '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: !<asdf://weldx.bam.de/weldx/tags/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:

in_memory_file = WeldxFile(file.write_to())

from IPython.display import display

display(in_memory_file["sensor"].wx_metadata)
display(in_memory_file["sensor"].wx_user)
{'calibration': Timestamp('2020-06-01 00:00:00')}
{'notes': ['wallmounted', 'The cable seems to be a bit loose.']}