Skip to content

Commit 16eeea6

Browse files
committed
compact rddat code
1 parent d2a95d7 commit 16eeea6

File tree

1 file changed

+46
-113
lines changed

1 file changed

+46
-113
lines changed

wfdb/_signals.py

Lines changed: 46 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# All defined WFDB dat formats
77
datformats = ["80","212","16","24","32"]
88

9+
specialfmts = ['212','310','311']
10+
911
# Class with signal methods
1012
# To be inherited by Record from records.py.
1113
class SignalsMixin(object):
@@ -540,7 +542,7 @@ def rddat(filename, dirname, pbdir, fmt, nsig,
540542

541543
# Read values from dat file, and append bytes/samples if needed.
542544
if extraflatsamples:
543-
if fmt in ['212', '310', '311']:
545+
if fmt in specialfmts:
544546
# Extra number of bytes to append onto the bytes read from the dat file.
545547
extrabytenum = totalprocessbytes - totalreadbytes
546548

@@ -554,125 +556,56 @@ def rddat(filename, dirname, pbdir, fmt, nsig,
554556

555557
# Continue to process the read values into proper samples
556558

557-
# Special formats
558-
if fmt in ['212', '310', '311']:
559-
560-
# No extra samples/frame. Obtain original uniform numpy array
561-
if tsampsperframe==nsig:
562-
563-
# Turn the bytes into actual samples. Flat 1d array.
564-
sig = bytes2samples(sigbytes, totalprocesssamples, fmt)
565-
566-
# Remove extra leading sample read within the byte block if any
567-
if blockfloorsamples:
568-
sig = sig[blockfloorsamples:]
569-
570-
# Reshape into multiple channels
571-
sig = sig.reshape(-1, nsig)
572-
573-
# Skew the signal
574-
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace)
575-
576-
# Extra frames present to be smoothed. Obtain averaged uniform numpy array
577-
elif smoothframes:
578-
579-
# Turn the bytes into actual samples. Flat 1d array. All samples for all frames.
580-
sigbytes = bytes2samples(sigbytes, totalprocesssamples, fmt)
581-
582-
# Remove extra leading sample read within the byte block if any
583-
if blockfloorsamples:
584-
sigbytes = sigbytes[blockfloorsamples:]
585-
586-
# Allocate memory for smoothed signal
587-
sig = np.zeros((int(len(sigbytes)/tsampsperframe) , nsig), dtype='int64')
588-
589-
# Transfer and average samples
590-
for ch in range(nsig):
591-
if sampsperframe[ch] == 1:
592-
sig[:, ch] = sigbytes[sum(([0] + sampsperframe)[:ch + 1])::tsampsperframe]
593-
else:
594-
for frame in range(sampsperframe[ch]):
595-
sig[:, ch] += sigbytes[sum(([0] + sampsperframe)[:ch + 1]) + frame::tsampsperframe]
596-
597-
# Have to change the dtype for averaging frames
598-
sig = (sig.astype('float64') / sampsperframe)
599-
600-
# Skew the signal
601-
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace)
602-
603-
# Extra frames present without wanting smoothing. Return all expanded samples.
604-
else:
605-
# Turn the bytes into actual samples. Flat 1d array. All samples for all frames.
606-
sigbytes = bytes2samples(sigbytes, totalprocesssamples, fmt)
559+
# For special fmts, Turn the bytes into actual samples
560+
if fmt in specialfmts:
561+
sigbytes = bytes2samples(sigbytes, totalprocesssamples, fmt)
562+
# Remove extra leading sample read within the byte block if any
563+
if blockfloorsamples:
564+
sigbytes = sigbytes[blockfloorsamples:]
565+
# Adjust for byte offset formats
566+
elif fmt == '80':
567+
sigbytes = sigbytes - 128
568+
elif fmt == '160':
569+
sigbytes = sigbytes - 32768
607570

608-
# Remove extra leading sample read within the byte block if any
609-
if blockfloorsamples:
610-
sigbytes = sigbytes[blockfloorsamples:]
571+
# No extra samples/frame. Obtain original uniform numpy array
572+
if tsampsperframe==nsig:
573+
# Reshape into multiple channels
574+
sig = sigbytes.reshape(-1, nsig)
575+
# Skew the signal
576+
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace)
611577

612-
# Arranged signals
613-
sig = []
578+
# Extra frames present to be smoothed. Obtain averaged uniform numpy array
579+
elif smoothframes:
614580

615-
# Transfer over samples
616-
for ch in range(nsig):
617-
# Indices of the flat signal that belong to the channel
618-
ch_indices = np.concatenate(([np.array(range(sampsperframe[ch])) + sum([0]+sampsperframe[:ch]) + tsampsperframe*framenum for framenum in range(int(len(sigbytes)/tsampsperframe))]))
619-
sig.append(sigbytes[ch_indices])
581+
# Allocate memory for smoothed signal
582+
sig = np.zeros((int(len(sigbytes)/tsampsperframe) , nsig), dtype='int64')
620583

621-
# Skew the signal
622-
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace, sampsperframe)
623-
# Simple format signals that are loaded as they are stored.
584+
# Transfer and average samples
585+
for ch in range(nsig):
586+
if sampsperframe[ch] == 1:
587+
sig[:, ch] = sigbytes[sum(([0] + sampsperframe)[:ch + 1])::tsampsperframe]
588+
else:
589+
for frame in range(sampsperframe[ch]):
590+
sig[:, ch] += sigbytes[sum(([0] + sampsperframe)[:ch + 1]) + frame::tsampsperframe]
591+
# Have to change the dtype for averaging frames
592+
sig = (sig.astype('float64') / sampsperframe)
593+
# Skew the signal
594+
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace)
595+
596+
# Extra frames present without wanting smoothing. Return all expanded samples.
624597
else:
625-
# Adjust for skew, reshape, and consider sampsperframe.
626-
627-
# Adjust for byte offset formats
628-
if fmt == '80':
629-
sigbytes = sigbytes - 128
630-
elif fmt == '160':
631-
sigbytes = sigbytes - 32768
632-
633-
# No extra samples/frame. Obtain original uniform numpy array
634-
if tsampsperframe==nsig:
635-
636-
# Reshape into multiple channels
637-
sig = sigbytes.reshape(-1, nsig)
638-
639-
# Skew the signal
640-
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace)
641-
642-
643-
# Extra frames present to be smoothed. Obtain averaged uniform numpy array
644-
elif smoothframes:
645-
646-
# Allocate memory for smoothed signal
647-
sig = np.zeros((int(len(sigbytes)/tsampsperframe) , nsig), dtype='int64')
598+
# List of 1d numpy arrays
599+
sig=[]
648600

649-
# Transfer and average samples
650-
for ch in range(nsig):
651-
if sampsperframe[ch] == 1:
652-
sig[:, ch] = sigbytes[sum(([0] + sampsperframe)[:ch + 1])::tsampsperframe]
653-
else:
654-
for frame in range(sampsperframe[ch]):
655-
sig[:, ch] += sigbytes[sum(([0] + sampsperframe)[:ch + 1]) + frame::tsampsperframe]
656-
# Have to change the dtype for averaging frames
657-
sig = (sig.astype('float64') / sampsperframe)
658-
659-
# Skew the signal
660-
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace)
661-
662-
# Extra frames present without wanting smoothing. Return all expanded samples.
663-
else:
664-
# List of 1d numpy arrays
665-
sig=[]
666-
667-
# Transfer over samples
668-
for ch in range(nsig):
669-
# Indices of the flat signal that belong to the channel
670-
ch_indices = np.concatenate([np.array(range(sampsperframe[ch])) + sum([0]+sampsperframe[:ch]) + tsampsperframe*framenum for framenum in range(int(len(sigbytes)/tsampsperframe))])
671-
sig.append(sigbytes[ch_indices])
601+
# Transfer over samples
602+
for ch in range(nsig):
603+
# Indices of the flat signal that belong to the channel
604+
ch_indices = np.concatenate([np.array(range(sampsperframe[ch])) + sum([0]+sampsperframe[:ch]) + tsampsperframe*framenum for framenum in range(int(len(sigbytes)/tsampsperframe))])
605+
sig.append(sigbytes[ch_indices])
606+
# Skew the signal
607+
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace, sampsperframe)
672608

673-
# Skew the signal
674-
sig = skewsig(sig, skew, nsig, readlen, fmt, nanreplace, sampsperframe)
675-
676609
# Integrity check of signal shape after reading
677610
checksigdims(sig, readlen, nsig, sampsperframe)
678611

0 commit comments

Comments
 (0)