wfdb 5.2.0__tar.gz → 5.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wfdb-5.3.0/PKG-INFO +143 -0
- wfdb-5.3.0/README.md +117 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/pyproject.toml +6 -3
- {wfdb-5.2.0 → wfdb-5.3.0}/pyproject.toml.orig +6 -3
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/__init__.py +1 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/__init__.py +2 -0
- wfdb-5.3.0/src/wfdb/io/_cloud.py +60 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/_header.py +2 -2
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/annotation.py +9 -2
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/convert/edf.py +201 -89
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/record.py +470 -65
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/plot/plot.py +60 -12
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/basic.py +6 -10
- wfdb-5.2.0/PKG-INFO +0 -83
- wfdb-5.2.0/README.md +0 -60
- {wfdb-5.2.0 → wfdb-5.3.0}/LICENSE +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/_coreio.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/_signal.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/_url.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/archive.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/convert/__init__.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/convert/csv.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/convert/matlab.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/convert/tff.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/convert/wav.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/datasource.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/download.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/header.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/io/util.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/plot/__init__.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/__init__.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/beat_comparison.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/evaluate.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/filter.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/hr.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/peaks.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/processing/qrs.py +0 -0
- {wfdb-5.2.0 → wfdb-5.3.0}/src/wfdb/version.py +0 -0
wfdb-5.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wfdb
|
|
3
|
+
Version: 5.3.0
|
|
4
|
+
Summary: Read, write, plot, and process physiologic waveforms and annotations.
|
|
5
|
+
Author: The Laboratory for Computational Physiology
|
|
6
|
+
Author-email: The Laboratory for Computational Physiology <contact@physionet.org>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: numpy>=1.26.4
|
|
10
|
+
Requires-Dist: scipy>=1.13.0
|
|
11
|
+
Requires-Dist: pandas>=2.2.3
|
|
12
|
+
Requires-Dist: soundfile>=0.13.1
|
|
13
|
+
Requires-Dist: matplotlib>=3.8.0
|
|
14
|
+
Requires-Dist: requests>=2.31.0
|
|
15
|
+
Requires-Dist: fsspec>=2025.7.0
|
|
16
|
+
Requires-Dist: aiohttp>=3.10.11
|
|
17
|
+
Requires-Dist: physionet>=0.7.2
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Project-URL: homepage, https://github.com/MIT-LCP/wfdb-python/
|
|
20
|
+
Project-URL: repository, https://github.com/MIT-LCP/wfdb-python/
|
|
21
|
+
Project-URL: documentation, https://wfdb.readthedocs.io/
|
|
22
|
+
Project-URL: wfdb-project, https://wfdb.io/
|
|
23
|
+
Project-URL: issues, https://github.com/MIT-LCP/wfdb-python/issues
|
|
24
|
+
Project-URL: changelog, https://wfdb.readthedocs.io/en/latest/changes.html
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# WFDB for Python
|
|
28
|
+
|
|
29
|
+
Read, write, plot, and process physiologic waveforms and annotations with
|
|
30
|
+
Python. WFDB works with local files and records hosted on
|
|
31
|
+
[PhysioNet](https://physionet.org/), so you can move from a published dataset
|
|
32
|
+
to NumPy arrays in a few lines of code.
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/wfdb/)
|
|
35
|
+
[](https://wfdb.readthedocs.io/)
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
WFDB requires Python 3.11 or newer. Install it from PyPI:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
pip install wfdb
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
If you use [uv](https://docs.astral.sh/uv/), run `uv add wfdb` instead. On
|
|
48
|
+
some systems, the `soundfile` dependency also needs a separate installation
|
|
49
|
+
of `libsndfile`; see the
|
|
50
|
+
[soundfile installation notes](https://pypi.org/project/soundfile/).
|
|
51
|
+
|
|
52
|
+
## Quick start
|
|
53
|
+
|
|
54
|
+
Read ten seconds of ECG and its beat annotations from the
|
|
55
|
+
[MIT-BIH Arrhythmia Database](https://physionet.org/content/mitdb/):
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import wfdb
|
|
59
|
+
|
|
60
|
+
record = wfdb.rdrecord("100", pn_dir="mitdb", sampto=3600)
|
|
61
|
+
annotation = wfdb.rdann("100", "atr", pn_dir="mitdb", sampto=3600)
|
|
62
|
+
|
|
63
|
+
print(record.fs) # Sampling frequency: 360 Hz
|
|
64
|
+
print(record.sig_name) # ['MLII', 'V5']
|
|
65
|
+
print(record.p_signal.shape) # (3600, 2): samples x channels
|
|
66
|
+
print(annotation.sample[:5]) # Sample positions of the first five annotations
|
|
67
|
+
|
|
68
|
+
wfdb.plot_wfdb(record=record, annotation=annotation, title="Record 100")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The `pn_dir` argument downloads data from PhysioNet. For files already on
|
|
72
|
+
your computer, pass the path without its `.hea` or `.dat` extension instead:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
record = wfdb.rdrecord("path/to/record")
|
|
76
|
+
annotation = wfdb.rdann("path/to/record", "atr")
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`record.p_signal` is a NumPy array of signals in physical units. Use
|
|
80
|
+
`physical=False` with `rdrecord` to read digital values in `record.d_signal`.
|
|
81
|
+
Annotations provide sample positions, symbols, and other label information.
|
|
82
|
+
|
|
83
|
+
## Write a record
|
|
84
|
+
|
|
85
|
+
Create a WFDB header and signal file from a NumPy array:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
import numpy as np
|
|
89
|
+
import wfdb
|
|
90
|
+
|
|
91
|
+
signal = np.array([0.0, 0.2, 0.0, -0.2])[:, None]
|
|
92
|
+
wfdb.wrsamp(
|
|
93
|
+
"example",
|
|
94
|
+
fs=250,
|
|
95
|
+
units=["mV"],
|
|
96
|
+
sig_name=["ECG"],
|
|
97
|
+
p_signal=signal,
|
|
98
|
+
fmt=["16"],
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
This writes `example.hea` and `example.dat` in the current directory. See
|
|
103
|
+
[`wrann`](https://wfdb.readthedocs.io/en/latest/io.html#wfdb.io.wrann)
|
|
104
|
+
to write annotations.
|
|
105
|
+
|
|
106
|
+
## What else can you do?
|
|
107
|
+
|
|
108
|
+
- **Work with remote data:** Read PhysioNet records with `pn_dir` or a
|
|
109
|
+
`physionet://` URI. Supported cloud URIs include `s3://` and `gs://` when
|
|
110
|
+
the corresponding `fsspec` backend is installed.
|
|
111
|
+
- **Use WFDB archives:** Read local `.wfdb` archives containing a record and
|
|
112
|
+
its annotations. See the
|
|
113
|
+
[archive documentation](https://wfdb.readthedocs.io/en/latest/io.html#wfdb-archives).
|
|
114
|
+
- **Analyze signals:** Use the
|
|
115
|
+
[processing tools](https://wfdb.readthedocs.io/en/latest/processing.html)
|
|
116
|
+
for tasks such as resampling and QRS detection.
|
|
117
|
+
- **Convert formats:** See the
|
|
118
|
+
[conversion guide](https://wfdb.readthedocs.io/en/latest/convert.html)
|
|
119
|
+
for supported waveform formats.
|
|
120
|
+
|
|
121
|
+
WFDB-Python follows the open
|
|
122
|
+
[WFDB specifications](https://github.com/wfdb/wfdb-spec/). It is inspired by
|
|
123
|
+
the original [WFDB Software Package](https://physionet.org/content/wfdb/),
|
|
124
|
+
but its Python API is documented separately. The
|
|
125
|
+
[WFDB project website](https://wfdb.io/) introduces the format and the
|
|
126
|
+
other WFDB implementations.
|
|
127
|
+
|
|
128
|
+
## Documentation and help
|
|
129
|
+
|
|
130
|
+
The [documentation](https://wfdb.readthedocs.io/) covers the full
|
|
131
|
+
[I/O API](https://wfdb.readthedocs.io/en/latest/io.html),
|
|
132
|
+
[plotting](https://wfdb.readthedocs.io/en/latest/plot.html), and
|
|
133
|
+
[processing](https://wfdb.readthedocs.io/en/latest/processing.html). The
|
|
134
|
+
[demo notebook](https://github.com/MIT-LCP/wfdb-python/blob/main/demo.ipynb)
|
|
135
|
+
shows more examples. Report bugs or request features in the
|
|
136
|
+
[issue tracker](https://github.com/MIT-LCP/wfdb-python/issues).
|
|
137
|
+
|
|
138
|
+
## Contributing and citing
|
|
139
|
+
|
|
140
|
+
See [DEVELOPING.md](https://github.com/MIT-LCP/wfdb-python/blob/main/DEVELOPING.md)
|
|
141
|
+
for setup, testing, and contribution guidance. If you use WFDB-Python in
|
|
142
|
+
research, please cite its
|
|
143
|
+
[PhysioNet publication](https://physionet.org/content/wfdb-python/).
|
wfdb-5.3.0/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# WFDB for Python
|
|
2
|
+
|
|
3
|
+
Read, write, plot, and process physiologic waveforms and annotations with
|
|
4
|
+
Python. WFDB works with local files and records hosted on
|
|
5
|
+
[PhysioNet](https://physionet.org/), so you can move from a published dataset
|
|
6
|
+
to NumPy arrays in a few lines of code.
|
|
7
|
+
|
|
8
|
+
[](https://pypi.org/project/wfdb/)
|
|
9
|
+
[](https://wfdb.readthedocs.io/)
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
WFDB requires Python 3.11 or newer. Install it from PyPI:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pip install wfdb
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
If you use [uv](https://docs.astral.sh/uv/), run `uv add wfdb` instead. On
|
|
22
|
+
some systems, the `soundfile` dependency also needs a separate installation
|
|
23
|
+
of `libsndfile`; see the
|
|
24
|
+
[soundfile installation notes](https://pypi.org/project/soundfile/).
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
Read ten seconds of ECG and its beat annotations from the
|
|
29
|
+
[MIT-BIH Arrhythmia Database](https://physionet.org/content/mitdb/):
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import wfdb
|
|
33
|
+
|
|
34
|
+
record = wfdb.rdrecord("100", pn_dir="mitdb", sampto=3600)
|
|
35
|
+
annotation = wfdb.rdann("100", "atr", pn_dir="mitdb", sampto=3600)
|
|
36
|
+
|
|
37
|
+
print(record.fs) # Sampling frequency: 360 Hz
|
|
38
|
+
print(record.sig_name) # ['MLII', 'V5']
|
|
39
|
+
print(record.p_signal.shape) # (3600, 2): samples x channels
|
|
40
|
+
print(annotation.sample[:5]) # Sample positions of the first five annotations
|
|
41
|
+
|
|
42
|
+
wfdb.plot_wfdb(record=record, annotation=annotation, title="Record 100")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The `pn_dir` argument downloads data from PhysioNet. For files already on
|
|
46
|
+
your computer, pass the path without its `.hea` or `.dat` extension instead:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
record = wfdb.rdrecord("path/to/record")
|
|
50
|
+
annotation = wfdb.rdann("path/to/record", "atr")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`record.p_signal` is a NumPy array of signals in physical units. Use
|
|
54
|
+
`physical=False` with `rdrecord` to read digital values in `record.d_signal`.
|
|
55
|
+
Annotations provide sample positions, symbols, and other label information.
|
|
56
|
+
|
|
57
|
+
## Write a record
|
|
58
|
+
|
|
59
|
+
Create a WFDB header and signal file from a NumPy array:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import numpy as np
|
|
63
|
+
import wfdb
|
|
64
|
+
|
|
65
|
+
signal = np.array([0.0, 0.2, 0.0, -0.2])[:, None]
|
|
66
|
+
wfdb.wrsamp(
|
|
67
|
+
"example",
|
|
68
|
+
fs=250,
|
|
69
|
+
units=["mV"],
|
|
70
|
+
sig_name=["ECG"],
|
|
71
|
+
p_signal=signal,
|
|
72
|
+
fmt=["16"],
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
This writes `example.hea` and `example.dat` in the current directory. See
|
|
77
|
+
[`wrann`](https://wfdb.readthedocs.io/en/latest/io.html#wfdb.io.wrann)
|
|
78
|
+
to write annotations.
|
|
79
|
+
|
|
80
|
+
## What else can you do?
|
|
81
|
+
|
|
82
|
+
- **Work with remote data:** Read PhysioNet records with `pn_dir` or a
|
|
83
|
+
`physionet://` URI. Supported cloud URIs include `s3://` and `gs://` when
|
|
84
|
+
the corresponding `fsspec` backend is installed.
|
|
85
|
+
- **Use WFDB archives:** Read local `.wfdb` archives containing a record and
|
|
86
|
+
its annotations. See the
|
|
87
|
+
[archive documentation](https://wfdb.readthedocs.io/en/latest/io.html#wfdb-archives).
|
|
88
|
+
- **Analyze signals:** Use the
|
|
89
|
+
[processing tools](https://wfdb.readthedocs.io/en/latest/processing.html)
|
|
90
|
+
for tasks such as resampling and QRS detection.
|
|
91
|
+
- **Convert formats:** See the
|
|
92
|
+
[conversion guide](https://wfdb.readthedocs.io/en/latest/convert.html)
|
|
93
|
+
for supported waveform formats.
|
|
94
|
+
|
|
95
|
+
WFDB-Python follows the open
|
|
96
|
+
[WFDB specifications](https://github.com/wfdb/wfdb-spec/). It is inspired by
|
|
97
|
+
the original [WFDB Software Package](https://physionet.org/content/wfdb/),
|
|
98
|
+
but its Python API is documented separately. The
|
|
99
|
+
[WFDB project website](https://wfdb.io/) introduces the format and the
|
|
100
|
+
other WFDB implementations.
|
|
101
|
+
|
|
102
|
+
## Documentation and help
|
|
103
|
+
|
|
104
|
+
The [documentation](https://wfdb.readthedocs.io/) covers the full
|
|
105
|
+
[I/O API](https://wfdb.readthedocs.io/en/latest/io.html),
|
|
106
|
+
[plotting](https://wfdb.readthedocs.io/en/latest/plot.html), and
|
|
107
|
+
[processing](https://wfdb.readthedocs.io/en/latest/processing.html). The
|
|
108
|
+
[demo notebook](https://github.com/MIT-LCP/wfdb-python/blob/main/demo.ipynb)
|
|
109
|
+
shows more examples. Report bugs or request features in the
|
|
110
|
+
[issue tracker](https://github.com/MIT-LCP/wfdb-python/issues).
|
|
111
|
+
|
|
112
|
+
## Contributing and citing
|
|
113
|
+
|
|
114
|
+
See [DEVELOPING.md](https://github.com/MIT-LCP/wfdb-python/blob/main/DEVELOPING.md)
|
|
115
|
+
for setup, testing, and contribution guidance. If you use WFDB-Python in
|
|
116
|
+
research, please cite its
|
|
117
|
+
[PhysioNet publication](https://physionet.org/content/wfdb-python/).
|
|
@@ -4,8 +4,8 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "wfdb"
|
|
7
|
-
version = "5.
|
|
8
|
-
description = "
|
|
7
|
+
version = "5.3.0"
|
|
8
|
+
description = "Read, write, plot, and process physiologic waveforms and annotations."
|
|
9
9
|
license = "MIT"
|
|
10
10
|
license-files = ["LICENSE"]
|
|
11
11
|
readme = "README.md"
|
|
@@ -14,7 +14,7 @@ dependencies = [
|
|
|
14
14
|
"numpy >= 1.26.4",
|
|
15
15
|
"scipy >= 1.13.0",
|
|
16
16
|
"pandas >= 2.2.3",
|
|
17
|
-
"soundfile >= 0.
|
|
17
|
+
"soundfile >= 0.13.1",
|
|
18
18
|
"matplotlib >= 3.8.0",
|
|
19
19
|
"requests >= 2.31.0",
|
|
20
20
|
"fsspec >= 2025.7.0",
|
|
@@ -30,6 +30,9 @@ email = "contact@physionet.org"
|
|
|
30
30
|
homepage = "https://github.com/MIT-LCP/wfdb-python/"
|
|
31
31
|
repository = "https://github.com/MIT-LCP/wfdb-python/"
|
|
32
32
|
documentation = "https://wfdb.readthedocs.io/"
|
|
33
|
+
wfdb-project = "https://wfdb.io/"
|
|
34
|
+
issues = "https://github.com/MIT-LCP/wfdb-python/issues"
|
|
35
|
+
changelog = "https://wfdb.readthedocs.io/en/latest/changes.html"
|
|
33
36
|
|
|
34
37
|
[dependency-groups]
|
|
35
38
|
dev = [
|
|
@@ -4,8 +4,8 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "wfdb"
|
|
7
|
-
version = "5.
|
|
8
|
-
description = "
|
|
7
|
+
version = "5.3.0"
|
|
8
|
+
description = "Read, write, plot, and process physiologic waveforms and annotations."
|
|
9
9
|
authors = [{name = "The Laboratory for Computational Physiology", email = "contact@physionet.org"}]
|
|
10
10
|
license = "MIT"
|
|
11
11
|
license-files = ["LICENSE"]
|
|
@@ -15,7 +15,7 @@ dependencies = [
|
|
|
15
15
|
"numpy >= 1.26.4",
|
|
16
16
|
"scipy >= 1.13.0",
|
|
17
17
|
"pandas >= 2.2.3",
|
|
18
|
-
"soundfile >= 0.
|
|
18
|
+
"soundfile >= 0.13.1",
|
|
19
19
|
"matplotlib >= 3.8.0",
|
|
20
20
|
"requests >= 2.31.0",
|
|
21
21
|
"fsspec >= 2025.7.0",
|
|
@@ -27,6 +27,9 @@ dependencies = [
|
|
|
27
27
|
homepage = "https://github.com/MIT-LCP/wfdb-python/"
|
|
28
28
|
repository = "https://github.com/MIT-LCP/wfdb-python/"
|
|
29
29
|
documentation = "https://wfdb.readthedocs.io/"
|
|
30
|
+
wfdb-project = "https://wfdb.io/"
|
|
31
|
+
issues = "https://github.com/MIT-LCP/wfdb-python/issues"
|
|
32
|
+
changelog = "https://wfdb.readthedocs.io/en/latest/changes.html"
|
|
30
33
|
|
|
31
34
|
[dependency-groups]
|
|
32
35
|
dev = [
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Write WFDB files to cloud filesystems through fsspec."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import shutil
|
|
5
|
+
import tempfile
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
|
|
8
|
+
from wfdb.io._coreio import CLOUD_PROTOCOLS, _fsspec_open, _join_path
|
|
9
|
+
from wfdb.io.archive import WFDBArchive
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def is_cloud_path(path):
|
|
13
|
+
"""Return whether a path uses one of the supported cloud protocols."""
|
|
14
|
+
return os.fspath(path).startswith(tuple(CLOUD_PROTOCOLS))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def check_cloud_write(write_dir, wfdb_archive=None):
|
|
18
|
+
"""Reject unsupported cloud destinations before any remote lookup."""
|
|
19
|
+
if os.fspath(write_dir).startswith("physionet://"):
|
|
20
|
+
raise ValueError("Writing to physionet:// is not supported")
|
|
21
|
+
if isinstance(wfdb_archive, (str, os.PathLike)) and is_cloud_path(wfdb_archive):
|
|
22
|
+
raise ValueError("Writing .wfdb archives to cloud storage is unsupported")
|
|
23
|
+
if is_cloud_path(write_dir) and wfdb_archive is not None:
|
|
24
|
+
if isinstance(wfdb_archive, WFDBArchive):
|
|
25
|
+
raise ValueError("A cloud write_dir is unused with an open WFDBArchive")
|
|
26
|
+
raise ValueError("Writing .wfdb archives to cloud storage is unsupported")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@contextmanager
|
|
30
|
+
def staged_cloud_write(write_dir, header_name=None):
|
|
31
|
+
"""Stage files locally and upload them to a cloud directory on success.
|
|
32
|
+
|
|
33
|
+
Signal and annotation files are uploaded before headers. The main record
|
|
34
|
+
header, if supplied, is uploaded last so readers do not discover an
|
|
35
|
+
incomplete new record while its files are still being uploaded.
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
with tempfile.TemporaryDirectory(prefix="wfdb-cloud-") as staging:
|
|
39
|
+
yield staging
|
|
40
|
+
paths = []
|
|
41
|
+
for root, _, names in os.walk(staging):
|
|
42
|
+
for name in names:
|
|
43
|
+
local_path = os.path.join(root, name)
|
|
44
|
+
relative_path = os.path.relpath(local_path, staging).replace(
|
|
45
|
+
os.sep, "/"
|
|
46
|
+
)
|
|
47
|
+
paths.append((relative_path, local_path))
|
|
48
|
+
|
|
49
|
+
paths.sort(
|
|
50
|
+
key=lambda item: (
|
|
51
|
+
item[0] == header_name,
|
|
52
|
+
item[0].endswith(".hea"),
|
|
53
|
+
item[0],
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
for relative_path, local_path in paths:
|
|
57
|
+
remote_path = _join_path(os.fspath(write_dir), relative_path)
|
|
58
|
+
with open(local_path, "rb") as source:
|
|
59
|
+
with _fsspec_open(remote_path, "wb") as destination:
|
|
60
|
+
shutil.copyfileobj(source, destination)
|
|
@@ -122,8 +122,8 @@ SEGMENT_SPECS = pd.DataFrame(
|
|
|
122
122
|
columns=_SPECIFICATION_COLUMNS,
|
|
123
123
|
dtype="object",
|
|
124
124
|
data=[
|
|
125
|
-
[(str,
|
|
126
|
-
[
|
|
125
|
+
[(str,), "", None, True, None, None], # seg_name
|
|
126
|
+
[int_types, " ", "seg_name", True, None, None], # seg_len
|
|
127
127
|
],
|
|
128
128
|
)
|
|
129
129
|
|
|
@@ -7,6 +7,7 @@ import numpy as np
|
|
|
7
7
|
import pandas as pd
|
|
8
8
|
|
|
9
9
|
from wfdb.io import _header, download, record, util
|
|
10
|
+
from wfdb.io._cloud import check_cloud_write, is_cloud_path, staged_cloud_write
|
|
10
11
|
from wfdb.io._coreio import _fsspec_open
|
|
11
12
|
from wfdb.io.archive import WFDBArchive, is_archive_path
|
|
12
13
|
|
|
@@ -290,13 +291,19 @@ class Annotation:
|
|
|
290
291
|
write_fs : bool, optional
|
|
291
292
|
Whether to write the `fs` attribute to the file.
|
|
292
293
|
write_dir : str, optional
|
|
293
|
-
The
|
|
294
|
+
The local directory or cloud URI in which to write the file.
|
|
294
295
|
|
|
295
296
|
Returns
|
|
296
297
|
-------
|
|
297
298
|
N/A
|
|
298
299
|
|
|
299
300
|
"""
|
|
301
|
+
if is_cloud_path(write_dir):
|
|
302
|
+
check_cloud_write(write_dir)
|
|
303
|
+
with staged_cloud_write(write_dir) as staging:
|
|
304
|
+
self.wrann(write_fs=write_fs, write_dir=staging)
|
|
305
|
+
return
|
|
306
|
+
|
|
300
307
|
for field in ["record_name", "extension"]:
|
|
301
308
|
if getattr(self, field) is None:
|
|
302
309
|
raise Exception(
|
|
@@ -1837,7 +1844,7 @@ def wrann(
|
|
|
1837
1844
|
`custom_labels` is defined, `custom_labels` must contain `label_store`
|
|
1838
1845
|
in its mapping. ie. it must come in format 1 or 3 above.
|
|
1839
1846
|
write_dir : str, optional
|
|
1840
|
-
The directory in which to write the annotation file
|
|
1847
|
+
The local directory or cloud URI in which to write the annotation file.
|
|
1841
1848
|
|
|
1842
1849
|
Returns
|
|
1843
1850
|
-------
|