11import numpy as np
22import pandas as pd
3- import sys
43import re
54import os
65from . import _headers
@@ -99,7 +98,7 @@ def checkfields(self):
9998 for field in ['recordname' , 'annotator' , 'annsamp' , 'anntype' ]:
10099 if getattr (self , field ) is None :
101100 print ('The ' , field , ' field is mandatory for writing annotation files' )
102- sys . exit ( )
101+ raise Exception ( 'Missing required annotation field' )
103102
104103 # Check all set fields
105104 for field in annfields :
@@ -114,32 +113,30 @@ def checkfield(self, field):
114113 if field in ['recordname' , 'annotator' , 'fs' ]:
115114 # Check the field type
116115 if type (getattr (self , field )) not in annfieldtypes [field ]:
117- print ('The ' + field + ' field must be one of the following types:' )
118116 print (annfieldtypes [field ])
119- sys . exit ( )
117+ raise TypeError ( 'The ' + field + ' field must be one of the above types.' )
120118
121119 # Field specific checks
122120 if field == 'recordname' :
123121 # Allow letters, digits, hyphens, and underscores.
124122 acceptedstring = re .match ('[-\w]+' , self .recordname )
125123 if not acceptedstring or acceptedstring .string != self .recordname :
126- 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.' )
127125 elif field == 'annotator' :
128126 # Allow letters only
129127 acceptedstring = re .match ('[a-zA-Z]+' , self .annotator )
130128 if not acceptedstring or acceptedstring .string != self .annotator :
131- sys . exit ('annotator must only comprise of letters' )
129+ raise ValueError ('annotator must only comprise of letters' )
132130 elif field == 'fs' :
133131 if self .fs <= 0 :
134- sys . exit ('The fs field must be a non-negative number' )
132+ raise ValueError ('The fs field must be a non-negative number' )
135133
136134 else :
137135 fielditem = getattr (self , field )
138136
139137 # Ensure the field item is a list or array.
140138 if type (fielditem ) not in [list , np .ndarray ]:
141- print ('The ' , field , ' field must be a list or numpy array' )
142- sys .exit ()
139+ raise TypeError ('The ' + field + ' field must be a list or numpy array' )
143140
144141 # Check the data types of the elements
145142 # annsamp and anntype may NOT have nones. Others may.
@@ -149,44 +146,44 @@ def checkfield(self, field):
149146 print ("All elements of the '" , field , "' field must be one of the following types:" )
150147 print (annfieldtypes [field ])
151148 print ("All elements must be present" )
152- sys . exit ()
149+ raise Exception ()
153150 else :
154151 for item in fielditem :
155152 if item is not None and type (item ) not in annfieldtypes [field ]:
156153 print ("All elements of the '" , field , "' field must be one of the following types:" )
157154 print (annfieldtypes [field ])
158155 print ("Elements may also be set to 'None'" )
159- sys . exit ()
156+ raise Exception ()
160157
161158 # Field specific checks
162159 # The C WFDB library stores num/sub/chan as chars.
163160 if field == 'annsamp' :
164161 sampdiffs = np .concatenate (([self .annsamp [0 ]], np .diff (self .annsamp )))
165162 if min (self .annsamp ) < 0 :
166- sys . exit ("The 'annsamp' field must only contain non-negative integers" )
163+ raise ValueError ("The 'annsamp' field must only contain non-negative integers" )
167164 if min (sampdiffs ) < 0 :
168- sys . exit ("The 'annsamp' field must contain monotonically increasing sample numbers" )
165+ raise ValueError ("The 'annsamp' field must contain monotonically increasing sample numbers" )
169166 if max (sampdiffs ) > 2147483648 :
170- 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' )
171168 elif field == 'anntype' :
172169 # Ensure all fields lie in standard WFDB annotation codes
173170 if set (self .anntype ) - set (annsyms .values ()) != set ():
174171 print ("The 'anntype' field contains items not encoded in the WFDB annotation library." )
175172 print ('To see the valid annotation codes call: showanncodes()' )
176173 print ('To transfer non-encoded anntype items into the aux field call: self.type2aux()' )
177- sys . exit ()
174+ raise Exception ()
178175 elif field == 'subtype' :
179176 # signed character
180177 if min (self .subtype ) < 0 or max (self .subtype ) > 127 :
181- 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" )
182179 elif field == 'chan' :
183180 # unsigned character
184181 if min (self .chan ) < 0 or max (self .chan ) > 255 :
185- 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" )
186183 elif field == 'num' :
187184 # signed character
188185 if min (self .num ) < 0 or max (self .num ) > 127 :
189- 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" )
190187 #elif field == 'aux': # No further conditions for aux field.
191188
192189
@@ -198,7 +195,7 @@ def checkfieldcohesion(self):
198195 for field in ['annsamp' , 'anntype' , 'num' , 'subtype' , 'chan' , 'aux' ]:
199196 if getattr (self , field ) is not None :
200197 if len (getattr (self , field )) != nannots :
201- 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" )
202199
203200 # Write an annotation file
204201 def wrannfile (self ):
@@ -260,10 +257,10 @@ def type2aux(self):
260257
261258 # Ensure that anntype is a list of strings
262259 if type (self .anntype )!= list :
263- sys . exit ('anntype must be a list' )
260+ raise TypeError ('anntype must be a list' )
264261 for at in self .anntype :
265262 if type (at ) != str :
266- sys . exit ('anntype elements must all be strings' )
263+ raise TypeError ('anntype elements must all be strings' )
267264
268265 external_anntypes = set (self .anntype ) - set (annsyms .values ())
269266
0 commit comments