Anonymization¶
Classes of ecgdatakit.anonymize. Usage, options and the catalog are described in Anonymization.
Import: from ecgdatakit.anonymize import Anonymizer, Report, Catalog, DatasetLocked
from ecgdatakit.anonymize import Anonymizer
reports = Anonymizer("/path/to/source", datasets=True, patients_dir_name="RAW").run()
for report in reports:
print(report.dataset, report.anonymized, report.failed, report.errors)
- class ecgdatakit.anonymize.Anonymizer[source]¶
Bases:
objectAnonymize the ECG files under a source path.
Raw files are only read, never modified.
- Parameters:
source (
str|Path) – Folder to anonymize, or a single file. A file is anonymized on its own: its folder holds the output folder and the catalog.datasets (
bool) –False(default): source is one dataset.True: every sub-folder of source is a separate dataset, with its own output folder and catalog.patients_dir_name (
str|None) – Name of the folder that holds the patient folders, the same in every dataset (for example"RAW"). Only files under this folder are read. WhenNone, every file of every folder is read (except the output folder).group_by_patient_folders (
bool) –False(default): files are not grouped; each file gets its own patient pseudonym, and the patient ID field is not replaced (it may hold something else than a patient ID).True: each patient folder is one patient and its files share one patient pseudonym. The patient folders are the sub-folders of the patients_dir_name folders, or without patients_dir_name the folders directly inside the dataset. The patient ID field of a file in a patient folder is replaced by the patient pseudonym (a date is kept); a value that does not match the folder name is reported as risk.anonymize_patient_folders (
bool) –False(default): folder names are copied as they are.True(needs group_by_patient_folders): each patient folder is named after the patient pseudonym in the output, and its name is replaced wherever it appears in the files. Every other folder, sub-folders of the patient folder included, keeps its name.out_dir (
str) – Output folder created in each dataset, with the same sub-folders as the dataset.catalog_name (
str) – CSV catalog file created in each dataset.threads (
int) – Parallel workers.settle_seconds (
float) – Files modified less than this many seconds ago are left for a later run (they may still be copied).lock_stale_seconds (
float) – Age after which a lock left by a stopped run is ignored.dry_run (
bool) – Report what would be done, write nothing.
- __init__(source, datasets=False, patients_dir_name=None, group_by_patient_folders=False, anonymize_patient_folders=False, out_dir='ANONYMIZED', catalog_name='anonymization_catalog.csv', threads=8, settle_seconds=0.0, lock_stale_seconds=43200, dry_run=False)[source]¶
- run(paths=None, recursive=True, datasets=None)[source]¶
Anonymize every dataset, the named datasets, or only paths.
- stats(dataset)[source]¶
Counts of the raw files and of the anonymized files, each read from disk (see
ecgdatakit.anonymize._stats).
- class ecgdatakit.anonymize.Report[source]¶
Bases:
objectReport(dataset: ‘str’, found: ‘int’ = 0, anonymized: ‘int’ = 0, copied: ‘int’ = 0, changed: ‘int’ = 0, unchanged: ‘int’ = 0, deleted: ‘int’ = 0, failed: ‘int’ = 0, risks: ‘int’ = 0, skipped_unsupported: ‘int’ = 0, waiting: ‘int’ = 0, raw_files: ‘int’ = 0, anonymized_files: ‘int’ = 0, ecg_codes: ‘int’ = 0, patients: ‘int’ = 0, patient_codes: ‘int’ = 0, errors: ‘list[str]’ = <factory>, stats: ‘list’ = <factory>, empty_patient_folders: ‘list[str]’ = <factory>)
- patients: int = 0¶
Distinct patients of these files (patient folders, plus files outside a patient folder, each counted as one patient).
- stats: list¶
Counts of the raw and anonymized files of the whole dataset, read from disk (see
Anonymizer.stats()), also written toanonymization_stats.csv.
- empty_patient_folders: list[str]¶
nothing is copied for them.
- Type:
Raw patient folders holding no ECG file
- __init__(dataset, found=0, anonymized=0, copied=0, changed=0, unchanged=0, deleted=0, failed=0, risks=0, skipped_unsupported=0, waiting=0, raw_files=0, anonymized_files=0, ecg_codes=0, patients=0, patient_codes=0, errors=<factory>, stats=<factory>, empty_patient_folders=<factory>)¶
- class ecgdatakit.anonymize.Catalog[source]¶
Bases:
objectRows keyed by raw path (relative to the dataset folder).
Writes go to a temporary file renamed over the catalog, so the CSV is never left half written.
- exception ecgdatakit.anonymize.DatasetLocked[source]¶
Bases:
RuntimeErrorAnother process is anonymizing this dataset.