Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix download URLs and Python 3 compatibility. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cwru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import glob
import errno
import random
import urllib
import urllib.request
import numpy as np
from scipy.io import loadmat

Expand All @@ -11,10 +11,10 @@ class CWRU:

def __init__(self, exp, rpm, length):
if exp not in ('12DriveEndFault', '12FanEndFault', '48DriveEndFault'):
print "wrong experiment name: {}".format(exp)
print("wrong experiment name: {}".format(exp))
exit(1)
if rpm not in ('1797', '1772', '1750', '1730'):
print "wrong rpm value: {}".format(rpm)
print("wrong rpm value: {}".format(rpm))
exit(1)
# root directory of all data
rdir = os.path.join(os.path.expanduser('~'), 'Datasets/CWRU')
Expand All @@ -41,12 +41,12 @@ def _mkdir(self, path):
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
print "can't create directory '{}''".format(path)
print("can't create directory '{}''".format(path))
exit(1)

def _download(self, fpath, link):
print "Downloading to: '{}'".format(fpath)
urllib.URLopener().retrieve(link, fpath)
print("Downloading from '{}' to '{}'".format(link,fpath))
urllib.request.URLopener().retrieve(link, fpath)

def _load_and_slice_data(self, rdir, infos):
self.X_train = np.zeros((0, self.length))
Expand All @@ -62,28 +62,28 @@ def _load_and_slice_data(self, rdir, infos):
self._download(fpath, info[3].rstrip('\n'))

mat_dict = loadmat(fpath)
key = filter(lambda x: 'DE_time' in x, mat_dict.keys())[0]
key = list(filter(lambda x: 'DE_time' in x, mat_dict.keys()))[0]
time_series = mat_dict[key][:, 0]

idx_last = -(time_series.shape[0] % self.length)
clips = time_series[:idx_last].reshape(-1, self.length)

n = clips.shape[0]
n_split = 3 * n / 4
n_split = int(3 * n / 4)
self.X_train = np.vstack((self.X_train, clips[:n_split]))
self.X_test = np.vstack((self.X_test, clips[n_split:]))
self.y_train += [idx] * n_split
self.y_test += [idx] * (clips.shape[0] - n_split)

def _shuffle(self):
# shuffle training samples
index = range(self.X_train.shape[0])
index = list(range(self.X_train.shape[0]))
random.Random(0).shuffle(index)
self.X_train = self.X_train[index]
self.y_train = tuple(self.y_train[i] for i in index)

# shuffle test samples
index = range(self.X_test.shape[0])
index = list(range(self.X_test.shape[0]))
random.Random(0).shuffle(index)
self.X_test = self.X_test[index]
self.y_test = tuple(self.y_test[i] for i in index)
Loading