Skip to content

Commit 5bfe3aa

Browse files
committed
adding sampsperframe framework
1 parent 24415d3 commit 5bfe3aa

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed

devtests.ipynb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@
4545
"wfdb.plotrec(rec,timeunits='seconds', ecggrids='all', figsize = (10, 4))"
4646
]
4747
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"metadata": {
52+
"collapsed": false
53+
},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"[1, 2, 3]\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"def stuff(a):\n",
65+
" a[0]=100\n",
66+
"\n",
67+
"x = [1,2,3]\n",
68+
"\n",
69+
"stuff(x[:])\n",
70+
"\n",
71+
"print(x)"
72+
]
73+
},
4874
{
4975
"cell_type": "code",
5076
"execution_count": null,

wfdb/_signals.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def wrdatfiles(self):
274274
# Read the samples from a single segment record's associated dat file(s)
275275
# 'channels', 'sampfrom', and 'sampto' are user desired input fields.
276276
# All other input arguments are specifications of the segment
277-
def rdsegment(filename, dirname, pbdir, nsig, fmt, siglen, byteoffset, sampsperframe, skew, sampfrom, sampto, channels):
277+
def rdsegment(filename, dirname, pbdir, nsig, fmt, siglen, byteoffset,
278+
sampsperframe, skew, sampfrom, sampto, channels, smoothframes):
278279

279280
# Avoid changing outer variables
280281
byteoffset = byteoffset[:]
@@ -290,12 +291,12 @@ def rdsegment(filename, dirname, pbdir, nsig, fmt, siglen, byteoffset, sampsperf
290291
if skew[i] == None:
291292
skew[i] = 0
292293

293-
# Get the set of dat files, and the
294-
# channels that belong to each file.
294+
# Get the set of dat files, and the
295+
# channels that belong to each file.
295296
filename, datchannel = orderedsetlist(filename)
296297

297-
# Some files will not be read depending on input channels.
298-
# Get the the wanted fields only.
298+
# Some files will not be read depending on input channels.
299+
# Get the the wanted fields only.
299300
w_filename = [] # one scalar per dat file
300301
w_fmt = {} # one scalar per dat file
301302
w_byteoffset = {} # one scalar per dat file
@@ -323,14 +324,25 @@ def rdsegment(filename, dirname, pbdir, nsig, fmt, siglen, byteoffset, sampsperf
323324
for fn in w_channel:
324325
r_w_channel[fn] = [c - min(datchannel[fn]) for c in w_channel[fn]]
325326
out_datchannel[fn] = [channels.index(c) for c in w_channel[fn]]
326-
327-
# Allocate signal array
328-
signals = np.empty([sampto-sampfrom, len(channels)], dtype = 'int64')
327+
328+
# Signals with multiple samples/frame are smoothed
329+
if smoothframes:
330+
331+
# Allocate signal array
332+
signals = np.empty([sampto-sampfrom, len(channels)], dtype = 'int64')
333+
334+
# Read each wanted dat file and store signals
335+
for fn in w_filename:
336+
signals[:, out_datchannel[fn]] = rddat(fn, dirname, pbdir, w_fmt[fn], len(datchannel[fn]),
337+
siglen, w_byteoffset[fn], w_sampsperframe[fn], w_skew[fn], sampfrom, sampto)[:, r_w_channel[fn]]
338+
339+
# Return each sample in signals with multiple samples/frame
340+
else:
341+
signals=[]
342+
343+
for fn in w_filename:
344+
signals.append(rddat())
329345

330-
# Read each wanted dat file and store signals
331-
for fn in w_filename:
332-
signals[:, out_datchannel[fn]] = rddat(fn, dirname, pbdir, w_fmt[fn], len(datchannel[fn]),
333-
siglen, w_byteoffset[fn], w_sampsperframe[fn], w_skew[fn], sampfrom, sampto)[:, r_w_channel[fn]]
334346

335347
return signals
336348

wfdb/records.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def __init__(self, p_signals=None, d_signals=None,
320320
"""
321321
# Constructor
322322
def __init__(self, p_signals=None, d_signals=None,
323+
e_p_signals=None, e_d_signals=None,
323324
recordname=None, nsig=None,
324325
fs=None, counterfreq=None, basecounter=None,
325326
siglen=None, basetime=None, basedate=None,
@@ -338,6 +339,9 @@ def __init__(self, p_signals=None, d_signals=None,
338339

339340
self.p_signals = p_signals
340341
self.d_signals = d_signals
342+
self.e_p_signals = e_p_signals
343+
self.e_d_signals = e_d_signals
344+
341345

342346
self.filename=filename
343347
self.fmt=fmt
@@ -695,12 +699,14 @@ def multi_to_single(self):
695699
#------------------- Reading Records -------------------#
696700

697701
# Read a WFDB single or multi segment record. Return a Record or MultiRecord object
698-
def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True, pbdir = None, m2s = True):
702+
def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True, pbdir = None,
703+
m2s = True, smoothframes = True):
699704
"""Read a WFDB record and return the signal and record descriptors as attributes in a
700705
Record or MultiRecord object.
701706
702707
Usage:
703-
record = rdsamp(recordname, sampfrom=0, sampto=None, channels=None, physical=True, pbdir = None, m2s=True)
708+
record = rdsamp(recordname, sampfrom=0, sampto=None, channels=None, physical=True, pbdir = None,
709+
m2s=True, smoothframes = True)
704710
705711
Input arguments:
706712
- recordname (required): The name of the WFDB record to be read (without any file extensions).
@@ -715,8 +721,13 @@ def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True
715721
directory from which to find the required record files.
716722
eg. For record '100' in 'http://physionet.org/physiobank/database/mitdb', pbdir = 'mitdb'.
717723
- m2s (default=True): Flag used when reading multi-segment records. Specifies whether to
718-
directly return a wfdb MultiRecord object (false), or to convert it into and return a wfdb
724+
directly return a wfdb MultiRecord object (False), or to convert it into and return a wfdb
719725
Record object (True).
726+
- smoothframes (default=True): Flag used when reading records with signals having multiple
727+
samples per frame. Specifies whether to smooth the samples in signals with more than
728+
one sample per frame to return an mxn uniform numpy array as the d_signals or p_signals
729+
field (True), or to return a list of 1d numpy arrays containing every expanded sample as
730+
the e_d_signals or e_p_signals field (False).
720731
721732
Output argument:
722733
- record: The wfdb Record or MultiRecord object representing the contents of the record read.
@@ -752,17 +763,25 @@ def rdsamp(recordname, sampfrom=0, sampto=None, channels = None, physical = True
752763
record.checkreadinputs(sampfrom, sampto, channels)
753764
# A single segment record
754765
if type(record) == Record:
755-
# Read signals from the associated dat files that contain wanted channels
756-
record.d_signals = _signals.rdsegment(record.filename, dirname, pbdir, record.nsig, record.fmt, record.siglen,
757-
record.byteoffset, record.sampsperframe, record.skew,
758-
sampfrom, sampto, channels)
759-
# Arrange/edit the object fields to reflect user channel and/or signal range input
760-
record.arrangefields(channels, sampto - sampfrom)
761-
if physical == 1:
762-
# Perform dac to get physical signal
763-
record.p_signals = record.dac()
764-
# Clear memory
765-
record.d_signals = None
766+
767+
# Multi-sample frames are smoothed to make a uniform numpy array
768+
if smoothframes or :
769+
# Read signals from the associated dat files that contain wanted channels
770+
record.d_signals = _signals.rdsegment(record.filename, dirname, pbdir, record.nsig, record.fmt, record.siglen,
771+
record.byteoffset, record.sampsperframe, record.skew,
772+
sampfrom, sampto, channels)
773+
# Arrange/edit the object fields to reflect user channel and/or signal range input
774+
record.arrangefields(channels, sampto - sampfrom)
775+
if physical == 1:
776+
# Perform dac to get physical signal
777+
record.p_signals = record.dac()
778+
# Clear memory
779+
record.d_signals = None
780+
781+
# Return each sample of the signals with multiple samples per frame
782+
else:
783+
record.e_d_signals = _signals.rdsegment()
784+
766785
# A multi segment record
767786

768787
# We can make another rdsamp function (called rdsamp_segment) to call

0 commit comments

Comments
 (0)