diff --git a/ecg/featurize.py b/ecg/featurize.py index 0f128908..bf4e9d79 100644 --- a/ecg/featurize.py +++ b/ecg/featurize.py @@ -1,9 +1,7 @@ from sklearn import preprocessing -import pywt import numpy as np from tqdm import tqdm import warnings -import scipy.signal as scs class Normalizer(object): def __init__(self, strategy): @@ -38,66 +36,3 @@ def transform(self, x): new_shape = (x.shape[0]*x.shape[1], x.shape[2]) return self.scaler.transform( x.reshape(new_shape)).reshape(original_shape) - - -class BandPassFilter(object): - def filt(self, data, lowcut=0.5, highcut=90, fs=200, order=5): - """ - http://scipy.github.io/old-wiki/pages/Cookbook/ButterworthBandpass - """ - nyq = 0.5 * fs - low = lowcut / nyq - high = highcut / nyq - b, a = scs.butter(order, [low, high], btype='bandpass') - y = scs.lfilter(b, a, data) - return y - - def transform(self, x): - print('Applying Butterworth Filter...') - return np.array([self.filt(x_indiv) for x_indiv in x]) - - -class DiscreteWaveletTransformer(object): - def __init__(self, wavelet_fns, level): - self.transforms = wavelet_fns - self.level = level - - def transform(self, x): - print('Applying Wavelet Transformations...') - x_new = [] - for x_indiv in tqdm(x): - x_indiv_trans = [] - for wavefn in self.transforms: - transform = np.array(pywt.wavedec( - x_indiv, wavefn, level=self.level)[:2]) - if(len(x_indiv_trans) > 0 and - transform.shape[1] != len(x_indiv_trans[0])): - warnings.warn( - "Reshaping to proper length after wavelet transform") - transform = transform[:, :len(x_indiv_trans[0])] - x_indiv_trans.extend(transform) - x_new.append(np.array(x_indiv_trans).T) - x_new = np.array(x_new) - return x_new - - -class ContinuousWaveletTransformer(object): - def __init__(self, wavelet_fns): - self.transforms = wavelet_fns - self.widths = np.linspace(1, 100, 10) # TODO: parameterize - - def transform(self, x): - print('Applying Wavelet Transformations...') - x_new = [] - for x_indiv in tqdm(x): - x_indiv_trans = [] - for wavefn in self.transforms: - transform, _ = pywt.cwt(x_indiv, self.widths, 'mexh') - if(transform.shape[1] != len(x_indiv)): - warnings.warn( - "Reshaping to proper length after wavelet transform") - transform = transform[:, :len(x_indiv)] - x_indiv_trans.extend(transform) - x_new.append(np.array(x_indiv_trans).T) - x_new = np.array(x_new) - return x_new diff --git a/ecg/load.py b/ecg/load.py index 5e584ce5..e6572664 100644 --- a/ecg/load.py +++ b/ecg/load.py @@ -2,8 +2,6 @@ from __future__ import division from __future__ import absolute_import -from builtins import zip -from builtins import range import json import numpy as np import fnmatch @@ -17,7 +15,6 @@ # FIXME: step and samp_rate and duration should be part of process, not load - class Loader(object): def __init__( self, diff --git a/ecg/process.py b/ecg/process.py index bc6de588..4c42fd13 100644 --- a/ecg/process.py +++ b/ecg/process.py @@ -1,56 +1,29 @@ from __future__ import division from __future__ import print_function -from builtins import dict + import numpy as np import featurize import collections - class Processor(object): def __init__( self, use_one_hot_labels=True, normalizer=False, - wavelet_fns=[], - wavelet_type='discrete', relabel_classes={}, ignore_classes=[], - wavelet_level=1, - use_bandpass_filter=False, **kwargs ): - self.wavelet_fns = wavelet_fns self.normalizer = normalizer - self.wavelet_type = wavelet_type self.ignore_classes = ignore_classes self.relabel_classes = relabel_classes - self.wavelet_level = wavelet_level self.use_one_hot_labels = use_one_hot_labels - self.use_bandpass_filter = use_bandpass_filter self.n = None self.classes = None self.int_to_class = None self.class_to_int = None def process_x(self, fit): - if self.use_bandpass_filter is True: - bp_filter = featurize.BandPassFilter() - self.x_train = bp_filter.transform(self.x_train) - self.x_test = bp_filter.transform(self.x_test) - - if len(self.wavelet_fns) != 0: - if (self.wavelet_type == 'discrete'): - wavelet_transformer = \ - featurize.DiscreteWaveletTransformer( - self.wavelet_fns, self.wavelet_level) - elif (self.wavelet_type == 'continuous'): - wavelet_transformer = \ - featurize.ContinuousWaveletTransformer(self.wavelet_fns) - else: - raise ValueError("Wavelet type not defined.") - self.x_train = wavelet_transformer.transform(self.x_train) - self.x_test = wavelet_transformer.transform(self.x_test) - if self.normalizer is not False: if fit is True and len(self.x_train) > 0: self.n = featurize.Normalizer(self.normalizer) diff --git a/ecg/train.py b/ecg/train.py index eede36bf..8b2372ad 100644 --- a/ecg/train.py +++ b/ecg/train.py @@ -2,9 +2,6 @@ from __future__ import unicode_literals from __future__ import division from __future__ import absolute_import -from builtins import open -from builtins import int -from builtins import str import argparse import json