Skip to main content

Custom graph/network/multi-weighted network class based on storing list of neighbors for each nodes (as opposed to edge list) for scalable sampling and searching algorithms

Project description

logo

NNetwork

PyPI Version Supported Python Versions

NNetwork is a Custom graph/network/multi-weighted network class optimized for scalable subgraph sampling and searching algorithms. NNetwork stores a dictionary that maps each node to a list of its neighbors to allow for O(1) access for finding neighbors.

The efficiency of neighbor access is import for sampling algorithm such as random walks and Markov chain Monte Carlo motif sampling on graphs, which rely on accessing neighborhood information at every iteration of sampling. In comparison, many packages rely on calculations involving powers of adjacency matrices to calculate random walks of length k.

The default class of NNetwork encodes a network with weighted edges, which can also have list-valued edge weights as its 'color'.

Update for 0.1.0:

Built-in functions contain sampling algorithms for mesoscale network patches using various MCMC motif sampling algorithms [1]. At stationary distribution, it computes a uniformly chosen k-walk in the graph, which can optionally enforced to be non-backtraking, and the induced adjacency pattern is returned as a k x k matrix. Algorithimically, a given k-walk is randomly updated using a suitable MCMC algorithm. The so-computed k x k mesoscale patches are basis of subgraph analysis and network dictionary learning in [2].

By Josh Vendrow and Hanbaek Lyu


Installation

To install NNetwork, run this command in your terminal:

$ pip install -U NNetwork

This is the preferred method to install NNetwork, as it will always install the most recent stable release.

If you don't have pip installed, these installation instructions can guide you through the process.

Usage

Undirected Graphs

Create an undirected (weighted) graph from an edgelist:

>>> from NNetwork import NNetwork
>>> edgelist = [[1,2],[2,3],[3,4]]
>>> G = NNetwork()
>>> G.add_edges(edgelist)
>>> G.has_edge(2,3)
True
>>> G.get_edge_weight(2,3)
1

Get the neighbors of a node:

>>> G.neighbors(3)
[2,4]

Find the intersection of edges with another network:

>>> edgelist2 = [[2,3],[3,4],[5,7]]
>>> G2 = NNetwork()
>>> G2.add_edges(edgelist2)
>>> G.intersection(G2)
[[2,3],[3,4]]

Weighted Graphs

Create a weighted graph from an edgelist:

>>> from NNetwork import NNetwork
>>> edgelist = [[1,2,0.5],[2,3,0.8]]]
>>> G = NNetwork()
>>> G.add_wtd_edges(edgelist)
>>> G.get_edge_weight([2,3])
0.8

Convert weighted graph to an unweighed graph by thresholding

>>> G_simple = G.threshold2simple(0.7)
>>> G_simple.edges()
[[2,3]]

Mesoscale patch computation

>>> edgelist = [[1,2],[2,3],[1,3],[1,4],[1,5]]
>>> G = nn.NNetwork()
>>> G.add_edges(edgelist)
>>> print(G.vertices)
['1', '2', '3', '4', '5']
>>> print(G.edges)
{"['1', '2']": 1, "['2', '1']": 1, "['2', '3']": 1, "['3', '2']": 1, "['1', '3']": 1, "['3', '1']": 1, "['1', '4']": 1, "['4', '1']": 1, "['1', '5']": 1, "['5', '1']": 1}
>>> X, embs = G.get_patches(k=3, sample_size=4, skip_folded_hom=False)
>>> print(X) # each column is a vectorizaiton of k x k induced adjacency matrix
array([[0., 0., 0., 0.],
       [1., 1., 1., 1.],
       [0., 1., 0., 0.],
       [1., 1., 1., 1.],
       [0., 0., 0., 0.],
       [1., 1., 1., 1.],
       [0., 1., 0., 0.],
       [1., 1., 1., 1.],
       [0., 0., 0., 0.]])
>>> print(embs) # four consecutive 3-walks in G
[array(['2', '3', '2'], dtype='<U32'),
 array(['1', '2', '3'], dtype='<U32'),
 array(['4', '1', '4'], dtype='<U32'),
 array(['4', '1', '4'], dtype='<U32')]

Citing

If you use our work in an academic setting, please cite our papers:

[1] Hanbaek Lyu, Facundo Memoli, and David Sivakoff, “Sampling random graph homomorphisms and applications to network data analysis.” https://arxiv.org/abs/1910.09483 (2019)

[2] Hanbaek Lyu, Yacoub Kureh, Joshua Vendrow, and Mason A. Porter, “Learning low-rank mesoscale structures of networks” https://arxiv.org/abs/2102.06984 (2021)

Development

See CONTRIBUTING.md for information related to developing the code.

Suggested Git Branch Strategy

  1. master is for the most up-to-date development, very rarely should you directly commit to this branch. Your day-to-day work should exist on branches separate from master. It is recommended to commit to development branches and make pull requests to master.4. It is recommended to use "Squash and Merge" commits when committing PR's. It makes each set of changes to master atomic and as a side effect naturally encourages small well defined PR's.

Additional Optional Setup Steps:

  • Create an initial release to test.PyPI and PyPI.

    • Follow This PyPA tutorial, starting from the "Generating distribution archives" section.
  • Create a blank github repository (without a README or .gitignore) and push the code to it.

  • Delete these setup instructions from README.md when you are finished with them.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

NNetwork-0.1.4.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

NNetwork-0.1.4-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file NNetwork-0.1.4.tar.gz.

File metadata

  • Download URL: NNetwork-0.1.4.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.50.2 importlib-metadata/4.10.1 keyring/21.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.5

File hashes

Hashes for NNetwork-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f178dff8ca2806926e44e10d4ba33d511a4f714c60313f3f8e958a65ed5c7d0b
MD5 41d4a3aa69b64772a1e0a37136b4cbd0
BLAKE2b-256 a3ff5dedbf52bdec545b9e57250debd143b41fe1b2622188e48de763d39a8c36

See more details on using hashes here.

File details

Details for the file NNetwork-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: NNetwork-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.50.2 importlib-metadata/4.10.1 keyring/21.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.5

File hashes

Hashes for NNetwork-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1855c0ec5e50053586571cefa86210e2e25f1e428dbe05e52aca1a4a6f24742d
MD5 029a2aaa604965254b2fd0eec6cd2d8b
BLAKE2b-256 1a8eea55757dc3c399f21327bc986b4ee6390dbe324dba249b34e13a2c6cdb9b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page