-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathannotation.py
3228 lines (2768 loc) · 107 KB
/
annotation.py
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import fsspec
import numpy as np
import os
import pandas as pd
import re
import posixpath
import sys
from wfdb.io import download
from wfdb.io import _header
from wfdb.io import record
from wfdb.io import util
from wfdb.io._coreio import CLOUD_PROTOCOLS
class Annotation(object):
"""
The class representing WFDB annotations.
Annotation objects can be created using the initializer, or by reading a
WFDB annotation file with `rdann`.
The attributes of the Annotation object give information about the
annotation as specified by:
https://www.physionet.org/physiotools/wag/annot-5.htm
Call `show_ann_labels()` to see the list of standard annotation codes. Any
text used to label annotations that are not one of these codes should go in
the 'aux_note' field rather than the 'sym' field.
The current annotation values organized as such:
AnnotationLabel(label_store (or subtype), symbol (or aux_note), short_description, description)
where the associated values are:
ann_labels = [
AnnotationLabel(0, " ", 'NOTANN', 'Not an actual annotation'),
AnnotationLabel(1, "N", 'NORMAL', 'Normal beat'),
AnnotationLabel(2, "L", 'LBBB', 'Left bundle branch block beat'),
AnnotationLabel(3, "R", 'RBBB', 'Right bundle branch block beat'),
AnnotationLabel(4, "a", 'ABERR', 'Aberrated atrial premature beat'),
AnnotationLabel(5, "V", 'PVC', 'Premature ventricular contraction'),
AnnotationLabel(6, "F", 'FUSION', 'Fusion of ventricular and normal beat'),
AnnotationLabel(7, "J", 'NPC', 'Nodal (junctional) premature beat'),
AnnotationLabel(8, "A", 'APC', 'Atrial premature contraction'),
AnnotationLabel(9, "S", 'SVPB', 'Premature or ectopic supraventricular beat'),
AnnotationLabel(10, "E", 'VESC', 'Ventricular escape beat'),
AnnotationLabel(11, "j", 'NESC', 'Nodal (junctional) escape beat'),
AnnotationLabel(12, "/", 'PACE', 'Paced beat'),
AnnotationLabel(13, "Q", 'UNKNOWN', 'Unclassifiable beat'),
AnnotationLabel(14, "~", 'NOISE', 'Signal quality change'),
AnnotationLabel(16, "|", 'ARFCT', 'Isolated QRS-like artifact'),
AnnotationLabel(18, "s", 'STCH', 'ST change'),
AnnotationLabel(19, "T", 'TCH', 'T-wave change'),
AnnotationLabel(20, "*", 'SYSTOLE', 'Systole'),
AnnotationLabel(21, "D", 'DIASTOLE', 'Diastole'),
AnnotationLabel(22, '"', 'NOTE', 'Comment annotation'),
AnnotationLabel(23, "=", 'MEASURE', 'Measurement annotation'),
AnnotationLabel(24, "p", 'PWAVE', 'P-wave peak'),
AnnotationLabel(25, "B", 'BBB', 'Left or right bundle branch block'),
AnnotationLabel(26, "^", 'PACESP', 'Non-conducted pacer spike'),
AnnotationLabel(27, "t", 'TWAVE', 'T-wave peak'),
AnnotationLabel(28, "+", 'RHYTHM', 'Rhythm change'),
AnnotationLabel(29, "u", 'UWAVE', 'U-wave peak'),
AnnotationLabel(30, "?", 'LEARN', 'Learning'),
AnnotationLabel(31, "!", 'FLWAV', 'Ventricular flutter wave'),
AnnotationLabel(32, "[", 'VFON', 'Start of ventricular flutter/fibrillation'),
AnnotationLabel(33, "]", 'VFOFF', 'End of ventricular flutter/fibrillation'),
AnnotationLabel(34, "e", 'AESC', 'Atrial escape beat'),
AnnotationLabel(35, "n", 'SVESC', 'Supraventricular escape beat'),
AnnotationLabel(36, "@", 'LINK', 'Link to external data (aux_note contains URL)'),
AnnotationLabel(37, "x", 'NAPC', 'Non-conducted P-wave (blocked APB)'),
AnnotationLabel(38, "f", 'PFUS', 'Fusion of paced and normal beat'),
AnnotationLabel(39, "(", 'WFON', 'Waveform onset'),
AnnotationLabel(40, ")", 'WFOFF', 'Waveform end'),
AnnotationLabel(41, "r", 'RONT', 'R-on-T premature ventricular contraction')
]
The current annotation classes are organized as such:
AnnotationClass(extension, description, human_reviewed)
where the associated values are:
ann_classes = [
AnnotationClass('atr', 'Reference ECG annotations', True),
AnnotationClass('blh', 'Human reviewed beat labels', True),
AnnotationClass('blm', 'Machine beat labels', False),
AnnotationClass('alh', 'Human reviewed alarms', True),
AnnotationClass('alm', 'Machine alarms', False),
AnnotationClass('qrsc', 'Human reviewed QRS detections', True),
AnnotationClass('qrs', 'Machine QRS detections', False),
AnnotationClass('bph', 'Human reviewed BP beat detections', True),
AnnotationClass('bpm', 'Machine BP beat detections', False)
]
Attributes
----------
record_name : str
The base file name (without extension) of the record that the
annotation is associated with.
extension : str
The file extension of the file the annotation is stored in.
sample : ndarray
A numpy array containing the annotation locations in samples relative to
the beginning of the record.
symbol : list, numpy array, optional
The symbols used to display the annotation labels. List or numpy array.
If this field is present, `label_store` must not be present.
subtype : ndarray, optional
A numpy array containing the marked class/category of each annotation.
chan : ndarray, optional
A numpy array containing the signal channel associated with each
annotation.
num : ndarray, optional
A numpy array containing the labelled annotation number for each
annotation.
aux_note : list, optional
A list containing the auxiliary information string (or None for
annotations without notes) for each annotation.
fs : int, float, optional
The sampling frequency of the record.
label_store : ndarray, optional
The integer value used to store/encode each annotation label
description : list, optional
A list containing the descriptive string of each annotation label.
custom_labels : pandas dataframe, optional
The custom annotation labels defined in the annotation file. Maps
the relationship between the three label fields. The data type is a
pandas DataFrame with three columns:
['label_store', 'symbol', 'description'].
contained_labels : pandas dataframe, optional
The unique labels contained in this annotation. Same structure as
`custom_labels`.
Examples
--------
>>> ann1 = wfdb.Annotation(record_name='rec1', extension='atr',
sample=[10,20,400], symbol=['N','N','['],
aux_note=[None, None, 'Serious Vfib'])
"""
def __init__(
self,
record_name,
extension,
sample,
symbol=None,
subtype=None,
chan=None,
num=None,
aux_note=None,
fs=None,
label_store=None,
description=None,
custom_labels=None,
contained_labels=None,
):
self.record_name = record_name
self.extension = extension
self.sample = sample
self.symbol = symbol
self.subtype = subtype
self.chan = chan
self.num = num
self.aux_note = aux_note
self.fs = fs
self.label_store = label_store
self.description = description
self.custom_labels = custom_labels
self.contained_labels = contained_labels
self.ann_len = len(self.sample)
# __label_map__: (storevalue, symbol, description) hidden attribute
def __eq__(self, other):
"""
Equal comparison operator for objects of this type.
Parameters
----------
other : object
The object that is being compared to self.
Returns
-------
bool
Determines if the objects are equal (True) or not equal (False).
"""
att1 = self.__dict__
att2 = other.__dict__
if set(att1.keys()) != set(att2.keys()):
print("keyset")
return False
for k in att1.keys():
v1 = att1[k]
v2 = att2[k]
if type(v1) != type(v2):
print(k)
return False
if isinstance(v1, np.ndarray):
if not np.array_equal(v1, v2):
print(k)
return False
elif isinstance(v1, pd.DataFrame):
if not v1.equals(v2):
print(k)
return False
else:
if v1 != v2:
print(k)
return False
return True
def apply_range(self, sampfrom=0, sampto=None):
"""
Filter the annotation attributes to keep only items between the
desired sample values.
Parameters
----------
sampfrom : int, optional
The minimum sample number for annotations to be returned.
sampto : int, optional
The maximum sample number for annotations to be returned.
"""
sampto = sampto or self.sample[-1]
kept_inds = np.intersect1d(
np.where(self.sample >= sampfrom), np.where(self.sample <= sampto)
)
for field in ["sample", "label_store", "subtype", "chan", "num"]:
setattr(self, field, getattr(self, field)[kept_inds])
self.aux_note = [self.aux_note[i] for i in kept_inds]
self.ann_len = len(self.sample)
def wrann(self, write_fs=False, write_dir=""):
"""
Write a WFDB annotation file from this object.
Parameters
----------
write_fs : bool, optional
Whether to write the `fs` attribute to the file.
write_dir : str, optional
The output directory in which the header is written.
Returns
-------
N/A
"""
for field in ["record_name", "extension"]:
if getattr(self, field) is None:
raise Exception(
"Missing required field for writing annotation file: ",
field,
)
present_label_fields = self.get_label_fields()
if not present_label_fields:
raise Exception(
"At least one annotation label field is required to write the annotation: ",
ann_label_fields,
)
# Check the validity of individual fields
self.check_fields()
# Standardize the format of the custom_labels field
self.standardize_custom_labels()
# Create the label map used in this annotaion
self.create_label_map()
# Check the cohesion of fields
self.check_field_cohesion(present_label_fields)
# Calculate the label_store field if necessary
if "label_store" not in present_label_fields:
self.convert_label_attribute(
source_field=present_label_fields[0], target_field="label_store"
)
# Calculate the symbol field if necessary
if "symbol" not in present_label_fields:
self.convert_label_attribute(
source_field=present_label_fields[0], target_field="symbol"
)
# Write the header file using the specified fields
self.wr_ann_file(write_fs=write_fs, write_dir=write_dir)
return
def get_label_fields(self):
"""
Get the present label fields in the object.
Parameters
----------
N/A
Returns
-------
present_label_fields : list
All of the present label fields in the object.
"""
present_label_fields = []
for field in ann_label_fields:
if getattr(self, field) is not None:
present_label_fields.append(field)
return present_label_fields
def check_fields(self):
"""
Check the set fields of the annotation object.
Parameters
----------
N/A
Returns
-------
N/A
"""
# Check all set fields
for field in ALLOWED_TYPES:
if getattr(self, field) is not None:
# Check the type of the field's elements
self.check_field(field)
return
def check_field(self, field):
"""
Check a particular annotation field.
Parameters
----------
field : str
The annotation field to be checked.
Returns
-------
N/A
"""
item = getattr(self, field)
if not isinstance(item, ALLOWED_TYPES[field]):
raise TypeError(
"The " + field + " field must be one of the following types:",
ALLOWED_TYPES[field],
)
# Numerical integer annotation fields: sample, label_store, sub,
# chan, num
if ALLOWED_TYPES[field] == (np.ndarray):
record.check_np_array(
item=item,
field_name=field,
ndim=1,
parent_class=np.integer,
channel_num=None,
)
# Field specific checks
if field == "record_name":
if bool(re.search(r"[^-\w]", self.record_name)):
raise ValueError(
"record_name must only comprise of letters, digits, hyphens, and underscores."
)
elif field == "extension":
if bool(re.search("[^a-zA-Z]", self.extension)):
raise ValueError("extension must only comprise of letters.")
elif field == "fs":
if self.fs <= 0:
raise ValueError("The fs field must be a non-negative number")
elif field == "custom_labels":
# The role of this section is just to check the
# elements of this item, without utilizing
# any other fields. No format conversion
# or free value looksups etc are done.
# Check the structure of the subelements
if isinstance(item, pd.DataFrame):
column_names = list(item)
if "symbol" in column_names and "description" in column_names:
if "label_store" in column_names:
label_store = list(item["label_store"].values)
else:
label_store = None
symbol = item["symbol"].values
description = item["description"].values
else:
raise ValueError(
"".join(
[
"If the "
+ field
+ " field is pandas dataframe, its columns",
" must be one of the following:\n-[label_store, symbol, description]",
"\n-[symbol, description]",
]
)
)
else:
if set([len(i) for i in item]) == {2}:
label_store = None
symbol = [i[0] for i in item]
description = [i[1] for i in item]
elif set([len(i) for i in item]) == {3}:
label_store = [i[0] for i in item]
symbol = [i[1] for i in item]
description = [i[2] for i in item]
else:
raise ValueError(
"".join(
[
"If the "
+ field
+ " field is an array-like object, its subelements",
" must be one of the following:\n- tuple triplets storing: ",
"(label_store, symbol, description)\n- tuple pairs storing: ",
"(symbol, description)",
]
)
)
# Check the values of the subelements
if label_store:
if len(item) != len(set(label_store)):
raise ValueError(
"The label_store values of the "
+ field
+ " field must be unique"
)
if min(label_store) < 1 or max(label_store) > 49:
raise ValueError(
"The label_store values of the custom_labels field must be between 1 and 49"
)
if len(item) != len(set(symbol)):
raise ValueError(
"The symbol values of the "
+ field
+ " field must be unique"
)
for i in range(len(item)):
if label_store:
if not hasattr(label_store[i], "__index__"):
raise TypeError(
"The label_store values of the "
+ field
+ " field must be integer-like"
)
if not isinstance(symbol[i], str_types) or len(
symbol[i]
) not in [
1,
2,
3,
]:
raise ValueError(
"The symbol values of the "
+ field
+ " field must be strings of length 1 to 3"
)
if bool(re.search("[ \t\n\r\f\v]", symbol[i])):
raise ValueError(
"The symbol values of the "
+ field
+ " field must not contain whitespace characters"
)
if not isinstance(description[i], str_types):
raise TypeError(
"The description values of the "
+ field
+ " field must be strings"
)
# Would be good to enfore this but existing garbage annotations have tabs and newlines...
# if bool(re.search('[\t\n\r\f\v]', description[i])):
# raise ValueError('The description values of the '+field+' field must not contain tabs or newlines')
# The string fields
elif field in ["symbol", "description", "aux_note"]:
uniq_elements = set(item)
for e in uniq_elements:
if not isinstance(e, str_types):
raise TypeError(
"Subelements of the " + field + " field must be strings"
)
if field == "symbol":
for e in uniq_elements:
if len(e) not in [1, 2, 3]:
raise ValueError(
"Subelements of the "
+ field
+ " field must be strings of length 1 to 3"
)
if bool(re.search("[ \t\n\r\f\v]", e)):
raise ValueError(
"Subelements of the "
+ field
+ " field may not contain whitespace characters"
)
else:
for e in uniq_elements:
if bool(re.search("[\t\n\r\f\v]", e)):
raise ValueError(
"Subelements of the "
+ field
+ " field must not contain tabs or newlines"
)
elif field == "sample":
if len(self.sample) == 1:
sampdiffs = np.array([self.sample[0]])
elif len(self.sample) > 1:
sampdiffs = np.concatenate(
([self.sample[0]], np.diff(self.sample))
)
else:
raise ValueError(
"The 'sample' field must be a numpy array with length greater than 0"
)
if min(self.sample) < 0:
raise ValueError(
"The 'sample' field must only contain non-negative integers"
)
if min(sampdiffs) < 0:
raise ValueError(
"The 'sample' field must contain monotonically increasing sample numbers"
)
elif field == "label_store":
if min(item) < 1 or max(item) > 49:
raise ValueError(
"The label_store values must be between 1 and 49"
)
# The C WFDB library stores num/sub/chan as chars.
elif field == "subtype":
# signed character
if min(self.subtype) < -128 or max(self.subtype) > 127:
raise ValueError(
"The 'subtype' field must only contain integers from -128 to 127"
)
elif field == "chan":
# un_signed character
if min(self.chan) < 0 or max(self.chan) > 255:
raise ValueError(
"The 'chan' field must only contain non-negative integers up to 255"
)
elif field == "num":
# signed character
if min(self.num) < 0 or max(self.num) > 127:
raise ValueError(
"The 'num' field must only contain non-negative integers up to 127"
)
return
def check_field_cohesion(self, present_label_fields):
"""
Check that the content and structure of different fields are consistent
with one another.
Parameters
----------
present_label_fields : list
All of the present label fields in the object.
Returns
-------
N/A
"""
# Ensure all written annotation fields have the same length
nannots = len(self.sample)
for field in [
"sample",
"num",
"subtype",
"chan",
"aux_note",
] + present_label_fields:
if getattr(self, field) is not None:
if len(getattr(self, field)) != nannots:
raise ValueError(
"The lengths of the 'sample' and '"
+ field
+ "' fields do not match"
)
# Ensure all label fields are defined by the label map. This has to be checked because
# it is possible the user defined (or lack of) custom_labels does not capture all the
# labels present.
for field in present_label_fields:
defined_values = self.__label_map__[field].values
if set(getattr(self, field)) - set(defined_values) != set():
raise ValueError(
"\n".join(
[
"\nThe "
+ field
+ " field contains elements not encoded in the stardard WFDB annotation labels, or this object's custom_labels field",
"- To see the standard WFDB annotation labels, call: show_ann_labels()",
"- To transfer non-encoded symbol items into the aux_note field, call: self.sym_to_aux()",
"- To define custom labels, set the custom_labels field as a list of tuple triplets with format: (label_store, symbol, description)",
]
)
)
return
def standardize_custom_labels(self):
"""
Set the custom_labels field of the object to a standardized format:
3 column pandas df with ann_label_fields as columns.
Does nothing if there are no custom labels defined.
Does nothing if custom_labels is already a df with all 3 columns.
If custom_labels is an iterable of pairs/triplets, this
function will convert it into a df.
If the label_store attribute is not already defined, this
function will automatically choose values by trying to use:
1. The undefined store values from the standard WFDB annotation
label map.
2. The unused label store values. This is extracted by finding the
set of all labels contained in this annotation object and seeing
which symbols/descriptions are not used.
If there are more custom labels defined than there are enough spaces,
even in condition 2 from above, this function will raise an error.
This function must work when called as a standalone.
Parameters
----------
N/A
Returns
-------
N/A
"""
custom_labels = self.custom_labels
if custom_labels is None:
return
self.check_field("custom_labels")
# Convert to dataframe if not already
if not isinstance(custom_labels, pd.DataFrame):
if len(self.custom_labels[0]) == 2:
symbol = self.get_custom_label_attribute("symbol")
description = self.get_custom_label_attribute("description")
custom_labels = pd.DataFrame(
{"symbol": symbol, "description": description}
)
else:
label_store = self.get_custom_label_attribute("label_store")
symbol = self.get_custom_label_attribute("symbol")
description = self.get_custom_label_attribute("description")
custom_labels = pd.DataFrame(
{
"label_store": label_store,
"symbol": symbol,
"description": description,
}
)
# Assign label_store values to the custom labels if not defined
if "label_store" not in list(custom_labels):
undefined_label_stores = self.get_undefined_label_stores()
if len(custom_labels) > len(undefined_label_stores):
available_label_stores = self.get_available_label_stores()
else:
available_label_stores = undefined_label_stores
n_custom_labels = custom_labels.shape[0]
if n_custom_labels > len(available_label_stores):
raise ValueError(
"There are more custom_label definitions than storage values available for them."
)
custom_labels["label_store"] = available_label_stores[
:n_custom_labels
]
custom_labels.set_index(
custom_labels["label_store"].values, inplace=True
)
custom_labels = custom_labels[list(ann_label_fields)]
self.custom_labels = custom_labels
return
def get_undefined_label_stores(self):
"""
Get the label_store values not defined in the
standard WFDB annotation labels.
Parameters
----------
N/A
Returns
-------
list
The label_store values not found in WFDB annotation labels.
"""
return list(set(range(50)) - set(ann_label_table["label_store"]))
def get_available_label_stores(self, usefield="tryall"):
"""
Get the label store values that may be used
for writing this annotation.
Available store values include:
- the undefined values in the standard WFDB labels
- the store values not used in the current
annotation object.
- the store values whose standard WFDB symbols/descriptions
match those of the custom labels (if custom_labels exists)
Parameters
----------
usefield : str, optional
If 'usefield' is explicitly specified, the function will use that
field to figure out available label stores. If 'usefield'
is set to 'tryall', the function will choose one of the contained
attributes by checking availability in the order: label_store, symbol, description.
Returns
-------
available_label_stores : set
The available store values used for writing the annotation.
"""
# Figure out which field to use to get available labels stores.
if usefield == "tryall":
if self.label_store is not None:
usefield = "label_store"
elif self.symbol is not None:
usefield = "symbol"
elif self.description is not None:
usefield = "description"
else:
raise ValueError(
"No label fields are defined. At least one of the following is required: ",
ann_label_fields,
)
return self.get_available_label_stores(usefield=usefield)
# Use the explicitly stated field to get available stores.
else:
# If usefield == 'label_store', there are slightly fewer/different steps
# compared to if it were another option
contained_field = getattr(self, usefield)
# Get the unused label_store values
if usefield == "label_store":
unused_label_stores = (
set(ann_label_table["label_store"].values) - contained_field
)
else:
# the label_store values from the standard WFDB annotation labels
# whose symbols are not contained in this annotation
unused_field = (
set(ann_label_table[usefield].values) - contained_field
)
unused_label_stores = ann_label_table.loc[
ann_label_table[usefield] in unused_field, "label_store"
].values
# Get the standard WFDB label_store values overwritten by the
# custom_labels if any
if self.custom_symbols is not None:
custom_field = set(self.get_custom_label_attribute(usefield))
if usefield == "label_store":
overwritten_label_stores = set(custom_field).intersection(
set(ann_label_table["label_store"])
)
else:
overwritten_fields = set(custom_field).intersection(
set(ann_label_table[usefield])
)
overwritten_label_stores = ann_label_table.loc[
ann_label_table[usefield] in overwritten_fields,
"label_store",
].values
else:
overwritten_label_stores = set()
# The undefined values in the standard WFDB labels
undefined_label_stores = self.get_undefined_label_stores()
# Final available label stores = undefined + unused + overwritten
available_label_stores = (
set(undefined_label_stores)
.union(set(unused_label_stores))
.union(overwritten_label_stores)
)
return available_label_stores
def get_custom_label_attribute(self, attribute):
"""
Get a list of the custom_labels attribute i.e. label_store,
symbol, or description. The custom_labels variable could be in
a number of formats.
Parameters
----------
attribute : str
The selected attribute to generate the list.
Returns
-------
a : list
All of the custom_labels attributes.
"""
if attribute not in ann_label_fields:
raise ValueError("Invalid attribute specified")
if isinstance(self.custom_labels, pd.DataFrame):
if "label_store" not in list(self.custom_labels):
raise ValueError("label_store not defined in custom_labels")
a = list(self.custom_labels[attribute].values)
else:
if len(self.custom_labels[0]) == 2:
if attribute == "label_store":
raise ValueError("label_store not defined in custom_labels")
elif attribute == "symbol":
a = [l[0] for l in self.custom_labels]
elif attribute == "description":
a = [l[1] for l in self.custom_labels]
else:
if attribute == "label_store":
a = [l[0] for l in self.custom_labels]
elif attribute == "symbol":
a = [l[1] for l in self.custom_labels]
elif attribute == "description":
a = [l[2] for l in self.custom_labels]
return a
def create_label_map(self, inplace=True):
"""
Creates mapping df based on ann_label_table and self.custom_labels. Table
composed of entire WFDB standard annotation table, overwritten/appended
with custom_labels if any. Sets __label_map__ attribute, or returns value.
Parameters
----------
inplace : bool, optional
Determines whether to add the label map to the current
object (True) or as a return variable (False).
Returns
-------
label_map : pandas DataFrame
Mapping based on ann_label_table and self.custom_labels.
"""
label_map = ann_label_table.copy()
if self.custom_labels is not None:
self.standardize_custom_labels()
for i in self.custom_labels.index:
label_map.loc[i] = self.custom_labels.loc[i]
if inplace:
self.__label_map__ = label_map
else:
return label_map
def wr_ann_file(self, write_fs, write_dir=""):
"""
Calculate the bytes used to encode an annotation set and
write them to an annotation file.
Parameters
----------
write_fs : bool
Whether to write the `fs` attribute to the file.
write_dir : str, optional
The output directory in which the header is written.
Returns
-------
N/A
"""
# Calculate the fs bytes to write if present and desired to write
if write_fs:
fs_bytes = self.calc_fs_bytes()
else:
fs_bytes = []
# Calculate the custom_labels bytes to write if present
cl_bytes = self.calc_cl_bytes()
# Calculate the core field bytes to write
core_bytes = self.calc_core_bytes()
# Mark the end of the special annotation types if needed
if len(fs_bytes) or len(cl_bytes):
end_special_bytes = [0, 236, 255, 255, 255, 255, 1, 0]
else:
end_special_bytes = []
# Write the file
with open(
os.path.join(write_dir, self.record_name + "." + self.extension),
"wb",
) as f:
# Combine all bytes to write: fs (if any), custom annotations (if any), main content, file terminator
np.concatenate(
(
fs_bytes,
cl_bytes,
end_special_bytes,
core_bytes,
np.array([0, 0]),
)
).astype("u1").tofile(f)
return
def calc_fs_bytes(self):
"""
Calculate the bytes written to the annotation file for the fs field.
Parameters
----------
N/A
Returns
-------
list, ndarray
All of the bytes to be written to the annotation file.
"""
if self.fs is None:
return []
# Initial indicators of encoding fs
data_bytes = [
0,
88,
0,
252,
35,
35,
32,
116,
105,
109,
101,
32,
114,