-
Notifications
You must be signed in to change notification settings - Fork 0
Add WFDB Support for PhysioNet Waveform Datasets #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """WFDB file handler for physiological waveform data.""" | ||
|
|
||
| from pathlib import Path | ||
| import wfdb | ||
|
|
||
| from croissant_maker.handlers.base_handler import FileTypeHandler | ||
| from croissant_maker.handlers.utils import compute_file_hash | ||
|
|
||
|
|
||
| class WFDBHandler(FileTypeHandler): | ||
| """ | ||
| Handler for WFDB format files (PhysioNet waveform databases). | ||
|
|
||
| WFDB records consist of multiple related files: | ||
| - .hea: Header file (text, contains metadata) | ||
| - .dat: Data file (binary, contains signals) | ||
| - .atr: Annotation file (optional, contains beat/event annotations) | ||
|
|
||
| The 'related_files' pattern in extract_metadata() allows handlers to indicate | ||
| that multiple physical files form a single logical record. This is a general | ||
| capability that other multi-file formats (DICOM series, HDF5 with external | ||
| links, Parquet partitions) can also use. Each related file becomes a separate | ||
| cr:FileObject in the Croissant metadata, but they all describe one RecordSet. | ||
| """ | ||
|
|
||
| def can_handle(self, file_path: Path) -> bool: | ||
| return file_path.suffix.lower() == ".hea" | ||
|
|
||
| def extract_metadata(self, file_path: Path) -> dict: | ||
| if not file_path.exists(): | ||
| raise FileNotFoundError(f"WFDB header file not found: {file_path}") | ||
|
|
||
| record_path = file_path.with_suffix("") | ||
| record = wfdb.rdheader(str(record_path)) | ||
|
|
||
| dat_file = file_path.with_suffix(".dat") | ||
| if not dat_file.exists(): | ||
| raise ValueError(f"WFDB data file missing for header: {file_path}") | ||
|
|
||
| # Build list of related files that form this logical WFDB record. | ||
| # Each becomes a separate cr:FileObject in Croissant metadata. | ||
| related_files = [ | ||
| { | ||
| "path": str(dat_file), | ||
| "name": dat_file.name, | ||
| "type": "data", | ||
| "encoding": "application/x-wfdb-data", | ||
| "size": dat_file.stat().st_size, | ||
| "sha256": compute_file_hash(dat_file), | ||
| } | ||
| ] | ||
|
|
||
| atr_file = file_path.with_suffix(".atr") | ||
| if atr_file.exists(): | ||
| related_files.append( | ||
| { | ||
| "path": str(atr_file), | ||
| "name": atr_file.name, | ||
| "type": "annotation", | ||
| "encoding": "application/x-wfdb-annotation", | ||
| "size": atr_file.stat().st_size, | ||
| "sha256": compute_file_hash(atr_file), | ||
| } | ||
| ) | ||
|
|
||
| signal_types = {sig: "sc:Float" for sig in record.sig_name} | ||
| duration_seconds = record.sig_len / record.fs if record.fs > 0 else 0 | ||
|
|
||
| metadata = { | ||
| "file_path": str(file_path), | ||
| "file_name": file_path.name, | ||
| "file_size": file_path.stat().st_size, | ||
| "sha256": compute_file_hash(file_path), | ||
| "encoding_format": "application/x-wfdb-header", | ||
| "record_name": record.record_name, | ||
| "related_files": related_files, | ||
| "signal_names": record.sig_name, | ||
| "signal_types": signal_types, | ||
| "units": record.units, | ||
| "sampling_frequency": record.fs, | ||
| "num_samples": record.sig_len, | ||
| "num_signals": record.n_sig, | ||
| "duration_seconds": duration_seconds, | ||
| "comments": record.comments if record.comments else [], | ||
| } | ||
|
|
||
| if hasattr(record, "base_datetime") and record.base_datetime: | ||
| metadata["base_datetime"] = record.base_datetime.isoformat() | ||
| if hasattr(record, "base_date") and record.base_date: | ||
| metadata["base_date"] = str(record.base_date) | ||
| if hasattr(record, "base_time") and record.base_time: | ||
| metadata["base_time"] = str(record.base_time) | ||
| if hasattr(record, "adc_gain") and record.adc_gain: | ||
| metadata["adc_gain"] = record.adc_gain | ||
| if hasattr(record, "baseline") and record.baseline: | ||
| metadata["baseline"] = record.baseline | ||
| if hasattr(record, "init_value") and record.init_value: | ||
| metadata["init_value"] = record.init_value | ||
| if hasattr(record, "checksum") and record.checksum: | ||
| metadata["checksum"] = record.checksum | ||
| if hasattr(record, "fmt") and record.fmt: | ||
| metadata["fmt"] = record.fmt | ||
|
|
||
| return metadata |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.