def test_basic(self): # Note: the docstrings for the argrel{min,max,extrema} functions # do not give a guarantee of the order of the indices, so we'll # sort them before testing. x = np.array([[1, 2, 2, 3, 2], [2, 1, 2, 2, 3], [3, 2, 1, 2, 2], [2, 3, 2, 1, 2], [1, 2, 3, 2, 1]]) row, col = argrelmax(x, axis=0) order = np.argsort(row) assert_equal(row[order], [1, 2, 3]) assert_equal(col[order], [4, 0, 1]) row, col = argrelmax(x, axis=1) order = np.argsort(row) assert_equal(row[order], [0, 3, 4]) assert_equal(col[order], [3, 1, 2]) row, col = argrelmin(x, axis=0) order = np.argsort(row) assert_equal(row[order], [1, 2, 3]) assert_equal(col[order], [1, 2, 3]) row, col = argrelmin(x, axis=1) order = np.argsort(row) assert_equal(row[order], [1, 2, 3]) assert_equal(col[order], [1, 2, 3])
def reset(self): """ Deletes stored computations. """ self.minima = keydefaultdict(lambda (key, axis): argrelmin(self.data[key][:, axis], order=self.w_ext)[0]) self.maxima = keydefaultdict(lambda (key, axis): argrelmax(self.data[key][:, axis], order=self.w_ext)[0]) self.derivative_minima = keydefaultdict( lambda (key, axis): argrelmin(np.diff(self.data[key][:, axis]), order=self.w_ext)[0]) self.derivative_maxima = keydefaultdict( lambda (key, axis): argrelmax(np.diff(self.data[key][:, axis]), order=self.w_ext)[0])
def test_highorder(self): order = 2 sigmas = [1.0, 2.0, 10.0, 5.0, 15.0] test_data, act_locs = _gen_gaussians_even(sigmas, 500) test_data[act_locs + order] = test_data[act_locs] * 0.99999 test_data[act_locs - order] = test_data[act_locs] * 0.99999 rel_max_locs = argrelmax(test_data, order=order, mode='clip')[0] assert_(len(rel_max_locs) == len(act_locs)) assert_((rel_max_locs == act_locs).all())
def test_highorder(self): order = 2 sigmas = [1.0, 2.0, 10.0, 5.0, 15.0] test_data, act_locs = _gen_gaussians_even(sigmas, 500) test_data[act_locs + order] = test_data[act_locs]*0.99999 test_data[act_locs - order] = test_data[act_locs]*0.99999 rel_max_locs = argrelmax(test_data, order=order, mode='clip')[0] assert_(len(rel_max_locs) == len(act_locs)) assert_((rel_max_locs == act_locs).all())
def test_2d_gaussians(self): sigmas = [1.0, 2.0, 10.0] test_data, act_locs = _gen_gaussians_even(sigmas, 100) rot_factor = 20 rot_range = np.arange(0, len(test_data)) - rot_factor test_data_2 = np.vstack([test_data, test_data[rot_range]]) rel_max_rows, rel_max_cols = argrelmax(test_data_2, axis=1, order=1) for rw in xrange(0, test_data_2.shape[0]): inds = (rel_max_rows == rw) assert_(len(rel_max_cols[inds]) == len(act_locs)) assert_((act_locs == (rel_max_cols[inds] - rot_factor * rw)).all())
def test_2d_gaussians(self): sigmas = [1.0, 2.0, 10.0] test_data, act_locs = _gen_gaussians_even(sigmas, 100) rot_factor = 20 rot_range = np.arange(0, len(test_data)) - rot_factor test_data_2 = np.vstack([test_data, test_data[rot_range]]) rel_max_rows, rel_max_cols = argrelmax(test_data_2, axis=1, order=1) for rw in xrange(0, test_data_2.shape[0]): inds = (rel_max_rows == rw) assert_(len(rel_max_cols[inds]) == len(act_locs)) assert_((act_locs == (rel_max_cols[inds] - rot_factor*rw)).all())
def test_wlen(self): """ Test if wlen actually shrinks the evaluation range. """ t = np.linspace(0, 4 * np.pi, 1000) x = abs(np.sin(t)) peaks = argrelmax(x)[0] # Raise 2 baseline of peaks in the middle x[250:750] += 0.3 # If entire x is used the middle two peaks should have a prominence # of approx. 1 + 0.3, otherwise minima between second and third peak # should be lowest contour line and prominence should be < 1.1 proms_wlen = peak_prominences(x, peaks, wlen=600)[0] assert_(np.all(proms_wlen < 1.1)) # If window length is 2 assert_equal(peak_prominences(x, peaks)[0], peak_prominences(x, peaks, wlen=(x.size * 2))[0])
def test_wlen(self): """ Test if wlen actually shrinks the evaluation range. """ t = np.linspace(0, 4 * np.pi, 1000) x = abs(np.sin(t)) peaks = argrelmax(x)[0] # Raise 2 baseline of peaks in the middle x[250:750] += 0.3 # If entire x is used the middle two peaks should have a prominence # of approx. 1 + 0.3, otherwise minima between second and third peak # should be lowest contour line and prominence should be < 1.1 proms_wlen = peak_prominences(x, peaks, wlen=600)[0] assert_(np.all(proms_wlen < 1.1)) # If window length is 2 assert_equal( peak_prominences(x, peaks)[0], peak_prominences(x, peaks, wlen=(x.size * 2))[0])
def train(self, data, target): """ Uses KDE to estimate the best classification threshold (delta). :param data: shapelet transformed dataset :type data: np.array, shape = (len(dataset),) :param target: list of 0 (event has NOT occurred during in this time series) or 1 if it has :type target: np.array, shape = (len(dataset),) :return: information gain, delta, f_c(delta) :rtype: tuple(float, float, float) """ nin_class = data[target == 0] np_c = len(nin_class) / (len(target) + .0) in_class = data[target == 1] p_c = len(in_class) / (len(target) + .0) density = int(self.resolution * (max(data) - min(data))) dist_space = linspace(min(data), max(data), density) f_c = gaussian_kde(in_class)(dist_space) nf_c = gaussian_kde(nin_class)(dist_space) P_c = p_c * f_c / (np_c * nf_c + p_c * f_c) P_c[P_c > self.p] = -P_c[P_c > self.p] delta_candidates = argrelmax(P_c, order=3)[0] bsf_information_gain = 0 bsf_delta = 0 bsf_f_c_delta = 0 for i in delta_candidates: i = [i] delta = dist_space[i][0] d1 = target[data < delta] d2 = target[data >= delta] igain = information_gain(target, d1, d2) f_c_delta = nf_c[i][0] if igain > bsf_information_gain: bsf_information_gain = igain bsf_delta = delta bsf_f_c_delta = f_c_delta elif igain == bsf_information_gain and f_c_delta < bsf_f_c_delta: bsf_information_gain = igain bsf_delta = delta bsf_f_c_delta = f_c_delta return bsf_information_gain, bsf_delta, bsf_f_c_delta
for i in dataset1['msg_stamp'].map(str): lt.append('.' + i.zfill(3)) dataset1['ros_time'] = pandas.Series(lt) dataset1['time_stamp'] = dataset1['eventtime'].map(str) + dataset1['ros_time'] del dataset1['eventtime'] del dataset1['msg_stamp'] del dataset1['ros_time'] del dataset1['symbols'] dataset1['time_stamp'] = pandas.to_datetime(dataset1['time_stamp'], errors = 'coerce') dataset1_ = dataset1.set_index('time_stamp') eigen = dataset1_.eigen_force.resample('1L') eigen.fillna(method = 'ffill', inplace = True) #print dataset1_ y = np.array(eigen) p = argrelmin(y) q = argrelmax(y) p = list(p) q = list(q) for i in p: c = i.flatten() for i in q: d = i.flatten() c = list(c) d = list(d) fig, ax = plt.subplots() #ax2.plot(range(len(skill)), skill, 'g', lw = 2) #ax.set_xlim([0, len(eigen)]) #ax.plot(range(len(eigen)), eigen, 'b', lw = 2) ax.set_xlim([0, len(eigen)]) ax.plot(range(len(eigen)), eigen, 'r', lw = 2) #ax3.axvline(87)