Exemplo n.º 1
0
 def check(method, directed_in):
     if directed_in:
         SP1 = shortest_path(directed_G, method=method, directed=False, overwrite=False)
         assert_array_almost_equal(SP1, undirected_SP)
     else:
         SP2 = shortest_path(undirected_G, method=method, directed=True, overwrite=False)
         assert_array_almost_equal(SP2, undirected_SP)
Exemplo n.º 2
0
def test_overwrite():
    G = np.array([[0, 3, 3, 1, 2],
                  [3, 0, 0, 2, 4],
                  [3, 0, 0, 0, 0],
                  [1, 2, 0, 0, 2],
                  [2, 4, 0, 2, 0]], dtype=float)
    foo = G.copy()
    shortest_path(foo, overwrite=False)
    assert_array_equal(foo, G)
Exemplo n.º 3
0
def test_undirected():
    for method in methods:
        SP1 = shortest_path(directed_G, method=method, directed=False,
                            overwrite=False)
        SP2 = shortest_path(undirected_G, method=method, directed=True,
                            overwrite=False)

        yield (assert_array_almost_equal, SP1, undirected_SP)
        yield (assert_array_almost_equal, SP2, undirected_SP)
Exemplo n.º 4
0
def test_buffer(method):
    # Smoke test that sparse matrices with read-only buffers (e.g., those from
    # joblib workers) do not cause::
    #
    #     ValueError: buffer source array is read-only
    #
    G = scipy.sparse.csr_matrix([[1.]])
    G.data.flags['WRITEABLE'] = False
    shortest_path(G, method=method)
Exemplo n.º 5
0
 def check(method, directed):
     SP1 = shortest_path(directed_G,
                         directed=directed,
                         overwrite=False,
                         unweighted=True)
     SP2 = shortest_path(unweighted_G,
                         directed=directed,
                         overwrite=False,
                         unweighted=False)
     assert_array_almost_equal(SP1, SP2)
Exemplo n.º 6
0
def test_unweighted_path():
    for method in methods:
        for directed in (True, False):
            SP1 = shortest_path(directed_G,
                                directed=directed,
                                overwrite=False,
                                unweighted=True)
            SP2 = shortest_path(unweighted_G,
                                directed=directed,
                                overwrite=False,
                                unweighted=False)

            yield (assert_array_almost_equal, SP1, SP2)
Exemplo n.º 7
0
 def check(method, directed):
     SP1, pred = shortest_path(directed_G,
                               directed=directed,
                               overwrite=False,
                               return_predecessors=True)
     SP2 = construct_dist_matrix(directed_G, pred, directed=directed)
     assert_array_almost_equal(SP1, SP2)
Exemplo n.º 8
0
 def _generateFeedback(self, **kwargs):
     distances = utils.pairwise(self.representatives, self.distance, self.symmetric_distance)
     
     mst = csgraph.minimum_spanning_tree(distances)
     distances = csgraph.shortest_path(mst,method="D", directed=False)
     
     super(MinimumSpanningTreeFeedback, self). generateFeedback(distances, **kwargs)
Exemplo n.º 9
0
    def run(self, phase=None, throats=None):
        logger.warning('This algorithm can take some time...')
        conduit_lengths = sp.sum(misc.conduit_lengths(network=self._net,
                                 mode='centroid'), axis=1)
        graph = self._net.create_adjacency_matrix(data=conduit_lengths,
                                                  sprsfmt='csr')

        if phase is not None:
            self._phase = phase
            if 'throat.occupancy' in self._phase.props():
                temp = conduit_lengths*(self._phase['throat.occupancy'] == 1)
                graph = self._net.create_adjacency_matrix(data=temp,
                                                          sprsfmt='csr',
                                                          prop='temp')
        path = spgr.shortest_path(csgraph=graph, method='D', directed=False)

        Px = sp.array(self._net['pore.coords'][:, 0], ndmin=2)
        Py = sp.array(self._net['pore.coords'][:, 1], ndmin=2)
        Pz = sp.array(self._net['pore.coords'][:, 2], ndmin=2)

        Cx = sp.square(Px.T - Px)
        Cy = sp.square(Py.T - Py)
        Cz = sp.square(Pz.T - Pz)
        Ds = sp.sqrt(Cx + Cy + Cz)

        temp = path / Ds

        temp[sp.isnan(temp)] = 0
        temp[sp.isinf(temp)] = 0

        return temp
Exemplo n.º 10
0
    def run(self,phase=None):
        r'''
        '''
        logger.warning('This algorithm can take some time...')
        graph = self._net.create_adjacency_matrix(data=self._net['throat.length'],sprsfmt='csr')

        if phase is not None:
            self._phase = phase
            if 'throat.occupancy' in self._phase.props():
                temp = self._net['throat.length']*(self._phase['throat.occupancy']==1)
                graph = self._net.create_adjacency_matrix(data=temp,sprsfmt='csr',prop='temp')

        #self._net.tic()
        path = spgr.shortest_path(csgraph = graph, method='D', directed = False)
        #self._net.toc()

        Px = sp.array(self._net['pore.coords'][:,0],ndmin=2)
        Py = sp.array(self._net['pore.coords'][:,1],ndmin=2)
        Pz = sp.array(self._net['pore.coords'][:,2],ndmin=2)

        Cx = sp.square(Px.T - Px)
        Cy = sp.square(Py.T - Py)
        Cz = sp.square(Pz.T - Pz)
        Ds = sp.sqrt(Cx + Cy + Cz)

        temp = path/Ds
        #temp = path

        temp[sp.isnan(temp)] = 0
        temp[sp.isinf(temp)] = 0

        return temp
Exemplo n.º 11
0
 def shortest_path(self, **kwargs):
   '''Mirrors the scipy.sparse.csgraph function of the same name:
   shortest_path(G, method='auto', directed=True, return_predecessors=False,
                 unweighted=False, overwrite=False)
   '''
   # ssc.shortest_path requires one of these formats:
   adj = self.matrix(dense=True, csr=True, csc=True)
   return ssc.shortest_path(adj, **kwargs)
Exemplo n.º 12
0
def main():
    data = LoadData()
    dijkstra = csgraph.shortest_path(data, method='D')
    floyd_warshall = csgraph.shortest_path(data, method='FW')

    #Impresion del calculo
    # print(data)
    print('\nDijkstra shortest path result:')
    print(dijkstra)
    print('\nFloyd-Warshall shortest path result:')
    print(floyd_warshall)

    #Impresion del grafo
    cities = CreateGraph(data) 
    nx.draw(cities, with_labels=True, font_weight='bold')   
    #nx.draw_networkx_edge_labels(cities, pos=nx.spring_layout(cities))    
    plt.show()
Exemplo n.º 13
0
non_clustered_adj[6, (5, 8, 9)] = 1
non_clustered_adj[7, (5, 8, 9)] = 1
non_clustered_adj[8, (6, 7, 0)] = 1
non_clustered_adj[9, (6, 7, 1)] = 1

non_clustered = [
    [(0, 2), (1, 3), (4, 5), (6, 8), (7, 9)],
    [(1, 2), (3, 4), (5, 7), (6, 9)],
    [(0, 3), (2, 4), (5, 6), (7, 8)],
    [(0, 8), (1, 9)],
]

clustered_graph = csgraph.csgraph_from_dense(clustered_adj)
non_clustered_graph = csgraph.csgraph_from_dense(non_clustered_adj)

clustered_shortest_paths = csgraph.shortest_path(clustered_graph)
non_clustered_shortest_paths = csgraph.shortest_path(non_clustered_graph)

# nc after, nc before, c after, c before
convergence_targets = np.array([0.66, 0.57, 0.606, 0.546])

alignment_targets = np.array(
    [0.1, 0.078, 0.066, 0.043, 0.039, 0.107, 0.075, 0.077])


def empty_mind(n_categories=4, n_exemplars=4):
    n_rows = n_categories * n_exemplars + n_categories
    adj = np.zeros((n_rows, n_rows))
    labels = []
    for category_i in range(n_categories):
        for exemplar_i in range(n_exemplars):
Exemplo n.º 14
0
label_cnt = [0] * 16
for seed in starting_points + label_specific_starting_points:
    st = time.time()
    label = ref_img[seed]

    err2 = np.logical_and(ref_img[:, :, seed[2]] == label,
                          seg_img[:, :, seed[2]] != label)
    dist2 = scipy.ndimage.morphology.distance_transform_edt(
        err2, return_distances=True)
    print(time.time() - st)
    prob2 = dist2 / np.sum(dist2)
    cc2 = measure.label(err2)
    print(time.time() - st)
    end_point_prob = prob2 * (cc2 == cc2[seed[0], seed[1]])
    end_point_cdf = np.cumsum(np.reshape(end_point_prob, [-1]))
    end_point_cdf_sample = np.random.uniform(0., 1., [1]) * end_point_cdf[-1]
    end_point_index = np.searchsorted(end_point_cdf, end_point_cdf_sample)
    end_point = list(zip(*np.unravel_index(end_point_index, dist2.shape)))[0]
    print(time.time() - st)

    graph_start_index = index_image[seed]
    graph_end_index = index_image[end_point[0], end_point[1], seed[2]]
    D = csgraph.shortest_path(graph,
                              indices=[graph_start_index, graph_end_index],
                              return_predecessors=True)
    print(time.time() - st)
    stroke = path(D[1][0, :], graph_start_index, graph_end_index)
    print(time.time() - st)
    stroke_points = list(zip(*np.unravel_index(stroke, index_image.shape)))
    label_cnt[label] += 1
def do_the_gossip(methods, T, W, initial_values, goal="average"):
    """
    methods: list composed of some of the following elements: 
    "simple", "shift-register", "splitting", "local averaging", 
    ("jacobi",d) where d is the parameter of the Jacobi iteration.
    goal: value that the estimators are compared to, can be set
    for instance to the mean of the distribution of the initial 
    values. Default is the average of the initial values. 
    """

    if goal == "average":
        average = np.sum(initial_values) / len(initial_values)
    else:
        average = goal

    # useful for the tuning of some methods
    def _need_spectral_gap(method):
        if method == "shift-register" or method == "splitting":
            return True
        elif type(method) == tuple and method[0] == "jacobi-gap":
            return True
        else:
            return False

    if True in map(_need_spectral_gap, methods):
        eigenvalues = eigs(W, k=2, return_eigenvectors=False, which='LR')
        radius = eigenvalues[0]

    results = {}

    for method in methods:
        variances = np.zeros(T)
        if method == "local averaging":
            n = len(initial_values)
            distances = shortest_path(W != 0, directed=False)
            for t in range(T):
                variances[t] = sum(
                    (((distances <= t) @ initial_values) /
                     ((distances <= t) @ np.ones(n)) - sum(initial_values) / n)
                    **2) / n
        else:
            if method == "simple":
                g = SimpleGossip(initial_values, lambda x: W.dot(x))
            elif method == "shift-register":
                g = ShiftRegisterGossip(
                    initial_values,
                    (8 - 4 * np.sqrt(4 - (1 + radius)**2)) / (1 + radius)**2,
                    lambda x: W.dot(x))
            elif method == "splitting":
                g = SplittingGossip(initial_values,
                                    2 / (1 + np.sqrt(1 - radius**2)),
                                    lambda x: W.dot(x))
            elif type(method) == tuple and method[0] == "general-jacobi":
                alpha = method[1]
                beta = method[2]
                coeffs = lambda t: coeffs_Jacobi(alpha, beta, t)
                g = PolynomialGossip(initial_values, coeffs,
                                     lambda x: W.dot(x))
            elif type(method) == tuple and method[0] == "jacobi":
                d = method[1]
                coeffs = lambda t: coeffs_Jacobi(d / 2, 0, t)
                g = PolynomialGossip(initial_values, coeffs,
                                     lambda x: W.dot(x))
            elif type(method) == tuple and method[0] == "jacobi-gap":
                d = method[1]
                coeffs = lambda t: coeffs_Jacobi_gap(d / 2, 0, 1 - radius, t)
                g = NormalizePolynomialGossip(initial_values, coeffs,
                                              lambda x: W.dot(x))
            elif method == "best polynomial gossip":
                g = ParameterFreePolynomialGossip(initial_values,
                                                  lambda x: W.dot(x))
            elif type(method) == tuple and method[0] == "message-passing":
                d = method[1]
                g = PolynomialGossip(initial_values,
                                     lambda t: coeffs_Kesten_McKay(d, t),
                                     lambda x: W.dot(x))
            else:
                raise NameError("The method " + str(method) +
                                " doesn't exist.")
            for t in range(T):
                variances[t] = np.linalg.norm(g.mu-average)**2\
                                    /g.mu.size
                g.gossip()
        results[method] = variances

    return results
Exemplo n.º 16
0
def adj_mat_to_dist_mat(adj_mat, directed=True, self_loop=False):
    dist_matrix = shortest_path(csgraph=csr_matrix(adj_mat), directed=directed)
    if self_loop:
        np.fill_diagonal(dist_matrix, 1)
    return dist_matrix
G[G < 0] = 0
G = np.sqrt(G)

e = 0.8 * np.median(G)
G[G > e] = 0

# Get rid of effectively Infinite distance for simplicity
sG = np.sum(G, axis = 0)
idx = np.where(sG != 0)[0]

G = G[idx,:][:,idx]
m = G.shape[0]

## Step 2: Using all pair shortest path algorithm, construct graph distance 
# matrix
D = csgraph.shortest_path(csc_matrix(G))

D[D > math.pow(10,20)] = 0
D2 = np.power(D, 2) # Using square for inner product
H = np.eye(m) - np.ones((m,1)).dot(np.ones((1,m)))/m # Construct special centring matrix H
Dt = -0.5 * H.dot(D2) # Apply H to both sides of D2
Dt = Dt.dot(H)

## Step 3: Low dim. representation that preserves distance information
k = 10
V, S, U = np.linalg.svd(Dt) # computes the k largest singular values and 
                            # associated singular vectors of distance matrix
        
# Use the eigenvectors corresponding to the largest eigenvalue as 1st
# coordinate and second larges eignevalue as 2nd coordinate
dim1_new = V[:,0] * np.sqrt(S[0])
Exemplo n.º 18
0
node_index = 0

for _ in range(H):
    s_list = list(input())
    s_table.append(s_list)

adj = [[0] * (W * H) for _ in range(W * H)]
node_index = -1
for h_i, s_list in enumerate(s_table):
    for w_i, s in enumerate(s_list):
        node_index += 1
        if s == "#":
            continue
        if w_i + 1 < W:
            # 右隣と隣接しているか
            if s_list[w_i + 1] == ".":
                adj[node_index][node_index + 1] = 1
                adj[node_index + 1][node_index] = 1
        if h_i + 1 < H:
            # 下と隣接しているか
            if s_table[h_i + 1][w_i] == ".":
                adj[node_index][(h_i + 1) * W + w_i] = 1
                adj[(h_i + 1) * W + w_i][node_index] = 1

adj = np.array(adj)
d_matrix = shortest_path(adj)

d_matrix[d_matrix == float("inf")] = -1
ans = int(d_matrix.max())
print(ans)
Exemplo n.º 19
0
def detect_peduncle(peduncle_img,
                    settings=None,
                    px_per_mm=None,
                    bg_img=None,
                    save=False,
                    name="",
                    pwd=""):
    if settings is None:
        settings = settings.detect_peduncle()

    if (bg_img is not None) and save:
        fig = plt.figure()
        plot_image(bg_img)
        save_fig(fig, pwd, name + "_00")

    if bg_img is None:
        bg_img = peduncle_img

    if px_per_mm:
        branch_length_min_px = px_per_mm * settings['branch_length_min_mm']
    else:
        branch_length_min_px = settings['branch_length_min_px']

    skeleton_img = skeletonize_img(peduncle_img)
    junc_coords, end_coords = get_node_coord(skeleton_img)

    if save:
        visualize_skeleton(bg_img,
                           skeleton_img,
                           coord_junc=junc_coords,
                           coord_end=end_coords,
                           name=name + "_01",
                           pwd=pwd)

    if save:
        fit_ransac(peduncle_img,
                   bg_img=bg_img.copy(),
                   save=True,
                   name=name + "_ransac",
                   pwd=pwd)

    # update_image = True
    # while update_image:
    skeleton_img, b_remove = threshold_branch_length(skeleton_img,
                                                     branch_length_min_px)
    # update_image = b_remove.any()
    junc_coords, end_coords = get_node_coord(skeleton_img)

    if save:
        visualize_skeleton(bg_img.copy(),
                           skeleton_img,
                           coord_junc=junc_coords,
                           coord_end=end_coords,
                           name=name + "_02",
                           pwd=pwd)

    graph, pixel_coordinates, degree_image = skan.skeleton_to_csgraph(
        skeleton_img, unique_junctions=True)
    dist, pred = csgraph.shortest_path(graph,
                                       directed=False,
                                       return_predecessors=True)

    end_nodes = coords_to_nodes(pixel_coordinates, end_coords[:, [1, 0]])
    junc_nodes = coords_to_nodes(pixel_coordinates, junc_coords[:, [1, 0]])

    path, path_length_px, branch_data = find_path(dist,
                                                  pred,
                                                  junc_nodes,
                                                  end_nodes,
                                                  pixel_coordinates,
                                                  bg_image=bg_img.copy(),
                                                  do_animate=False)

    branch_data = get_branch_center(branch_data, dist, pixel_coordinates,
                                    skeleton_img)

    path_img = path_mask(path, pixel_coordinates, skeleton_img.shape)
    junc_coords = pixel_coordinates[get_ids_on_path(path, pixel_coordinates,
                                                    junc_nodes)][:, [1, 0]]
    end_coords = pixel_coordinates[get_ids_on_path(path, pixel_coordinates,
                                                   end_nodes)][:, [1, 0]]

    end_coords = np.array([
        pixel_coordinates[path[0]][[1, 0]], pixel_coordinates[path[-1]][[1, 0]]
    ])

    # make sure that end nodes are not labeled as junctions
    if junc_coords.shape[0] != 0:
        for end_coord in end_coords:
            dst = distance(junc_coords, end_coord)
            mask = dst > 0.1
            junc_coords = junc_coords[mask]

    if save:
        visualize_skeleton(
            bg_img,
            path_img,
            coord_junc=
            junc_coords,  # junc_nodes=junc_nodes, end_nodes=end_nodes,
            coord_end=end_coords,
            name=name + "_03",
            pwd=pwd)

    if save:
        visualize_skeleton(bg_img,
                           path_img,
                           coord_junc=junc_coords,
                           branch_data=branch_data,
                           coord_end=end_coords,
                           name=name + "_04",
                           pwd=pwd)

    return path_img, branch_data, junc_coords, end_coords
Exemplo n.º 20
0
def root(
    adata: AnnData, root: Union[int, str], layer: Optional = None, copy: bool = False
):
    """\
    Define the root of the trajectory.

    Parameters
    ----------
    adata
        Annotated data matrix.
    root
        Either an Id (int) of the tip of the fork to be considered as a root. Or a key (str) from obs/X (such as CytoTRACE) for automatic selection.
    layer
        If key is in X, choose which layer to use for the averaging.
    copy
        Return a copy instead of writing to adata.
    Returns
    -------
    adata : anndata.AnnData
        if `copy=True` it returns or else add fields to `adata`:

        `.uns['graph']['root']`
            selected root.
        `.uns['graph']['pp_info']`
            for each PP, its distance vs root and segment assignment.
        `.uns['graph']['pp_seg']`
            segments network information.
    """

    adata = adata.copy() if copy else adata

    if "graph" not in adata.uns:
        raise ValueError(
            "You need to run `tl.tree` or `tl.curve` first to compute a princal graph before choosing a root."
        )

    graph = adata.uns["graph"]

    if type(root) == str:
        if root in adata.obs:
            root_val = adata.obs[root]
        if root in adata.var_names:
            root_val = get_X(adata, adata.obs_names, root, layer).ravel()

        logg.info("automatic root selection using " + root + " values", time=False)
        avgs = list(
            map(
                lambda n: np.average(root_val, weights=graph["R"][:, n]),
                range(graph["R"].shape[1]),
            )
        )
        root = np.argmax(np.array(avgs))

    d = 1e-6 + pairwise_distances(graph["F"].T, graph["F"].T, metric=graph["metrics"])

    to_g = graph["B"] * d

    csr = csr_matrix(to_g)

    g = igraph.Graph.Adjacency((to_g > 0).tolist(), mode="undirected")
    g.es["weight"] = to_g[to_g.nonzero()]

    root_dist_matrix = shortest_path(csr, directed=False, indices=root)
    pp_info = pd.DataFrame(
        {"PP": g.vs.indices, "time": root_dist_matrix, "seg": np.zeros(csr.shape[0])}
    )

    nodes = np.argwhere(
        np.apply_along_axis(arr=(csr > 0).todense(), axis=0, func1d=np.sum) != 2
    ).flatten()
    nodes = np.unique(np.append(nodes, root))

    pp_seg = pd.DataFrame(columns=["n", "from", "to", "d"])
    for node1, node2 in itertools.combinations(nodes, 2):
        paths12 = g.get_shortest_paths(node1, node2)
        paths12 = np.array([val for sublist in paths12 for val in sublist])

        if np.sum(np.isin(nodes, paths12)) == 2:
            fromto = np.array([node1, node2])
            path_root = root_dist_matrix[[node1, node2]]
            fro = fromto[np.argmin(path_root)]
            to = fromto[np.argmax(path_root)]
            pp_info.loc[paths12, "seg"] = pp_seg.shape[0] + 1
            pp_seg = pp_seg.append(
                pd.DataFrame(
                    {
                        "n": pp_seg.shape[0] + 1,
                        "from": fro,
                        "to": to,
                        "d": shortest_path(csr, directed=False, indices=fro)[to],
                    },
                    index=[pp_seg.shape[0] + 1],
                )
            )

    pp_seg["n"] = pp_seg["n"].astype(int).astype(str)
    pp_seg["n"] = pp_seg["n"].astype(int).astype(str)

    pp_seg["from"] = pp_seg["from"].astype(int)
    pp_seg["to"] = pp_seg["to"].astype(int)

    pp_info["seg"] = pp_info["seg"].astype(int).astype(str)
    pp_info["seg"] = pp_info["seg"].astype(int).astype(str)

    graph["pp_info"] = pp_info
    graph["pp_seg"] = pp_seg
    graph["root"] = root

    adata.uns["graph"] = graph

    logg.info(
        "node " + str(root) + " selected as a root",
        time=False,
        end=" " if settings.verbosity > 2 else "\n",
    )
    logg.hint(
        "added\n"
        "    .uns['graph']['root'] selected root.\n"
        "    .uns['graph']['pp_info'] for each PP, its distance vs root and segment assignment.\n"
        "    .uns['graph']['pp_seg'] segments network information."
    )

    return adata if copy else None
Exemplo n.º 21
0
def roots(adata: AnnData, roots, meeting, copy: bool = False):

    """\
    Define 2 roots of the tree.

    Parameters
    ----------
    adata
        Annotated data matrix.
    roots
        list of tips or forks to be considered a roots.
    meeting
         node ID of the meeting point fo the two converging paths.
    copy
        Return a copy instead of writing to adata.
    Returns
    -------
    adata : anndata.AnnData
        if `copy=True` it returns or else add fields to `adata`:

        `.uns['graph']['root']`
            farthest root selected.
        `.uns['graph']['root2']`
            2nd root selected.
        `.uns['graph']['meeting']`
            meeting point on the tree.
        `.uns['graph']['pp_info']`
            for each PP, its distance vs root and segment assignment).
        `.uns['graph']['pp_seg']`
            segments network information.
    """

    adata = adata.copy() if copy else adata

    if "graph" not in adata.uns:
        raise ValueError(
            "You need to run `tl.tree` first to compute a princal tree before choosing two roots."
        )

    graph = adata.uns["graph"]

    d = 1e-6 + pairwise_distances(graph["F"].T, graph["F"].T, metric=graph["metrics"])

    to_g = graph["B"] * d

    csr = csr_matrix(to_g)

    g = igraph.Graph.Adjacency((to_g > 0).tolist(), mode="undirected")
    g.es["weight"] = to_g[to_g.nonzero()]

    root = roots[
        np.argmax(shortest_path(csr, directed=False, indices=roots)[:, meeting])
    ]
    root2 = roots[
        np.argmin(shortest_path(csr, directed=False, indices=roots)[:, meeting])
    ]

    root_dist_matrix = shortest_path(csr, directed=False, indices=root)
    pp_info = pd.DataFrame(
        {"PP": g.vs.indices, "time": root_dist_matrix, "seg": np.zeros(csr.shape[0])}
    )

    nodes = np.argwhere(
        np.apply_along_axis(arr=(csr > 0).todense(), axis=0, func1d=np.sum) != 2
    ).flatten()
    pp_seg = pd.DataFrame(columns=["n", "from", "to", "d"])
    for node1, node2 in itertools.combinations(nodes, 2):
        paths12 = g.get_shortest_paths(node1, node2)
        paths12 = np.array([val for sublist in paths12 for val in sublist])

        if np.sum(np.isin(nodes, paths12)) == 2:
            fromto = np.array([node1, node2])
            path_root = root_dist_matrix[[node1, node2]]
            fro = fromto[np.argmin(path_root)]
            to = fromto[np.argmax(path_root)]
            pp_info.loc[paths12, "seg"] = pp_seg.shape[0] + 1
            pp_seg = pp_seg.append(
                pd.DataFrame(
                    {
                        "n": pp_seg.shape[0] + 1,
                        "from": fro,
                        "to": to,
                        "d": shortest_path(csr, directed=False, indices=fro)[to],
                    },
                    index=[pp_seg.shape[0] + 1],
                )
            )

    pp_seg["n"] = pp_seg["n"].astype(int).astype(str)
    pp_seg["n"] = pp_seg["n"].astype(int).astype(str)

    pp_info["seg"] = pp_info["seg"].astype(int).astype(str)
    pp_info["seg"] = pp_info["seg"].astype(int).astype(str)

    tips = graph["tips"]
    tips = tips[~np.isin(tips, roots)]

    edges = pp_seg[["from", "to"]].astype(str).apply(tuple, axis=1).values
    img = igraph.Graph()
    img.add_vertices(np.unique(pp_seg[["from", "to"]].values.flatten().astype(str)))
    img.add_edges(edges)

    root2paths = pd.Series(
        shortest_path(csr, directed=False, indices=root2)[tips.tolist() + [meeting]],
        index=tips.tolist() + [meeting],
    )

    toinvert = root2paths.index[(root2paths <= root2paths[meeting])]

    for toinv in toinvert:
        pathtorev = np.array(img.vs[:]["name"])[
            np.array(img.get_shortest_paths(str(root2), str(toinv)))
        ][0]
        for i in range((len(pathtorev) - 1)):
            segtorev = pp_seg.index[
                pp_seg[["from", "to"]]
                .astype(str)
                .apply(lambda x: all(x.values == pathtorev[[i + 1, i]]), axis=1)
            ]

            pp_seg.loc[segtorev, ["from", "to"]] = pp_seg.loc[segtorev][
                ["to", "from"]
            ].values
            pp_seg["from"] = pp_seg["from"].astype(int)
            pp_seg["to"] = pp_seg["to"].astype(int)

    pptoinvert = np.unique(np.concatenate(g.get_shortest_paths(root2, toinvert)))
    reverted_dist = (
        shortest_path(csr, directed=False, indices=root2)
        + np.abs(
            np.diff(shortest_path(csr, directed=False, indices=roots)[:, meeting])
        )[0]
    )
    pp_info.loc[pptoinvert, "time"] = reverted_dist[pptoinvert]

    graph["pp_info"] = pp_info
    graph["pp_seg"] = pp_seg
    graph["root"] = root
    graph["root2"] = root2
    graph["meeting"] = meeting

    adata.uns["graph"] = graph

    logg.info("root selected", time=False, end=" " if settings.verbosity > 2 else "\n")
    logg.hint(
        "added\n" + "    " + str(root) + " is the farthest root.\n"
        "    .uns['graph']['root'] farthest root selected.\n"
        "    .uns['graph']['root2'] 2nd root selected.\n"
        "    .uns['graph']['meeting'] meeting point on the tree.\n"
        "    .uns['graph']['pp_info'] for each PP, its distance vs root and segment assignment.\n"
        "    .uns['graph']['pp_seg'] segments network information."
    )

    return adata if copy else None
Exemplo n.º 22
0
def darMatrizCantidadPersonasCaminos(coorX, coorY, matrix_ct,
                                     dic_edificios_nodos, list_dict_syd):

    numNodes = len(coorX)

    # Matriz que guardara la cantidad de estudiantes que han pasado por los caminos
    matrix_cantidad_E = np.zeros((numNodes, numNodes))

    # Se define una función que retorne la matriz de nodos intermedios entre el origen y el destino a partir de la matriz total de nodos precursores
    def get_path(Pr, i, j):
        path = [j]
        k = j
        while Pr[i, k] != -9999:
            path.append(Pr[i, k])
            k = Pr[i, k]
        return path[::-1]

    matrix_ct_csr = csr_matrix(matrix_ct)

    # Se obtienen los nodos precursos de la función shortest_path importada de la libreria SciPy
    sp, pr = shortest_path(csgraph=matrix_ct_csr,
                           directed=False,
                           method='D',
                           return_predecessors=True)

    # TEST: Se declara un contador de cuantos caminos de costo minímo se deben calcular. Esto se hace con el fin de hacer mas rápidas las computaciones

    countEstudiantes = 0
    maxEstudiantes = len(list_dict_syd)

    for i in list_dict_syd:

        if countEstudiantes < maxEstudiantes:

            source = dic_edificios_nodos[i['s']]
            destiny = dic_edificios_nodos[i['d']]

            sp_indexes = get_path(pr, source, destiny)

            # Se cuentan la cantidad de estudiantes que transitaron por los caminos de costo minímo transitados
            # para que después se puedan usar estos datos en la función objetivo del costo de interacción
            # Ademas, se dibuja con una línea azul el camino de costo minímo entre dos nodos

            i = 0

            while i < len(sp_indexes) - 1:
                matrix_cantidad_E[sp_indexes[i]][sp_indexes[i + 1]] += 1

                plt.plot([coorX[sp_indexes[i]], coorX[sp_indexes[i + 1]]],
                         [coorY[sp_indexes[i]], coorY[sp_indexes[i + 1]]],
                         'b',
                         linewidth=2)

                i = i + 1

            if len(sp_indexes) == 2 and matrix_ct[source, destiny] > RC:
                print("\nNo se ha encontrado camino")
                print(sp_indexes)

                print('STATUS: Calculating Dijkstra Shortest Path on %s' % (i))

            countEstudiantes += 1

        else:
            break

    return matrix_cantidad_E, numNodes
Exemplo n.º 23
0
def _make_bibc_bcbv(bus, branch, graph):
    """
    performs depth-first-search bus ordering and creates Direct Load Flow (DLF) matrix
    which establishes direct relation between bus current injections and voltage drops from each bus to the root bus

    :param ppc: matpower-type case data
    :return: DLF matrix DLF = BIBC * BCBV where
                    BIBC - Bus Injection to Branch-Current
                    BCBV - Branch-Current to Bus-Voltage
            ppc with bfs ordering
            original bus names bfs ordered (used to convert voltage array back to normal)
    """

    nobus = bus.shape[0]
    nobranch = branch.shape[0]

    # reference bus is assumed as root bus for a radial network
    refs = bus[bus[:, BUS_TYPE] == 3, BUS_I]
    norefs = len(refs)

    G = graph.copy()  # network graph

    # dictionary with impedance values keyed by branch tuple (frombus, tobus)
    # TODO use list or array, not both
    branches_lst = list(
        zip(branch[:, F_BUS].real.astype(int), branch[:,
                                                      T_BUS].real.astype(int)))
    branches_arr = branch[:, F_BUS:T_BUS + 1].real.astype(int)
    branches_ind_dict = dict(
        zip(zip(branches_arr[:, 0], branches_arr[:, 1]), range(0, nobranch)))
    branches_ind_dict.update(
        dict(
            zip(zip(branches_arr[:, 1], branches_arr[:, 0]),
                range(0, nobranch))))

    tap = branch[:, TAP]  # * np.exp(1j * np.pi / 180 * branch[:, SHIFT])
    z_ser = (branch[:, BR_R].real +
             1j * branch[:, BR_X].real) * tap  # series impedance
    z_brch_dict = dict(zip(branches_lst, z_ser))

    # initialization of lists for building sparse BIBC and BCBV matrices
    rowi_BIBC = []
    coli_BIBC = []
    data_BIBC = []
    data_BCBV = []

    buses_ordered_bfs_nets = []
    for ref in refs:
        # ordering buses according to breadth-first-search (bfs)
        buses_ordered_bfs, predecs_bfs = csgraph.breadth_first_order(
            G, ref, directed=False, return_predecessors=True)
        buses_ordered_bfs_nets.append(buses_ordered_bfs)
        branches_ordered_bfs = list(
            zip(predecs_bfs[buses_ordered_bfs[1:]], buses_ordered_bfs[1:]))
        G_tree = csgraph.breadth_first_tree(G, ref, directed=False)

        # if multiple networks get subnetwork branches
        if norefs > 1:
            branches_sub_mask = (
                np.in1d(branches_arr[:, 0], buses_ordered_bfs)
                & np.in1d(branches_arr[:, 1], buses_ordered_bfs))
            branches = np.sort(branches_arr[branches_sub_mask, :], axis=1)
        else:
            branches = np.sort(branches_arr, axis=1)

        # identify loops if graph is not a tree
        branches_loops = []
        if G_tree.nnz < branches.shape[0]:
            G_tree_nnzs = G_tree.nonzero()
            branches_tree = np.sort(np.array([G_tree_nnzs[0],
                                              G_tree_nnzs[1]]).T,
                                    axis=1)
            branches_loops = (
                set(zip(branches[:, 0], branches[:, 1])) -
                set(zip(branches_tree[:, 0], branches_tree[:, 1])))

        # #------ building BIBC and BCBV martrices ------
        # branches in trees
        brchi = 0
        for brch in branches_ordered_bfs:
            tree_down, predecs = csgraph.breadth_first_order(
                G_tree, brch[1], directed=True, return_predecessors=True)
            if len(tree_down) == 1:  # If at leaf
                pass
            if brch in z_brch_dict:
                z_br = z_brch_dict[brch]
            else:
                z_br = z_brch_dict[brch[::-1]]
            rowi_BIBC += [branches_ind_dict[brch]] * len(tree_down)
            coli_BIBC += list(tree_down)
            data_BCBV += [z_br] * len(tree_down)
            data_BIBC += [1] * len(tree_down)

        # branches from loops
        for loop_i, brch_loop in enumerate(branches_loops):
            path_lens, path_preds = csgraph.shortest_path(
                G_tree,
                directed=False,
                indices=brch_loop,
                return_predecessors=True)
            init, end = brch_loop
            loop = [end]
            while init != end:
                end = path_preds[0, end]
                loop.append(end)

            loop_size = len(loop)
            coli_BIBC += [nobus + loop_i] * loop_size
            for i in range(len(loop)):
                brch = (loop[i - 1], loop[i])
                if np.argwhere(buses_ordered_bfs == brch[0]) < np.argwhere(
                        buses_ordered_bfs == brch[1]):
                    brch_direct = 1
                else:
                    brch_direct = -1
                data_BIBC.append(brch_direct)

                if brch in branches_ind_dict:
                    rowi_BIBC.append(branches_ind_dict[brch])
                else:
                    rowi_BIBC.append(branches_ind_dict[brch[::-1]])

                if brch in z_brch_dict:
                    data_BCBV.append(z_brch_dict[brch] * brch_direct)
                else:
                    data_BCBV.append(z_brch_dict[brch[::-1]] * brch_direct)

                brchi += 1

    # construction of the BIBC matrix
    # column indices correspond to buses: assuming root bus is always 0 after ordering indices are subtracted by 1
    BIBC = csr_matrix((data_BIBC, (rowi_BIBC, np.array(coli_BIBC) - norefs)),
                      shape=(nobranch, nobranch))
    BCBV = csr_matrix((data_BCBV, (rowi_BIBC, np.array(coli_BIBC) - norefs)),
                      shape=(nobranch, nobranch)).transpose()

    if BCBV.shape[0] > nobus - 1:  # if nbrch > nobus - 1 -> network has loops
        DLF_loop = BCBV * BIBC
        # DLF = [A  M.T ]
        #       [M  N   ]
        A = DLF_loop[0:nobus - 1, 0:nobus - 1]
        M = DLF_loop[nobus - 1:, 0:nobus - 1]
        N = DLF_loop[nobus - 1:, nobus - 1:].A
        # considering the fact that number of loops is relatively small, N matrix is expected to be small and dense
        # ...in that case dense version is more efficient, i.e. N is transformed to dense and
        # inverted using sp.linalg.inv(N)
        DLF = A - M.T * csr_matrix(sp.linalg.inv(N)) * M  # Kron's Reduction
    else:  # no loops -> radial network
        DLF = BCBV * BIBC

    return DLF, buses_ordered_bfs_nets
    pos = nx.kamada_kawai_layout(g)
    nx.draw(g,
            pos=pos,
            node_color=graph_colors(g, vmin=-1, vmax=1),
            with_labels=False,
            node_size=100)
plt.suptitle('Dataset of noisy graphs. Color indicates the label', fontsize=20)
plt.show()

##############################################################################
# Barycenter computation
# ----------------------

#%% We compute the barycenter using FGW. Structure matrices are computed using the shortest_path distance in the graph
# Features distances are the euclidean distances
Cs = [shortest_path(nx.adjacency_matrix(x)) for x in X0]
ps = [np.ones(len(x.nodes())) / len(x.nodes()) for x in X0]
Ys = [
    np.array([v for (k, v) in nx.get_node_attributes(x, 'attr_name').items()
              ]).reshape(-1, 1) for x in X0
]
lambdas = np.array([np.ones(len(Ys)) / len(Ys)]).ravel()
sizebary = 15  # we choose a barycenter with 15 nodes

A, C, log = fgw_barycenters(sizebary,
                            Ys,
                            Cs,
                            ps,
                            lambdas,
                            alpha=0.95,
                            log=True)
Exemplo n.º 25
0
def boxStats(boxNet): #fordavid other three calculated here?
    ## matrices    
    boxNodes = len(boxNet)
    boxMat = nx.to_numpy_matrix(boxNet)
    boxSparse = csgraph_from_dense(boxMat)
    boxMatPath = shortest_path(boxSparse, method='auto', directed=False, return_predecessors=False, unweighted=True, overwrite=False)    
    boxPathList = []
    pairsNumBox = len(list(combinations(range(boxNodes), 2)))
    for i in range(boxNodes-1):
        for j in range(i+1, boxNodes):
            tempDist = boxMatPath[i][j]
            if tempDist > 0 and np.isfinite(tempDist):
                boxPathList.append(tempDist)
    
    ##boxNet characteristics
    degreeRaw = list(boxNet.degree())
    degreeBox = []
    for i in degreeRaw:
        degreeBox.append(i)
    degreeNormBox = np.divide(degreeBox, np.sum(degreeBox), dtype = float)
    
    diameterPathBox = np.max(boxPathList)
    avgPathDistBox = np.mean(boxPathList)
    nEdgesBox = np.divide(np.sum(degreeBox), 2, dtype = float)
    edgePBox = nx.density(boxNet)
    globalEfficiencyBox = np.divide(sum(np.divide(1, boxPathList, dtype = float)),pairsNumBox , dtype = float)
    radiusBox = nx.radius(boxNet)
    kCoreBox = max(list(nx.core_number(boxNet).values()))
    degreeAssortBox = nx.degree_assortativity_coefficient(boxNet)
    avgDegreeBox = np.mean(degreeBox)
    maxDegreeBox = max(degreeBox)
    eValsBox = np.linalg.eigvals(boxMat)
    spectralRadiusAdjBox = max(abs(eValsBox))
    eigenCentDictBox = nx.eigenvector_centrality_numpy(boxNet, weight=None)
    eigenCentRawBox = list(eigenCentDictBox.values())
    eigenCentBox = np.divide(eigenCentRawBox, sum(eigenCentRawBox), dtype = float)
    colorsBox = nx.coloring.greedy_color(boxNet, strategy=nx.coloring.strategy_connected_sequential_bfs)
    colorNumBox = len(list(set(list(colorsBox.values()))))
    avgClustCoeffBox = nx.average_clustering(boxNet)                        
    scaledSpectralRadiusBox = np.divide(spectralRadiusAdjBox, avgDegreeBox, dtype = float)
    if motifChoice == 1:
        freqMBox = motifCalc4(boxNet)
    else:
        freqMBox =  [0.166666667, 0.166666667, 0.166666667, 0.166666667, 0.166666667, 0.166666667]
    # network entropy
    lapMatBox= np.asarray(nx.to_numpy_matrix(nx.from_scipy_sparse_matrix(nx.laplacian_matrix(boxNet))))
    eValsLapBox = np.linalg.eigvals(lapMatBox)
    eValsLapBoxSorted = sorted(np.real(eValsLapBox))
    spectralGapBox = eValsLapBoxSorted[1]
    degreeSumBox = np.sum(degreeBox)
    lapMatBoxNorm =  np.divide(lapMatBox, degreeSumBox, dtype = float)
    eValsLapBoxNorm = np.linalg.eigvals(lapMatBoxNorm)
    eValsLapNonZeroBoxNorm = []
    for i in eValsLapBoxNorm:
        j = abs(i)
        if j > 0:
            eValsLapNonZeroBoxNorm.append(j)
    vonEntropyBox = np.divide(entropyCalc(eValsLapNonZeroBoxNorm), math.log(boxNodes,2), dtype = float)
    degreeEntropyBox = np.divide(entropyCalc(degreeNormBox), math.log(boxNodes,2), dtype = float)
    KSEntropyBox = np.divide(math.log(spectralRadiusAdjBox, 2), math.log(boxNodes-1,2), dtype = float)
    motifEntropyBox = np.divide(entropyCalc(freqMBox), math.log(len(freqMBox),2), dtype = float)
    popEntropyBox = np.divide(entropyCalc(eigenCentBox), math.log(boxNodes,2), dtype = float)
    graphEntropyBox = np.divide(graphEntropyCalc(colorsBox), math.log(boxNodes,2), dtype = float)
    
    return edgePBox, radiusBox, kCoreBox, degreeAssortBox, diameterPathBox, avgPathDistBox, nEdgesBox, globalEfficiencyBox, avgDegreeBox, maxDegreeBox, spectralRadiusAdjBox, spectralGapBox, scaledSpectralRadiusBox, colorNumBox, avgClustCoeffBox, freqMBox, motifEntropyBox, vonEntropyBox, graphEntropyBox, popEntropyBox, KSEntropyBox, degreeEntropyBox
Exemplo n.º 26
0
#definition of combined perspectives and similarities matrices
groups = ['marriage', 'loan', 'business', 'patronage']
s1 = similarities[0]  #+similarities[7]
s2 = similarities[6]  #+similarities[3]+similarities[4]
s3 = similarities[1] + similarities[2]
s4 = similarities[5] + similarities[8]
combined_similarities = np.array([s1, s2, s3, s4])

#definition of distances
with np.errstate(divide='ignore', invalid='ignore'):
    distances = np.true_divide(1.0, combined_similarities)
    distances[distances == np.inf] = 0
    distances = np.nan_to_num(distances)
for i in range(4):
    distances[i] = shortest_path(distances[i], directed=False)

#indices of families in Medici network for marriage and loan attributes:
i = families.index('Medici')
perspectives = groups[0:2]
neighs1 = np.where(distances[0][i] != np.inf)[0]
neighs2 = np.where(distances[1][i] != np.inf)[0]
indices = list(set(list(neighs1)) & set(list(neighs2)))

#corresponding families and distance matrices
reduced_families = [families[i] for i in indices]
reduced_distances = distances[0:2][:, indices][:, :, indices]

#edges:
edges1 = []
edges2 = []
Exemplo n.º 27
0
import numpy as np
from scipy.sparse.csgraph import shortest_path

M = np.array([[0, 6, 7, 0, 0, 15], [6, 0, 9, 18, 0, 0], [7, 9, 0, 12, 0, 3],
              [0, 18, 12, 0, 8, 0], [0, 0, 0, 8, 0, 9], [15, 0, 3, 0, 9, 0]])

D, Pr = shortest_path(M, directed=False, method='D', return_predecessors=True)


def get_path(Pr, i, j):
    path = [j]
    k = j
    while Pr[i, k] != -9999:
        path.append(Pr[i, k])
        k = Pr[i, k]
    return path[::-1]


print("shortest path    [0..4]: ", get_path(Pr, 0, 4))
print("length           [0..4]: ", D[0, 4])
print("")
print("shortest path    [0..5]: ", get_path(Pr, 0, 5))
print("length           [0..5]: ", D[0, 5])
Exemplo n.º 28
0
def connectivity_geodesic_subparc(surf_path,
                                  annot_path,
                                  con_verts_idx,
                                  out_annot_path=None,
                                  ref_vol_path=None,
                                  con_mat_path=None,
                                  parc_area=100,
                                  labels=None,
                                  hemi=None,
                                  mode="con+geod+adj",
                                  t2d=None,
                                  lut_path=os.path.join(
                                      FREESURFER_HOME,
                                      'FreeSurferColorLUT.txt')):
    #Read the surface...
    (verts, faces,
     volume_info) = nibabel.freesurfer.io.read_geometry(surf_path,
                                                        read_metadata=True)
    #...its annotation
    lab, ctab, names = nibabel.freesurfer.read_annot(annot_path)
    #...and get the correspoding labels:
    labels_annot = annot_names_to_labels(names, hemi, lut_path=lut_path)
    #Read the indexes of vertices neighboring tract ends voxels:
    con_verts_idx = np.load(con_verts_idx)
    #Set the target labels:
    (labels, nLbl) = read_input_labels(labels=labels, hemi=hemi)
    if "con" in mode:
        #Compute voxel connectivity similarity matrix:
        con = node_connectivity_metric(con_mat_path,
                                       metric="cosine",
                                       mode='sim')
        #Read the T1 to DTI transform:
        t2d = np.loadtxt(t2d)
        #Read the reference tdi_lbl volume:
        vollbl = nibabel.load(ref_vol_path)
        vox = vollbl.get_data()
        voxdim = vox.shape
        #Get only the voxels that correspond to connectome nodes:
        voxijk, = np.where(vox.flatten() > 0)
        voxijk = np.unravel_index(voxijk, voxdim)
        vox = vox[voxijk[0], voxijk[1], voxijk[2]]
        #...and get their coordinates in xyz space
        voxxzy = vollbl.affine.dot(np.c_[voxijk[0], voxijk[1], voxijk[2],
                                         np.ones(vox.shape[0])].T)[:3].T
        del vollbl, voxijk
    #Initialize the output:
    out_names = []
    out_ctab = []
    out_lab = lab
    nL = -1
    #For every annotation name:
    for iL in range(len(names)):
        #Get the mask of the respective vertices
        iVmask = lab == iL
        #...and their indexes
        iV, = np.where(iVmask)
        nV = len(iV)
        #TODO: reconsider this point
        if nV == 0:
            continue
        #Find the corresponding label:
        lbl = labels_annot[iL]
        #If it is not one of the target labels:
        if lbl not in labels:
            #Just add this label to the new annotation as it stands:
            out_names.append(names[iL])
            out_ctab.append(ctab[iL])
            #and change the output indices
            nL += 1
            out_lab[iV] = nL
            continue
        #Get the vertices and faces of this label:
        (verts_lbl, faces_lbl) = extract_subsurf(verts, faces, iVmask)
        #Mask of label vertices that are neighbors of tract end voxels ("con"):
        iV_con = np.in1d(iV, con_verts_idx)
        nVcon = np.sum(iV_con)
        #Get the con vertices and faces of this label:
        (verts_lbl_con,
         faces_lbl_con) = extract_subsurf(verts_lbl, faces_lbl, iV_con)
        #Calculate total area:
        roi_area = np.sum(tri_area(verts_lbl_con[faces_lbl_con]))
        #Calculate number of parcels:
        nParcs = int(np.round(roi_area / parc_area))
        #If no further parcellation is needed
        if nParcs < 2:
            #Just add this label to the new annotation as it stands:
            out_names.append(names[iL])
            out_ctab.append(ctab[iL])
            #and change the output indices
            out_lab[iV] = nL + 1
            nL += 1
            continue
        connectivity = None
        geodist = vertex_connectivity(verts_lbl,
                                      faces_lbl,
                                      mode="sparse",
                                      metric='euclidean')
        if "adj" in mode:
            connectivity = geodist[iV_con, :][:, iV_con]
            connectivity.data = np.ones(connectivity.data.shape)
        #Find the closest neighbors of each non-con-vert to a con-vert...
        iV_noncon = ~iV_con
        noncon2con = np.argmin(geodist.todense()[iV_noncon, :][:, iV_con],
                               axis=1)
        #...and map them
        noncon2con = iV[noncon2con]
        affinity = np.zeros((nVcon, nVcon))
        if "geod" in mode:
            #Compute geodesic distances
            #geodist=gdist.local_gdist_matrix(verts_lbl, faces_lbl.astype('<i4'), max_distance=parc_area)
            #Unpack sparse matrix
            #geodist=geodist.todense()
            from scipy.sparse.csgraph import shortest_path
            #Set 0 values to maximum distance
            geodist = shortest_path(geodist.todense()[iV_con, :][:, iV_con],
                                    method='auto',
                                    directed=False,
                                    return_predecessors=False,
                                    unweighted=False,
                                    overwrite=False)
            max_gdist = np.max(geodist.flatten())
            geodist[geodist == 0] = max_gdist
            #Set diagonal to 0
            geodist = geodist - max_gdist * np.identity(nVcon)
            #Convert them to normalized similarities
            geodist = 1 - geodist / max_gdist
            #...and add them to the affinity metric
            affinity += geodist
        del geodist
        if "con" in mode:
            #Find the voxels of this label:
            iVox_lbl, = np.where(vox == lbl)
            vox_lbl = vox[iVox_lbl]
            #Find for each vertex the closest voxel node xyz coordinates:
            vertsINvox = t2d.dot(np.c_[verts_lbl_con, np.ones(nVcon)].T)[:3].T
            from scipy.spatial.distance import cdist
            v2n = np.argmin(cdist(vertsINvox, voxxzy[iVox_lbl], 'euclidean'),
                            axis=1)
            del vertsINvox
            v2n = vox_lbl[v2n, :]
            #... assign the respective connectivity similarity
            con = con[v2n - 1, :][:, v2n - 1]
            #...and add them to the affinity metric
            affinity += con
            del v2n, con, iVox_lbl, vox_lbl
        from sklearn.cluster import AgglomerativeClustering
        model = AgglomerativeClustering(n_clusters=nParcs,
                                        affinity="precomputed",
                                        connectivity=connectivity,
                                        linkage='average')
        model.fit(affinity)
        #Create the new annotation for these sub-labels:
        resLbl = [
            names[iL] + "-" + str(item).zfill(2)
            for item in np.unique(model.labels_)
        ]
        #Add the new label names
        out_names.append(resLbl)
        #Initialize ctab for these labels
        ctab_lbl = np.repeat(ctab[iL, np.newaxis], nParcs, axis=0)
        #For the RGB coordinate with the bigger distance to 255 or 0
        #distribute that distance  to nParcs values:
        iC = np.argsort(ctab[iL, :3])
        x = 255 - ctab[iL, iC[0]] >= ctab[iL, iC[2]]
        dist = np.where(x, 255 - ctab[iL, iC[0]], -ctab[iL, iC[2]])
        iC = np.where(x, iC[0], iC[2])
        step = dist / (nParcs - 1)
        dist = step * nParcs
        ctab_lbl[:, iC] = np.array(range(ctab[iL, iC], ctab[iL, iC] + dist,
                                         step),
                                   dtype='int')
        ctab_lbl[:, :3][ctab_lbl[:, :3] < 0] = 0
        ctab_lbl[:, :3][ctab_lbl[:, :3] > 255] = 255
        ctab_lbl[:, 4] = np.array(
            [rgb_to_fs_magic_number(ctab_lbl[iP, :3]) for iP in range(nParcs)])
        out_ctab.append(ctab_lbl)
        #Finally change the output indices
        #of the "con" vertices...
        out_lab[iV[iV_con]] = nL + model.labels_ + 1
        #and of the "non-con" vertices that follow their nearest con neighbor
        out_lab[iV[iV_noncon]] = out_lab[noncon2con]
        nL += nParcs
    #Stack everything together
    out_ctab = np.vstack(out_ctab)
    out_names = np.hstack(out_names)
    #...and write the annotation to a file
    if out_annot_path == None:
        out_annot_path = os.path.splitext(annot_path)[0] + str(
            parc_area) + ".annot"
    nibabel.freesurfer.write_annot(out_annot_path, out_lab, out_ctab,
                                   out_names)
Exemplo n.º 29
0
            alti = neighbor[3]
            if alti < 0:
                vec[neighbor_id] = dis*10
            else:
                vec[neighbor_id] = dis/10
        link_matrix.append(vec)

    return np.array(link_matrix), np.array(coord_matrix)

if __name__ == '__main__':
    c = [[0, 4],
        [2, 4],
        [1, 2],
        [3, 1],
        [4, 3]]

    l = [[0, 6, 3, 0, 0],
        [0, 0, 0, 1, 2],
        [2, 1, 0, 3, 0],
        [0, 0, 2, 0, 1],
        [0, 3, 0, 0, 0]]

    c = np.array(c)
    csr = csr_matrix(l)

    start = 4
    end = 0
    d, p = shortest_path(csr, return_predecessors=True, indices=start)
    route = get_path_row(start, end, p)
    print(d[end])
    print(route)
Exemplo n.º 30
0
    def _fit_transform(self, X):
        self.nbrs_ = NearestNeighbors(
            n_neighbors=self.n_neighbors,
            algorithm=self.neighbors_algorithm,
            metric=self.metric,
            p=self.p,
            metric_params=self.metric_params,
            n_jobs=self.n_jobs,
        )
        self.nbrs_.fit(X)
        self.n_features_in_ = self.nbrs_.n_features_in_
        if hasattr(self.nbrs_, "feature_names_in_"):
            self.feature_names_in_ = self.nbrs_.feature_names_in_

        self.kernel_pca_ = KernelPCA(
            n_components=self.n_components,
            kernel="precomputed",
            eigen_solver=self.eigen_solver,
            tol=self.tol,
            max_iter=self.max_iter,
            n_jobs=self.n_jobs,
        )

        kng = kneighbors_graph(
            self.nbrs_,
            self.n_neighbors,
            metric=self.metric,
            p=self.p,
            metric_params=self.metric_params,
            mode="distance",
            n_jobs=self.n_jobs,
        )

        # Compute the number of connected components, and connect the different
        # components to be able to compute a shortest path between all pairs
        # of samples in the graph.
        # Similar fix to cluster._agglomerative._fix_connectivity.
        n_connected_components, labels = connected_components(kng)
        if n_connected_components > 1:
            if self.metric == "precomputed":
                raise RuntimeError(
                    "The number of connected components of the neighbors graph"
                    f" is {n_connected_components} > 1. The graph cannot be "
                    "completed with metric='precomputed', and Isomap cannot be"
                    "fitted. Increase the number of neighbors to avoid this "
                    "issue.")
            warnings.warn(
                "The number of connected components of the neighbors graph "
                f"is {n_connected_components} > 1. Completing the graph to fit"
                " Isomap might be slow. Increase the number of neighbors to "
                "avoid this issue.",
                stacklevel=2,
            )

            # use array validated by NearestNeighbors
            kng = _fix_connected_components(
                X=self.nbrs_._fit_X,
                graph=kng,
                n_connected_components=n_connected_components,
                component_labels=labels,
                mode="distance",
                metric=self.nbrs_.effective_metric_,
                **self.nbrs_.effective_metric_params_,
            )

        if parse_version(scipy.__version__) < parse_version("1.3.2"):
            # make identical samples have a nonzero distance, to account for
            # issues in old scipy Floyd-Warshall implementation.
            kng.data += 1e-15

        self.dist_matrix_ = shortest_path(kng,
                                          method=self.path_method,
                                          directed=False)

        G = self.dist_matrix_**2
        G *= -0.5

        self.embedding_ = self.kernel_pca_.fit_transform(G)
Exemplo n.º 31
0
    shape_df['filo_score'] = ((shape_df['filo_points']/shape_df['area'])
                             - (shape_df['round_points']/shape_df['area'])
    )
    shape_df.reset_index(drop=True, inplace=True)
#*********************************************************************************************#
    filo_df = shape_df[(shape_df.filo_score >= 0.25) & (shape_df.area > 9)]
    filo_lengths, vertices = [],[]
    for ix in filo_df.index:
        coords = filo_df.coords[ix]
        box = filo_df.bbox_verts[ix]
        # skel = np.argwhere(medial_axis(pic_binary[box[0][0]:box[2][0],box[0][1]:box[2][1]]) == True)

        sparse_matrix = csr_matrix(squareform(pdist(coords, metric='cityblock')))
        distances = csgraph.shortest_path(sparse_matrix,
                                          method = 'FW',
                                          return_predecessors=False
        )
        ls_path = np.max(distances)
        farpoints = np.where(distances == ls_path)

        filo_lengths.append(float(round(ls_path / pix_per_um, 3)))
        vertices.append([coords[farpoints[0][0]],coords[farpoints[0][len(farpoints[0]) // 2]]])
        filo_pic = pic_maxmin[box[0][0]:box[2][0],box[0][1]:box[2][1]]
        # vimage.gen_img(filo_pic,name = filo_df.label_bin[ix], savedir = '{}/filo'.format(img_name), show=False)


    filo_df['filo_lengths'] = filo_lengths
    filo_df['vertices'] = vertices

    shape_df = pd.concat([shape_df, filo_df[['filo_lengths','vertices']]],axis=1)
    # vimage.gen_img(pic_skels)
Exemplo n.º 32
0
print "\n\n cosine matrix of friendship matrix \n"
print cosine_mat

new_mat = np.zeros(shape=(w1, w1))
for i in range(0, w1):
    for j in range(0, w1):
        new_mat[i][j] = max(friend_mat[i][j], cosine_mat[i][j])
print '\n\n After put 1 in rating matrix from friendship matrix \n'
print new_mat

rec = np.reciprocal(new_mat)
print '\n\nreciprocal\n'
print rec
dijkstra = shortest_path(
    rec,
    method='D',
    directed=False,
    unweighted=False,
)
print "\n\n dijkstra for shortest path \n"

print dijkstra

trust = np.reciprocal(dijkstra)
print '\n\n Direct Trust \n'
print trust

for i in range(len(trust)):
    for j in range(len(trust)):
        if (trust[i][j] == inf):
            trust[i][j] = 0
 def check(method, directed):
     SP, pred = shortest_path(directed_G, method, directed=directed,
                              overwrite=False,
                              return_predecessors=True)
     assert_array_almost_equal(SP, SP_res[directed])
     assert_array_almost_equal(pred, pred_res[directed])
Exemplo n.º 34
0
            [0,     0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
            [0,     5,  0,  0,  0,  4,  0,  14, 0,  0,  0],
            [0,     0,  11, 0,  0,  0,  0,  0,  9,  0,  0],
            [0,     0,  0,  12, 0,  17, 0,  0,  8,  0,  0],
    [0,0,0,0,0,0,0,0,3,0,6],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,8,0,0],
    [0,0,0,0,0,0,0,0,0,7,0]]

INICIO = 0
FIM = 1
print("START: " + str(INICIO))
print("END: " + str(FIM))
print("")

print("##### scipy floyd_warshall")
graph = csr_matrix(G_dense)
dist_matrix, predecessors = floyd_warshall(csgraph=graph, directed=False, return_predecessors=True)
print("##### Route:")
print(get_path(predecessors, 0,4))
print("##### Distance:")
print(dist_matrix[0][4])

print("")
print("##### scipy shortest_path")
D, Pr = shortest_path(G_dense, directed=False, method='FW', return_predecessors=True)
print("##### Route:")
print(get_path(Pr, 0,4))
print("##### Distance:")
print(D[0][4])
Exemplo n.º 35
0
def tree_to_dist_mat(sent_len, tree, directed=True, self_loop=False):
    adj_mat = tree_to_adj(sent_len, tree, directed, self_loop=False)
    dist_matrix = shortest_path(csgraph=csr_matrix(adj_mat), directed=directed)
    if self_loop:
        np.fill_diagonal(dist_matrix, 1)
    return dist_matrix
Exemplo n.º 36
0
 os.mkdir('figures/'+emb_name+'/genealogy_distance/')
 for t in np.arange(next(x[0] for x in enumerate(size) if x[1]>5),int(max(cell_lineage[:,0]))):    
     adjacency = np.zeros((len(names_unique),len(names_unique)))
     indices = np.where(cell_lineage[:,0] == t)
     points = cell_lineage[indices[0],:]
     points = points[:,[1,2,3]]
     tri = Delaunay(points)
     edges = collect_edges(tri)
     
     G = np.zeros((points.shape[0],points.shape[0]))
     
     for e in edges:
         G[e[0],e[1]] = 1
         G[e[1],e[0]] = 1
     
     dist_matrix = shortest_path(G, return_predecessors=False, directed=False, unweighted=True)
     X = []
     Y = []
     dist_matrix_gen = np.zeros((points.shape[0],points.shape[0]))
     for i in range(points.shape[0]):
         for j in range(i+1,points.shape[0]):
             dist_matrix_gen[i,j] = genealogical_distance_static[names_unique.index(names[indices[0][i]]),names_unique.index(names[indices[0][j]])]
             X.append(dist_matrix[i,j])
             Y.append(dist_matrix_gen[i,j])
     dist_matrix_gen = dist_matrix_gen + np.transpose(dist_matrix_gen)
     
     plt.figure(figsize=(7, 7))
     xedges = np.array(range(0,11))+0.5
     yedges = np.array(range(0,21))+0.5
     plt.hist2d(X,Y, bins=(xedges, yedges))
     axes = plt.gca()
Exemplo n.º 37
0
    clustering_dist = probability_distribution(clustering_frequency,
                                               number_of_nodes)
    print_scatter_plot(clustering_dist,
                       xaxis='Clustering Coeffecient',
                       yaxis='Probability of Clustering Coeffecient',
                       title=name + ' Clustering Coeffecient Distribution',
                       log=True,
                       logx=False,
                       tofit=False,
                       message='\nClustering Coeffecient Average: ' +
                       str(np.round(clustering_average, 6)))
elif (option == 'c'):

    min_dist_matrix = shortest_path(csgraph=sparse_matrix,
                                    method='auto',
                                    directed=not undirected,
                                    return_predecessors=False,
                                    unweighted=True)
    #print('Minimum Distance Matrix:\n', min_dist_matrix)

    min_dist_frequency = {}

    for i in range(number_of_nodes):
        min_dist_frequency = list_frequency(min_dist_matrix[i],
                                            min_dist_frequency)
    #print('Minimum Distance Frequency:\n', min_dist_frequency)

    number_min_dist_val = number_of_nodes * number_of_nodes
    #print('Number of Minimum Distance Values:', number_min_dist_val)

    if float('Inf') in min_dist_frequency:
Exemplo n.º 38
0
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か? DP法では??
#本当に貪欲法か? DP法では??
#本当に貪欲法か? DP法では??

import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

h,w=mii()

mat=[limii() for _ in range(10)]

arr=[limii() for _ in range(h)]

chk=shortest_path(csr_matrix(mat))

ans=0

for i in arr:
    for j in i:
        if j!=-1:
            ans+=int(chk[j][1])
print(ans)

Exemplo n.º 39
0
graph_name = 'dwt_1005'
mat_data = io.loadmat(graph_name + '.mat')

graph = mat_data['Problem']['A'][0][0]
graph_arr = graph.toarray()

# get number of vertices
n = graph.shape[0]
num_of_pivots = 100

# maximum iteration for K_means
max_iter = 50

start_time = time.time()

shortest_paths = cs.shortest_path(graph)

# randomly choose a pivot
p0 = np.random.randint(0, n)
pivots = [p0]

# initialize list of minimum distances to each pivots
mins = []
for i in range(n):
    mins.append([shortest_paths[p0][i], p0])

for i in range(1, num_of_pivots):
    # normal max/min sp:
    argmax = 0
    for k in range(1, n):
        if mins[k][0] > mins[argmax][0]:
Exemplo n.º 40
0
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
import numpy as np
h, w = map(int, input().split())

l = []
for i in range(10):
    l.append(list(map(int, input().split())))
a = csr_matrix(np.array(l))

data = shortest_path(a)

data = list(data)

wall = []
for i in range(h):
    wall.append(list(map(int, input().split())))
ans = 0
for i in range(h):
    for j in range(w):
        if wall[i][j] == -1:
            pass
        else:
            ans += data[wall[i][j]][1]
print(int(ans))
 def check(method):
     SP = shortest_path(directed_G, method=method, directed=True,
                        overwrite=False)
     assert_array_almost_equal(SP, directed_SP)
Exemplo n.º 42
0
            name1, name2 = line.strip().split('\t')
            j = characters.index(name1)
            k = characters.index(name2)
            counts[i, j, k] += 1
            counts[i, k, j] += 1
        f.close()

#setup distance matrices
n_samples = 40
counts = counts[:, 0:n_samples, 0:n_samples]
distances = np.empty((3, n_samples, n_samples))
for i in range(3):
    distances[i] = 1.0 / np.maximum(0.5, counts[i])
    #distances[i] /= np.maximum(1,distances[i].sum(axis=1))[:,None]
    import scipy.sparse.csgraph as csgraph
    distances[i] = csgraph.shortest_path(distances[i])


def mds():
    mds = mview.MDS(distances[0], weights=None, dim=2, verbose=2)
    mds.gd(batch_size=None, max_iter=30)
    mds.plot_embedding(labels=range(n_samples))
    mds.plot_computations()


def mpse():
    mv = mview.MPSE([distances[0], distances[1]],
                    data_args={'weights': 'reciprocal'},
                    verbose=2)
    mv.gd()
    mv.plot_embedding()