-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmisc.py
More file actions
926 lines (804 loc) · 28.9 KB
/
misc.py
File metadata and controls
926 lines (804 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
""" """
import datetime
import json
import logging
import os
import random
import re
import sys
from collections import namedtuple
from copy import deepcopy
from functools import reduce
from glob import glob
from numbers import Number, Real
from typing import Any, Dict, List, Optional, Sequence, Union
import numpy as np
np.set_printoptions(precision=5, suppress=True)
from easydict import EasyDict as ED
from sklearn.utils import compute_class_weight
from wfdb import MultiRecord, Record
from wfdb.io import _header
__all__ = [
"get_record_list_recursive",
"get_record_list_recursive2",
"get_record_list_recursive3",
"dict_to_str",
"str2bool",
"diff_with_step",
"ms2samples",
"samples2ms",
"get_mask",
"class_weight_to_sample_weight",
"plot_single_lead",
"init_logger",
"get_date_str",
"rdheader",
"ECGWaveForm",
"masks_to_waveforms",
"mask_to_intervals",
"nildent",
"list_sum",
"save_dict",
"uniform",
"WFDB_Beat_Annotations",
"WFDB_Non_Beat_Annotations",
"WFDB_Rhythm_Annotations",
]
def get_record_list_recursive(db_dir: str, rec_ext: str) -> List[str]:
"""finished, checked,
get the list of records in `db_dir` recursively,
for example, there are two folders "patient1", "patient2" in `db_dir`,
and there are records "A0001", "A0002", ... in "patient1"; "B0001", "B0002", ... in "patient2",
then the output would be "patient1{sep}A0001", ..., "patient2{sep}B0001", ...,
sep is determined by the system
Parameters
----------
db_dir: str,
the parent (root) path of the whole database
rec_ext: str,
extension of the record files
Returns
-------
res: list of str,
list of records, in lexicographical order
"""
res = []
db_dir = os.path.join(db_dir, "tmp").replace("tmp", "") # make sure `db_dir` ends with a sep
roots = [db_dir]
while len(roots) > 0:
new_roots = []
for r in roots:
tmp = [os.path.join(r, item) for item in os.listdir(r)]
res += [item for item in tmp if os.path.isfile(item)]
new_roots += [item for item in tmp if os.path.isdir(item)]
roots = deepcopy(new_roots)
res = [os.path.splitext(item)[0].replace(db_dir, "") for item in res if item.endswith(rec_ext)]
res = sorted(res)
return res
def get_record_list_recursive2(db_dir: str, rec_pattern: str) -> List[str]:
"""finished, checked,
get the list of records in `db_dir` recursively,
for example, there are two folders "patient1", "patient2" in `db_dir`,
and there are records "A0001", "A0002", ... in "patient1"; "B0001", "B0002", ... in "patient2",
then the output would be "patient1{sep}A0001", ..., "patient2{sep}B0001", ...,
sep is determined by the system
Parameters
----------
db_dir: str,
the parent (root) path of the whole database
rec_pattern: str,
pattern of the record filenames, e.g. "A*.mat"
Returns
-------
res: list of str,
list of records, in lexicographical order
"""
res = []
db_dir = os.path.join(db_dir, "tmp").replace("tmp", "") # make sure `db_dir` ends with a sep
roots = [db_dir]
while len(roots) > 0:
new_roots = []
for r in roots:
tmp = [os.path.join(r, item) for item in os.listdir(r)]
# res += [item for item in tmp if os.path.isfile(item)]
res += glob(os.path.join(r, rec_pattern), recursive=False)
new_roots += [item for item in tmp if os.path.isdir(item)]
roots = deepcopy(new_roots)
res = [os.path.splitext(item)[0].replace(db_dir, "") for item in res]
res = sorted(res)
return res
def get_record_list_recursive3(db_dir: str, rec_patterns: Union[str, Dict[str, str]]) -> Union[List[str], Dict[str, List[str]]]:
r"""finished, checked,
get the list of records in `db_dir` recursively,
for example, there are two folders "patient1", "patient2" in `db_dir`,
and there are records "A0001", "A0002", ... in "patient1"; "B0001", "B0002", ... in "patient2",
then the output would be "patient1{sep}A0001", ..., "patient2{sep}B0001", ...,
sep is determined by the system
Parameters
----------
db_dir: str,
the parent (root) path of the whole database
rec_patterns: str or dict,
pattern of the record filenames, e.g. "A(?:\d+).mat",
or patterns of several subsets, e.g. `{"A": "A(?:\d+).mat"}`
Returns
-------
res: list of str,
list of records, in lexicographical order
"""
if isinstance(rec_patterns, str):
res = []
elif isinstance(rec_patterns, dict):
res = {k: [] for k in rec_patterns.keys()}
db_dir = os.path.join(db_dir, "tmp").replace("tmp", "") # make sure `db_dir` ends with a sep
roots = [db_dir]
while len(roots) > 0:
new_roots = []
for r in roots:
tmp = os.listdir(r)
# tmp = [os.path.join(r, item) for item in os.listdir(r)]
# res += [item for item in tmp if os.path.isfile(item)]
if isinstance(rec_patterns, str):
to_add = list(filter(re.compile(rec_patterns).search, tmp))
res += [os.path.join(r, item) for item in to_add]
elif isinstance(rec_patterns, dict):
for k in rec_patterns.keys():
to_add = list(filter(re.compile(rec_patterns[k]).search, tmp))
res[k] += [os.path.join(r, item) for item in to_add]
new_roots += [os.path.join(r, item) for item in tmp if os.path.isdir(os.path.join(r, item))]
roots = deepcopy(new_roots)
if isinstance(rec_patterns, str):
res = [os.path.splitext(item)[0].replace(db_dir, "") for item in res]
res = sorted(res)
elif isinstance(rec_patterns, dict):
for k in rec_patterns.keys():
res[k] = [os.path.splitext(item)[0].replace(db_dir, "") for item in res[k]]
res[k] = sorted(res[k])
return res
def dict_to_str(d: Union[dict, list, tuple], current_depth: int = 1, indent_spaces: int = 4) -> str:
"""finished, checked,
convert a (possibly) nested dict into a `str` of json-like formatted form,
this nested dict might also contain lists or tuples of dict (and of str, int, etc.)
Parameters
----------
d: dict, or list, or tuple,
a (possibly) nested `dict`, or a list of `dict`
current_depth: int, default 1,
depth of `d` in the (possible) parent `dict` or `list`
indent_spaces: int, default 4,
the indent spaces of each depth
Returns
-------
s: str,
the formatted string
"""
assert isinstance(d, (dict, list, tuple))
if len(d) == 0:
s = f"{{}}" if isinstance(d, dict) else "[]" # noqa: F541
return s
# flat_types = (Number, bool, str,)
flat_types = (
Number,
bool,
)
flat_sep = ", "
s = "\n"
unit_indent = " " * indent_spaces
prefix = unit_indent * current_depth
if isinstance(d, (list, tuple)):
if all([isinstance(v, flat_types) for v in d]):
len_per_line = 110
current_len = len(prefix) + 1 # + 1 for a comma
val = []
for idx, v in enumerate(d):
add_v = f"\042{v}\042" if isinstance(v, str) else str(v)
add_len = len(add_v) + len(flat_sep)
if current_len + add_len > len_per_line:
val = ", ".join([item for item in val])
s += f"{prefix}{val},\n"
val = [add_v]
current_len = len(prefix) + 1 + len(add_v)
else:
val.append(add_v)
current_len += add_len
if len(val) > 0:
val = ", ".join([item for item in val])
s += f"{prefix}{val}\n"
else:
for idx, v in enumerate(d):
if isinstance(v, (dict, list, tuple)):
s += f"{prefix}{dict_to_str(v, current_depth+1)}"
else:
val = f"\042{v}\042" if isinstance(v, str) else v
s += f"{prefix}{val}"
if idx < len(d) - 1:
s += ",\n"
else:
s += "\n"
elif isinstance(d, dict):
for idx, (k, v) in enumerate(d.items()):
key = f"\042{k}\042" if isinstance(k, str) else k
if isinstance(v, (dict, list, tuple)):
s += f"{prefix}{key}: {dict_to_str(v, current_depth+1)}"
else:
val = f"\042{v}\042" if isinstance(v, str) else v
s += f"{prefix}{key}: {val}"
if idx < len(d) - 1:
s += ",\n"
else:
s += "\n"
s += unit_indent * (current_depth - 1)
s = f"{{{s}}}" if isinstance(d, dict) else f"[{s}]"
return s
def str2bool(v: Union[str, bool]) -> bool:
"""finished, checked,
converts a "boolean" value possibly in the format of str to bool
Parameters
----------
v: str or bool,
the "boolean" value
Returns
-------
b: bool,
`v` in the format of bool
References
----------
[1] https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
"""
if isinstance(v, bool):
b = v
elif v.lower() in ("yes", "true", "t", "y", "1"):
b = True
elif v.lower() in ("no", "false", "f", "n", "0"):
b = False
else:
raise ValueError("Boolean value expected.")
return b
def diff_with_step(a: np.ndarray, step: int = 1, **kwargs) -> np.ndarray:
"""finished, checked,
compute a[n+step] - a[n] for all valid n
Parameters
----------
a: ndarray,
the input data
step: int, default 1,
the step to compute the difference
kwargs: dict,
Returns
-------
d: ndarray:
the difference array
"""
if step >= len(a):
raise ValueError(f"step ({step}) should be less than the length ({len(a)}) of `a`")
d = a[step:] - a[:-step]
return d
def ms2samples(t: Real, fs: Real) -> int:
"""finished, checked,
convert time `t` with units in ms to number of samples
Parameters
----------
t: real number,
time with units in ms
fs: real number,
sampling frequency of a signal
Returns
-------
n_samples: int,
number of samples corresponding to time `t`
"""
n_samples = t * fs // 1000
return n_samples
def samples2ms(n_samples: int, fs: Real) -> Real:
"""finished, checked,
inverse function of `ms2samples`
Parameters
----------
n_samples: int,
number of sample points
fs: real number,
sampling frequency of a signal
Returns
-------
t: real number,
time duration correponding to `n_samples`
"""
t = n_samples * 1000 / fs
return t
def get_mask(
shape: Union[int, Sequence[int]],
critical_points: np.ndarray,
left_bias: int,
right_bias: int,
return_fmt: str = "mask",
) -> Union[np.ndarray, list]:
"""finished, checked,
get the mask around the `critical_points`
Parameters
----------
shape: int, or sequence of int,
shape of the mask (and the original data)
critical_points: ndarray,
indices (of the last dimension) of the points around which to be masked (value 1)
left_bias: int, non-negative
bias to the left of the critical points for the mask
right_bias: int, non-negative
bias to the right of the critical points for the mask
return_fmt: str, default "mask",
format of the return values,
"mask" for the usual mask,
can also be "intervals", which consists of a list of intervals
Returns
-------
mask: ndarray or list,
"""
if isinstance(shape, int):
shape = (shape,)
l_itv = [[max(0, cp - left_bias), min(shape[-1], cp + right_bias)] for cp in critical_points]
if return_fmt.lower() == "mask":
mask = np.zeros(shape=shape, dtype=int)
for itv in l_itv:
mask[..., itv[0] : itv[1]] = 1
elif return_fmt.lower() == "intervals":
mask = l_itv
return mask
def class_weight_to_sample_weight(
y: np.ndarray, class_weight: Union[str, List[float], np.ndarray, dict] = "balanced"
) -> np.ndarray:
"""finished, checked,
transform class weight to sample weight
Parameters
----------
y: ndarray,
the label (class) of each sample
class_weight: str, or list, or ndarray, or dict, default "balanced",
the weight for each sample class,
if is "balanced", the class weight will automatically be given by
if `y` is of string type, then `class_weight` should be a dict,
if `y` is of numeric type, and `class_weight` is array_like,
then the labels (`y`) should be continuous and start from 0
"""
if not class_weight:
sample_weight = np.ones_like(y, dtype=float)
return sample_weight
try:
sample_weight = y.copy().astype(int)
except Exception:
sample_weight = y.copy()
assert (
isinstance(class_weight, dict) or class_weight.lower() == "balanced"
), "if `y` are of type str, then class_weight should be 'balanced' or a dict"
if isinstance(class_weight, str) and class_weight.lower() == "balanced":
classes = np.unique(y).tolist()
cw = compute_class_weight("balanced", classes=classes, y=y)
trans_func = lambda s: cw[classes.index(s)]
else:
trans_func = lambda s: class_weight[s]
sample_weight = np.vectorize(trans_func)(sample_weight)
sample_weight = sample_weight / np.max(sample_weight)
return sample_weight
def plot_single_lead(
t: np.ndarray,
sig: np.ndarray,
ax: Optional[Any] = None,
ticks_granularity: int = 0,
**kwargs,
) -> None:
"""finished, NOT checked,
Parameters
----------
to write
"""
if "plt" not in dir():
import matplotlib.pyplot as plt
palette = {
"p_waves": "green",
"qrs": "red",
"t_waves": "pink",
}
plot_alpha = 0.4
y_range = np.max(np.abs(sig)) + 100
if ax is None:
fig_sz_w = int(round(4.8 * (t[-1] - t[0])))
fig_sz_h = 6 * y_range / 1500
fig, ax = plt.subplots(figsize=(fig_sz_w, fig_sz_h))
label = kwargs.get("label", None)
if label:
ax.plot(t, sig, label=kwargs.get("label"))
else:
ax.plot(t, sig)
ax.axhline(y=0, linestyle="-", linewidth="1.0", color="red")
# NOTE that `Locator` has default `MAXTICKS` equal to 1000
if ticks_granularity >= 1:
ax.xaxis.set_major_locator(plt.MultipleLocator(0.2))
ax.yaxis.set_major_locator(plt.MultipleLocator(500))
ax.grid(which="major", linestyle="-", linewidth="0.5", color="red")
if ticks_granularity >= 2:
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.04))
ax.yaxis.set_minor_locator(plt.MultipleLocator(100))
ax.grid(which="minor", linestyle=":", linewidth="0.5", color="black")
waves = kwargs.get("waves", {"p_waves": [], "qrs": [], "t_waves": []})
for w, l_itv in waves.items():
for itv in l_itv:
ax.axvspan(itv[0], itv[1], color=palette[w], alpha=plot_alpha)
if label:
ax.legend(loc="upper left")
ax.set_xlim(t[0], t[-1])
ax.set_ylim(-y_range, y_range)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Voltage [μV]")
def init_logger(log_dir: str, log_file: Optional[str] = None, mode: str = "a", verbose: int = 0) -> logging.Logger:
"""finished, checked,
Parameters
----------
log_dir: str,
directory of the log file
log_file: str, optional,
name of the log file
mode: str, default "a",
mode of writing the log file, can be one of "a", "w"
verbose: int, default 0,
log verbosity
Returns
-------
logger: Logger
"""
if log_dir is None:
log_dir = "~/temp/log/"
if log_file is None:
log_file = f"log_{get_date_str()}.txt"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = os.path.join(log_dir, log_file)
print(f"log file path: {log_file}")
logger = logging.getLogger("CPSC2021")
c_handler = logging.StreamHandler(sys.stdout)
f_handler = logging.FileHandler(log_file)
if verbose >= 2:
print("levels of c_handler and f_handler are set DEBUG")
c_handler.setLevel(logging.DEBUG)
f_handler.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
elif verbose >= 1:
print("level of c_handler is set INFO, level of f_handler is set DEBUG")
c_handler.setLevel(logging.INFO)
f_handler.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
else:
print("levels of c_handler and f_handler are set WARNING")
c_handler.setLevel(logging.WARNING)
f_handler.setLevel(logging.WARNING)
logger.setLevel(logging.WARNING)
c_format = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
f_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
logger.addHandler(c_handler)
logger.addHandler(f_handler)
return logger
def get_date_str(fmt: Optional[str] = None):
"""finished, checked,
Parameters
----------
fmt: str, optional,
format of the string of date
Returns
-------
date_str: str,
current time in the `str` format
"""
now = datetime.datetime.now()
date_str = now.strftime(fmt or "%m-%d_%H-%M")
return date_str
def rdheader(header_data: Union[str, Sequence[str]]) -> Union[Record, MultiRecord]:
"""finished, checked,
modified from `wfdb.rdheader`
Parameters
----------
head_data: str, or sequence of str,
path of the .hea header file, or lines of the .hea header file
Returns
-------
record: wfdb.Record or wfdb.MultiRecord,
header in the format of wfdb Record
"""
if isinstance(header_data, str):
if not header_data.endswith(".hea"):
_header_data = header_data + ".hea"
else:
_header_data = header_data
if not os.path.isfile(_header_data):
raise FileNotFoundError
with open(_header_data, "r") as f:
_header_data = f.read().splitlines()
else:
_header_data = deepcopy(header_data)
# Read the header file. Separate comment and non-comment lines
header_lines, comment_lines = [], []
for line in _header_data:
striped_line = line.strip()
# Comment line
if striped_line.startswith("#"):
comment_lines.append(striped_line)
# Non-empty non-comment line = header line.
elif striped_line:
# Look for a comment in the line
ci = striped_line.find("#")
if ci > 0:
header_lines.append(striped_line[:ci])
# comment on same line as header line
comment_lines.append(striped_line[ci:])
else:
header_lines.append(striped_line)
# Get fields from record line
record_fields = _header._parse_record_line(header_lines[0])
# Single segment header - Process signal specification lines
if record_fields["n_seg"] is None:
# Create a single-segment WFDB record object
record = Record()
# There are signals
if len(header_lines) > 1:
# Read the fields from the signal lines
signal_fields = _header._parse_signal_lines(header_lines[1:])
# Set the object's signal fields
for field in signal_fields:
setattr(record, field, signal_fields[field])
# Set the object's record line fields
for field in record_fields:
if field == "n_seg":
continue
setattr(record, field, record_fields[field])
# Multi segment header - Process segment specification lines
else:
# Create a multi-segment WFDB record object
record = MultiRecord()
# Read the fields from the segment lines
segment_fields = _header._read_segment_lines(header_lines[1:])
# Set the object's segment fields
for field in segment_fields:
setattr(record, field, segment_fields[field])
# Set the objects' record fields
for field in record_fields:
setattr(record, field, record_fields[field])
# Determine whether the record is fixed or variable
if record.seg_len[0] == 0:
record.layout = "variable"
else:
record.layout = "fixed"
# Set the comments field
record.comments = [line.strip(" \t#") for line in comment_lines]
return record
ECGWaveForm = namedtuple(
typename="ECGWaveForm",
field_names=["name", "onset", "offset", "peak", "duration"],
)
def masks_to_waveforms(
masks: np.ndarray,
class_map: Dict[str, int],
fs: Real,
mask_format: str = "channel_first",
leads: Optional[Sequence[str]] = None,
) -> Dict[str, List[ECGWaveForm]]:
"""
convert masks into lists of waveforms
Parameters
----------
masks: ndarray,
wave delineation in the form of masks,
of shape (n_leads, seq_len), or (seq_len,)
class_map: dict,
class map, mapping names to waves to numbers from 0 to n_classes-1,
the keys should contain "pwave", "qrs", "twave"
fs: real number,
sampling frequency of the signal corresponding to the `masks`,
used to compute the duration of each waveform
mask_format: str, default "channel_first",
format of the mask, used only when `masks.ndim = 2`
"channel_last" (alias "lead_last"), or
"channel_first" (alias "lead_first")
leads: str or list of str, optional,
the names of leads corresponding to the channels of the `masks`
Returns
-------
waves: dict,
each item value is a list containing the `ECGWaveForm`s corr. to the lead;
each item key is from `leads` if `leads` is set,
otherwise would be "lead_1", "lead_2", ..., "lead_n"
"""
if masks.ndim == 1:
_masks = masks[np.newaxis, ...]
elif masks.ndim == 2:
if mask_format.lower() not in [
"channel_first",
"lead_first",
]:
_masks = masks.T
else:
_masks = masks.copy()
else:
raise ValueError(f"masks should be of dim 1 or 2, but got a {masks.ndim}d array")
_leads = [f"lead_{idx+1}" for idx in range(_masks.shape[0])] if leads is None else leads
assert len(_leads) == _masks.shape[0]
_class_map = ED(deepcopy(class_map))
waves = ED({lead_name: [] for lead_name in _leads})
for channel_idx, lead_name in enumerate(_leads):
current_mask = _masks[channel_idx, ...]
for wave_name, wave_number in _class_map.items():
if wave_name.lower() not in [
"pwave",
"qrs",
"twave",
]:
continue
current_wave_inds = np.where(current_mask == wave_number)[0]
if len(current_wave_inds) == 0:
continue
np.where(np.diff(current_wave_inds) > 1)
split_inds = np.where(np.diff(current_wave_inds) > 1)[0].tolist()
split_inds = sorted(split_inds + [i + 1 for i in split_inds])
split_inds = [0] + split_inds + [len(current_wave_inds) - 1]
for i in range(len(split_inds) // 2):
itv_start = current_wave_inds[split_inds[2 * i]]
itv_end = current_wave_inds[split_inds[2 * i + 1]] + 1
w = ECGWaveForm(
name=wave_name.lower(),
onset=itv_start,
offset=itv_end,
peak=np.nan,
duration=1000 * (itv_end - itv_start) / fs, # ms
)
waves[lead_name].append(w)
waves[lead_name].sort(key=lambda w: w.onset)
return waves
def mask_to_intervals(
mask: np.ndarray,
vals: Optional[Union[int, Sequence[int]]] = None,
right_inclusive: bool = False,
) -> Union[list, dict]:
"""finished, checked,
Parameters
----------
mask: ndarray,
1d mask
vals: int or sequence of int, optional,
values in `mask` to obtain intervals
right_inclusive: bool, default False,
if True, the intervals will be right inclusive
otherwise, right exclusive
Returns
-------
intervals: dict or list,
the intervals corr. to each value in `vals` if `vals` is `None` or `Sequence`;
or the intervals corr. to `vals` if `vals` is int.
each interval is of the form `[a,b]`
"""
if vals is None:
_vals = list(set(mask))
elif isinstance(vals, int):
_vals = [vals]
else:
_vals = vals
# assert set(_vals) & set(mask) == set(_vals)
bias = 0 if right_inclusive else 1
intervals = {v: [] for v in _vals}
for v in _vals:
valid_inds = np.where(np.array(mask) == v)[0]
if len(valid_inds) == 0:
continue
split_indices = np.where(np.diff(valid_inds) > 1)[0]
split_indices = split_indices.tolist() + (split_indices + 1).tolist()
split_indices = sorted([0] + split_indices + [len(valid_inds) - 1])
for idx in range(len(split_indices) // 2):
intervals[v].append(
[
valid_inds[split_indices[2 * idx]],
valid_inds[split_indices[2 * idx + 1]] + bias,
]
)
if isinstance(vals, int):
intervals = intervals[vals]
return intervals
def nildent(text: str) -> str:
"""finished, checked,
kill all leading white spaces in each line of `text`,
while keeping all lines (including empty)
"""
new_text = "\n".join([line.lstrip() for line in text.splitlines()]) + ("\n" if text.endswith("\n") else "")
return new_text
def list_sum(lst: Sequence[list]) -> list:
"""finished, checked,
Parameters
----------
lst: sequence of list,
the sequence of lists to obtain the summation
Returns
-------
l_sum: list,
sum of `lst`,
i.e. if lst = [list1, list2, ...], then l_sum = list1 + list2 + ...
"""
l_sum = reduce(lambda a, b: a + b, lst, [])
return l_sum
def save_dict(filename, dic):
"""save dict into json file"""
with open(filename, "w") as json_file:
json.dump(dic, json_file, ensure_ascii=False)
def uniform(low: Real, high: Real, num: int) -> List[float]:
"""finished, checked,
Parameters
----------
low: real number,
lower bound of the interval of the uniform distribution
high: real number,
upper bound of the interval of the uniform distribution
num: int,
number of random numbers to generate
Returns
-------
arr: list of float,
array of randomly generated numbers with uniform distribution
"""
arr = [random.uniform(low, high) for _ in range(num)]
return arr
WFDB_Beat_Annotations = {
"N": "Normal beat",
"L": "Left bundle branch block beat",
"R": "Right bundle branch block beat",
"B": "Bundle branch block beat (unspecified)",
"A": "Atrial premature beat",
"a": "Aberrated atrial premature beat",
"J": "Nodal (junctional) premature beat",
"S": "Supraventricular premature or ectopic beat (atrial or nodal)",
"V": "Premature ventricular contraction",
"r": "R-on-T premature ventricular contraction",
"F": "Fusion of ventricular and normal beat",
"e": "Atrial escape beat",
"j": "Nodal (junctional) escape beat",
"n": "Supraventricular escape beat (atrial or nodal)",
"E": "Ventricular escape beat",
"/": "Paced beat",
"f": "Fusion of paced and normal beat",
"Q": "Unclassifiable beat",
"?": "Beat not classified during learning",
}
WFDB_Non_Beat_Annotations = {
"[": "Start of ventricular flutter/fibrillation",
"!": "Ventricular flutter wave",
"]": "End of ventricular flutter/fibrillation",
"x": "Non-conducted P-wave (blocked APC)",
"(": "Waveform onset",
")": "Waveform end",
"p": "Peak of P-wave",
"t": "Peak of T-wave",
"u": "Peak of U-wave",
"`": "PQ junction",
"'": "J-point",
"^": "(Non-captured) pacemaker artifact",
"|": "Isolated QRS-like artifact",
"~": "Change in signal quality",
"+": "Rhythm change",
"s": "ST segment change",
"T": "T-wave change",
"*": "Systole",
"D": "Diastole",
"=": "Measurement annotation",
'"': "Comment annotation",
"@": "Link to external data",
}
WFDB_Rhythm_Annotations = {
"(AB": "Atrial bigeminy",
"(AFIB": "Atrial fibrillation",
"(AFL": "Atrial flutter",
"(B": "Ventricular bigeminy",
"(BII": "2° heart block",
"(IVR": "Idioventricular rhythm",
"(N": "Normal sinus rhythm",
"(NOD": "Nodal (A-V junctional) rhythm",
"(P": "Paced rhythm",
"(PREX": "Pre-excitation (WPW)",
"(SBR": "Sinus bradycardia",
"(SVTA": "Supraventricular tachyarrhythmia",
"(T": "Ventricular trigeminy",
"(VFL": "Ventricular flutter",
"(VT": "Ventricular tachycardia",
}