Skip to content

About

Parser for Digital Reference Material Documents (DRMD, ISO 33401) using the bam-masterdata interface.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

drmd-parser

Parser for Digital Reference Material Documents (DRMD) using the bam-masterdata parser interface.

A DRMD is the machine-readable form of the documents that ISO 33401:2024 requires for reference materials: the reference material certificate and the product information sheet. This package reads DRMD XML documents (DRMD schema 1.0.0) into the DRMD object types of bam-masterdata (bam_masterdata/datamodel/v0/drmd/), and writes such objects back as DRMD XML. The datamodel itself is explained in the bam-masterdata documentation, Explanations → Digital Reference Material Documents (DRMD).

The repository was created from the template openbis-parser-example.

Contents

Module Purpose
drmd_parser/parser.py DrmdParser, the parser registered for the openbis-upload-helper
drmd_parser/mapping.py xml_to_graph() reads a DRMD document into objects, graph_to_xml() writes objects as a DRMD document
drmd_parser/rules.py check_graph() checks the objects of one document against the business rules of the DRMD schema before XML is written
drmd_parser/validation.py Validation with drmd.xsd and drmd-business-rules.sch of the DRMD schema repository
drmd_parser/check.py The drmd-check command

Installation

git clone https://github.com/BAMresearch/drmd-parser.git
cd drmd-parser
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install -e '.[dev]'

The DRMD object types are part of bam-masterdata from version 0.18.0 on.

Use

In the openBIS Upload Helper

The parser is registered under the entry-point group bam.parsers as drmd_parser_entry_point. Each uploaded XML file becomes one DRMD object with its parts linked as openBIS parents. Embedded files (for example the PDF rendition) are written to a folder <name>_files next to the uploaded file and attached as datasets of their DRMD_FILE objects. Content that the datamodel does not hold is logged as a warning.

In Python

xml_to_graph() reads a document, check_graph() checks the objects and graph_to_xml() writes them:

from drmd_parser.mapping import graph_to_xml, xml_to_graph
from drmd_parser.rules import check_graph, is_compliant

graph = xml_to_graph("certificate.xml", bam_oe="BAM")
print(graph.warnings)  # parts of the document that are not imported
findings = check_graph(graph)
print(is_compliant(findings))
xml = graph_to_xml(graph)

The following example builds a minimal certificate from objects, checks it and writes it as XML:

from bam_masterdata.datamodel.v0.drmd.object_types import (
    Drmd,
    DrmdIdentifier,
    DrmdProcedure,
    DrmdProducer,
    DrmdPropertySet,
    DrmdQuantity,
    DrmdResponsiblePerson,
    DrmdResult,
    ReferenceMaterial,
)
from bam_masterdata.metadata.entities import CollectionType

from drmd_parser.mapping import DrmdGraph, graph_to_xml
from drmd_parser.rules import check_graph

BAM_OE = "EXAMPLE_OE"  # organisational unit responsible for the material
collection = CollectionType()


def add(obj, *children):
    """Add an object and link it as parent of the given objects."""
    object_id = collection.add(obj)
    obj.code = object_id
    for child in children:
        collection.add_relationship(object_id, child)
    return object_id


producer = DrmdProducer(
    affiliation="Example Institute for Materials Testing",
    name="Reference Materials Sales",
    city="Berlin",
    country_code="DE",
)
add(producer)
document_id = add(
    Drmd(
        language="EN",
        drmd_document_title="REFERENCE_MATERIAL_CERTIFICATE",
        uuid="5d0f8f3c-2b8e-4a55-9a43-0c7b1f2e8d11",
        version="1",
        drmd_validity_type="UNTIL_REVOKED",
        drmd_producer=producer,
        drmd_intended_use="Calibration of particle sizing instruments.",
        drmd_commutability="Commutability was not assessed.",
        drmd_storage_information="Store at 2 °C to 8 °C.",
        drmd_handling_instructions="Invert the bottle gently before use.",
        drmd_metrological_traceability="Traceable to the SI metre.",
        drmd_health_and_safety="No hazards in the supplied form.",
    )
)
add(
    DrmdResponsiblePerson(
        name="Dr A. Musterfrau",
        family_name="Musterfrau",
        affiliation="Example Institute for Materials Testing",
        role="Head of Division",
    ),
    document_id,
)
material_id = add(
    ReferenceMaterial(
        name="Example nanoparticle suspension EXA-N2",
        description="Aqueous suspension in 10 mL glass bottles.",
        bam_oe=BAM_OE,
    ),
    document_id,
)
add(DrmdIdentifier(drmd_scheme="ProductCode", drmd_identifier_value="EXA-N2"), material_id)
add(
    DrmdQuantity(
        drmd_value_type="SI_REAL",
        drmd_quantity_role="MINIMUM_SAMPLE_SIZE",
        drmd_si_value=0.5,
        drmd_si_unit="\\milli\\litre",
    ),
    material_id,
)
values_id = add(
    DrmdPropertySet(name="Certified values", drmd_is_certified=True), document_id
)
collection.add_relationship(material_id, values_id)
add(DrmdProcedure(name="Dynamic light scattering", drmd_norms="ISO 22412"), values_id)
result_id = add(DrmdResult(name="Hydrodynamic diameter"), values_id)
add(
    DrmdQuantity(
        drmd_value_type="SI_REAL",
        drmd_quantity_role="RESULT_DATA",
        drmd_quantity_kind="Diameter",
        drmd_si_value=174.0,
        drmd_si_unit="\\nano\\metre",
        drmd_uncertainty_type="EXPANDED_MU",
        drmd_expanded_uncertainty=26.0,
        drmd_coverage_factor=2.0,
        drmd_coverage_probability=0.95,
    ),
    result_id,
)

graph = DrmdGraph(collection, document_id)
findings = check_graph(graph)  # [] - no rule is violated
xml = graph_to_xml(graph)  # the DRMD document as UTF-8 bytes

Checking documents

drmd-check certificate.xml [more.xml ...] [--output-dir DIR]

For each document the command imports it, lists what is not imported, checks the rules, writes the regenerated document as <name>_regenerated.xml, compares it with the original and, if the DRMD schema repository is available, validates it with drmd.xsd and drmd-business-rules.sch. For the validation, set the environment variable DRMD_SCHEMA_DIR to the folder of the schema repository (the folder with drmd.xsd and tools/validate.py) and install lxml (pip install -e '.[validation]').

Mapping

  • Elements that occur once are properties; elements that can repeat are objects, linked as openBIS parents of the object that contains them. Repeated elements are written in the order in which the objects were created.
  • All texts are in the language of DRMD.LANGUAGE, which is written as @lang. Texts in other languages are reported and not imported.
  • @id, @refId and @refType are not stored. The export gives the first identifier of each material the ID matid_<n> and refers to it from the properties blocks of that material (Convention B of the DRMD guidance).
  • Vocabulary terms are stored with their openBIS code; mapping.py maps them to the values of the schema, for example CONDITIONAL_PASS to conditionalPass.
  • The producer organisation is AFFILIATION of PERSON.DRMD_PRODUCER, its contact point is $NAME. A responsible person gets the producer as AFFILIATION, and the last word of its name as FAMILY_NAME, because dcc:respPerson states neither.
  • Files are DRMD_FILE objects with the file as dataset: as parents of the document they are drmd:document, as parents of a statement they are dcc:file of that statement.

Not imported, and reported as warnings: texts in a second language, fax, post office box, state, further address details and coordinates, description data of contacts, the signature flags (crypt...), links, addresses and descriptions of responsible persons, files outside statements and drmd:document, headings and formulas outside statements, some elements of dcc:metaData, @refType, @refId between elements other than properties and materials, and ds:Signature, which is created when a generated document is signed. Complex values (si:complex) and multivariate uncertainties raise UnsupportedContentError.

Rules

check_graph() returns findings in four groups:

Identifiers Meaning
DRMD-…, RMC-…, PIS-… The rules of drmd-business-rules.sch, with the same identifier, severity and scope
XSD-… Requirements of drmd.xsd and its supporting schemas that a mandatory flag cannot express, e.g. an expanded uncertainty needs a coverage factor
MODEL-… Conditions for writing the objects unambiguously, e.g. all elements of a list need the same uncertainty type
DSI-SIGNIFICANT-DIGIT, BP-MATERIAL-LINK Warnings: digits below the significant digit, and properties blocks without material in documents with several materials

Errors and conditional errors make a document non-compliant; warnings do not.

Tests

pytest tests

The tests read the documents in tests/data, write them back and compare the result, exercise every Schematron rule on the objects, and check what is reported for content outside the datamodel. With DRMD_SCHEMA_DIR set, the regenerated documents are also validated with the DRMD schema, and the findings of check_graph() are compared with those of drmd-business-rules.sch.

About

Parser for Digital Reference Material Documents (DRMD, ISO 33401) using the bam-masterdata interface.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages