def test_cs_graph_components(): D = np.eye(4, dtype=bool) with suppress_warnings() as sup: sup.filter(DeprecationWarning, "`cs_graph_components` is deprecated!") n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 4) assert_equal(flag, [0, 1, 2, 3]) D[0, 1] = D[1, 0] = 1 with suppress_warnings() as sup: sup.filter(DeprecationWarning, "`cs_graph_components` is deprecated!") n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 3) assert_equal(flag, [0, 0, 1, 2]) # A pathological case... D[2, 2] = 0 with suppress_warnings() as sup: sup.filter(DeprecationWarning, "`cs_graph_components` is deprecated!") n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 2) assert_equal(flag, [0, 0, -2, 1])
def test_cs_graph_components(): D = np.eye(4, dtype=np.bool) warn_ctx = WarningManager() warn_ctx.__enter__() try: warnings.filterwarnings("ignore", message="`cs_graph_components` is deprecated") n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 4) assert_equal(flag, [0, 1, 2, 3]) D[0, 1] = D[1, 0] = 1 n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 3) assert_equal(flag, [0, 0, 1, 2]) # A pathological case... D[2, 2] = 0 n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 2) assert_equal(flag, [0, 0, -2, 1]) finally: warn_ctx.__exit__()
def category_mat(samples, mat, cutplot, cutoff=None): """ Predicts the category for each data point """ if cutoff == None: cutoff = find_cutoff(cutplot) # Build a new graph on samples with edges mat[i][j] > cutoff newG = mat > cutoff n_comp, labels = cs.cs_graph_components(newG) return labels
def test_cs_graph_components(): D = np.eye(4, dtype=np.bool) with warnings.catch_warnings(): warnings.filterwarnings("ignore", message="`cs_graph_components` is deprecated") n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 4) assert_equal(flag, [0, 1, 2, 3]) D[0, 1] = D[1, 0] = 1 n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 3) assert_equal(flag, [0, 0, 1, 2]) # A pathological case... D[2, 2] = 0 n_comp, flag = csgraph.cs_graph_components(csr_matrix(D)) assert_(n_comp == 2) assert_equal(flag, [0, 0, -2, 1])
def check_size(flds, W, H, min_size, mesh_p=None, fields_dat=None): """ Returns the number of fields that are above the minimum size as well as a layout indicating the positions and identities of the fields and the sizes of the fields. Finds fields by generating a 'connections' matrix that indicates adjacencies for active cells. We call a 'connected-components' algorithm on the matrix.""" side = flds.shape[0] nodes = flds.shape[0] * flds.shape[1] # Create connection matrix connections = lil((nodes, nodes)) for i in range(len(flds)): for j in range(len(flds[i])): if flds[i, j] != 1: continue for xd in [0, 1]: # This also does -1, implicitly for yd in [0, 1]: # This also does -1, implicitly if i + xd < side and i + xd >= 0 and j + yd < side and j + yd >= 0: if flds[i + xd, j + yd] == 1: connections[i * side + j, (i + xd) * side + (j + yd)] = 1 connections[(i + xd) * side + (j + yd), i * side + j] = 1 # Determine connected components _, labels = cs.cs_graph_components(connections) one_node = 1.0 * W * H / (mesh_p ** 2) # area of one node (m) nodes_required_for_fld = min_size / one_node # Find labels with min size fld_areas = [] large_enough_flds = [] for node_num in range(np.amax(labels) + 1): if node_num < 0: break if (labels == node_num).sum() >= nodes_required_for_fld: large_enough_flds.append(node_num) fld_areas.append((labels == node_num).sum() * one_node) # Remap labels back to graph labels = labels.reshape([side, side]) if large_enough_flds == []: fld_layout = np.zeros([side, side]) else: fld_layout = sum([labels == component for component in large_enough_flds]) return len(large_enough_flds), fld_layout, fld_areas
def connection_matrix(samples, n=10, distr=(10,30)): """ samples : set S of r points in Rn. n : number n of clusterings to perform. distr : numbers of clusters. <- this should be a distribution over 0,...,n. Let us use a uniform integer for a moment. cutoff : weight cutoff, 0 <= cutoff <= 1. could make n,distr,cutoff optional by setting default values. :returns: mat : connection matrix cutplot : A cut plot f : [0,1] -> Z """ r = len(samples) mat = np.zeros((r, r)) for n_ in xrange(n): # select integer d from distr[0] to distr[1] d = randrange(distr[0],distr[1]+1) km = KMeans(n_clusters=d, init='k-means++', max_iter=100, n_init=1,verbose=False) km.fit(samples) labels = km.labels_ for i in range(r): for j in range(i,r): if labels[i] == labels[j]: mat[i][j] += 1 mat = mat + mat.T mat /= n cutplot = np.zeros((n+1 ,2), dtype='f2<') for l in xrange(n+1): # Construct graph for which mat[i][j] > l/n graph = mat > 1.0 * l /n n_comp, labels = cs.cs_graph_components(graph) cutplot[l][0] = l * 1.0 /n cutplot[l][1] = n_comp return mat, cutplot
nextedge=0 while not connected: i=I[order[nextedge]] j=J[order[nextedge]] d=dist[order[nextedge]] if np.mod(nextedge,100)==0: print "edge %d of %d"%(nextedge,len(order)) if not A[i,j]: A[i,j] = d A[j,i] = d # check connectivity minedges = A.nnz/2.0 > nv-1 everyrow = A.sum(axis=1).min()>0.0 if minedges and everyrow: cstree = csg.cs_graph_components(A) print " ...checking connectivity (%d components)"%cstree[0] if cstree[0]==1 and len(np.where(cstree[1]==-2)[0])==0: connected=True nextedge+=1 ################################## #[4] ################################## randne=2000 randI = np.random.randint(nv,size=randne) randJ = np.random.randint(nv,size=randne) while len(np.where(randI==randJ)[0])>0: loc = np.where(randI==randJ)[0] print "fixing %d random edges..."%len(loc)