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

Adapt to python3.7 version and fix path #5

Open
wants to merge 2 commits 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
26 changes: 13 additions & 13 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
from urllib.request import urlretrieve
import numpy as np
from scipy.io import loadmat

Expand All @@ -11,13 +11,13 @@ 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: {%s}" % exp)
exit(1)
if rpm not in ('1797', '1772', '1750', '1730'):
print "wrong rpm value: {}".format(rpm)
print("wrong rpm value: {%s}" % rpm)
exit(1)
# root directory of all data
rdir = os.path.join(os.path.expanduser('~'), 'Datasets/CWRU')
rdir = os.path.join(os.path.dirname(__file__), 'Datasets\\CWRU')

fmeta = os.path.join(os.path.dirname(__file__), 'metadata.txt')
all_lines = open(fmeta).readlines()
Expand All @@ -41,12 +41,13 @@ 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 {%s}" % path)
exit(1)

def _download(self, fpath, link):
print "Downloading to: '{}'".format(fpath)
urllib.URLopener().retrieve(link, fpath)
print("Downloading to: {%s} " % fpath)
urlretrieve(link,fpath)
# urllib.URLopener().retrieve(link, fpath)

def _load_and_slice_data(self, rdir, infos):
self.X_train = np.zeros((0, self.length))
Expand All @@ -60,30 +61,29 @@ def _load_and_slice_data(self, rdir, infos):
fpath = os.path.join(fdir, info[2] + '.mat')
if not os.path.exists(fpath):
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)
self.y_test = tuple(self.y_test[i] for i in index)
Loading