Skip to content

Commit

Permalink
remove unused featurization code
Browse files Browse the repository at this point in the history
  • Loading branch information
Awni Hannun committed May 26, 2018
1 parent a7bffa3 commit 0198bb9
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 99 deletions.
65 changes: 0 additions & 65 deletions ecg/featurize.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
3 changes: 0 additions & 3 deletions ecg/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +15,6 @@

# FIXME: step and samp_rate and duration should be part of process, not load


class Loader(object):
def __init__(
self,
Expand Down
29 changes: 1 addition & 28 deletions ecg/process.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 0 additions & 3 deletions ecg/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0198bb9

Please sign in to comment.