Skip to content

ontometer_corpus.pipeline.dedup

Phase 2: Dedup.

About

Identifies duplicate ontologies across registries and marks non-canonical rows in corpus_registry (is_duplicate=1, canonical_entry_id=), then flags one latest canonical representative per release family.

Version tracking: Different versions of the same ontology are preserved as separate canonical rows. Only rows that represent the SAME version fetched from multiple registries are collapsed as duplicates.

Passes, in order:

Pass 0 — Exact-byte dedup (file_hash) Groups all parse_status='ok' rows by file_hash (SHA-256 of the raw fetched bytes). Byte-identical files are unambiguously the same content regardless of any parsed metadata, so this needs no guard — it is the strongest, cheapest identity signal and collapses mirrors serving the exact same file before either semantic pass runs.

Pass A — Version-aware IRI dedup Groups all remaining is_duplicate=0 rows by (ontology_iri, ontology_version_iri). Rows with the same IRI but DIFFERENT version IRI are distinct versions and are NEVER marked as duplicates. Rows where ontology_version_iri IS NULL are preserved: a stable ontology IRI and class/property signature do not prove that two files are the same release, because relationships, individuals and annotations may change.

Pass B — Content-hash dedup Groups remaining rows by content_hash (the coarse class+property IRI set), but only an identical, non-null ontology_version_iri is strong enough to mark a duplicate. Rows with no classes or properties are excluded (they would all share the empty-set hash) and logged to dedup_exclusions.log.

Pass C — Release-family selection Groups the remaining canonical rows by release family (the fetch identifier before its final ::), then sets is_latest_version=1 on the most recent representative in each family. Earlier releases stay canonical so longitudinal analyses retain their full history.

Pass D — One latest row per ontology Release families are per registry, so Pass C keeps one latest row per registry: RO arrives as obo:ro, agroportal:RO and okg:Q28729320, each a different release. Two latest rows are one ontology when either holds: * they share an ontology IRI (ontology_key: scheme, trailing separator and OBO build artifacts such as x/x-full.owl or x-base.owl ignored) and their declared terms (structural sidecars) overlap: across registries, at least CONTAINMENT_SAME of the smaller copy's terms are in the larger, since a registry often holds an older, smaller release; within one registry, which lists them as separate entries, the two sets must overlap by CONTAINMENT_SAME of their union, so a module or extract published under its parent's IRI stays its own ontology. Two releases under the ontology's own OBO PURL that share a term, and a copy declaring nothing, also join; * one of them, or an earlier canonical release of its family, declares the identical non-empty class and property set (content_hash) as the other or one of its family's releases, with or without an IRI (_same_terms): a copy of an old release is that ontology even after a release renamed its terms. Only the newest of each such group keeps is_latest_version (see _newness_key). When an IRI covers several distinct ontologies (a placeholder such as http://iri/, or a tool's default IRI reused by several projects), the IRI is no identity: those rows get shared_iri=1 and become their own network nodes (row:). The IRI evidence needs the sidecar store; without it the pass is skipped with a warning.

Pass E — An ontology declares something A latest row declaring no class, property, individual or SKOS concept (an import-only aggregator, an empty submission, a failed conversion) loses is_latest_version: it is a pointer, not an ontology.

Winner election within a group (no registry is treated as inherently more authoritative than another): 1. Highest triple_count 2. Tie-break: latest retrieval_date (lexicographic ISO-8601) 3. Tie-break: lowest row id (first one seeded, purely for determinism)

Raw-byte, declared-IRI, content-hash, and release-family checks avoid a graph-isomorphism digest, whose rdflib canonicalisation can run unboundedly long on blank-node-dense graphs.

Attributes

CONTAINMENT_SAME = 0.5 module-attribute

MIN_IDENTICAL_TERMS = 20 module-attribute

Functions:

run_dedup(db_path, exclusion_log=None, sidecar_path=None)

Run all deduplication passes on the corpus_registry.

Parameters:

Name Type Description Default
db_path Path

Path to corpus.db.

required
exclusion_log Path | None

Where rows excluded from Pass B are logged. Defaults to dedup_exclusions.log next to corpus.db.

None
sidecar_path Callable[[str], Path] | None

Maps a content hash to its structural sidecar (CorpusPaths.sidecar_path). Pass D needs it; without it the pass is skipped.

None

Returns:

Type Description
dict

Summary counts keyed pass_0_marked, pass_a_marked,

dict

pass_b_marked, latest_flagged (after Passes D and E),

dict

latest_unflagged, shared_iris, iris_undecided and

dict

stubs_dropped.

Source code in ontometer_corpus/pipeline/dedup.py
def run_dedup(
    db_path: Path,
    exclusion_log: Path | None = None,
    sidecar_path: Callable[[str], Path] | None = None,
) -> dict:
    """
    Run all deduplication passes on the corpus_registry.

    Args:
        db_path: Path to corpus.db.
        exclusion_log: Where rows excluded from Pass B are logged. Defaults to
            ``dedup_exclusions.log`` next to corpus.db.
        sidecar_path: Maps a content hash to its structural sidecar
            (``CorpusPaths.sidecar_path``). Pass D needs it; without it the
            pass is skipped.

    Returns:
        Summary counts keyed ``pass_0_marked``, ``pass_a_marked``,
        ``pass_b_marked``, ``latest_flagged`` (after Passes D and E),
        ``latest_unflagged``, ``shared_iris``, ``iris_undecided`` and
        ``stubs_dropped``.
    """
    conn = get_connection(db_path)
    exclusion_log_path = exclusion_log or db_path.parent / "dedup_exclusions.log"
    counts = {"pass_0_marked": 0, "pass_a_marked": 0, "pass_b_marked": 0}

    # These flags are wholly derived by this phase. Recompute them from the
    # parsed evidence so a rule correction can restore rows that an earlier
    # run classified as duplicates.
    conn.execute(
        """
        UPDATE corpus_registry SET
            canonical_entry_id = NULL,
            is_duplicate = 0,
            shared_iri = 0,
            is_latest_version = 0
        """
    )
    conn.commit()

    # ---- Pass 0: Exact-byte dedup (file_hash) --------------------------
    print("\n  Pass 0: Exact-byte dedup (file_hash)")
    print("  (identical raw bytes = duplicate, unconditionally — no guard needed)")

    rows_with_file_hash = conn.execute(
        """
        SELECT id, source_registry, file_hash, triple_count, retrieval_date
        FROM corpus_registry
        WHERE parse_status = 'ok'
          AND file_hash IS NOT NULL
          AND is_duplicate = 0
        ORDER BY file_hash
        """
    ).fetchall()

    file_hash_groups: dict[str, list] = defaultdict(list)
    for row in rows_with_file_hash:
        file_hash_groups[row["file_hash"]].append(row)

    dup_file_hash = {h: rows for h, rows in file_hash_groups.items() if len(rows) > 1}
    print(f"  Found {len(dup_file_hash)} file_hash groups with duplicates")

    for h, rows in dup_file_hash.items():
        winner_id = _elect_winner(rows)
        for row in rows:
            if row["id"] != winner_id:
                update_duplicate(conn, row["id"], winner_id)
                counts["pass_0_marked"] += 1
                print(
                    f"    [dup] {row['source_registry']}#{row['id']} "
                    f"-> canonical #{winner_id} | file_hash={h[:16]}..."
                )

    # ---- Pass A: Version-aware IRI dedup ------------------------------
    print("\n  Pass A: Version-aware IRI dedup")
    print("  (same IRI + same version_iri = duplicate; different version = separate canonical row)")

    rows_with_iri = conn.execute(
        """
        SELECT id, source_registry, ontology_iri, ontology_version_iri,
               content_hash, triple_count, retrieval_date
        FROM corpus_registry
        WHERE parse_status = 'ok'
          AND ontology_iri IS NOT NULL
          AND is_duplicate = 0
        ORDER BY ontology_iri, ontology_version_iri
        """
    ).fetchall()

    # Group A1: rows where version_iri is known — key = (iri, version_iri)
    versioned_groups: dict[tuple, list] = defaultdict(list)
    # Group A2: rows where version_iri is NULL — key = iri, further guarded by content hash
    unversioned_by_iri: dict[str, list] = defaultdict(list)

    for row in rows_with_iri:
        if row["ontology_version_iri"] is not None:
            key = (row["ontology_iri"], row["ontology_version_iri"])
            versioned_groups[key].append(row)
        else:
            unversioned_by_iri[row["ontology_iri"]].append(row)

    # A1: deduplicate versioned groups
    dup_versioned = {k: rows for k, rows in versioned_groups.items() if len(rows) > 1}
    print(f"  A1 (IRI+version): {len(dup_versioned)} groups with cross-registry duplicates")
    for (iri, ver), rows in dup_versioned.items():
        winner_id = _elect_winner(rows)
        for row in rows:
            if row["id"] != winner_id:
                update_duplicate(conn, row["id"], winner_id)
                counts["pass_a_marked"] += 1
                print(
                    f"    [dup] {row['source_registry']}#{row['id']} "
                    f"-> canonical #{winner_id} | version={ver}"
                )

    # A2: preserve unversioned rows. A matching ontology IRI and coarse
    # class/property signature is not release identity: relationships,
    # individuals, concepts and annotations may all have changed.
    dup_unversioned = {iri: rows for iri, rows in unversioned_by_iri.items() if len(rows) > 1}
    print(f"  A2 (IRI, no version): {len(dup_unversioned)} IRIs with multiple unversioned entries")
    for iri, rows in dup_unversioned.items():
        print(f"    [keep] {iri[:60]}: {len(rows)} unversioned rows require release selection")

    # ---- Pass B: Content-hash dedup ------------------------------------
    print("\n  Pass B: Content-hash dedup")

    hash_groups: dict[str, list] = defaultdict(list)

    rows_with_hash = conn.execute(
        """
        SELECT id, source_id, source_registry, ontology_iri, ontology_version_iri,
               content_hash, triple_count, retrieval_date
        FROM corpus_registry
        WHERE parse_status = 'ok'
          AND content_hash IS NOT NULL
          AND content_hash != ?
          AND is_duplicate = 0
          AND (class_count + property_count) > 0
        ORDER BY content_hash
        """,
        (_EMPTY_CONTENT_HASH,),
    ).fetchall()
    # Grouping key is the coarse class+property content_hash — see the
    # version-IRI guard below for how distinct versions with an identical
    # class/property set stay separate despite the coarse hash colliding.

    for row in rows_with_hash:
        key = _dup_key(row)
        if key is not None:  # SQL excludes NULL; this also satisfies the typed boundary.
            hash_groups[key].append(row)

    # Rows excluded from Pass B entirely: zero classes/properties would all
    # hash to the SHA-256 of an empty string and form a false duplicate group.
    # Logged (not silently dropped) so there's a record of what Pass B never
    # got to consider.
    excluded_empty = conn.execute(
        """
        SELECT id, source_id, triple_count
        FROM corpus_registry
        WHERE parse_status = 'ok'
          AND content_hash IS NOT NULL
          AND is_duplicate = 0
          AND ((class_count + property_count) = 0 OR content_hash = ?)
        """,
        (_EMPTY_CONTENT_HASH,),
    ).fetchall()
    for row in excluded_empty:
        log_dedup_exclusion(
            exclusion_log_path,
            row["source_id"],
            "pass_b_excluded_empty",
            f"zero classes/properties (triple_count={row['triple_count']}) — "
            "would collide with every other empty ontology on the SHA-256 of "
            "an empty string, so excluded from content-hash grouping",
        )

    duplicate_groups_b = {h: rows for h, rows in hash_groups.items() if len(rows) > 1}
    print(f"  Found {len(duplicate_groups_b)} content-hash groups with duplicates")

    for h, rows in duplicate_groups_b.items():
        # A coarse signature is only corroborating evidence. Without an
        # explicit version IRI, matching hashes may be genuine releases (or
        # unrelated ontologies), so leave them canonical for Pass C/D.
        version_subgroups: dict[str | None, list] = defaultdict(list)
        for row in rows:
            version_subgroups[row["ontology_version_iri"]].append(row)

        for ver, sub in version_subgroups.items():
            if ver is None or len(sub) <= 1:
                continue
            winner_id = _elect_winner(sub)
            for row in sub:
                if row["id"] != winner_id:
                    update_duplicate(conn, row["id"], winner_id)
                    counts["pass_b_marked"] += 1
                    print(
                        f"    [dup] {row['source_registry']}#{row['id']} "
                        f"-> canonical #{winner_id} | hash={h[:16]}..."
                    )

    counts["chains_flattened"] = _flatten_canonical_chains(conn)

    # ---- Pass C: Flag the latest version per ontology ------------------
    counts["latest_flagged"] = mark_latest_versions(conn)

    # ---- Pass D: One latest row per ontology ---------------------------
    print("\n  Pass D: One latest row per ontology")
    if sidecar_path is None:
        print("  [warn] no sidecar store (--store): skipped, so copies of one")
        print("         ontology from several registries all stay latest")
        counts["latest_unflagged"] = counts["shared_iris"] = counts["iris_undecided"] = 0
    else:
        unflagged, shared, undecided = unify_latest_by_iri(
            conn, _sidecar_entities(sidecar_path), _newest_duplicate_dates(conn)
        )
        counts["latest_unflagged"], counts["shared_iris"] = unflagged, shared
        counts["iris_undecided"] = undecided
        counts["latest_flagged"] -= unflagged
        print(
            f"  {unflagged} older copies unflagged; {shared} IRIs shared by distinct "
            f"ontologies; {undecided} IRIs undecided (sidecar missing or unreadable)"
        )

    # ---- Pass E: An ontology declares something -----------------------
    counts["stubs_dropped"] = drop_stub_latest(conn, exclusion_log_path)
    counts["latest_flagged"] -= counts["stubs_dropped"]
    print(f"\n  Pass E: {counts['stubs_dropped']} latest rows declaring nothing unflagged")

    # ---- Home registry -------------------------------------------------
    counts["home_registry_moved"] = assign_home_registries(conn)
    print(
        f"\n  Home registry: {counts['home_registry_moved']} latest ontologies belong to a "
        "registry other than the one their latest copy came from"
    )

    conn.close()

    total = counts["pass_0_marked"] + counts["pass_a_marked"] + counts["pass_b_marked"]
    print("\nDedup summary:")
    print(f"  Pass 0 (exact-byte):         {counts['pass_0_marked']} rows marked")
    print(f"  Pass A (IRI-based):          {counts['pass_a_marked']} rows marked")
    print(f"  Pass B (content-hash-based): {counts['pass_b_marked']} rows marked")
    print(f"  Total duplicates marked:     {total}")
    print(f"  Duplicate chains flattened:  {counts['chains_flattened']}")
    print(f"  Latest versions flagged:     {counts['latest_flagged']}")

    return counts

mark_latest_versions(conn)

Flag the most-recent canonical release in each release family with is_latest_version=1; every other row is reset to 0, including rows that were flagged earlier and have since become duplicates or failed to parse.

Only successfully-parsed, non-duplicate rows (parse_status='ok', is_duplicate=0) are considered — those are the usable canonical versions. Experiments that don't care about history can then simply filter WHERE is_latest_version=1. Idempotent: safe to re-run.

A stub release is flagged only when every release of its family is a stub; a canonical row competes with the newest date of its in-family duplicates.

Returns the number of ontologies flagged (one winner per group).

Source code in ontometer_corpus/pipeline/dedup.py
def mark_latest_versions(conn) -> int:
    """
    Flag the most-recent canonical release in each release family with
    is_latest_version=1; every other row is reset to 0, including rows that
    were flagged earlier and have since become duplicates or failed to parse.

    Only successfully-parsed, non-duplicate rows (parse_status='ok',
    is_duplicate=0) are considered — those are the usable canonical versions.
    Experiments that don't care about history can then simply filter
    ``WHERE is_latest_version=1``. Idempotent: safe to re-run.

    A stub release is flagged only when every release of its family is a
    stub; a canonical row competes with the newest date of its in-family
    duplicates.

    Returns the number of ontologies flagged (one winner per group).
    """
    from .db import _now

    now = _now()

    # Reset the flag on every row, not only the eligible ones, so a row that a
    # later run marks as a duplicate does not keep a stale flag.
    conn.execute("UPDATE corpus_registry SET is_latest_version = 0 WHERE is_latest_version != 0")

    rows = conn.execute(
        """
        SELECT id, source_id, source_version_date, retrieval_date,
               class_count, property_count, individual_count, declared_individual_count,
               skos_concept_count
        FROM corpus_registry
        WHERE parse_status = 'ok' AND is_duplicate = 0
        """
    ).fetchall()
    inherited = _newest_duplicate_dates(conn)

    groups: dict[str, list] = defaultdict(list)
    for row in rows:
        groups[_ontology_group_key(row)].append(row)

    flagged = 0
    for _key, group in groups.items():
        winner = max(group, key=lambda r: _recency_key(r, inherited.get(r["id"], "")))
        conn.execute(
            "UPDATE corpus_registry SET is_latest_version = 1, updated_at = ? WHERE id = ?",
            (now, winner["id"]),
        )
        flagged += 1

    conn.commit()
    return flagged

ontology_key(iri)

Match key for the ontology an IRI names.

iri_key (scheme and trailing separator ignored), with an OBO build artifact mapped to its ontology's PURL: …/obo/pco/pco-full.owl, …/obo/oba/oba-base.owl, …/obo/go/extensions/go-plus.owl and …/obo/subsets/envo-basic.obo.owl all read as …/obo/<id>.owl, with the id lower-cased as OBO file names are. Other files under an OBO folder (…/obo/uberon/chebi_import.owl) keep their own key.

Source code in ontometer_corpus/pipeline/dedup.py
def ontology_key(iri: str) -> str:
    """Match key for the ontology an IRI names.

    ``iri_key`` (scheme and trailing separator ignored), with an OBO build
    artifact mapped to its ontology's PURL: ``…/obo/pco/pco-full.owl``,
    ``…/obo/oba/oba-base.owl``, ``…/obo/go/extensions/go-plus.owl`` and
    ``…/obo/subsets/envo-basic.obo.owl`` all read as ``…/obo/<id>.owl``, with
    the id lower-cased as OBO file names are. Other files under an OBO folder
    (``…/obo/uberon/chebi_import.owl``) keep their own key.
    """
    key = iri_key(iri).lstrip("/")
    if not key.lower().startswith(_OBO_PURL):
        return key
    *dirs, name = key[len(_OBO_PURL) :].lower().split("/")
    for ext in (".obo.owl", ".owl", ".obo", ".ttl"):
        if name.endswith(ext):
            name = name[: -len(ext)]
            break
    else:
        return key
    stem = _OBO_ARTIFACT.sub("", name)
    if not stem or (dirs and (dirs[0] != stem and dirs != ["subsets"])):
        return key
    if any(d not in _OBO_STAGE_DIRS for d in dirs[1:]):
        return key
    return f"{_OBO_PURL}{stem}.owl"

release_day(row)

The release date of a row: the date its version IRI adds to its ontology IRI, which the ontology itself publishes, else the registry's release date.

Source code in ontometer_corpus/pipeline/dedup.py
def release_day(row) -> str | None:
    """The release date of a row: the date its version IRI adds to its
    ontology IRI, which the ontology itself publishes, else the registry's
    release date."""
    day = _version_iri_date(row["ontology_iri"], row["ontology_version_iri"])
    if day:
        return day
    return release_date(row["source_registry_metadata"]) or (
        (row["source_version_date"] or "")[:10] or None
    )

unify_latest_by_iri(conn, entities, inherited=None, folded=None)

Pass D. Returns (rows unflagged as latest, IRIs marked shared, IRIs left undecided for want of a readable sidecar). inherited maps a canonical row id to the newest date among its in-family duplicates; folded, when given, receives each unflagged row id mapped to the row id kept for it.

An undecided IRI contributes no IRI evidence and is never marked shared; its copies can still join others through an identical term set.

Source code in ontometer_corpus/pipeline/dedup.py
def unify_latest_by_iri(
    conn,
    entities: Callable[[str | None], set[str] | None],
    inherited: dict[int, str] | None = None,
    folded: dict[int, int] | None = None,
) -> tuple[int, int, int]:
    """Pass D. Returns (rows unflagged as latest, IRIs marked shared, IRIs left
    undecided for want of a readable sidecar). ``inherited`` maps a canonical
    row id to the newest date among its in-family duplicates; ``folded``, when
    given, receives each unflagged row id mapped to the row id kept for it.

    An undecided IRI contributes no IRI evidence and is never marked shared;
    its copies can still join others through an identical term set.
    """
    inherited = inherited or {}
    from .db import _now

    now = _now()
    conn.execute("UPDATE corpus_registry SET shared_iri = 0 WHERE shared_iri != 0")
    rows = conn.execute(
        """
        SELECT id, source_id, source_registry, ontology_iri, ontology_version_iri,
               source_version_date, source_registry_metadata, file_hash, content_hash,
               class_count, property_count, individual_count, declared_individual_count,
               skos_concept_count
        FROM corpus_registry
        WHERE is_latest_version = 1
        """
    ).fetchall()
    parent = {row["id"]: row["id"] for row in rows}

    def find(i: int) -> int:
        while parent[i] != i:
            parent[i] = parent[parent[i]]
            i = parent[i]
        return i

    def union(a: int, b: int) -> None:
        parent[find(a)] = find(b)

    # IRI evidence: one group per ontology key, whatever the IRI's spelling.
    by_iri: dict[str, list] = defaultdict(list)
    for row in rows:
        if row["ontology_iri"]:
            by_iri[ontology_key(row["ontology_iri"])].append(row)
    decided: list[list] = []
    undecided = 0
    for group in by_iri.values():
        if len(group) < 2:
            continue
        iri = min(row["ontology_iri"] for row in group)
        found = {row["id"]: entities(row["file_hash"]) for row in group}
        if any(s is None for s in found.values()):
            # Without every copy's terms, merging could drop a distinct
            # ontology and splitting could mark the IRI shared.
            undecided += 1
            print(f"    [undecided] {iri}: sidecar missing or unreadable")
            continue
        sets = {i: {_term_key(t) for t in s} for i, s in found.items() if s is not None}
        for i, a in enumerate(group):
            for b in group[i + 1 :]:
                if _same_ontology(a, b, sets):
                    union(a["id"], b["id"])
        # A copy declaring nothing and carrying no concepts (a failed
        # conversion, an empty submission) is no evidence of a different
        # ontology: it joins the largest copy.
        largest = max(group, key=lambda r: len(sets[r["id"]]))
        for row in group:
            if not sets[row["id"]] and not row["skos_concept_count"]:
                union(row["id"], largest["id"])
        decided.append(group)

    # Term evidence: the identical non-empty class and property set, in any
    # canonical release. A copy equal to an earlier release of another family
    # is that family's ontology even when its latest release renamed every term.
    latest_of = {family_key(row["source_id"]): row["id"] for row in rows}
    by_terms: dict[str, list] = defaultdict(list)
    for row in conn.execute(
        """
        SELECT id, source_id, ontology_iri, content_hash, class_count, property_count,
               skos_concept_count
        FROM corpus_registry
        WHERE parse_status = 'ok' AND is_duplicate = 0
          AND content_hash IS NOT NULL AND content_hash != ?
        """,
        (_EMPTY_CONTENT_HASH,),
    ):
        if _declared_terms(row) and family_key(row["source_id"]) in latest_of:
            by_terms[row["content_hash"]].append(row)
    for group in by_terms.values():
        for i, a in enumerate(group):
            la = latest_of[family_key(a["source_id"])]
            for b in group[i + 1 :]:
                lb = latest_of[family_key(b["source_id"])]
                if find(la) != find(lb) and _same_terms(a, b):
                    union(la, lb)

    components: dict[int, list] = defaultdict(list)
    for row in rows:
        components[find(row["id"])].append(row)
    unflagged = 0
    for members in components.values():
        winner = max(members, key=lambda r: _newness_key(r, inherited.get(r["id"], "")))
        for row in members:
            if row["id"] != winner["id"]:
                conn.execute(
                    "UPDATE corpus_registry SET is_latest_version = 0, updated_at = ? WHERE id = ?",
                    (now, row["id"]),
                )
                unflagged += 1
                if folded is not None:
                    folded[row["id"]] = winner["id"]

    shared = 0
    for group in decided:
        n = len({find(row["id"]) for row in group})
        if n > 1:
            # Every row declaring any spelling of the IRI, latest or not.
            for spelling in {row["ontology_iri"] for row in group}:
                conn.execute(
                    "UPDATE corpus_registry SET shared_iri = 1, updated_at = ? "
                    "WHERE ontology_iri = ?",
                    (now, spelling),
                )
            shared += 1
            print(
                f"    [shared] {min(row['ontology_iri'] for row in group)}: {n} distinct ontologies"
            )
    conn.commit()
    return unflagged, shared, undecided

drop_stub_latest(conn, exclusion_log=None)

Pass E. Unflag every latest row that declares no class, property, individual or SKOS concept (_is_stub); returns the rows unflagged. Each is logged to exclusion_log when given.

Source code in ontometer_corpus/pipeline/dedup.py
def drop_stub_latest(conn, exclusion_log: Path | None = None) -> int:
    """Pass E. Unflag every latest row that declares no class, property,
    individual or SKOS concept (``_is_stub``); returns the rows unflagged.
    Each is logged to *exclusion_log* when given."""
    from .db import _now

    now = _now()
    rows = conn.execute(
        """
        SELECT id, source_id, triple_count, class_count, property_count, individual_count,
               declared_individual_count, skos_concept_count
        FROM corpus_registry
        WHERE is_latest_version = 1
        """
    ).fetchall()
    stubs = [row for row in rows if _is_stub(row)]
    for row in stubs:
        conn.execute(
            "UPDATE corpus_registry SET is_latest_version = 0, updated_at = ? WHERE id = ?",
            (now, row["id"]),
        )
        if exclusion_log is not None:
            log_dedup_exclusion(
                exclusion_log,
                row["source_id"],
                "pass_e_declares_nothing",
                f"no class, property, individual or concept (triple_count="
                f"{row['triple_count']}), so not counted as an ontology",
            )
    conn.commit()
    return len(stubs)