-
Notifications
You must be signed in to change notification settings - Fork 1
/
wfdbcheck.c
1707 lines (1557 loc) · 56.7 KB
/
wfdbcheck.c
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
/*
* wfdbcheck - check for common mistakes in a WFDB record
*
* Copyright (c) 2018 Benjamin Moody
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <wfdb/wfdb.h>
#include <wfdb/ecgcodes.h>
#include <wfdb/wfdblib.h>
#ifndef NO_MESSAGES
#include "messages.h"
#endif
enum {
MSG_INTERNAL,
MSG_ERROR,
MSG_WARNING,
MSG_INFO,
N_MSG_LEVELS
};
static int calopen_failed;
static int nsig;
static WFDB_Siginfo *siginfo;
static int nmssig;
static WFDB_Siginfo *mssiginfo;
static int *mssigskew;
static int *mssigused;
static WFDB_Sample *msminv;
static WFDB_Sample *msmaxv;
static WFDB_Frequency msffreq;
static int message_count[N_MSG_LEVELS];
static int output_level = MSG_INFO;
static int show_detail = 0;
/* Check if DESC appears to be a description that was
automatically generated by isigopen, or a meaningless
description generated by 'wrsamp'. */
static int is_auto_sigdesc(const char *desc)
{
char *p;
if (!strncmp(desc, "record ", 7) && strstr(desc, ", signal ") != NULL)
return 1;
if (!strncmp(desc, "col ", 4)) {
strtol(desc + 4, &p, 10);
if (*p == 0)
return 1;
}
return 0;
}
/* Print an error/warning/info message, prefixed with
details of the location of the error. If FMT begins with
an underscore, print the last error message from WFDB.
Otherwise, treat FMT as a printf format string, and print
the message followed by a newline.
Each message should also have a detailed description,
which is defined in messages.h. That file is generated
by the 'gen-messages' script, by extracting the text of
the "DESCRIPTION" comments in this source file. */
#if __GNUC__ >= 3
__attribute__((format(printf, 6, 7)))
#endif
static void print_msg(int level, const char *msrec,
const char *rec, const char *ann,
int sig, const char *fmt, ...)
{
va_list ap;
int i;
message_count[level]++;
if (level > output_level)
return;
switch (level) {
case MSG_INTERNAL: printf("(!!) "); break;
case MSG_ERROR: printf("(EE) "); break;
case MSG_WARNING: printf("(WW) "); break;
case MSG_INFO: printf("(II) "); break;
}
if (msrec != NULL)
printf("%s:", msrec);
if (rec != NULL)
printf("%s:", rec);
if (msrec != NULL || rec != NULL)
printf(" ");
if (ann != NULL)
printf("annotator %s: ", ann);
if (sig >= 0) {
printf("signal %d", sig);
if (rec) {
if (sig < nsig && siginfo[sig].desc &&
!is_auto_sigdesc(siginfo[sig].desc))
printf(" (%s)", siginfo[sig].desc);
}
else {
if (sig < nmssig && mssiginfo[sig].desc &&
!is_auto_sigdesc(mssiginfo[sig].desc))
printf(" (%s)", mssiginfo[sig].desc);
}
printf(": ");
}
if (fmt[0] == '_') {
/* output from wfdberror includes \n */
printf("%s", wfdberror());
}
else {
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
if (!show_detail || !fmt)
return;
for (i = 0; message_description[i]; i++) {
if (!strcmp(message_description[i], fmt)) {
for (i++; message_description[i]; i++)
printf(" %s\n", message_description[i]);
printf("\n");
return;
}
while (message_description[i])
i++;
}
}
/* Check for problematic characters in record names. */
static int bad_record_name(const char *name)
{
int i;
for (i = 0; name[i]; i++) {
if ((name[i] < 'a' || name[i] > 'z') &&
(name[i] < 'A' || name[i] > 'Z') &&
(name[i] < '0' || name[i] > '9') &&
name[i] != '.' && name[i] != '-' &&
name[i] != '_' && name[i] != '/')
return 1;
}
while (name) {
if (name[0] == '.' || name[0] == '-')
return 1;
if ((!strncasecmp(name, "aux", 3) ||
!strncasecmp(name, "con", 3) ||
!strncasecmp(name, "nul", 3) ||
!strncasecmp(name, "prn", 3)) &&
(name[3] == 0 || name[3] == '/' || name[3] == '.'))
return 1;
if ((!strncasecmp(name, "com", 3) ||
!strncasecmp(name, "lpt", 3)) &&
(name[3] >= '1' && name[3] <= '9') &&
(name[4] == 0 || name[4] == '/' || name[4] == '.'))
return 1;
if ((name = strchr(name, '/')))
name++;
}
return 0;
}
/* Check for problematic characters in annotator names. */
static int bad_annotator_name(const char *name)
{
int i;
for (i = 0; name[i]; i++) {
if ((name[i] < 'a' || name[i] > 'z') &&
(name[i] < 'A' || name[i] > 'Z') &&
(name[i] < '0' || name[i] > '9') &&
name[i] != '_')
return 1;
}
return 0;
}
/* Check if string contains ASCII control characters. */
static int has_control_chars(const char *name)
{
int i;
for (i = 0; name[i]; i++) {
if ((unsigned char) name[i] < 0x20 ||
(unsigned char) name[i] == 0x7f)
return 1;
}
return 0;
}
/* Check if string is all digits */
static int is_all_digits(const char *name)
{
int i;
for (i = 0; name[i]; i++) {
if (name[i] < '0' || name[i] > '9')
return 0;
}
return 1;
}
/* Check consistency of starting time/date. */
static int cmp_base_times(const char *t1,
const char *t2)
{
const char *d1, *d2;
/* If either is not an absolute timestamp, no comparison is
possible */
if (t1[0] != '[' || t2[0] != '[')
return 0;
/* Check whether t1 and/or t2 contain starting dates */
d1 = strchr(t1, ' ');
d2 = strchr(t2, ' ');
if (d1 && d2)
/* Both t1 and t2 include starting dates */
return strcmp(t1, t2);
else if (!d1 && !d2)
/* both t1 and t2 include starting time of day only */
return strcmp(t1, t2);
else if (d1) {
/* t1 includes a date but t2 includes only time of day */
if ((int) strlen(t2) != d1 - t1 + 1)
return 1;
return strncmp(t1, t2, d1 - t1);
}
else {
/* t2 includes a date but t1 includes only time of day */
if ((int) strlen(t1) != d2 - t2 + 1)
return 1;
return strncmp(t1, t2, d2 - t2);
}
}
/* Check that size of file is acceptable. */
static void check_file_size(const char *msrec, const char *rec,
const char *fname)
{
WFDB_FILE *f;
int size_valid = 1;
f = wfdb_open(fname, NULL, WFDB_READ);
#ifdef EOVERFLOW
if (!f && errno == EOVERFLOW)
size_valid = 0;
#endif
if (f) {
if (!wfdb_fseek(f, 0L, SEEK_END) && wfdb_ftell(f) > 2147483647)
size_valid = 0;
wfdb_fclose(f);
}
if (!size_valid) {
print_msg(MSG_WARNING, msrec, rec, NULL, -1,
"file %s larger than 2^31 bytes", fname);
/*DESCRIPTION: file %s larger than 2^31 bytes *//*
The length of this file is greater than 2,147,483,647 bytes,
which will cause problems for 32-bit applications and
filesystems.
*/
}
}
/* Check for duplicate signal descriptions. */
static void check_sigdesc(const char *msrec, const char *rec,
int ns, const WFDB_Siginfo *si)
{
int i, j, n;
for (i = 0; i < ns; i++) {
for (j = 0; j < i; j++)
if (!strcmp(si[i].desc, si[j].desc))
break;
if (j != i)
continue;
n = 1;
for (j++; j < ns; j++) {
if (!strcmp(si[i].desc, si[j].desc))
n++;
}
if (n > 1) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"%d signals named %s", n, si[i].desc);
/*DESCRIPTION: %d signals named %s *//*
This record contains two or more signals with the same
description. If two or more signals have the same
description, users will be unable to select signals by
name. In the case of variable-format multi-segment
records, only one of the two signals will be visible to
applications.
*/
}
}
}
/* Check for mismatches in signal info between first/layout segment
and later segments. */
static void check_seg_siginfo(const char *msrec, const char *rec,
const WFDB_Sample *minv,
const WFDB_Sample *maxv)
{
int i, j;
int skew;
const char *s1, *s2;
double scale, offset, min_mapped, max_mapped;
/* FIXME: for fixed layout, require signals to match exactly */
for (i = 0; i < nsig; i++) {
for (j = 0; j < nmssig; j++) {
if (!strcmp(siginfo[i].desc, mssiginfo[j].desc)) {
/* mark signal as used */
mssigused[j] = 1;
break;
}
}
if (j == nmssig) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"signal not present in layout");
/*DESCRIPTION: signal not present in layout *//*
A signal in this segment is not listed in the record's
layout header. As a result, this signal is not usable,
since applications will not be able to see it.
*/
}
else {
s1 = siginfo[i].units;
s2 = mssiginfo[j].units;
if ((s1 && !s2) || (s2 && !s1) ||
(s1 && s2 && strcmp(s1, s2) != 0)) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"wrong units (%s, expected %s)",
s1 ? s1 : "NULL",
s2 ? s2 : "NULL");
/*DESCRIPTION: wrong units (%s, expected %s) *//*
A signal in this segment uses different physical units than
the units specified in the layout header. Physical units
must match in order for sample values to be scaled
correctly.
*/
}
if (siginfo[i].spf != mssiginfo[j].spf) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"wrong spf (%d, expected %d)",
siginfo[i].spf, mssiginfo[j].spf);
/*DESCRIPTION: wrong spf (%d, expected %d) *//*
A signal in this segment has a different number of samples
per frame than the number specified in the layout header.
The number of samples per frame for each signal must be
consistent throughout the record in order for applications
to read the signals correctly.
*/
}
/* FIXME: I'm pretty sure this actually doesn't work with
10.6... and skewing is somewhat broken for VLMS
anyway... */
skew = wfdbgetskew(i);
if (skew != mssigskew[j]) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"wrong skew (%d, expected %d)",
skew, mssigskew[j]);
/*DESCRIPTION: wrong skew (%d, expected %d) *//*
A signal in this segment has a different skew than the skew
specified in the layout header. The skew for each signal
must be consistent throughout the record in order for
applications to read the signals correctly.
*/
}
if (siginfo[i].gain > mssiginfo[j].gain) {
print_msg(MSG_WARNING, msrec, rec, NULL, i,
"loss of precision due to sample scaling");
/*DESCRIPTION: loss of precision due to sample scaling *//*
A signal in this segment has a larger gain than the gain
specified in the layout header. As a result, the scaled
samples seen by the application will be less precise than
the original data. Some programs that generate
multi-segment records have been known to cause this problem
by rounding fractional gain values, or by deliberately
reducing precision in order to avoid 16-bit overflows (which
are not an issue for modern WFDB applications.)
*/
}
if (minv[i] < maxv[i] && siginfo[i].gain != 0.0) {
scale = mssiginfo[j].gain / siginfo[i].gain;
offset = mssiginfo[j].baseline - scale * siginfo[i].baseline;
min_mapped = minv[i] * scale + offset;
max_mapped = maxv[i] * scale + offset;
/* min_mapped could be greater than
max_mapped if one gain is negative.
that'd be weird but arguably useful in
some cases */
if (min_mapped > WFDB_SAMPLE_MAX + 0.5 ||
max_mapped > WFDB_SAMPLE_MAX + 0.5 ||
min_mapped < WFDB_SAMPLE_MIN - 0.5 ||
max_mapped < WFDB_SAMPLE_MIN - 0.5) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"integer overflow in scaled range"
" (%.0f, %.0f)", min_mapped, max_mapped);
/*DESCRIPTION: integer overflow in scaled range (%.0f, %.0f) *//*
The scaled range of this signal (i.e. the range of sample
values when translated into physical units according to the
segment header, then translated back to ADC units according
to the layout header) is larger than the range of sample
values supported by this version of the WFDB library.
(Currently, the maximum range is 32 bits on all supported
platforms.)
*/
}
else if (msminv[j] < msmaxv[j] &&
((WFDB_Sample) min_mapped > msmaxv[j] ||
(WFDB_Sample) max_mapped > msmaxv[j] ||
(WFDB_Sample) min_mapped < msminv[j] ||
(WFDB_Sample) max_mapped < msminv[j])) {
print_msg(MSG_WARNING, msrec, rec, NULL, i,
"scaled range (%d, %d) exceeds"
" expected range (%d, %d)",
(int) min_mapped, (int) max_mapped,
msminv[j], msmaxv[j]);
/*DESCRIPTION: scaled range (%d, %d) exceeds expected range (%d, %d) *//*
The scaled range of this signal (i.e. the range of sample
values when translated into physical units according to the
segment header, then translated back to ADC units according
to the layout header) exceeds the stated ADC range given in
the layout header. This will cause problems for
applications that rely on knowing the possible range of
sample values, such as applications that convert the signal
into other formats. The 'resolution' and 'zero' values in
the layout header should be set according to the maximum
possible range of scaled sample values, rounded up to the
next power of two.
*/
}
}
}
}
}
/* Check if signal types are known. */
static void check_calinfo(const char *msrec, const char *rec,
int ns, const WFDB_Siginfo *si)
{
int i;
WFDB_Calinfo info;
const char *units;
if (calopen_failed)
return;
for (i = 0; i < ns; i++) {
if (si[i].desc && is_auto_sigdesc(si[i].desc)) {
print_msg(MSG_WARNING, msrec, rec, NULL, i,
"signal description missing");
/*DESCRIPTION: signal description missing *//*
A signal in this record has no description, or a generic
description such as "record foo, signal 0". Signals should
be given a meaningful description, both for users' and for
applications' benefit.
*/
continue;
}
else if (si[i].desc && is_all_digits(si[i].desc)) {
print_msg(MSG_WARNING, msrec, rec, NULL, i,
"signal description is a number");
/*DESCRIPTION: signal description is a number *//*
A signal in this record has a description that is a decimal
number. Signals should be given a meaningful description,
both for users' and for applications' benefit. Moreover,
this name can cause confusion when using applications that
allow signals to be identified either by name or by number.
*/
continue;
}
else if (si[i].desc && has_control_chars(si[i].desc)) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"control characters in signal description");
/*DESCRIPTION: control characters in signal description *//*
A signal in this record includes control characters in its
description. Signal descriptions must be plain text.
*/
continue;
}
units = (si[i].units ? si[i].units : "mV");
if (has_control_chars(units)) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"control characters in unit name");
/*DESCRIPTION: control characters in unit name *//*
A signal in this record includes control characters in its
physical units. Unit names must be plain text and may not
include whitespace.
*/
continue;
}
if (getcal(si[i].desc, (char *) units, &info) < 0) {
print_msg(MSG_WARNING, msrec, rec, NULL, -1,
"signal %s (units %s) not listed in calibration file",
si[i].desc, units);
/*DESCRIPTION: signal %s (units %s) not listed in calibration file *//*
A signal in this record does not correspond to a known entry
in the WFDB calibration file. As a result, viewers will use
a default plotting scale for this signal, which tends to
give poor results. If none of the existing signal types
accurately describe the signal, a new type should be added
to the calibration file.
*/
}
}
}
/* Get maximum resolution of a given WFDB signal format. */
static int format_bits(int fmt)
{
switch (fmt) {
case 0: return 0;
case 80: return 8;
case 310: return 10;
case 311: return 10;
case 212: return 12;
case 16: return 16;
case 61: return 16;
case 160: return 16;
case 24: return 24;
case 32: return 32;
case 8: return 32;
default:
if (fmt > 100 && fmt % 100 > 0 && fmt % 100 <= 32)
return (fmt % 100);
else
return -1;
}
}
/* Get the internal sample value corresponding to WFDB_INVALID_SAMPLE. */
static WFDB_Sample format_sample_sentinel(int fmt)
{
int bits = format_bits(fmt);
if (fmt == 8 || bits < 2)
return WFDB_INVALID_SAMPLE;
return -((WFDB_Sample) 1 << (bits - 2)) * 2;
}
/* Check consistency of a list of annotators. */
static void check_annotators(char *msrec, char *rec,
char **annotators, int optional,
WFDB_Time nframes)
{
WFDB_Time nsamp;
WFDB_Anninfo ai;
WFDB_Annotation annot, prev;
int type_predefined[ACMAX+1], type_defined[ACMAX+1],
type_described[ACMAX+1], type_checked[ACMAX+1];
int warned_invalid;
int i, type;
int first;
char empty[] = "", invalid[] = "\377";
nsamp = nframes * getspf();
for (i = 0; i <= ACMAX; i++)
type_predefined[i] = (anndesc(i) && anndesc(i)[0] != '\377');
for (; *annotators; annotators++) {
ai.name = *annotators;
ai.stat = WFDB_READ;
/* reset type strings so that we can see what custom types are
defined by the annotation file */
for (i = 0; i <= ACMAX; i++) {
if (type_predefined[i]) {
setannstr(i, ecgstr(i));
setanndesc(i, empty);
}
else {
setannstr(i, invalid);
setanndesc(i, invalid);
}
type_checked[i] = 0;
}
warned_invalid = 0;
i = annopen(rec, &ai, 1);
if (i < 0) {
if (i != -3 || !optional) {
print_msg(MSG_ERROR, msrec, rec, ai.name, -1, "_annopen");
/*DESCRIPTION: _annopen *//*
This annotation file cannot be read or is not properly
formatted.
*/
}
continue;
}
/* FIXME: time resolution should be explicit
(especially if record is multifrequency, but it's
a good idea in general.)
also, make sure we are correctly checking bounds
for multifrequency records (both EDF and non-EDF)
*/
for (i = 0; i <= ACMAX; i++) {
type_defined[i] = (annstr(i) && annstr(i)[0] != '\377');
type_described[i] = (anndesc(i) && anndesc(i)[0] != '\377');
if (type_defined[i] && !type_described[i]) {
print_msg(MSG_WARNING, msrec, rec, ai.name, -1,
"no description for annotation type '%s'",
annstr(i));
/*DESCRIPTION: no description for annotation type '%s' *//*
A custom annotation type defined in this file does not
include a description. This usually means that the program
that created the annotation file called the setannstr()
function without calling setanndesc().
*/
}
if (type_defined[i] && strann(annstr(i)) != i) {
print_msg(MSG_WARNING, msrec, rec, ai.name, -1,
"multiple annotation codes with mnemonic '%s'",
annstr(i));
/*DESCRIPTION: multiple annotation codes with mnemonic '%s' *//*
A custom annotation type defined in this file uses a
mnemonic that conflicts with another custom annotation type,
or with a built-in annotation type. To avoid ambiguity,
annotation mnemonics should be unique.
*/
}
}
first = 1;
prev.time = 0;
while (getann(0, &annot)) {
if (first) {
first = 0;
if (annot.time < 0) {
print_msg(MSG_WARNING, msrec, rec, ai.name, -1,
"first annotation is at s%ld",
annot.time);
/*DESCRIPTION: first annotation is at s%ld *//*
Annotations in this file occur before the start of the
record. This is usually a mistake.
*/
}
}
else {
if (annot.time < prev.time) {
print_msg(MSG_ERROR, msrec, rec, ai.name, -1,
"annotations out of order (s%ld > s%ld)",
prev.time, annot.time);
/*DESCRIPTION: annotations out of order (s%ld > s%ld) *//*
Annotations in this file are not stored in chronological
order. If the program that generates the annotations writes
them out of order, they should be sorted afterwards using
sortann (which is done automatically if the annotations are
written using the WFDB library.)
*/
}
}
type = annot.anntyp;
if (type < 0 || type > ACMAX) {
if (!warned_invalid) {
print_msg(MSG_ERROR, msrec, rec, ai.name, -1,
"invalid annotation [%d] at s%ld",
type, annot.time);
/*DESCRIPTION: invalid annotation [%d] at s%ld *//*
This file contains one or more annotations with invalid
types. These may have been created by a broken application,
or by a future version of WFDB that is incompatible with the
version you are using.
*/
warned_invalid = 1;
}
}
else if (!type_checked[type]) {
if (type == 0) {
print_msg(MSG_WARNING, msrec, rec, ai.name, -1,
"null annotation at s%ld",
annot.time);
/*DESCRIPTION: null annotation at s%ld *//*
This file contains one or more "type 0" annotations.
Annotation type 0 is reserved for internal use, and should
not appear in published annotation files.
*/
}
else if (!type_defined[type]) {
print_msg(MSG_WARNING, msrec, rec, ai.name, -1,
"undefined annotation [%d] at s%ld",
type, annot.time);
/*DESCRIPTION: undefined annotation [%d] at s%ld *//*
This file contains one or more custom annotation types, but
these types have not been defined in the file header.
Custom annotation types should be defined by calling
setannstr() and setanndesc() before writing the annotation
file.
*/
}
type_checked[type] = 1;
}
prev = annot;
}
if (nsamp > 0 && prev.time > nsamp) {
print_msg(MSG_WARNING, msrec, rec, ai.name, -1,
"last annotation is after end of signals (s%ld)",
prev.time);
/*DESCRIPTION: last annotation is after end of signals (s%ld) *//*
Annotations in this file occur after the end of the signals.
This is usually a mistake.
*/
}
}
for (i = 0; i <= ACMAX; i++) {
if (type_predefined[i])
setanndesc(i, empty);
else
setanndesc(i, invalid);
}
}
/* Check consistency of a single-segment record */
static void check_segment(char *msrec, char *rec,
char *wfdbpath, int nheasig,
const char *baset, WFDB_Time nframes,
WFDB_Time *nframes_read)
{
WFDB_Seginfo *segs;
WFDB_Sample *vec = NULL, *minv = NULL, *maxv = NULL, *invv = NULL,
*minsamp = NULL, *maxsamp = NULL, v;
unsigned int *sum = NULL;
int *sample_warned = NULL;
int totalspf, maxspf, i, j, s, fsmin, fsmax, dmax, stat,
plusadcres, minusadcres, baseadcres;
WFDB_Frequency ffreq;
WFDB_Time t;
wfdbquit();
setwfdb(wfdbpath);
if (nframes_read)
*nframes_read = 0;
if (msrec)
/* kludge to ensure msrec is added to the search path, as if
we called isigopen() */
wfdbfile((char *) "hea", msrec);
if (nheasig == 0) {
nheasig = isigopen(rec, NULL, 0);
if (nheasig < 0) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1, "_isigopen");
/*DESCRIPTION: _isigopen *//*
The header file for this record (or the EDF header) cannot
be read or is not properly formatted.
*/
return;
}
else if (nheasig == 0) {
if (msrec) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"segment with no signals");
/*DESCRIPTION: segment with no signals *//*
This segment contains no signals. Empty portions of the
record must be indicated with a gap segment ('~'), rather
than a header file with no signals.
*/
}
else {
print_msg(MSG_INFO, msrec, rec, NULL, -1, "no signals");
/*DESCRIPTION: no signals *//*
This record contains no signals, which is usually a mistake.
*/
}
return;
}
}
SALLOC(siginfo, nheasig, sizeof(WFDB_Siginfo));
nsig = isigopen(rec, siginfo, -nheasig);
if (nsig < 0) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1, "_isigopen");
SFREE(siginfo);
return;
}
else if (nsig != nheasig) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"unable to read signal info");
/*DESCRIPTION: unable to read signal info *//*
The WFDB library was unable to read the metadata for all
signals. This is probably a bug.
*/
SFREE(siginfo);
return;
}
totalspf = 0;
maxspf = 1;
for (i = 0; i < nsig; i++) {
totalspf += siginfo[i].spf;
if (siginfo[i].spf > maxspf)
maxspf = siginfo[i].spf;
}
if (nsig > 0) {
SUALLOC(minv, nsig, sizeof(WFDB_Sample));
SUALLOC(maxv, nsig, sizeof(WFDB_Sample));
SUALLOC(vec, totalspf, sizeof(WFDB_Sample));
SUALLOC(invv, nsig, sizeof(WFDB_Sample));
SUALLOC(minsamp, nsig, sizeof(WFDB_Sample));
SUALLOC(maxsamp, nsig, sizeof(WFDB_Sample));
SUALLOC(sum, nsig, sizeof(unsigned int));
SUALLOC(sample_warned, nsig, sizeof(int));
}
if (siginfo[0].nsamp < 0) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"negative record length (%ld)", siginfo[0].nsamp);
/*DESCRIPTION: negative record length (%ld) *//*
The length specified in this record's header file is
negative. This makes no sense.
*/
}
else if (siginfo[0].nsamp == LONG_MAX ||
siginfo[0].nsamp - 1 >= 2147483647 / maxspf) {
/* Note that this will complain about records that are exactly
LONG_MAX frames long (since isigopen can't distinguish this
from a record longer than LONG_MAX.) This isn't really a
problem; records anywhere near that length should be split
up if 32-bit compatibility is an issue. */
print_msg(MSG_INFO, msrec, rec, NULL, -1,
"record length greater than 2^31 samples");
/*DESCRIPTION: record length greater than 2^31 samples *//*
The length of this record is greater than 2,147,483,647
samples, which will cause problems for 32-bit applications.
*/
}
setgvmode(WFDB_LOWRES);
ffreq = sampfreq(NULL);
if (ffreq <= 0.0) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"record frame frequency invalid");
/*DESCRIPTION: record frame frequency invalid *//*
The frame frequency of this record is invalid or
unspecified.
*/
}
else if (msffreq > 0.0 && ffreq != msffreq) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"wrong frame frequency (%g, expected %g)",
ffreq, msffreq);
/*DESCRIPTION: wrong frame frequency (%g, expected %g) *//*
The frame frequency of this segment does not match the frame
frequency of the master header.
*/
}
if (baset && cmp_base_times(baset, mstimstr(0))) {
print_msg(MSG_WARNING, msrec, rec, NULL, -1,
"wrong base time (%s, expected %s)",
mstimstr(0), baset);
/*DESCRIPTION: wrong base time (%s, expected %s) *//*
The starting time (and/or date) of this segment are not
consistent with the starting time of the master header.
*/
}
if (getseginfo(&segs) > 0) {
print_msg(MSG_ERROR, msrec, rec, NULL, -1,
"nested multi-segment record");
/*DESCRIPTION: nested multi-segment record *//*
This segment is a multi-segment record. Multi-segment
records may not be nested within another multi-segment
record.
*/
}
/* Check signal descriptions */
check_sigdesc(msrec, rec, nsig, siginfo);
/* Check that signals are known */
if (!msrec)
check_calinfo(msrec, rec, nsig, siginfo);
/* Check signal information */
for (i = 0; i < nsig; i++) {
if (siginfo[i].fmt == 0)
continue;
if (i == 0 || siginfo[i].group != siginfo[i - 1].group) {
if (bad_record_name(siginfo[i].fname)) {
print_msg(MSG_WARNING, msrec, rec, NULL, -1,
"problematic file name: %s",
siginfo[i].fname);
/*DESCRIPTION: problematic file name: %s *//*
The name of this signal file contains characters that may be
problematic on some platforms. The only characters that are
universally safe to use are the ASCII digits (0-9), letters
(a-z, A-Z), underscore (_), dot and dash (., -) other than
at the beginning of a file name, and slash (/) used as a
directory separator. Other characters are potentially
problematic, as they are either considered special by the
shell, disallowed or considered special in URLs, disallowed
in Windows filenames, or considered special by the WFDB
library or applications themselves. Certain names (such as
'con' and 'aux') are also forbidden as Windows filenames.
*/
}
check_file_size(msrec, rec, siginfo[i].fname);
}
if (siginfo[i].fmt == 8) {
print_msg(MSG_WARNING, msrec, rec, NULL, i,
"signal stored in difference format");
/*DESCRIPTION: signal stored in difference format *//*
A signal in this record is stored in a "format 8" compressed
data file. This format is not recommended, as it causes
problems for applications that do not read the signal file
sequentially. (The format was designed as an intermediate
format for data collection on embedded systems with
extremely limited storage, but nowadays there are much
better options.)
*/
}
if (siginfo[i].gain == 0.0) {
if (msrec) {
print_msg(MSG_ERROR, msrec, rec, NULL, i,
"gain not specified");
/*DESCRIPTION: gain not specified *//*
The gain of this signal is not specified. The gain must be
given explicitly for all signals and all segments of a
multi-segment record. Note that dimensionless values should
typically use units of "NU".
*/
}
else {
print_msg(MSG_WARNING, msrec, rec, NULL, i,
"gain not specified");
}
}
else if (siginfo[i].gain < 0.0) {
print_msg(MSG_INFO, msrec, rec, NULL, i,
"negative gain (%g)", siginfo[i].gain);
/*DESCRIPTION: negative gain (%g) *//*
The gain of this signal is negative (meaning that larger
sample values represent smaller physical values.) This is
usually a mistake.
*/
}
if (siginfo[i].adcres == 0) {
print_msg(MSG_WARNING, msrec, rec, NULL, i,