79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
import sqlite3
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
import edfio
|
|
import numpy as np
|
|
|
|
SEX_MAP = {
|
|
"resourceType": "ConceptMap",
|
|
"status": "active",
|
|
"group": [
|
|
{
|
|
"source": "urn:edf",
|
|
"target": "http://hl7.org/fhir/administrative-gender",
|
|
"element": [
|
|
{"code": "F", "target": [{"code": "female", "equivalence": "equivalent"}]},
|
|
{"code": "M", "target": [{"code": "male", "equivalence": "equivalent"}]},
|
|
],
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
def small_edf(
|
|
path: Path,
|
|
*,
|
|
patient_code="N1001",
|
|
sex="F",
|
|
birthdate=date(2025, 1, 5),
|
|
startdate=date(2026, 3, 2),
|
|
record_code="LAB-1",
|
|
seconds=20,
|
|
channels=("Fp1", "Fp2", "Cz"),
|
|
) -> bytes:
|
|
edf = edfio.Edf(
|
|
[
|
|
edfio.EdfSignal(
|
|
np.random.default_rng(1).normal(0, 20, 100 * seconds),
|
|
sampling_frequency=100,
|
|
label=name,
|
|
)
|
|
for name in channels
|
|
],
|
|
patient=edfio.Patient(code=patient_code, sex=sex, birthdate=birthdate),
|
|
recording=edfio.Recording(startdate=startdate, hospital_administration_code=record_code),
|
|
)
|
|
edf.write(path)
|
|
return path.read_bytes()
|
|
|
|
|
|
def hospital(path: Path) -> str:
|
|
with sqlite3.connect(path) as con:
|
|
con.execute("create table pacientes (record_number text primary key, birth_date text)")
|
|
con.executemany("insert into pacientes values (?, ?)",
|
|
[("N1001", "2025-01-05"), ("N1002", "2025-02-05")]) # fmt: skip
|
|
return f"sqlite:///{path}"
|
|
|
|
|
|
def pipeline(hospital_url: str, bucket_url: str) -> dict:
|
|
return {"nodes": [
|
|
{"id": "patients", "kind": "source", "source": "dlt.sources.sql_database:sql_table",
|
|
"args": {"credentials": hospital_url, "table": "pacientes"}},
|
|
{"id": "patients_identity", "kind": "map", "inputs": ["patients"],
|
|
"fields": {"record_number": "record_number", "birth_date": "birth_date"},
|
|
"formats": {"birth_date": "%Y-%m-%d"}},
|
|
{"id": "uploads", "kind": "source", "source": "dlt.sources.filesystem:filesystem",
|
|
"args": {"bucket_url": bucket_url, "file_glob": "*.edf"},
|
|
"then": "persistance.readers:read_bytes"},
|
|
{"id": "header", "kind": "edf", "inputs": ["uploads"], "column": "content"},
|
|
{"id": "uploads_identity", "kind": "map", "inputs": ["header"], "concept_map": "edf_sex",
|
|
"fields": {"record_number": "edf_patient_code", "birth_date": "edf_birth_date",
|
|
"sex": "edf_sex"}, "formats": {"birth_date": "%Y-%m-%d"}},
|
|
{"id": "eegs", "kind": "map", "inputs": ["header"],
|
|
"fields": {"study_date": "edf_study_date", "lab_record_id": "edf_record_code",
|
|
"edf": "content"}, "formats": {"study_date": "%Y-%m-%d"}},
|
|
{"id": "people", "kind": "link", "inputs": ["patients_identity", "uploads_identity"]},
|
|
{"id": "target", "kind": "target", "inputs": ["eegs", "people"], "entity": "eeg"},
|
|
]} # fmt: skip
|