1
1
import numpy as np
2
2
import pandas as pd
3
- import sys
4
3
import re
5
4
import os
6
- from IPython .display import display
7
5
from . import _headers
8
6
from . import downloads
9
7
@@ -100,7 +98,7 @@ def checkfields(self):
100
98
for field in ['recordname' , 'annotator' , 'annsamp' , 'anntype' ]:
101
99
if getattr (self , field ) is None :
102
100
print ('The ' , field , ' field is mandatory for writing annotation files' )
103
- sys . exit ( )
101
+ raise Exception ( 'Missing required annotation field' )
104
102
105
103
# Check all set fields
106
104
for field in annfields :
@@ -115,79 +113,77 @@ def checkfield(self, field):
115
113
if field in ['recordname' , 'annotator' , 'fs' ]:
116
114
# Check the field type
117
115
if type (getattr (self , field )) not in annfieldtypes [field ]:
118
- print ('The ' + field + ' field must be one of the following types:' )
119
- display (annfieldtypes [field ])
120
- sys .exit ()
116
+ print (annfieldtypes [field ])
117
+ raise TypeError ('The ' + field + ' field must be one of the above types.' )
121
118
122
119
# Field specific checks
123
120
if field == 'recordname' :
124
121
# Allow letters, digits, hyphens, and underscores.
125
122
acceptedstring = re .match ('[-\w]+' , self .recordname )
126
123
if not acceptedstring or acceptedstring .string != self .recordname :
127
- sys . exit ('recordname must only comprise of letters, digits, hyphens, and underscores.' )
124
+ raise ValueError ('recordname must only comprise of letters, digits, hyphens, and underscores.' )
128
125
elif field == 'annotator' :
129
126
# Allow letters only
130
127
acceptedstring = re .match ('[a-zA-Z]+' , self .annotator )
131
128
if not acceptedstring or acceptedstring .string != self .annotator :
132
- sys . exit ('annotator must only comprise of letters' )
129
+ raise ValueError ('annotator must only comprise of letters' )
133
130
elif field == 'fs' :
134
131
if self .fs <= 0 :
135
- sys . exit ('The fs field must be a non-negative number' )
132
+ raise ValueError ('The fs field must be a non-negative number' )
136
133
137
134
else :
138
135
fielditem = getattr (self , field )
139
136
140
137
# Ensure the field item is a list or array.
141
138
if type (fielditem ) not in [list , np .ndarray ]:
142
- print ('The ' , field , ' field must be a list or numpy array' )
143
- sys .exit ()
139
+ raise TypeError ('The ' + field + ' field must be a list or numpy array' )
144
140
145
141
# Check the data types of the elements
146
142
# annsamp and anntype may NOT have nones. Others may.
147
143
if field in ['annsamp' ,'anntype' ]:
148
144
for item in fielditem :
149
145
if type (item ) not in annfieldtypes [field ]:
150
146
print ("All elements of the '" , field , "' field must be one of the following types:" )
151
- display (annfieldtypes [field ])
147
+ print (annfieldtypes [field ])
152
148
print ("All elements must be present" )
153
- sys . exit ()
149
+ raise Exception ()
154
150
else :
155
151
for item in fielditem :
156
152
if item is not None and type (item ) not in annfieldtypes [field ]:
157
153
print ("All elements of the '" , field , "' field must be one of the following types:" )
158
- display (annfieldtypes [field ])
154
+ print (annfieldtypes [field ])
159
155
print ("Elements may also be set to 'None'" )
160
- sys . exit ()
156
+ raise Exception ()
161
157
162
158
# Field specific checks
163
159
# The C WFDB library stores num/sub/chan as chars.
164
160
if field == 'annsamp' :
165
161
sampdiffs = np .concatenate (([self .annsamp [0 ]], np .diff (self .annsamp )))
166
162
if min (self .annsamp ) < 0 :
167
- sys . exit ("The 'annsamp' field must only contain non-negative integers" )
163
+ raise ValueError ("The 'annsamp' field must only contain non-negative integers" )
168
164
if min (sampdiffs ) < 0 :
169
- sys . exit ("The 'annsamp' field must contain monotonically increasing sample numbers" )
165
+ raise ValueError ("The 'annsamp' field must contain monotonically increasing sample numbers" )
170
166
if max (sampdiffs ) > 2147483648 :
171
- sys . exit ('WFDB annotation files cannot store sample differences greater than 2**31' )
167
+ raise ValueError ('WFDB annotation files cannot store sample differences greater than 2**31' )
172
168
elif field == 'anntype' :
173
169
# Ensure all fields lie in standard WFDB annotation codes
174
170
if set (self .anntype ) - set (annsyms .values ()) != set ():
175
171
print ("The 'anntype' field contains items not encoded in the WFDB annotation library." )
176
172
print ('To see the valid annotation codes call: showanncodes()' )
177
173
print ('To transfer non-encoded anntype items into the aux field call: self.type2aux()' )
178
- sys . exit ()
174
+ raise Exception ()
179
175
elif field == 'subtype' :
180
176
# signed character
181
177
if min (self .subtype ) < 0 or max (self .subtype ) > 127 :
182
- sys . exit ("The 'subtype' field must only contain non-negative integers up to 127" )
178
+ raise ValueError ("The 'subtype' field must only contain non-negative integers up to 127" )
183
179
elif field == 'chan' :
184
180
# unsigned character
185
181
if min (self .chan ) < 0 or max (self .chan ) > 255 :
186
- sys . exit ("The 'chan' field must only contain non-negative integers up to 255" )
182
+ raise ValueErrort ("The 'chan' field must only contain non-negative integers up to 255" )
187
183
elif field == 'num' :
188
184
# signed character
189
185
if min (self .num ) < 0 or max (self .num ) > 127 :
190
- sys . exit ("The 'num' field must only contain non-negative integers up to 127" )
186
+ raise ValueError ("The 'num' field must only contain non-negative integers up to 127" )
191
187
#elif field == 'aux': # No further conditions for aux field.
192
188
193
189
@@ -199,7 +195,7 @@ def checkfieldcohesion(self):
199
195
for field in ['annsamp' , 'anntype' , 'num' , 'subtype' , 'chan' , 'aux' ]:
200
196
if getattr (self , field ) is not None :
201
197
if len (getattr (self , field )) != nannots :
202
- sys . exit ("All written annotation fields: ['annsamp', 'anntype', 'num', 'subtype', 'chan', 'aux'] must have the same length" )
198
+ raise ValueError ("All written annotation fields: ['annsamp', 'anntype', 'num', 'subtype', 'chan', 'aux'] must have the same length" )
203
199
204
200
# Write an annotation file
205
201
def wrannfile (self ):
@@ -261,10 +257,10 @@ def type2aux(self):
261
257
262
258
# Ensure that anntype is a list of strings
263
259
if type (self .anntype )!= list :
264
- sys . exit ('anntype must be a list' )
260
+ raise TypeError ('anntype must be a list' )
265
261
for at in self .anntype :
266
262
if type (at ) != str :
267
- sys . exit ('anntype elements must all be strings' )
263
+ raise TypeError ('anntype elements must all be strings' )
268
264
269
265
external_anntypes = set (self .anntype ) - set (annsyms .values ())
270
266
@@ -409,7 +405,7 @@ def showanncodes():
409
405
Usage:
410
406
showanncodes()
411
407
"""
412
- display (symcodes )
408
+ print (symcodes )
413
409
414
410
## ------------- Reading Annotations ------------- ##
415
411
0 commit comments