Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Created February 20, 2013 03:16
Show Gist options
  • Save tpoisot/4992542 to your computer and use it in GitHub Desktop.
Save tpoisot/4992542 to your computer and use it in GitHub Desktop.
Niche model of food webs in python
def niche_foodweb(S, C):
G = nx.DiGraph()
G.add_nodes_from([n for n in xrange(S)])
## Step 1 - create random species
for n in G:
G.node[n]['n'] = np.round(np.random.uniform(),2)
G.node[n]['r'] = np.random.beta(1, 1/float(2*C)-1) * G.node[n]['n']
G.node[n]['c'] = np.random.uniform(G.node[n]['r']/float(2),G.node[n]['n'])
## Step 2 - smallest species are basal
Smallest = np.min([G.node[n]['n'] for n in G])
for n in G:
if G.node[n]['n'] == Smallest:
G.node[n]['r'] = 0
## Step 3 - create the links
for n1 in G:
for n2 in G:
if G.node[n2]['n'] <= G.node[n1]['c']+G.node[n1]['r']:
if G.node[n2]['n'] >= G.node[n1]['c']-G.node[n1]['r']:
G.add_edge(n1, n2)
return G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment