Skip to content
Snippets Groups Projects
Commit 359402bd authored by chris1218-de's avatar chris1218-de
Browse files

first push

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #5085 failed
__pycache__
sirg.egg-info
language: python
dist: xenial
python:
- "3.8"
- "3.7"
- "3.6"
branches:
only:
- master
install:
- pip install -e .[test]
script:
- python -m pytest --color=yes --cov=torsk
This diff is collapsed.
# SIR and GRPAHS
run.py 0 → 100644
import networkx as nx
from sirg import generate as gg
from sirg import sir as sir
G = gg.random_connected_graph(20, 1)
sir.sis_simulation(G, 1, 0.5, 0.2, 10)
setup.py 0 → 100644
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="sirg",
version="0.0.1",
author="Christopher Fichtlscherer",
author_email="fichtlscherer@mailbox.org",
description="Running SIR on random graphs",
long_description=long_description,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU GPL",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
"""
Feb 03, 2020
Christopher Fichtlscherer (fichtlscherer@mailbox.org)
GNU General Public License
In this file we define functions to generate random graphs and to check
certain of their properties.
"""
import networkx as nx
import numpy as np
def random_connected_graph(n, p):
"""generates a random connected graph with n nodes and every edges
exists with probability"""
for i in range(10**5):
seed = int(np.random.random() * 10**9)
G = nx.erdos_renyi_graph(n, p, seed=seed, directed=False)
if nx.is_connected(G) == True:
return G
print("ERROR: Over 100 000 unsuccesfull iterations")
def density(G):
""" returns the density of the graph"""
nodes = G.number_of_nodes()
edges = G.number_of_edges()
density = edges / nodes
return density
"""
Feb 03, 2020
Christopher Fichtlscherer (fichtlscherer@mailbox.org)
GNU General Public License
Run sir model on a networkx graph.
"""
import networkx as nx
import numpy as np
def sis_infection_step(adjacency, ill_vector, beta):
""" Perform one step of infection on the graph G.
every node which is connected by an edge with a node which is healthy
will be infected with a probability of beta"""
ad_ill = adjacency[ill_vector == 1]
ad_ill_prob = np.random.random(np.shape(ad_ill))
new_ill_matrix = (ad_ill_prob < beta).astype(int) * ad_ill
new_ill_people = np.sum(new_ill_matrix, axis = 0)
# hier ist ein problem muss noch gecheckt werden
next_ill = ((ill_vector + new_ill_people) >= 1).astype(int)
return ill_vector, next_ill
def sis_recover_step(ill_vector, next_ill, gamma):
"""every node which was ill in the step before has the chance to become
ill by probability gamma. Nodes which were just infected can't become
directly health, nodes which have been ill and became infected again can
recover
"""
recover_prob = np.random.random(ill_vector.shape)
recovered = ill_vector * (recover_prob < gamma).astype(int)
status_next = next_ill - recovered
return status_next
def sis_simulation(G, ill_node, beta, gamma, steps):
"""performs a sis simulation on the graph G, starting from node ill_node
for steps time steps."""
adjacency = nx.to_numpy_matrix(G)
ill_vector = np.zeros(G.number_of_nodes())
ill_vector[ill_node] = 1
for i in range(steps):
ill_vector, next_ill = sis_infection_step(adjacency, ill_vector, beta)
ill_vector = sis_recover_step(ill_vector, next_ill, gamma)
print(ill_vector)
"""
def simultation(start, steps, beta, gamma):
nx.set_node_attributes(G, 1, 'illness')
nx.set_node_attributes(G, 'blue', 'color')
G.node[start]['illness'] = 2 #welche Node wird krank (illness 2) 15 = Hamburg
ill_vector = np.zeros(steps)
for i in range(steps):
#print(i)
#attriplot()
#simplebericht()
#bericht = nx.get_node_attributes(G,'illness').values()
#bericht = nx.get_node_attributes(G,'color').values()
#print(bericht)
data_array_maker(ill_vector, i) # same as simplebericht but stores in the array ill_vector
for j in range(nodes):
# gesunden
if (G.node[j]['illness'] == 2) and (np.random.random() < gamma):
G.node[j]['illness'] = 1
for m in G.neighbors(j):
weightchance = np.random.random() * G.edges[m,j]['weight']
if weightchance < beta:G.node[m]['illness'] = 3
for jj in range(nodes):
if G.node[jj]['illness']==3:G.node[jj]['illness'] = 2
return ill_vector
"""
File added
"""
Feb 11, 2020
Christopher Fichtlscherer (fichtlscherer@mailbox.org)
GNU General Public License
Testing if the functions which are made for generating random graphs and
analyzing them working correctly
"""
import pytest
import networkx as nx
import sirg.generate
def test_random_connected_graph_1():
"""
we test if the number of nodes and edges is correct.
"""
n = 50
p = 1
g = sirg.generate.random_connected_graph(n, p)
number_nodes = len(g.nodes())
number_edges = len(g.edges())
assert (number_nodes == 50) and (number_edges == (50 * (50-1))/2)
def test_density():
G = nx.Graph()
G.add_nodes_from(range(10))
G.add_edges_from([(1,2), (1,5), (9,3), (4,5)])
density = sirg.generate.density(G)
assert density == 0.4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment