def test_1D_array(self): a = np.array([1, 2, 3, 4]) try: vsplit(a, 2) assert_(0) except ValueError: pass
def _removeNonTracklikeClusterCenters(self): '''NOTE : Much of this code is copied from LPCMImpl.followXSingleDirection (factor out?) ''' labels = self._meanShift.labels_ labels_unique = unique(labels) cluster_centers = self._meanShift.cluster_centers_ rsp = lpcRandomStartPoints() cluster_representatives = [] for k in range(len(labels_unique)): cluster_members = labels == k cluster_center = cluster_centers[k] cluster = self._Xi[cluster_members,:] mean_sub = cluster - cluster_center cov_x = dot(transpose(mean_sub), mean_sub) eigen_cov = eigh(cov_x) sorted_eigen_cov = zip(eigen_cov[0],map(ravel,vsplit(eigen_cov[1].transpose(),len(eigen_cov[1])))) sorted_eigen_cov.sort(key = lambda elt: elt[0], reverse = True) rho = sorted_eigen_cov[1][0] / sorted_eigen_cov[0][0] #Ratio of two largest eigenvalues if rho < self._lpcParameters['rho_threshold']: cluster_representatives.append(cluster_center) else: #append a random element of the cluster random_cluster_element = rsp(cluster, 1)[0] cluster_representatives.append(random_cluster_element) return array(cluster_representatives)
def test_2D_array(self): a = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) res = vsplit(a, 2) desired = [np.array([[1, 2, 3, 4]]), np.array([[1, 2, 3, 4]])] compare_results(res, desired)
def _followxSingleDirection( self, x, direction = Direction.FORWARD, forward_curve = None, last_eigenvector = None, weights = 1.): '''Generates a partial lpc curve dictionary from the start point, x. Arguments --------- x : 1-dim, length m, numpy.array of floats, start point for the algorithm when m is dimension of feature space direction : bool, proceeds in Direction.FORWARD or Direction.BACKWARD from this point (just sets sign for first eigenvalue) forward_curve : dictionary as returned by this function, is used to detect crossing of the curve under construction with a previously constructed curve last_eigenvector : 1-dim, length m, numpy.array of floats, a unit vector that defines the initial direction, relative to which the first eigenvector is biased and initial cos_neu_neu is calculated weights : 1-dim, length n numpy.array of observation weights (can also be used to exclude individual observations from the computation by setting their weight to zero.), where n is the number of feature points ''' x0 = copy(x) N = self.Xi.shape[0] d = self.Xi.shape[1] it = self._lpcParameters['it'] h = array(self._lpcParameters['h']) t0 = self._lpcParameters['t0'] rho0 = self._lpcParameters['rho0'] save_xd = empty((it,d)) eigen_vecd = empty((it,d)) c0 = ones(it) cos_alt_neu = ones(it) cos_neu_neu = ones(it) lamb = empty(it) #NOTE this is named 'lambda' in the original R code rho = zeros(it) high_rho_points = empty((0,d)) count_points = 0 for i in range(it): kernel_weights = self._kernd(self.Xi, x0, c0[i]*h) * weights mu_x = average(self.Xi, axis = 0, weights = kernel_weights) sum_weights = sum(kernel_weights) mean_sub = self.Xi - mu_x cov_x = dot( dot(transpose(mean_sub), numpy.diag(kernel_weights)), mean_sub) / sum_weights #assert (abs(cov_x.transpose() - cov_x)/abs(cov_x.transpose() + cov_x) < 1e-6).all(), 'Covariance matrix not symmetric, \n cov_x = {0}, mean_sub = {1}'.format(cov_x, mean_sub) save_xd[i] = mu_x #save first point of the branch count_points += 1 #calculate path length if i==0: lamb[0] = 0 else: lamb[i] = lamb[i-1] + sqrt(sum((mu_x - save_xd[i-1])**2)) #calculate eigenvalues/vectors #(sorted_eigen_cov is a list of tuples containing eigenvalue and associated eigenvector, sorted descending by eigenvalue) eigen_cov = eigh(cov_x) sorted_eigen_cov = zip(eigen_cov[0],map(ravel,vsplit(eigen_cov[1].transpose(),len(eigen_cov[1])))) sorted_eigen_cov.sort(key = lambda elt: elt[0], reverse = True) eigen_norm = sqrt(sum(sorted_eigen_cov[0][1]**2)) eigen_vecd[i] = direction * sorted_eigen_cov[0][1] / eigen_norm #Unit eigenvector corresponding to largest eigenvalue #rho parameters rho[i] = sorted_eigen_cov[1][0] / sorted_eigen_cov[0][0] #Ratio of two largest eigenvalues if i != 0 and rho[i] > rho0 and rho[i-1] <= rho0: high_rho_points = vstack((high_rho_points, x0)) #angle between successive eigenvectors if i==0 and last_eigenvector is not None: cos_alt_neu[i] = direction * dot(last_eigenvector, eigen_vecd[i]) if i > 0: cos_alt_neu[i] = dot(eigen_vecd[i], eigen_vecd[i-1]) #signum flipping if cos_alt_neu[i] < 0: eigen_vecd[i] = -eigen_vecd[i] cos_neu_neu[i] = -cos_alt_neu[i] else: cos_neu_neu[i] = cos_alt_neu[i] #angle penalization pen = self._lpcParameters['pen'] if pen > 0: if i == 0 and last_eigenvector is not None: a = abs(cos_alt_neu[i])**pen eigen_vecd[i] = a * eigen_vecd[i] + (1-a) * last_eigenvector if i > 0: a = abs(cos_alt_neu[i])**pen eigen_vecd[i] = a * eigen_vecd[i] + (1-a) * eigen_vecd[i-1] #check curve termination criteria if i not in (0, it-1): #crossing cross = self._lpcParameters['cross'] if forward_curve is None: full_curve_points = save_xd[0:i+1] else: full_curve_points = vstack((forward_curve['save_xd'],save_xd[0:i+1])) #inefficient, initialize then append? if not cross: prox = where(ravel(cdist(full_curve_points,[mu_x])) <= mean(h))[0] if len(prox) != max(prox) - min(prox) + 1: break #convergence convergence_at = self._lpcParameters['convergence_at'] conv_ratio = abs(lamb[i] - lamb[i-1]) / (2 * (lamb[i] + lamb[i-1])) if conv_ratio < convergence_at: break #boundary boundary = self._lpcParameters['boundary'] if conv_ratio < boundary: c0[i+1] = 0.995 * c0[i] else: c0[i+1] = min(1.01*c0[i], 1) #step along in direction eigen_vecd[i] x0 = mu_x + t0 * eigen_vecd[i] #trim output in the case where convergence occurs before 'it' iterations curve = { 'save_xd': save_xd[0:count_points], 'eigen_vecd': eigen_vecd[0:count_points], 'cos_neu_neu': cos_neu_neu[0:count_points], 'rho': rho[0:count_points], 'high_rho_points': high_rho_points, 'lamb': lamb[0:count_points], 'c0': c0[0:count_points] } return curve