Skip to main content

Fast Scalabe Spread Process Simulator Over Large Scale Complex Multi-Layer Networks

Project description

License GitHub last commit GitHub All Releases Hits

FastGEMF

Scalable High-Speed Simulation of Stochastic Spreading Processes over Complex Multilayer Networks

Alt text

FastGEMF is scalabe spread process simulator for small to large scale multi-layer complex networks. You can define mechanistic model with mutiple infleuncer through simple steps and simualte the difussion process over networks.

Table of Contents

  • [Features]
  • [Installation]
  • [Quick implementation]
  • [Usage]
  • [Repository Structure]
  • [How To Cite]

Features

  • Fast and efficient event based simulator( logarithmic time complexity w.r.t. number of nodes in networks).
  • Multi-layer networks are supported.
  • Networks can be weighted/unweighted or directed/undirected.
  • Mechanistic models can have different influencing agents competing over same or different layers.

Installation

FastGEMF can be installed on Linux, macOS, and Windows operating systems. Ensure you have Python and pip installed on your system by following the official pip documentation.

Using PyPI To install FastGEMF from the Python Package Index (PyPI), execute the following command in your terminal:

python3 -m pip install fastgemf --user --upgrade

Using the Source Code Repository If you prefer to install the latest version directly from the source, you can use pip to install FastGEMF from GitHub:

python3 -m pip install https://github.com/KSUNetse/FastGEMF/archive/main.zip --user --upgrade

Alternatively, you can clone the repository and install FastGEMF locally:

bash Copy code

git clone https://github.com/KSUNetse/FastGEMF.git
cd FastGEMF
python3 -m pip install . --user --upgrade

After installation, you can verify that FastGEMF is correctly installed by running:

python -c "import fastgemf; print(fastgemf.__version__)"

Quick implementation

In the following example, we define a simple SIR model over a single layer contact network.

import fastgemf as fg
import networkx as nx

# Define the SIR model schema
sir_model = (
    fg.ModelSchema("SIR")
    .define_compartment(['S', 'I', 'R'])
    .add_network_layer('contact_network')
    .add_node_transition(
        name='recovery',
        from_state='I',
        to_state='R',
        rate='delta'
    )
    .add_edge_interaction(
        name='infection',
        from_state='S',
        to_state='I',
        inducer='I',
        network_layer='contact_network',
        rate='beta'
    )
)

# Print the model schema
print(sir_model)

# Visualize the model graph
sir_model.draw_model_graph()

# Generate a Barabási-Albert graph as an example network using popular module NetworkX
num_nodes = 10000
num_edges = 6
contact_network = nx.barabasi_albert_graph(num_nodes, num_edges)

# Convert the network to a sparse matrix format
contact_network_csr= nx.to_scipy_sparse_array(contact_network)

# Create a model configuration instance
sir_instance = (
    fg.ModelConfiguration(sir_model)
    .add_parameter(beta=0.3, delta=0.1)
    .get_networks(contact_network=contact_network_csr)
)

# Print the configured model
print(sir_instance)


# Creating  the Simulation object
sim = fg.Simulation(sir_instance, initial_condition={'percentage': {'I': 10, 'S': 90}}, stop_condition={'time': 30}, nsim=1)
sim.run()
sim.plot_results()

Usage

By using Two modules ModelSchema and ModelConfiguration we can define any competing-like mechanistic model over different or same layers. ModelSchema module enabes user to create any multi-agent mechanistic model. The module can be from FastGEMF as:

import fastgemf as fg 

#Here we define a simple, yet popular `SIR` model.
SIR_model = (fg.ModelSchema(name='SIR') # model name
    .define_compartment(compartments=["S", "I", "R"]) # the compartments of the model
    .add_network_layer("contact_network") # Defining the layers' 
    .add_node_transition(name="recovery", from_state="I", to_state="R", rate="delta") # Defining node-based transition
    .add_edge_interaction(name="infection", from_state="S", to_state="I", inducer="I", network_layer="contact_network", rate="beta") # Defining edge-based transition
)

The user can now save the parametric mechanistic model defined in a yaml file, as a template for easy future use.

SIR_model.to_yaml("your_directory\SIR.yaml") # to save the defined model in `your_directory`

The user can draw or print the model they defined as:

SIR_model.draw_model_graph()

Below are two visual representations of the SIR model we defined:

SIR Node Transition <\p>

SIR Edge Interaction

or user can easily print the details as:
print(SIR_model)

The printed result is as:

Model: SIR
Compartments: S, I, R
Network Layers: ['layer_1']
Node-Based Transitions: recovery: I --> R (rate: delta)
Edge-Based Transitions: infection: S --(I)-> I on contact_network (rate: beta)

In the next step, we use ModelConfiguration module to assign values to the parameters of the model instane we defined(If you want to save to YAML make sure you have loaded the directories for networks):

SIR_instnace=(fg.ModelConfiguration(SIR_model)
.add_parameter(beta=.05,delta=.05) # set the defined parameters to numeric values
.get_networks(contact_network=network1 )) # passing the network object to model instnace
#.load_network_directory(contact_network="directory/for/contact_network") # If you want to save to YAML make sure you have loaded the directories for network
SIR_instnace.to_yaml("your_directory\SIR_instance.yaml") # saving the instance of the model as a yaml file

and the instance of ModelConfiguration by printing it:

print(SIR_instance)

The printed result is as:

Model: SIR
Compartments: S, I, R
Network Layers:
contact_network: Network With N Nodes and L Edges # N and L will replace with real size of the passed network
Node-Based Transitions:
recovery: I -> R (delta: 0.05)
Edge-Based Transitions:
infection: S --(I)-> I on contact_network (beta: 0.05)

Finally to run the simulation and plotting the results, we need to create an object of the Simulation class and create a YAML file of the object for reprocibility of the results as follows:

# Creating  the Simulation object
sim = fg.Simulation(SIR_instance, initial_condition={'percentage': {'I': 10, 'S': 90}}, stop_condition={'time': 30}, nsim=1)
sim.run()
sim.plot_results()
sim.to_yaml('sim.yaml')

initial_condition={'percentage': {'I': 10, 'S': 90} specifies the percentage of nodes to be initially and randomly at different states , stop_condition={'time': 30} can be number of events as number of transitions(each transition is counted as one event) or time as in this example. nsim determines the number of simulation to run.

Also, we can pass two YAML files for ModelCofiguration and Simulation instances as:

sim = fg.Simulation.from_yaml('SIR_instance.yaml','sim.yaml')
sim.run()
sim.plot_results()

Now Similiarly now we can define the SI_1I_2 model, when two influencers are cometing over two different layers, while each of the influencer have different rate for transmiting the disease and different curing rates. We can define this model as:

# Define the SI_1I_2 model schema
si1i2_model = (
    fg.ModelSchema("SI_1I_2")
    .define_compartment(['S', 'I_1', 'I_2'])
    .add_network_layer('primary_layer')
    .add_network_layer('secondary_layer')
    .add_node_transition(name='recovery1', from_state='I_1', to_state='S', rate='delta1')
    .add_node_transition(name='recovery2', from_state='I_2', to_state='S', rate='delta2')
    .add_edge_interaction(
        name='infection1', from_state='S', to_state='I_1', inducer='I_1',
        network_layer='primary_layer', rate='beta1'
    )
    .add_edge_interaction(
        name='infection2', from_state='S', to_state='I_2', inducer='I_2',
        network_layer='secondary_layer', rate='beta2'
    )
)

print(si1i2_model)
si1i2_model.draw_model_graph()

# Generate two random geometric graphs
primary_network = nx.random_geometric_graph(1000, 0.02)
secondary_network = nx.random_geometric_graph(1000, 0.05)

# Convert networks to sparse matrix format
primary_network_csr = nx.to_scipy_sparse_array(primary_network)
secondary_network_csr = nx.to_scipy_sparse_array(secondary_network)

# Create an isntance of ModelConfiguration, as SI_1I_2 model which also
si1i2_instance = (
    fg.ModelConfiguration(si1i2_model)
    .add_parameter(beta1=0.1, delta1=0.2, beta2=0.05, delta2=0.1)
    .get_networks(primary_layer=primary_network_csr, secondary_layer=secondary_network_csr)
)

print(si1i2_instance)

# Creating  the Simulation object
sim = fg.Simulation(si1i2_instance, initial_condition={'percentage': {'I_1': 5, 'I_2': 5, 'S':90}}, stop_condition={'time': 100}, nsim=1)
sim.run()
sim.plot_results()

Repository Structure

FastGEMF/                  
├── docs/                      
├── predefined_instants/       
├── src/                       
│   └── fastgemf/              
│       ├── __init__.py        
│       ├── error.py          
│       ├── GEMFCore.py       
│       ├── GEMFSimulation.py  
│       ├── initial_condition.py
│       ├── initializer.py     
│       ├── modelconfiguration.py
│       ├── modelschema.py    
│       ├── network.py         
│       ├── post_population.py 
│       ├── stop_conditions.py 
│       ├── times_structure.py 
│       ├── utils.py           
│       ├── visualization.py   
├── tests/                     
├── pyproject.toml             
├── README.md                  
├── requirements.txt          
├── setup.cfg                  
└── setup.py

Citation

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

FastGEMF-0.1.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

FastGEMF-0.1.0-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file FastGEMF-0.1.0.tar.gz.

File metadata

  • Download URL: FastGEMF-0.1.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for FastGEMF-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d89284490c6c2693ba12174d789586cfbe5765f3e8d89c6a067ee6362c8f50d3
MD5 98a509a8777691bab96f964cd7559bc7
BLAKE2b-256 6f9e7911fb326f66a5d7c8300c33e847a9a56cf980474ea717cf78f274013477

See more details on using hashes here.

File details

Details for the file FastGEMF-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: FastGEMF-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for FastGEMF-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9aa144acae5e41b40b1fc46e78a1a6b57cd08029de103d6653fcaab0ebf2fff
MD5 8e093121effeac8ab4bd44de49fa9dbe
BLAKE2b-256 6915dfa4a46798d38a05340475d857d805ef9ab55f478b15aebbdce71d5499b7

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