# SPDX-License-Identifier: Apache-2.0 # Copyright 2026 Scienting # # Source Code: https://git.scient.ing/papers/paperspec # See the LICENSE.md file for full license terms. """Data tied to words: kinds of data, quantities, subjects and datasets (paperspec.data).""" import copy import json import pytest from hypothesis import given, strategies as st from conftest import FIXTURES, read_jsonl, vector_cases from helpers_lib2 import ( HALE, PROPS, RECORDS, accepted_by_schema, rule_chapters, schema_store, ) from paperspec import data CASES = vector_cases("data") RULES = ( "layer.data.type", "layer.data.quantity", "layer.data.subject", "layer.data.dataset", ) LAYER = read_jsonl(RECORDS / "data" / "annotations.jsonl") BOND_PARAMS = json.loads( ( FIXTURES / "packages" / HALE / "datasets" / "ds-hale-bond-params" / "datapackage.json" ).read_text(encoding="utf-8") ) DESCRIPTORS = { "ds-hale-bond-params": BOND_PARAMS, "ds-hale-ctct-scan": json.loads( (RECORDS / "datasets" / "ds-hale-ctct-scan" / "datapackage.json").read_text( encoding="utf-8" ) ), } TABLE = next(c for c in CASES if c["id"] == "valid-a-table-against-its-dataset")[ "annotation" ] WATER = "XLYOFNOQVPJJNP-UHFFFAOYSA-N" PEPTIDE = "protein:5807fcbf6d5567ca" def edited(annotation, **body): """A copy of `annotation` whose body has the given fields replaced; None removes a field.""" out = copy.deepcopy(annotation) for key, value in body.items(): if value is None: out["body"].pop(key, None) else: out["body"][key] = value return out def messages(annotation, dataset=None): """The (rule, message) pairs `problems` reports for `annotation`.""" return [(x["rule"], x["message"]) for x in data.problems(annotation, dataset)] # --- the vocabulary --------------------------------------------------------------------------------------------- def test_the_kinds_of_data_are_those_of_the_annotation_schema(): bodies = schema_store().load("annotation.schema.json")["properties"]["body"][ "oneOf" ] body = next(b for b in bodies if b["properties"]["kind"]["const"] == "data") assert set(data.DATA_TYPES) == set(body["properties"]["data_type"]["enum"]) @pytest.mark.parametrize("kind", data.DATA_TYPES) def test_each_kind_of_data_is_described(kind): assert data.DATA_TYPES[kind].endswith(".") def test_the_roles_and_subject_kinds(): assert data.ROLES == ("reports", "uses") assert set(data.SUBJECT_KINDS) == {"structure", "sequence"} @pytest.mark.parametrize("rule", RULES) def test_the_rules_are_registered_in_the_layers_chapter(rule): assert rule_chapters()[rule] == "spec/18-layers.md" # --- vectors ---------------------------------------------------------------------------------------------------- @pytest.mark.parametrize("case", CASES, ids=lambda c: c["id"]) def test_data_vectors(case): got = data.problems(case["annotation"], case.get("dataset")) assert got == case["expect"] assert accepted_by_schema(case["annotation"]) is case["schema"] assert {x["rule"] for x in got} <= set(rule_chapters()) @pytest.mark.parametrize("case", CASES, ids=lambda c: c["id"]) def test_a_vector_is_named_for_the_rule_it_breaks(case): stem = case["id"].split("-", 1)[0] rules = {x["rule"] for x in case["expect"]} if stem == "valid": assert rules == set() else: assert "layer.data." + stem in rules def test_every_rule_is_broken_by_some_vector(): assert {x["rule"] for c in CASES for x in c["expect"]} == set(RULES) def test_every_kind_of_data_has_a_valid_vector(): ids = {c["id"] for c in CASES} assert {"valid-kind-" + kind for kind in data.DATA_TYPES} <= ids # --- the fixture layer ------------------------------------------------------------------------------------------- @pytest.mark.parametrize("annotation", LAYER, ids=lambda a: a["id"].rsplit("/", 1)[1]) def test_the_data_layer_breaks_no_rule_alone_or_against_its_dataset(annotation): assert annotation["body"]["kind"] == "data" assert data.problems(annotation) == [] descriptor = DESCRIPTORS.get(annotation["body"].get("dataset")) assert data.problems(annotation, descriptor) == [] def test_the_data_layers_datasets_are_held_by_the_project(): project = json.loads((RECORDS / "project.json").read_text(encoding="utf-8")) named = {a["body"]["dataset"] for a in LAYER if "dataset" in a["body"]} assert named <= set(project["datasets"]) # --- type and role ----------------------------------------------------------------------------------------------- def test_an_annotation_of_another_kind_is_refused(): with pytest.raises(ValueError, match="not a data annotation"): data.problems({"body": {"kind": "comment"}}) with pytest.raises(ValueError, match="not a data annotation"): data.problems({"body": {}}) @pytest.mark.parametrize("value", ["spectroscopy", "Spectrum", None, ""]) def test_a_kind_of_data_that_is_not_listed_is_refused(value): got = messages(edited(TABLE, data_type=value)) assert [r for r, _ in got] == ["layer.data.type"] assert got[0][1].startswith("the kind of data is not one of abundance, benchmark") @pytest.mark.parametrize("role", ["names", None, ""]) def test_a_role_that_is_not_listed_is_refused(role): assert messages(edited(TABLE, role=role)) == [ ("layer.data.type", "the role is not one of reports, uses") ] def test_both_a_wrong_kind_and_a_wrong_role_are_reported_in_that_order(): got = messages(edited(TABLE, data_type="nope", role="nope")) assert [m.split(" is not")[0] for _, m in got] == ["the kind of data", "the role"] # --- quantities ---------------------------------------------------------------------------------------------------- def test_a_field_may_be_given_once(): quantities = [{"field": "a", "name": "A"}, {"field": "b", "name": "B"}] assert data.problems(edited(TABLE, quantities=quantities, dataset=None)) == [] def test_a_repeated_field_is_reported_at_every_repeat(): quantities = [{"field": "a"}, {"field": "b"}, {"field": "a"}, {"field": "a"}] got = messages(edited(TABLE, quantities=quantities, dataset=None)) assert got == [ ("layer.data.quantity", "quantity 2 repeats the field a"), ("layer.data.quantity", "quantity 3 repeats the field a"), ] def test_quantities_without_a_field_repeat_the_empty_field(): got = messages(edited(TABLE, quantities=[{}, {}], dataset=None)) assert got == [("layer.data.quantity", "quantity 1 repeats the field ")] # --- subjects ------------------------------------------------------------------------------------------------------ @pytest.mark.parametrize( "subject", [ {"kind": "structure", "identity": WATER}, {"kind": "structure", "identity": "smiles:0123456789ab"}, {"kind": "structure", "identity": "smarts:0123456789ab"}, {"kind": "sequence", "identity": PEPTIDE}, {"kind": "sequence", "identity": "dna:0123456789abcdef"}, {"kind": "sequence", "identity": "rna:0123456789abcdef"}, ], ) def test_subjects_take_the_identities_of_the_structure_and_sequence_bodies(subject): assert data.problems(edited(TABLE, subjects=[subject], dataset=None)) == [] @pytest.mark.parametrize( ("subject", "fragment"), [ ( {"kind": "reaction", "identity": WATER}, "is of a kind that is not structure or sequence", ), ({"identity": WATER}, "is of a kind that is not structure or sequence"), ( {"kind": "structure", "identity": PEPTIDE}, "is not the form of a structure identity", ), ( {"kind": "sequence", "identity": WATER}, "is not the form of a sequence identity", ), ( {"kind": "structure", "identity": "smiles:0123456789a"}, "not the form of a structure", ), ( {"kind": "structure", "identity": "smiles:0123456789ABCDEF"}, "not the form of a structure", ), ({"kind": "structure", "identity": WATER + "X"}, "not the form of a structure"), ({"kind": "structure", "identity": ""}, "not the form of a structure"), ( {"kind": "sequence", "identity": "protein:0123456789abcdef0"}, "not the form of a sequence", ), ( {"kind": "sequence", "identity": "peptide:0123456789abcdef"}, "not the form of a sequence", ), ], ) def test_a_subject_of_the_wrong_kind_or_form_is_refused(subject, fragment): got = messages(edited(TABLE, subjects=[subject], dataset=None)) assert [r for r, _ in got] == ["layer.data.subject"] assert fragment in got[0][1] @pytest.mark.parametrize("suffix", ["\n", " ", "\x00"]) def test_an_identity_is_matched_whole_so_a_trailing_character_is_refused(suffix): for identity, kind in ((WATER, "structure"), (PEPTIDE, "sequence")): subject = {"kind": kind, "identity": identity + suffix} got = messages(edited(TABLE, subjects=[subject], dataset=None)) assert [r for r, _ in got] == ["layer.data.subject"] def test_a_subject_listed_twice_is_reported_at_its_second_place_only(): first = {"kind": "structure", "identity": WATER} other = {"kind": "sequence", "identity": PEPTIDE} got = messages(edited(TABLE, subjects=[first, other, first], dataset=None)) assert got == [("layer.data.subject", "subject 2 repeats the structure " + WATER)] def test_the_same_identity_under_two_kinds_is_not_a_repeat(): subjects = [ {"kind": "structure", "identity": WATER}, {"kind": "sequence", "identity": WATER}, ] got = messages(edited(TABLE, subjects=subjects, dataset=None)) assert [m for _, m in got] == [ f"subject 1: {WATER} is not the form of a sequence identity" ] def test_a_wrong_subject_that_is_repeated_is_reported_twice(): bad = {"kind": "structure", "identity": "ethane"} got = messages(edited(TABLE, subjects=[bad, bad], dataset=None)) assert [m.split(":")[0].split(" repeats")[0] for _, m in got] == [ "subject 0", "subject 1", "subject 1", ] @given( st.text(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", min_size=14, max_size=14), st.text(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ", min_size=10, max_size=10), st.sampled_from("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), ) @PROPS def test_any_inchikey_shaped_identity_is_a_structure_and_never_a_sequence( first, second, last ): identity = f"{first}-{second}-{last}" assert data.SUBJECT_KINDS["structure"].fullmatch(identity) assert not data.SUBJECT_KINDS["sequence"].fullmatch(identity) assert not data.SUBJECT_KINDS["structure"].fullmatch(identity + "-") # --- datasets --------------------------------------------------------------------------------------------------------- @pytest.mark.parametrize( "dataset", ["Table 1", "ds-", "DS-a", "ds-A", "ds-a b", "ds-a\n", "", "dataset-a"] ) def test_a_dataset_that_is_no_id_is_refused(dataset): got = messages(edited(TABLE, dataset=dataset)) assert got == [("layer.data.dataset", f"{dataset} is not the ID of a dataset")] @pytest.mark.parametrize("dataset", ["ds-a", "ds-0", "ds-hale.bond_params-2", "ds-a-"]) def test_a_dataset_id_starts_with_ds_and_a_lower_case_letter_or_digit(dataset): assert data.problems(edited(TABLE, dataset=dataset, quantities=None)) == [] def test_without_a_dataset_there_is_nothing_to_compare(): annotation = edited(TABLE, dataset=None) assert data.problems(annotation, BOND_PARAMS) == [] def test_without_a_descriptor_only_the_id_is_checked(): assert data.problems(TABLE) == [] assert data.problems(TABLE, None) == [] def test_the_main_examples_bond_table_agrees_with_its_descriptor(): assert TABLE["body"]["dataset"] == BOND_PARAMS["id"] assert data.problems(TABLE, BOND_PARAMS) == [] def test_another_dataset_than_the_descriptor_is_named_in_the_message(): got = messages(TABLE, DESCRIPTORS["ds-hale-ctct-scan"]) assert got[0] == ( "layer.data.dataset", "the dataset is ds-hale-ctct-scan, not ds-hale-bond-params", ) def test_a_field_the_dataset_lacks_is_reported_by_quantity(): quantities = [{"field": "k_r"}, {"field": "nope", "name": "Nothing"}] got = messages(edited(TABLE, quantities=quantities), BOND_PARAMS) assert got == [ ( "layer.data.dataset", "quantity 1 names the field nope, which ds-hale-bond-params does not have", ) ] def test_a_unit_that_differs_from_the_datasets_is_reported_with_both(): quantities = [ {"field": "k_r", "unit": "N / m"}, {"field": "r_eq", "unit": "angstrom"}, ] got = messages(edited(TABLE, quantities=quantities), BOND_PARAMS) assert got == [ ( "layer.data.dataset", "quantity 0 gives k_r the unit N / m, but ds-hale-bond-params gives it kcal / mol / angstrom ** 2", ) ] def test_a_quantity_without_a_unit_takes_the_datasets(): quantities = [{"field": "k_r"}, {"field": "r_eq"}] assert data.problems(edited(TABLE, quantities=quantities), BOND_PARAMS) == [] def test_a_unit_given_for_a_field_that_has_none_in_the_dataset_is_a_difference(): dataset = {"id": "ds-x", "resources": [{"schema": {"fields": [{"name": "point"}]}}]} annotation = edited( TABLE, dataset="ds-x", quantities=[{"field": "point", "unit": "m"}] ) assert messages(annotation, dataset) == [ ( "layer.data.dataset", "quantity 0 gives point the unit m, but ds-x gives it None", ) ] def test_the_fields_of_every_resource_of_the_dataset_count(): dataset = { "id": "ds-x", "resources": [ {"schema": {"fields": [{"name": "a", "unit": "m"}]}}, {"name": "no schema"}, {"schema": {"fields": [{"name": "b", "unit": "s"}]}}, ], } annotation = edited( TABLE, dataset="ds-x", quantities=[{"field": "a", "unit": "m"}, {"field": "b", "unit": "s"}], ) assert data.problems(annotation, dataset) == [] def test_a_descriptor_without_resources_has_no_fields(): annotation = edited(TABLE, dataset="ds-x", quantities=[{"field": "a"}]) got = messages(annotation, {"id": "ds-x"}) assert got == [ ("layer.data.dataset", "quantity 0 names the field a, which ds-x does not have") ] def test_a_descriptor_with_another_id_reports_the_mismatch_before_the_fields(): annotation = edited(TABLE, dataset="ds-x", quantities=[{"field": "a"}]) got = messages(annotation, {"id": "ds-y"}) assert [m.split(" ")[0] for _, m in got] == ["the", "quantity"]