Skip to content

Commit 070c20b

Browse files
committed
remove redundant date time parsing functions
1 parent d5512ca commit 070c20b

File tree

2 files changed

+11
-59
lines changed

2 files changed

+11
-59
lines changed

wfdb/io/_header.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from calendar import monthrange
21
from collections import OrderedDict
32
import numpy as np
43
import os

wfdb/io/record.py

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
# The check_field_cohesion() function will be called in wrheader which checks all the header fields.
66
# The check_sig_cohesion() function will be called in wrsamp in wrdat to check the d_signal against the header fields.
77

8-
from calendar import monthrange
8+
99
from collections import OrderedDict
10+
import datetime
1011
import multiprocessing
1112
import numpy as np
1213
import os
@@ -105,9 +106,12 @@ def check_field(self, field, channels=None):
105106
if self.sig_len <0:
106107
raise ValueError('sig_len must be a non-negative integer')
107108
elif field == 'base_time':
108-
_ = parse_timestring(self.base_time)
109+
try:
110+
_ = datetime.datetime.strptime(self.base_time, '%H:%M/%S.%f')
111+
except ValueError:
112+
_ = datetime.datetime.strptime(self.base_time, '%H:%M/%S')
109113
elif field == 'base_date':
110-
_ = parse_datestring(self.base_date)
114+
_ = datetime.datetime.strptime(self.base_date, '%d/%m/%Y')
111115

112116
# Signal specification fields. Lists of elements to check.
113117
elif field in _header.sig_field_specs:
@@ -422,8 +426,10 @@ def wrsamp(self, expanded=False, write_dir=''):
422426

423427

424428
def arrange_fields(self, channels, expanded=False):
425-
# Arrange/edit object fields to reflect user channel and/or signal range input
426-
# Account for case when signals are expanded
429+
"""
430+
Arrange/edit object fields to reflect user channel and/or signal range input
431+
Account for case when signals are expanded
432+
"""
427433

428434
# Rearrange signal specification fields
429435
for field in _header.sig_field_specs:
@@ -1295,59 +1301,6 @@ def wrsamp(record_name, fs, units, sig_name, p_signal=None, d_signal=None,
12951301
record.wrsamp(write_dir=write_dir)
12961302

12971303

1298-
# Time string parser for WFDB header - H(H):M(M):S(S(.sss)) format.
1299-
def parse_timestring(timestring):
1300-
times = re.findall("(?P<hours>\d{1,2}):(?P<minutes>\d{1,2}):(?P<seconds>\d{1,2}[.\d+]*)", timestring)
1301-
1302-
if not times:
1303-
raise ValueError("Invalid time string: "+timestring+". Acceptable format is: 'Hours:Minutes:Seconds'")
1304-
else:
1305-
hours, minutes, seconds = times[0]
1306-
1307-
if not hours or not minutes or not seconds:
1308-
raise ValueError("Invalid time string: "+timestring+". Acceptable format is: 'Hours:Minutes:Seconds'")
1309-
1310-
hours = int(hours)
1311-
minutes = int(minutes)
1312-
seconds = float(seconds)
1313-
1314-
if int(hours) >23:
1315-
raise ValueError('hours must be < 24')
1316-
elif hours<0:
1317-
raise ValueError('hours must be positive')
1318-
if minutes>59:
1319-
raise ValueError('minutes must be < 60')
1320-
elif minutes<0:
1321-
raise ValueError('minutes must be positive')
1322-
if seconds>59:
1323-
raise ValueError('seconds must be < 60')
1324-
elif seconds<0:
1325-
raise ValueError('seconds must be positive')
1326-
1327-
return (hours, minutes, seconds)
1328-
1329-
# Date string parser for WFDB header - DD/MM/YYYY
1330-
def parse_datestring(datestring):
1331-
dates = re.findall(r"(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})", datestring)
1332-
1333-
if not dates:
1334-
raise ValueError("Invalid date string. Acceptable format is: 'DD/MM/YYYY'")
1335-
else:
1336-
day, month, year = dates[0]
1337-
1338-
day = int(day)
1339-
month = int(month)
1340-
year = int(year)
1341-
1342-
if year<1:
1343-
raise ValueError('year must be positive')
1344-
if month<1 or month>12:
1345-
raise ValueError('month must be between 1 and 12')
1346-
if day not in range(1, monthrange(year, month)[1]+1):
1347-
raise ValueError('day does not exist for specified year and month')
1348-
1349-
return (day, month, year)
1350-
13511304
# Returns the unique elements in a list in the order that they appear.
13521305
# Also returns the indices of the original list that correspond to each output element.
13531306
def orderedsetlist(fulllist):

0 commit comments

Comments
 (0)