Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
44ed4ea
add distributions file
nwlandry Jul 18, 2022
e2f9037
Merge branch 'netsiphd:main' into main
nwlandry Jul 18, 2022
81f8dcd
added init files
nwlandry Jul 18, 2022
cfa2a3f
added distance_trajectory.py
Jul 18, 2022
be70ba2
updated distribution
nwlandry Jul 18, 2022
d4c7d3f
Merge branch 'main' of https://github.com/nwlandry/netrw into main
Jul 18, 2022
c22f370
added playground
Jul 19, 2022
424f339
Merge branch 'main' of https://github.com/netsiphd/netrw
nwlandry Jul 19, 2022
b6cd41c
added networkx edge swap method
acuschwarze Jul 19, 2022
5cd8b99
Merge branch 'main' of https://github.com/nwlandry/netrw
acuschwarze Jul 19, 2022
0b72c12
Create properties_overtime.py
clarabay Jul 19, 2022
ed74d15
Added rewiring and viz methods
piazza-b Jul 19, 2022
e73053c
Merge branch 'main' of https://github.com/nwlandry/netrw
piazza-b Jul 19, 2022
4526f1b
updates
nwlandry Jul 19, 2022
bbecc17
Merge branch 'main' of https://github.com/nwlandry/netrw
nwlandry Jul 19, 2022
8293131
change to copy_graph
nwlandry Jul 19, 2022
afacd32
added a doc string to distance_trajectory.py
acuschwarze Jul 19, 2022
c8da982
Formatting
piazza-b Jul 19, 2022
2eb427a
Merge branch 'main' of https://github.com/nwlandry/netrw
piazza-b Jul 19, 2022
e78ebab
Formatting
piazza-b Jul 19, 2022
70a8671
Update robust_rewiring.py
piazza-b Jul 19, 2022
7843000
Update robust_rewiring.py
piazza-b Jul 19, 2022
6744074
Create properties_overtime.py
clarabay Jul 19, 2022
d9b0854
trying to make changes
clarabay Jul 19, 2022
58e789f
Delete properties_overtime.py
clarabay Jul 19, 2022
5b4bfac
Merge branch 'main' of
clarabay Jul 19, 2022
ea84f92
Merge branch 'main' of https://github.com/nwlandry/netrw
clarabay Jul 19, 2022
70fee06
fixed small merge problems i think
clarabay Jul 19, 2022
2c7af91
updated distributions
nwlandry Jul 19, 2022
76f11be
Merge branch 'main' of https://github.com/nwlandry/netrw
nwlandry Jul 19, 2022
95f10c7
making it so each iteration copies original graph to rewire
clarabay Jul 19, 2022
10bda49
Merge branch 'main' of https://github.com/nwlandry/netrw
clarabay Jul 19, 2022
43b127a
network properties over time
clarabay Jul 19, 2022
bee8d27
small change
clarabay Jul 19, 2022
18dfd92
playing on the playground
acuschwarze Jul 19, 2022
4b948be
Merge branch 'main' of https://github.com/nwlandry/netrw into main
acuschwarze Jul 19, 2022
326cb72
Create playground.ipynb
acuschwarze Jul 19, 2022
76acd2a
network properties over time
clarabay Jul 19, 2022
5a019b0
Merge branch 'main' of https://github.com/nwlandry/netrw
clarabay Jul 19, 2022
5a963c1
Merge branch 'main' of https://github.com/netsiphd/netrw
nwlandry Jul 19, 2022
ba7ccb0
Merge branch 'main' of https://github.com/nwlandry/netrw
nwlandry Jul 19, 2022
cce4ed3
distance_trajecotry thing works now
acuschwarze Jul 19, 2022
812bd58
Update playground.ipynb
acuschwarze Jul 19, 2022
c3c39e2
Merge branch 'main' of https://github.com/nwlandry/netrw
acuschwarze Jul 19, 2022
5f363d4
updates
nwlandry Jul 19, 2022
93178c5
change plot label
clarabay Jul 19, 2022
3e5bd52
change axes
clarabay Jul 19, 2022
ca98646
axes changes
clarabay Jul 19, 2022
d0c1577
undo change
clarabay Jul 19, 2022
983ba29
updates
nwlandry Jul 19, 2022
8525a47
Added docstring to plotDistanceTrajectory
acuschwarze Jul 19, 2022
0fef9b0
Merge branch 'main' of https://github.com/nwlandry/netrw
acuschwarze Jul 19, 2022
82076bf
documentation and step rewire addition
clarabay Jul 20, 2022
85270fb
added documentation
clarabay Jul 20, 2022
bc26b02
removed plotting
clarabay Jul 20, 2022
36d1e81
removed plotting
clarabay Jul 20, 2022
f0752fc
updated docs
clarabay Jul 20, 2022
3965885
Add notebook measuring various network properties over time
hartle Jul 20, 2022
9ebadd4
separate plot function for properties over time
clarabay Jul 20, 2022
e600d30
Merge branch 'main' of https://github.com/nwlandry/netrw
clarabay Jul 20, 2022
f114fc4
updates
nwlandry Jul 20, 2022
8b94dd6
plot
clarabay Jul 20, 2022
203ac9b
plots for properties_overtime
clarabay Jul 20, 2022
bc4d14e
style: format with black
nwlandry Jul 20, 2022
c488483
Merge branch 'main' of https://github.com/nwlandry/netrw
clarabay Jul 20, 2022
b19f61f
plot for properties_overtime
clarabay Jul 20, 2022
1a0d8dc
small updates
clarabay Jul 20, 2022
0f377b9
Added analysis code
hartle Jul 20, 2022
e2b3e96
updates
nwlandry Jul 20, 2022
68dde67
style: format with black
nwlandry Jul 20, 2022
056a798
Merge branch 'main' of https://github.com/netsiphd/netrw
nwlandry Jul 20, 2022
a6ab4bf
style: format with black
nwlandry Jul 20, 2022
41f4976
added netrd as a requirement
nwlandry Jul 20, 2022
16a4ced
update output format
clarabay Jul 20, 2022
bcb7b4f
update output format
clarabay Jul 20, 2022
aa0a61c
Merge branch 'main' of https://github.com/nwlandry/netrw
clarabay Jul 20, 2022
c637a97
updated input type
clarabay Jul 20, 2022
8121fd7
fixing errors
clarabay Jul 20, 2022
769ad2d
Merge branch 'main' of https://github.com/netsiphd/netrw
nwlandry Jul 20, 2022
6d8c384
Merge branch 'main' of https://github.com/nwlandry/netrw
nwlandry Jul 20, 2022
5841208
fix format
nwlandry Jul 20, 2022
36655f8
change kwarg default
nwlandry Jul 20, 2022
b7e67b4
Update robust_rewiring.py
jkbren Jul 20, 2022
b922fd9
Update robust_rewiring.py
jkbren Jul 20, 2022
be358c3
Update local_edge_rewire.py
jkbren Jul 20, 2022
917cf27
Update visualization.py
jkbren Jul 20, 2022
31417de
update NetworkXEdgeSwap
nwlandry Jul 20, 2022
658d2f9
Merge branch 'main' of https://github.com/nwlandry/netrw
nwlandry Jul 20, 2022
6beb32e
Update requirements.txt
jkbren Jul 20, 2022
afb3af3
deleted test files
nwlandry Jul 20, 2022
6d36075
update __init__
nwlandry Jul 20, 2022
24c9738
added imports
nwlandry Jul 20, 2022
1305796
removed unnecessary file
nwlandry Jul 20, 2022
b7b637e
bug fixes
hartle Jul 20, 2022
6b31dc1
Merge branch 'main' into main
jkbren Jul 20, 2022
4ab7ea5
update
nwlandry Jul 20, 2022
0d821c9
style: format with black and isort
nwlandry Jul 20, 2022
8f4c1ad
delete distributions file. (Redundant)
nwlandry Jul 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions netrw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
A 2022 NetSI Collabathon Product.
"""

from .analysis import *
from .rewire import *
5 changes: 5 additions & 0 deletions netrw/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .confusion import *
from .distance_trajectory import *
from .plot_property_values_over_time import *
from .properties_heatmap import *
from .properties_overtime import *
45 changes: 45 additions & 0 deletions netrw/analysis/confusion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import numpy as np


def rewiring_distance_confusion_matrix(
G, rewiring_methods, distance_measures, timesteps=100, ensemble_size=10
):
"""Plotting distances from start graph for different rewiring schemes and distance metrics

Parameters
----------
G : NetworkX Graph
The starting graph
rewiring_methods : list of Rewiring classes in the rewiring module.
methods for rewiring graphs. each class specified must have a
`full_rewire` method and that method must have `timesteps` as
a keyword argument.
distance_measures : netrd Distance
metric for measuring the distance between the before and after graphs
timesteps : int, default: 100
the number of iterations
ensemble_size : int, default: 10
the number of rewiring trajectories to run.

Returns
-------
numpy matrix
a matrix where rows are rewiring methods, columns are distance metrics
and entries are average distances.

Notes
-----
Currently this method does not support keyword args for the rewiring methods
and distance metrics.
"""
n = len(rewiring_methods)
m = len(distance_measures)
C = np.zeros([n, m])
for i in range(n):
rw = rewiring_methods[i]()
for j in range(m):
dist = distance_measures[i]()
for k in range(ensemble_size):
rG = rw.full_rewire(G, timesteps=timesteps)
C[i, j] += dist(rG, G) / ensemble_size
return C
164 changes: 164 additions & 0 deletions netrw/analysis/distance_trajectory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import copy
import warnings

import netrd
import networkx as nx
import numpy as np
from matplotlib import pyplot as plt

from ..rewire import NetworkXEdgeSwap


def distanceTrajectory(
G,
distance=netrd.distance.Hamming,
rewire=NetworkXEdgeSwap,
num_steps=100,
num_runs=100,
distance_kwargs={},
rewire_kwargs={},
):
"""
Get some data on graph distances as a function of number of rewiring steps.

Parameters
----------
G : networkx Graph or DiGraph

distance : netrd graph distance class

rewire : netrw rewire class

num_steps : integer or list
number of rewiring steps to be tracked or ordered list of rewiring
steps to be tracked

num_runs : integer
number of trajectories to be generated for evaluating the standard
deviation for a set of rewiring trajectories

distance_kwargs : dictionary
a dictionary of keyword arguments for an instantiation of the netrd
distance class

rewire_kwargs : dictionary
a dictionary of keyword arguments for an instantiation of
the netrw rewire class
"""

G0 = copy.deepcopy(G)

# check whether input for num rewire in a number of rewiring steps (int)
# or a list of steps
if isinstance(num_steps, list):
rewire_steps = num_steps
else:
rewire_steps = range(num_steps)

# initialize data array
data = np.zeros((len(rewire_steps), num_runs))

# define a rewire function
step_rewire = rewire().step_rewire
rewire_function = lambda g: step_rewire(g, copy_graph=False, **rewire_kwargs)

# define a distance function
distfun = distance() # get a class instantiation
distance_function = lambda g1, g2: distfun(g1, g2, **distance_kwargs)

for j in range(num_runs):
for i in range(max(rewire_steps)):
rewire_function(G0)
data[i + 1, j] = distance_function(G0, G)

return data


def plotDistanceTrajectory(
G,
distance=netrd.distance.Hamming,
num_steps=100,
show=["mean", "median", "std-env"],
labels=None,
add_legend=True,
fig=None,
ax=None,
linecolors=None,
envcolor="cyan",
xlabel="Number of rewiring steps",
ylabel=None,
**kwargs
):

# check whether input for num steps in a number of rewiring steps (int)
# or a list of steps
if hasattr(num_steps, "__iter__"):
rewire_steps = num_steps
else:
rewire_steps = range(num_steps)

# set ylabel
if ylabel is None:
ylabel = distance.__name__ + r" distance to $G_0$"

# set line labels
if labels is None:
labels = show
elif hasattr(labels, "__iter__"):
if len(labels) != len(show):
raise ValueError(
"List for keyword argument `show` and list for"
+ "`keyword argument` must have the same length."
)
else:
raise ValueError("Keyword argument `labels` must be None or list.")

# set line colors
if linecolors is None:
tabcolors = ["tab:blue", "tab:orange", "tab:green", "tab:red", "tab:purple"]
linecolors = [tabcolors[i % len(tabcolors)] for i in range(len(show))]
elif hasattr(linecolors, "__iter__"):
if len(linecolors) != len(show):
raise ValueError(
"List for keyword argument `show` and list for"
+ "`keyword argument` must have the same length."
)
else:
raise ValueError("Keyword argument `labels` must be None or list.")

# get data
data = distanceTrajectory(G, distance=distance, num_steps=num_steps, **kwargs)

# get data for lines for plot
line_data = []
for s in show:
if s == "mean":
line_data += [np.mean(data, axis=1)]
elif s == "std":
line_data += [np.std(data, axis=1)]
elif s == "median":
line_data += [np.median(data, axis=1)]
elif s == "std-env":
mean = np.mean(data, axis=1)
std = np.std(data, axis=1)
env_data = [std - mean, std + mean]
else:
warnings.warn("Unknown summary statistic", s, "will be ignored.")

std = np.std(data, axis=1)

if fig is None:
fig = plt.gcf()
if ax is None:
ax = plt.subplot(111)
if "std-env" in show:
ax.fill_between(rewire_steps, env_data[0], env_data[1], color=envcolor)

for i in range(len(line_data)):
ax.plot(rewire_steps, line_data[i], color=linecolors[i], label=labels[i])

if add_legend:
plt.legend()

plt.xlabel(xlabel)
plt.ylabel(ylabel)
67 changes: 67 additions & 0 deletions netrw/analysis/plot_property_values_over_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

import netrw


def plot_property_values_over_time(propvals, ylabel=""):
"""
Plot mean and standard deviation of how a network property changes over time during multiple iterations of a rewiring process.

Parameters
----------
propvals :2d nump array
2d numpy array of output values from dictionary of properties_overtime.
The network property is calculated at each step and iteration of the rewiring process. Rows are single iteration over a rewiring process.
Columns show different iterations of the rewiring process from the initial graph.

ylabel: string, optional
Label for y axis of graph for mean and standard deviation of network property
Default is no label.

Returns
-------
fig: matplotlib figure
Figure of mean and standard deviation for a network property throughout the rewiring process.

"""

alllist = [] # list of all properties for all iterations at each of the time steps

valarray = propvals
num_rows, num_cols = valarray.shape

for k in range(num_cols):
alllist.append([])
for l in range(num_rows):
alllist[k].append(valarray[l][k])

# find mean and standard deviation over different iterations of rewiring process
meanlist = []
sdlist = []
for k in range(num_cols):
meanlist.append(np.mean(alllist[k]))
sdlist.append(np.std(alllist[k]))

# find upper and lower bound of standard deviation interval around the mean
upperbd = []
lowerbd = []
for a in range(len(meanlist)):
upperbd.append(meanlist[a] + sdlist[a])
lowerbd.append(meanlist[a] - sdlist[a])

fig, (ax0) = plt.subplots(nrows=1)
ax0.plot(range(num_cols), meanlist, color="blue", linewidth=2)
ax0.plot(range(num_cols), upperbd, color="blue")
ax0.plot(range(num_cols), lowerbd, color="blue")
ax0.fill_between(
range(num_cols), upperbd, lowerbd, color="cornflowerblue", alpha=0.5
)

ax0.set_xlabel("number of rewiring steps")
ax0.set_ylabel(ylabel)

fig.show()

return fig
Empty file.
51 changes: 51 additions & 0 deletions netrw/analysis/properties_overtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from copy import deepcopy

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

import netrw
from netrw.rewire import AlgebraicConnectivity, KarrerRewirer, NetworkXEdgeSwap


def properties_overtime(init_graph, rewire_method, property1, tmax, numit):
"""
Analyze the property values of a network as a function of rewire steps.
Looks at how a network property changes as a rewiring process occurs.

Parameters
----------
init_graph : NetworkX graph
Initial graph upon which rewiring will occur.
rewire_method : netrw rewire class object
Algorithm for rewiring a network with step_rewire option
property1 : NetworkX function
Network description property that outputs a single value for a given network. Should work with any function that
summarizes a NetworkX graph object into a single value. For example, nx.average_clustering, nx.average_shortest_path_length, etc.
tmax : int
Number of rewiring steps to perform for each iteration.
numit : int
Number of rewiring iterations to perform on the initial graph. The given rewiring process will be performed numit
times on the initial graph to look at the distribution of outcomes for this rewiring process on the initial graph.
Returns
-------
property_dict: dictionary
Dictionary of output where the keys are the property name and the values are a 2D numpy arry of the network property
calculated at each step and iteration of the rewiring process. Rows are single iteration over a rewiring process.
Columns show different iterations of the rewiring process from the initial graph.

"""
property_dict = {}
property_dict[property1.__name__] = np.zeros((numit, tmax))
rw = rewire_method()

for i in range(numit):
G0 = deepcopy(init_graph)
propertyval = property1(G0) # calculate property of initial network
property_dict[property1.__name__][i, 0] = propertyval
for j in range(1, tmax):
G0 = rw.step_rewire(G0, copy_graph=False) # rewire
propertyval = property1(G0) # calculate property of the rewired network
property_dict[property1.__name__][i, j] = propertyval

return property_dict
Loading