Skip to main content

MLN

Library for working with large multilayer networks.

Authors:

  • Eszter Bokányi
  • Rachel de Jong
  • Yuliia Kazmina

Contact: e.bokanyi@uva.nl

The code is distirbuted under the MIT Licence and should be properly attributed and cited upon reuse - see LICENCE.md for details.

MultiLayeredNetwork class

The MultiLayeredNetwork class from src/mln.py contains methods and attributes to work with a large multilayer network using different edge types and layers efficiently.

The network has to be unweighted, but can be directed.

The network is either loaded from an already saved sparse matrix and a node attribute CSV using the following attributes:

  • adjacency_file: scipy.sparse.csr matrix saved as an npz file
  • node_attribute_file: CSV to be loaded as a pd.DataFrame, rows in order of previous matrix rows should contain a column called "label" that is the primary key for node identification

Or in-memory objects can also be given to the constructor:

  • adjacency_matrix: scipy.sparse.csr matrix
  • node_attribute_dataframe: pd.DataFrame, rows in order of previous matrix rows

Pre-stored files with standard names can be called with the shorthand:

  • from_library: str Then the npz and csv.gz files will be read from the library_path/from_library folder similarly to the first method.

After loading these two key elements, class attributes and methods work the same.

The adjacency matrix self.A is stored in a scipy.sparse.csr_matrix class, that only saves nonzero elements, and on which scipy csgraph algorithms run. People are indexed from 0 to N-1, where N is the total number of nodes in this network.

Two dictionaries mapping user id (label) to integer node ids (NID) and back are created based on the node attribute file in the self.map_label_to_nid and self.map_nid_to_label attributes. These mappings only refer to one instance of the class - if you create a subgraph (see later) the coding is going to change, and the mapping should be found in the new instance that represents the subgraph.

The adjacency matrix contains integers that encode linktypes if viewed as binary numbers. Each possible linktype is assigned an integer of the form 2**i. For example, if both type i and type j edge is present between two people, then the corresponding value in self.A would be 2**i+2**j. It means that we can test for a certain edgetype using bitwise AND operation very cheaply. E.g. a certain element of self.A is 7, then 7=1+2+4 which means that edgetypes 0,1, and 2 are present between the two people, and 7&2 = 2 in Python (it behaves like a mask 111 & 010 = 010).

scipy.csr matrices are cheap to slice rowwise, but beware, some operation that seem straightforward in numpy might be costly (e.g. getting random coordinates after each other or colwise slicing)! If something is running too long, consult the scipy reference manual.

Node attributes are stored in self.node_attributes which is a pandas.DataFrame. It is possible to store human-readable or longer column names in self.attribute_colnames, and code tables for used variables in self.attribute_code_table, keys of that variable are the colnames in the values of self.attribute_colnames.

Example loadings:

# FROM FILES
# import custom class for the network from mln
import MultiLayeredNetwork

# read the whole network
popnet = MultiLayeredNetwork(
    adjacency_file = 'path_to/adjacency.npz',
    node_attribute_file = 'path_to/attributes.csv.gz'
)

# FROM MEMORY
# import custom class for the network
from mln import MultiLayeredNetwork

# read the whole network
popnet = MultiLayeredNetwork(
    adjacency_matrix = A, # NxN scipy.sparse.csr_matrix
    node_attribute_file = df # pd.DataFrame containing N rows in the order of the matrix
)

# FROM LIBRARY
# import custom class for the network
from mln import MultiLayeredNetwork

# read the whole network
popnet = MultiLayeredNetwork(
    from_library="full"
)

Download files

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

Source Distribution

mlnlib-0.2.36.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

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

mlnlib-0.2.36-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file mlnlib-0.2.36.tar.gz.

File metadata

  • Download URL: mlnlib-0.2.36.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mlnlib-0.2.36.tar.gz
Algorithm Hash digest
SHA256 5156a63d515edc40f662c5981e5ee1e509bb62f0ebd150a55cd569f5c145a17f
MD5 3e96ac37de121c5ab96df5567b9b4be4
BLAKE2b-256 8b7270617f1c2036ebd3837e90d586a6ef2071baf06d130ccd0f063f70985651

See more details on using hashes here.

File details

Details for the file mlnlib-0.2.36-py3-none-any.whl.

File metadata

  • Download URL: mlnlib-0.2.36-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mlnlib-0.2.36-py3-none-any.whl
Algorithm Hash digest
SHA256 b6f4b44455a6c23f1607eae2b71500779367b9d7ebf71455d7521cb955a5488f
MD5 95efba195b10361a76b0a0ea8290abb1
BLAKE2b-256 7a3d18d5e94173232187e25af3cbaadbba7ab9352cdbfb849def4610421601d5

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.39

2 files

0.2.38

2 files

0.2.37

2 files

This release

0.2.36 This release

2 files

0.2.35

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page