Skip to content

Commit f66fa80

Browse files
authored
Update seed.py
1 parent 928cf2b commit f66fa80

1 file changed

Lines changed: 41 additions & 57 deletions

File tree

neuro_registry/seed.py

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"""
2424

2525
from __future__ import annotations
26-
import datetime, uuid
2726
import click, httpx, rdflib
28-
import ladybug as lb
27+
28+
from db import get_connection, make_base, make_uid, now_iso, REG
2929

3030
# ---------------------------------------------------------------------------
3131
# Config
@@ -35,53 +35,50 @@
3535
"https://schema.org/version/latest/schemaorg-current-https.jsonld"
3636
)
3737

38-
REG = "https://registry.sensein.io/"
3938
SCHEMA = rdflib.Namespace("https://schema.org/")
4039
RDFS = rdflib.RDFS
4140
RDF = rdflib.RDF
4241

43-
# Top-level types to seed. Everything reachable via subClassOf from these
44-
# is also included automatically.
45-
SEED_ROOTS = [
46-
"Thing",
47-
"CreativeWork",
48-
"Event",
49-
"Organization",
50-
"Person",
51-
"Place",
52-
"Product",
53-
"Action",
54-
"MedicalEntity",
55-
"AudioObject",
56-
"ImageObject",
57-
"VideoObject",
42+
# Curated type list — explicit rather than BFS so we don't pull in
43+
# ComedyEvent, AMRadioChannel, and 900 other irrelevant schema.org types.
44+
#
45+
# Core identity
46+
SEED_ROOTS_CORE = [
47+
"Thing", "Person", "Organization",
5848
]
5949

60-
# ---------------------------------------------------------------------------
61-
# Helpers (mirrors make_base / make_uri from schema_registry.py)
62-
# ---------------------------------------------------------------------------
63-
64-
def now_iso() -> str:
65-
return datetime.datetime.now(datetime.UTC).isoformat()
50+
# Bio / neuro — MedicalEntity and BioChemEntity subtrees are fully relevant
51+
SEED_ROOTS_BIO = [
52+
"MedicalEntity",
53+
"AnatomicalStructure", "BrainStructure", "Nerve", "AnatomicalSystem",
54+
"MedicalCondition", "InfectiousDisease", "MedicalSignOrSymptom",
55+
"MedicalStudy", "MedicalObservationalStudy", "MedicalTrial",
56+
"MedicalProcedure", "DiagnosticProcedure", "TherapeuticProcedure",
57+
"MedicalTest", "ImagingTest", "BloodTest",
58+
"MedicalDevice", "Drug", "Substance",
59+
"BioChemEntity", "Gene", "Protein", "MolecularEntity", "ChemicalSubstance",
60+
]
6661

67-
def make_uid() -> str:
68-
return str(uuid.uuid4())
62+
# Research output
63+
SEED_ROOTS_RESEARCH = [
64+
"CreativeWork", "Article", "ScholarlyArticle", "Dataset",
65+
"SoftwareApplication", "SoftwareSourceCode",
66+
]
6967

70-
def make_iri(object_id: str) -> str:
71-
return f"{REG}obj/{object_id}"
68+
# Supporting
69+
SEED_ROOTS_SUPPORTING = [
70+
"Event", "ConferenceEvent", "EducationEvent",
71+
"Place",
72+
]
7273

73-
def make_uri(object_id: str, version: str = "1.0.0") -> str:
74-
return f"{REG}obj/{object_id}/v/{version}"
74+
SEED_ROOTS = (
75+
SEED_ROOTS_CORE
76+
+ SEED_ROOTS_BIO
77+
+ SEED_ROOTS_RESEARCH
78+
+ SEED_ROOTS_SUPPORTING
79+
)
7580

76-
def make_base(object_id: str, iri: str | None = None,
77-
version: str = "1.0.0") -> dict:
78-
return {
79-
"uid": make_uid(),
80-
"iri": iri or make_iri(object_id),
81-
"uri": make_uri(object_id, version),
82-
"version": version,
83-
"created_at": now_iso(),
84-
}
81+
# Helpers imported from db.py
8582

8683
# ---------------------------------------------------------------------------
8784
# Fetch + parse schema.org
@@ -101,23 +98,9 @@ def collect_classes(g: rdflib.Graph) -> dict[str, dict]:
10198
"""
10299
Return a dict keyed by short name (e.g. "Person") with:
103100
iri, label, comment, subclass_of (list of short names), props (list)
104-
Only includes classes reachable from SEED_ROOTS.
101+
Only includes types in SEED_ROOTS — no BFS expansion.
105102
"""
106-
# BFS from roots following subClassOf *upward* (child → parent already in
107-
# set) and *downward* via subjects of subClassOf pointing to our roots.
108-
wanted: set[str] = set()
109-
queue = list(SEED_ROOTS)
110-
while queue:
111-
name = queue.pop()
112-
if name in wanted:
113-
continue
114-
wanted.add(name)
115-
node = SCHEMA[name]
116-
# children: anything that declares subClassOf this node
117-
for child in g.subjects(RDFS.subClassOf, node):
118-
short = str(child).replace("https://schema.org/", "")
119-
if short not in wanted:
120-
queue.append(short)
103+
wanted: set[str] = set(SEED_ROOTS)
121104

122105
classes: dict[str, dict] = {}
123106
for name in wanted:
@@ -163,9 +146,10 @@ def collect_classes(g: rdflib.Graph) -> dict[str, dict]:
163146

164147
def seed(db_path: str = "./registry.lbug",
165148
dry_run: bool = False,
166-
wipe: bool = False) -> None:
149+
wipe: bool = False,
150+
registry_version: str = "1.0.0") -> None:
167151

168-
conn = lb.Connection(lb.Database(db_path))
152+
conn = get_connection(db_path)
169153

170154
if wipe and not dry_run:
171155
print("Wiping existing SchemaClass and SchemaProperty nodes …")

0 commit comments

Comments
 (0)