How to use the pynets.core.thresholding function in pynets

To help you get started, we’ve selected a few pynets examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dPys / PyNets / tests / test_thresholding.py View on Github external
def test_density(x, thr):
        d_known = thresholding.est_density(thresholding.threshold_absolute(x, thr, copy=True))
        x = thresholding.density_thresholding(x, d_known)
        d_test = thresholding.est_density(x)
        assert np.equal(np.round(d_known, 1), np.round(d_test, 1))
github dPys / PyNets / tests / test_thresholding.py View on Github external
def test_invert(x, thr, cp):
        x_cp = x.copy() # invert modifies array in place and need orig to assert.
        x_cp = thresholding.threshold_proportional(x_cp, thr, copy=cp)
        s = thresholding.invert(x_cp)
        x = x.flatten() # flatten arrays to more easily check s > x.
        s = s.flatten()
        s_gt_x = [inv_val > x[idx] for idx, inv_val in enumerate(s) if inv_val > 0]
        assert False not in s_gt_x
github dPys / PyNets / tests / test_thresholding.py View on Github external
def test_thr2prob(x, thr):
        s = thresholding.threshold_absolute(thresholding.normalize(x), thr)
        s[0][0] = 0.0000001
        t = thresholding.thr2prob(s)
        assert float(len(t[np.logical_and(t < 0.001, t > 0)])) == float(0.0)
github dPys / PyNets / pynets / core / thresholding.py View on Github external
G = nx.from_numpy_matrix(conn_matrix)
    if not nx.is_connected(G):
        [G, pruned_nodes] = netstats.prune_disconnected(G)
        pruned_nodes.sort(reverse=True)
        coords_pre = list(coords)
        labels_pre = list(labels)
        if len(pruned_nodes) > 0:
            for j in pruned_nodes:
                labels_pre.pop(j)
                coords_pre.pop(j)
            conn_matrix = nx.to_numpy_array(G)
            labels = labels_pre
            coords = coords_pre

    maximum_edges = G.number_of_edges()
    G = thresholding.weight_to_distance(G)
    min_t = nx.minimum_spanning_tree(G, weight="distance")
    len_edges = min_t.number_of_edges()
    upper_values = np.triu_indices(np.shape(conn_matrix)[0], k=1)
    weights = np.array(conn_matrix[upper_values])
    weights = weights[~np.isnan(weights)]
    edgenum = int(float(thr) * float(len(weights)))
    if len_edges > edgenum:
        print("%s%s%s" % ('Warning: The minimum spanning tree already has: ', len_edges,
                          ' edges, select more edges. Local Threshold will be applied by just retaining the Minimum '
                          'Spanning Tree'))
        conn_matrix_thr = nx.to_numpy_array(G)
        return conn_matrix_thr, coords, labels

    k = 1
    len_edge_list = []
    while len_edges < edgenum and k <= np.shape(conn_matrix)[0] and (len(len_edge_list[-fail_tol:]) -