Skip to content

Commit 54ddfff

Browse files
author
Chen Xie
committed
rdsamp more flat
1 parent 5394ad2 commit 54ddfff

File tree

2 files changed

+43
-71
lines changed

2 files changed

+43
-71
lines changed

devtests.ipynb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,17 @@
203203
},
204204
"outputs": [
205205
{
206-
"ename": "UnboundLocalError",
207-
"evalue": "local variable 'returninds' referenced before assignment",
208-
"output_type": "error",
209-
"traceback": [
210-
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
211-
"\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
212-
"\u001b[1;32m<ipython-input-1-4902248f2f9c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m# Return list of arrays\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0msig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfields\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrdsamp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'sampledata/matched/s00001/s00001-2896-10-10-00-31'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msampfrom\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msampto\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10250\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchannels\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacksegments\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\n\\nOutput of rdsamp:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
213-
"\u001b[1;32m/home/cx1111/PhysionetProjects/wfdb-python/wfdb/_rdsamp.py\u001b[0m in \u001b[0;36mrdsamp\u001b[1;34m(recordname, sampfrom, sampto, channels, physical, stacksegments)\u001b[0m\n\u001b[0;32m 1216\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1217\u001b[0m \u001b[1;31m# Work out the relative channels to return this segment\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1218\u001b[1;33m \u001b[0msegchannels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreturninds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0memptyinds\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetsegmentchannels\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstartseg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msegrecordname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdirname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlayoutfields\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchannels\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1219\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1220\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
214-
"\u001b[1;32m/home/cx1111/PhysionetProjects/wfdb-python/wfdb/_rdsamp.py\u001b[0m in \u001b[0;36mgetsegmentchannels\u001b[1;34m(startseg, segrecordname, dirname, layoutfields, channels)\u001b[0m\n\u001b[0;32m 1124\u001b[0m \u001b[0msegchannels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1125\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1126\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0msegchannels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreturninds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0memptyinds\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1127\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1128\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
215-
"\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'returninds' referenced before assignment"
206+
"name": "stdout",
207+
"output_type": "stream",
208+
"text": [
209+
"[[ nan nan nan]\n",
210+
" [ nan nan nan]\n",
211+
" [ nan nan nan]\n",
212+
" ..., \n",
213+
" [-0.45454545 nan -0.72413793]\n",
214+
" [-0.54545455 nan -0.72413793]\n",
215+
" [-0.63636364 nan -0.72413793]]\n",
216+
"['Empty Segment', {'signame': ['V', 'II', 'No Channel'], 'basetime': '31:51.982', 'initvalue': [0, 0, 'No Channel'], 'nseg': 1, 'gain': [1.0, 1.0, 'No Channel'], 'fs': 125.0, 'units': ['mV', 'mV', 'No Channel'], 'byteoffset': [0, 0, 'No Channel'], 'fmt': ['80', '80', 'No Channel'], 'nsamp': 6750, 'filename': ['3975656_0001.dat', '3975656_0001.dat', 'No Channel'], 'comments': [], 'baseline': [0, 0, 'No Channel'], 'nsampseg': [], 'nsig': 2, 'sampsperframe': [1, 1, 'No Channel'], 'skew': [0, 0, 'No Channel'], 'basedate': ''}, {'signame': ['V', 'II', 'No Channel'], 'basetime': '32:45.982', 'initvalue': [-11, -33, 'No Channel'], 'nseg': 1, 'gain': [11.0, 29.0, 'No Channel'], 'fs': 125.0, 'units': ['mV', 'mV', 'No Channel'], 'byteoffset': [0, 0, 'No Channel'], 'fmt': ['80', '80', 'No Channel'], 'nsamp': 125, 'filename': ['3975656_0002.dat', '3975656_0002.dat', 'No Channel'], 'comments': [], 'baseline': [0, 0, 'No Channel'], 'nsampseg': [], 'nsig': 2, 'sampsperframe': [1, 1, 'No Channel'], 'skew': [0, 0, 'No Channel'], 'basedate': ''}, {'signame': ['V', 'No Channel', 'MCL1'], 'basetime': '32:46.982', 'initvalue': [-4, 'No Channel', -16], 'nseg': 1, 'gain': [11.0, 'No Channel', 29.0], 'fs': 125.0, 'units': ['mV', 'No Channel', 'mV'], 'byteoffset': [0, 'No Channel', 0], 'fmt': ['80', 'No Channel', '80'], 'nsamp': 1354, 'filename': ['3975656_0003.dat', 'No Channel', '3975656_0003.dat'], 'comments': [], 'baseline': [0, 'No Channel', 0], 'nsampseg': [], 'nsig': 2, 'sampsperframe': [1, 'No Channel', 1], 'skew': [0, 'No Channel', 0], 'basedate': ''}]\n"
216217
]
217218
}
218219
],
@@ -222,17 +223,18 @@
222223
"from wfdb import rdsamp\n",
223224
"\n",
224225
"# Return list of arrays\n",
225-
"sig, fields=rdsamp('sampledata/matched/s00001/s00001-2896-10-10-00-31', sampfrom=10, sampto=10250, channels=[2,3], stacksegments=0)\n",
226-
"\n",
227-
"print(\"\\n\\nOutput of rdsamp:\")\n",
228-
"print(\"sig: \", sig)\n",
229-
"print(\"fields: \", fields)\n",
226+
"#sig, fields=rdsamp('sampledata/matched/s00001/s00001-2896-10-10-00-31', sampfrom=10, sampto=10250, channels=[2,3], stacksegments=0)\n",
227+
"# rdsamp -r s00001-2896-10-10-00-31 -s 2 3 -t 82 -P\n",
228+
"#print(\"\\n\\nOutput of rdsamp:\")\n",
229+
"#print(\"sig: \", sig)\n",
230+
"#print(\"fields: \", fields)\n",
230231
"\n",
231232
"\n",
232233
"# Return single array\n",
233-
"#sig, fields=rdsamp('sampledata/matched/s00001/s00001-2896-10-10-00-31', sampfrom=10, sampto=10250, channels=[1,0,2],stacksegments=1)\n",
234+
"sig, fields=rdsamp('sampledata/matched/s00001/s00001-2896-10-10-00-31', sampfrom=10, sampto=10250, channels=[1,0,2],stacksegments=1)\n",
234235
"\n",
235-
"#print(sig)\n"
236+
"print(sig)\n",
237+
"print(fields[2])"
236238
]
237239
},
238240
{

wfdb/_rdsamp.py

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,12 +1091,7 @@ def allocateoutput(fields, channels, stacksegments, sampfrom, sampto, physical,
10911091
segmentfields = [None] * len(readsegs)
10921092

10931093
return sig, channels, nsamp, segmentfields, indstart
1094-
1095-
1096-
1097-
1098-
1099-
1094+
11001095
# Determine the channels to be returned for each segment
11011096
def getsegmentchannels(startseg, segrecordname, dirname, layoutfields, channels):
11021097

@@ -1116,17 +1111,28 @@ def getsegmentchannels(startseg, segrecordname, dirname, layoutfields, channels)
11161111
else:
11171112
returninds.append(0)
11181113
returninds = np.array(returninds)
1119-
# The channels of the overall array that the segment doesn't contain
1114+
# emptyinds: the channels of the overall array that the segment doesn't contain
11201115
emptyinds = np.where(returninds == 0)[0]
1121-
# The channels of the overall array that the segment contains
1116+
# returninds: the channels of the overall array that the segment does contain
11221117
returninds = np.where(returninds == 1)[0]
11231118
else:
1124-
segchannels = []
1119+
segchannels = []
1120+
returninds=[]
1121+
emptyinds=[]
11251122

11261123
return segchannels, returninds, emptyinds
11271124

1128-
1129-
1125+
# Expand the channel dependent fields of a segment to match the overall layout specification header.
1126+
def expandfields(segmentfields, segnum, startseg, readsegs, channels, returninds):
1127+
1128+
expandedfields = dict.copy(segmentfields[segnum - startseg - readsegs[0]])
1129+
for fielditem in arrangefields:
1130+
expandedfields[fielditem] = ['No Channel'] * len(channels)
1131+
for c in range(0, len(returninds)):
1132+
expandedfields[fielditem][returninds[c]] = segmentfields[segnum - startseg - readsegs[0]][fielditem][c]
1133+
segmentfields[segnum - startseg - readsegs[0]][fielditem] = expandedfields[fielditem]
1134+
# Keep fields['nsig'] as the number of returned channels from the segments.
1135+
return segmentfields
11301136

11311137

11321138
def rdsamp(
@@ -1173,10 +1179,8 @@ def rdsamp(
11731179

11741180
if int(config.get('pbdownload','keepdledfiles')) == 0: # Flag specifying whether to keep downloaded physiobank files
11751181
filestoremove = dledfiles
1176-
11771182

11781183
fields = readheader(recordname) # Get the info from the header file
1179-
11801184

11811185
if fields["nsig"] == 0:
11821186
sys.exit("This record has no signals. Use rdann to read annotations")
@@ -1209,19 +1213,9 @@ def rdsamp(
12091213

12101214
segrecordname = fields["filename"][segnum]
12111215

1212-
1213-
1214-
1215-
1216-
1217-
# Work out the relative channels to return this segment
1216+
# Work out the relative channels to return from this segment
12181217
segchannels, returninds, emptyinds = getsegmentchannels(startseg, segrecordname, dirname, layoutfields, channels)
12191218

1220-
1221-
1222-
1223-
1224-
12251219
if stacksegments == 0: # Return list of np arrays
12261220
# Empty segment or no desired channels in segment. Store indicator and segment
12271221
# length.
@@ -1239,11 +1233,7 @@ def rdsamp(
12391233
readsegs[0]], segmentfields[segnum -
12401234
startseg -
12411235
readsegs[0]] = rdsamp(recordname=os.path.join(dirname,
1242-
segrecordname), physical=physical, sampfrom=readsamps[segnum -
1243-
startseg -
1244-
readsegs[0]][0], sampto=readsamps[segnum -
1245-
startseg -
1246-
readsegs[0]][1], channels=segchannels)
1236+
segrecordname), physical=physical, sampfrom=readsamps[segnum - startseg -readsegs[0]][0], sampto=readsamps[segnum - startseg - readsegs[0]][1], channels=segchannels)
12471237

12481238
else: # Return single stacked np array of all (selected) channels
12491239

@@ -1259,35 +1249,15 @@ def rdsamp(
12591249
if startseg == 1: # Variable layout format. Load data then rearrange channels.
12601250
sig[indstart:indend, returninds], segmentfields[segnum -
12611251
startseg -
1262-
readsegs[0]] = rdsamp(recordname=os.path.join(dirname, segrecordname), physical=physical, sampfrom=readsamps[segnum -
1263-
startseg -
1264-
readsegs[0]][0], sampto=readsamps[segnum -
1265-
startseg -
1266-
readsegs[0]][1], channels=segchannels) # Load all the wanted channels that the segment contains
1252+
readsegs[0]] = rdsamp(recordname=os.path.join(dirname, segrecordname), physical=physical, sampfrom=readsamps[segnum - startseg - readsegs[0]][0], sampto=readsamps[segnum -startseg - readsegs[0]][1], channels=segchannels) # Load all the wanted channels that the segment contains
12671253
if physical == 0: # Fill the rest with invalids
12681254
sig[indstart:indend, emptyinds] = -2147483648
12691255
else:
12701256
sig[indstart:indend, emptyinds] = np.nan
1271-
1272-
# Expand the channel dependent fields to match the
1273-
# overall layout. Remove this following block if you
1274-
# wish to keep the returned channels' fields without
1275-
# any 'no channel' placeholders between.
1276-
expandedfields = dict.copy(
1277-
segmentfields[segnum - startseg - readsegs[0]])
1278-
for fielditem in arrangefields:
1279-
expandedfields[fielditem] = [
1280-
'No Channel'] * len(channels)
1281-
for c in range(0, len(returninds)):
1282-
expandedfields[fielditem][
1283-
returninds[c]] = segmentfields[
1284-
segnum - startseg - readsegs[0]][fielditem][c]
1285-
segmentfields[
1286-
segnum - startseg - readsegs[0]][fielditem] = expandedfields[fielditem]
1287-
# Keep fields['nsig'] as the value of returned channels
1288-
# from the segments.
1289-
1290-
else: # Fixed layout - channels already arranged
1257+
# Expand the channel dependent fields to match the overall layout.
1258+
segmentfields=expandfields(segmentfields, segnum, startseg, readsegs, channels, returninds)
1259+
1260+
else: # Fixed layout - channels are already arranged
12911261
sig[
12921262
indstart:indend, :], segmentfields[
12931263
segnum - startseg] = rdsamp(

0 commit comments

Comments
 (0)