flownetwork Python project
Project description
flownetwork
A python package for flow network analysis https://pypi.python.org/pypi/flownetwork
install the most updated github version
pip install -U git+https://github.com/chengjun/flownetwork.git
install and upgrade
Open a terminal, and input:
pip install flownetwork
if your want to ungrade to a new version, just input:
pip install --upgrade flownetwork
if your want to uninstall, please input:
pip uninstall flownetwork
import
# import packages
import flownetwork.flownetwork as fn
import networkx as nx
import matplotlib.pyplot as plt
print(fn.__version__)
$version = 3.0.9$
flow network analysis
help(fn.constructFlowNetwork)
Help on function constructFlowNetwork in module flownetwork.flownetwork:
constructFlowNetwork(C)
C is an array of two dimentions, e.g.,
C = np.array([[user1, item1],
[user1, item2],
[user2, item1],
[user2, item3]])
Return a balanced flow network
# constructing a flow network
demo = fn.attention_data
gd = fn.constructFlowNetwork(demo)
# drawing a demo network
fig = plt.figure(figsize=(12, 8),facecolor='white')
pos={0: np.array([ 0.2 , 0.8]),
2: np.array([ 0.2, 0.2]),
1: np.array([ 0.4, 0.6]),
6: np.array([ 0.4, 0.4]),
4: np.array([ 0.7, 0.8]),
5: np.array([ 0.7, 0.5]),
3: np.array([ 0.7, 0.2 ]),
'sink': np.array([ 1, 0.5]),
'source': np.array([ 0, 0.5])}
width=[float(d['weight']*1.2) for (u,v,d) in gd.edges(data=True)]
edge_labels=dict([((u,v,),d['weight']) for u,v,d in gd.edges(data=True)])
nx.draw_networkx_edge_labels(gd,pos,edge_labels=edge_labels, font_size = 15, alpha = .5)
nx.draw(gd, pos, node_size = 3000, node_color = 'orange',
alpha = 0.2, width = width, edge_color='orange',style='solid')
nx.draw_networkx_labels(gd,pos,font_size=18)
plt.show()
nx.info(gd)
'Name: \nType: DiGraph\nNumber of nodes: 9\nNumber of edges: 15\nAverage in degree: 1.6667\nAverage out degree: 1.6667'
# balancing the network
# if it is not balanced
gh = fn.flowBalancing(gd)
nx.info(gh)
'Name: \nType: DiGraph\nNumber of nodes: 9\nNumber of edges: 15\nAverage in degree: 1.6667\nAverage out degree: 1.6667'
# flow matrix
m = fn.getFlowMatrix(gd)
m
matrix([[ 0., 1., 0., 0., 3., 1., 0., 0., 0.],
[ 0., 0., 3., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 2., 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 1., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 2.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 5., 2., 1., 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
fn.getMarkovMatrix(m)
array([[ 0. , 0.2 , 0. , 0. , 0.6 ,
0.2 , 0. , 0. , 0. ],
[ 0. , 0. , 1. , 0. , 0. ,
0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0.5 , 0. ,
0. , 0. , 0. , 0.5 ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. ],
[ 0. , 0. , 0. , 0. , 0. ,
0.33333333, 0. , 0. , 0.66666667],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. ],
[ 0.55555556, 0.22222222, 0.11111111, 0. , 0. ,
0. , 0.11111111, 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ]])
fn.getUmatrix(gd)
matrix([[ 1. , 0.2 , 0.2 , 0.1 , 0.6 ,
0.4 , 0. ],
[ 0. , 1. , 1. , 0.5 , 0. ,
0. , 0. ],
[ 0. , 0. , 1. , 0.5 , 0. ,
0. , 0. ],
[ 0. , 0. , 0. , 1. , 0. ,
0. , 0. ],
[ 0. , 0. , 0. , 0. , 1. ,
0.33333333, 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
1. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 1. ]])
# return dissipationToSink,totalFlow,flowFromSource
fn.networkDissipate(gd)
defaultdict(<function flownetwork.flownetwork.<lambda>>,
{0: [0, 5, 5],
1: [0, 3, 2],
2: [2, 4, 1],
3: [2, 2, 0],
4: [2, 3, 0],
5: [2, 2, 0],
6: [1, 1, 1]})
# flow distance
fn.flowDistanceFromSource(gd)
{0: 1.0,
1: 1.333333333333333,
2: 2.0,
3: 3.0,
4: 2.0,
5: 2.5,
6: 1.0,
'sink': 3.2222222222222214}
fn.outflow(gd, 1)
3
fn.inflow(gd, 1)
3
fn.averageFlowLength(gd)
3.2222222222222223
# fn.getAverageTimeMatrix(gd)
Plot
fig = plt.figure(figsize=(9, 9),facecolor='white')
ax = fig.add_subplot(111)
fn.plotTree(gd,ax)
plt.show()
from random import random
x = np.array(range(1, 100))
y = (x+random()*x)**3
plt.plot(x, y)
plt.xscale('log');plt.yscale('log')
plt.show()
fn.alloRegressPlot(x,y,'r','s','$x$','$y$', loglog=True)
rg = np.array([ 20.7863444 , 9.40547933, 8.70934714, 8.62690145,
7.16978087, 7.02575052, 6.45280959, 6.44755478,
5.16630287, 5.16092884, 5.15618737, 5.05610068,
4.87023561, 4.66753197, 4.41807645, 4.2635671 ,
3.54454372, 2.7087178 , 2.39016885, 1.9483156 ,
1.78393238, 1.75432688, 1.12789787, 1.02098332,
0.92653501, 0.32586582, 0.1514813 , 0.09722761])
fn.powerLawExponentialCutOffPlot(rg, '$x$', '$p(x)$')
[-0.0099301962503268171,
-0.064764460567964449,
-0.17705123513352666,
0.89999847894045781]
fn.DGBDPlot(rg)
from networkx.utils import powerlaw_sequence
pl_sequence = powerlaw_sequence(1000,exponent=2.5)
fig = plt.figure(figsize=(4, 4),facecolor='white')
ax = fig.add_subplot(111)
fn.plotPowerlaw(pl_sequence,ax,'r','$x$')
Calculating best minimal value for power law fit
fig = plt.figure(figsize=(4, 4),facecolor='white')
ax = fig.add_subplot(111)
fn.plotCCDF(pl_sequence,ax,'b','$x$')
Calculating best minimal value for power law fit
bins, result, gini_val = fn.gini_coefficient(np.array(pl_sequence))
plt.plot(bins, bins, '--', label="perfect")
plt.plot(bins, result, label="observed")
plt.title("$GINI: %.4f$" %(gini_val))
plt.legend(loc = 0, frameon = False)
plt.show()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flownetwork-3.1.0.tar.gz
(125.1 kB
view details)
File details
Details for the file flownetwork-3.1.0.tar.gz
.
File metadata
- Download URL: flownetwork-3.1.0.tar.gz
- Upload date:
- Size: 125.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 845b23c8f63817a2187b914b5d948973f9c2157c20de1d5e9fdb985739b29da7 |
|
MD5 | 87d5f427f72a61c67784759150f62d73 |
|
BLAKE2b-256 | 766855c476628c159761b993d13baa845f2cabd4cdf03a1dff49af8de06ccb25 |