Skip to content

Commit

Permalink
Updated plots, added a function that will get data for a heatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
FalafelGood committed Mar 21, 2021
1 parent 0002afc commit 4842329
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 69 deletions.
7 changes: 3 additions & 4 deletions QuNet-Paper/MultiPathDemo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function plot_with_userpairs(max_pairs::Int64,

plot(x, avepath, linewidth=2, legend = false)
xaxis!(L"$\textrm{Number of End User Pairs}$")
yaxis!(L"$\textrm{Average Number of Paths Used}$")
savefig("plots/avepath_userpair.png")
savefig("plots/avepath_userpair.pdf")
end
Expand Down Expand Up @@ -649,18 +650,16 @@ function plot_bandwidth_ratio_with_memory_rate(num_trials::Int64, perc_range::Tu
end



# MAIN
"""
Uncomment functions to reproduce plots from the paper / create your own
Note: Reproducing plots with the default parameters (those used in the paper)
will take between 2 to 12 hours each. Reader beware!
"""
# Usage : (max_pairs::Int64, num_trials::Int64)
# plot_with_userpairs(50, 5000)

# Usage : (perc_range::Tuple{Float64, Float64, Float64}, num_trials::Int64)
plot_with_percolations((0.0, 0.01, 0.7), 5000)
# plot_with_percolations((0.0, 0.01, 0.7), 5000)

# Usage : (num_trials::Int64, max_depth::Int64)
# plot_with_timedepth(1000, 15)
Expand Down
13 changes: 13 additions & 0 deletions QuNet-Paper/heatmap.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using QuNet

grid_size = 10
num_trials = 100
num_pairs = 50

net = GridNetwork(grid_size, grid_size)
#usage:
# (network::Union{QNetwork, QuNet.TemporalGraph},
# num_trials::Int64, num_pairs::Int64; max_paths=3, src_layer::Int64=-1,
# dst_layer::Int64=-1, edge_perc_rate=0.0)
coord_list = QuNet.heat_data(net, num_trials, num_pairs, max_paths=4)
println(coord_list)
47 changes: 47 additions & 0 deletions QuNet-Paper/heatmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# https://www.python-graph-gallery.com/86-avoid-overlapping-in-scatterplot-with-2d-density

# Libraries
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import kde

# Create data: 200 points
data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
x, y = data.T

# Create a figure with 6 plot areas
fig, axes = plt.subplots(ncols=6, nrows=1, figsize=(21, 5))

# Everything starts with a Scatterplot
axes[0].set_title('Scatterplot')
axes[0].plot(x, y, 'ko')
# As you can see there is a lot of overlapping here!

# Thus we can cut the plotting window in several hexbins
nbins = 20
axes[1].set_title('Hexbin')
axes[1].hexbin(x, y, gridsize=nbins, cmap=plt.cm.BuGn_r)

# 2D Histogram
axes[2].set_title('2D Histogram')
axes[2].hist2d(x, y, bins=nbins, cmap=plt.cm.BuGn_r)

# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
k = kde.gaussian_kde(data.T)
xi, yi = np.mgrid[x.min():x.max():nbins * 1j, y.min():y.max():nbins * 1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))

# plot a density
axes[3].set_title('Calculate Gaussian KDE')
axes[3].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto', cmap=plt.cm.BuGn_r)

# add shading
axes[4].set_title('2D Density with shading')
axes[4].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)

# contour
axes[5].set_title('Contour')
axes[5].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap=plt.cm.BuGn_r)
axes[5].contour(xi, yi, zi.reshape(xi.shape))
plt.show()

12 changes: 12 additions & 0 deletions data/gridsize.txt

Large diffs are not rendered by default.

Binary file modified plots/bandwidth_with_userpairs.pdf
Binary file not shown.
Binary file modified plots/bandwidth_with_userpairs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plots/cost_maxpaths.pdf
Binary file not shown.
Binary file modified plots/cost_maxpaths.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plots/path_gridsize.pdf
Binary file not shown.
Binary file modified plots/path_gridsize.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plots/path_userpair.pdf
Binary file not shown.
Binary file added plots/path_userpair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plots/temporal_drawing.pdf
Binary file not shown.
51 changes: 51 additions & 0 deletions src/Benchmarking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,54 @@ function net_performance(network::Union{QNetwork, QuNet.TemporalGraph},

return performance, performance_err, ave_pathcounts, ave_pathcounts_err
end


"""
Takes a network as input and returns greedy_multi_path! heatmap data for
some number of user pairs. (i.e. a list of efficiency fidelity coordinates
for all end users over all num_trials)
"""
function heat_data(network::Union{QNetwork, QuNet.TemporalGraph},
num_trials::Int64, num_pairs::Int64; max_paths=3, src_layer::Int64=-1,
dst_layer::Int64=-1, edge_perc_rate=0.0)

# e,f coords for all end users over all num_trials
coord_list = []

for i in 1:num_trials
net = deepcopy(network)

# Generate random communication pairs
if typeof(network) == TemporalGraph
user_pairs = make_user_pairs(network, num_pairs, src_layer=src_layer, dst_layer=dst_layer)
# Add asynchronus nodes to the network copy
add_async_nodes!(net, user_pairs)
else
user_pairs = make_user_pairs(network, num_pairs)
end

# Percolate edges
# WARNING: Edge percolation will not be consistant between temporal layers
# if typeof(net) = QuNet.TemporalGraph
if edge_perc_rate != 0.0
@assert 0 <= edge_perc_rate <= 1.0 "edge_perc_rate out of bounds"
net = QuNet.percolate_edges(net, edge_perc_rate)
refresh_graph!(net)
end

# Get data from greedy_multi_path
dummy, routing_costs, pathuse_count = QuNet.greedy_multi_path!(net, purify, user_pairs, max_paths)

# Filter out entries where no paths were found and costs are not well defined
filter!(x->x!=nothing, routing_costs)

# Put end-user costs into tuples (e, f)
for usercost in routing_costs
coord = Vector{Float64}()
push!(coord, usercost["loss"])
push!(coord, usercost["Z"])
push!(coord_list, coord)
end
end
return coord_list
end
53 changes: 7 additions & 46 deletions src/Plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,10 @@ function plot_network(graph::AbstractGraph, user_paths, locs_x, locs_y)
end


# function plot_network(tempnet::QuNet.TemporalGraph, user_paths)
# # Get an instance of the graph from the tempgraph and plot the thing
# graph = tempnet.graph["Z"]
#
# # Visualisation coordinates specified by temporal graph
# locs_x = tempnet.locs_x
# locs_y = tempnet.locs_y
#
# # Makeup bag
# colour_pal = [colorant"lightgrey", colorant"orange", colorant"lightslateblue", colorant"green"]
# # nodecolor = ["blue" for i in 1:nv(graph)]
#
# # Presumably user_paths will have paths directed from async node to async node.
# # Try removing these so that routing is no problem.
#
# # Write a list of all the used edges, and write a dict of who they're used by:
# used_edges = []
# used_by = Dict()
#
# this_user = 0
# for paths in user_paths
# this_user += 1
# for path in paths
# for edge in path
# push!(used_edges, edge)
# used_by[(edge.src,edge.dst)] = this_user
# end
# end
# end
#
# # Assign colors and edge properties
# colours = []
# widths = []
# for edge in edges(graph)
# if edge in used_edges
# push!(colours, used_by[(edge.src,edge.dst)] + 1)
# push!(widths, 5)
# else
# push!(colours, 1)
# push!(widths, 1)
# end
# end
#
# gplot(graph, locs_x, locs_y, edgestrokec=colour_pal[colours],
# edgelinewidth=widths, arrowlengthfrac=0.04)#, layout=spring_layout)
# end
# TODO: Heatmap
function cost_heatmap()
xs = [string("x", i) for i = 1:10]
ys = [string("y", i) for i = 1:4]
z = float((1:4) * reshape(1:10, 1, :))
heatmap(xs, ys, z, aspect_ratio = 1)
end
57 changes: 38 additions & 19 deletions test/sandbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@
Scratch file for testing with Juno Debugger
"""

using QuNet
using LightGraphs
using SimpleWeightedGraphs

# include("network-library/smalltemp.jl")
# Set up a Python backend before importing PyPlot
using PyCall
# Specify user GUI from options (:tk, :gtk3, :gtk, :qt5, :qt4, :qt, or :wx)
# pygui(:qt)
using PyPlot

function draw_network_routing()
timedepth = 3
grid_size = 10
# # Test PyPlot
# # use x = linspace(0,2*pi,1000) in Julia 0.6
# x = range(0; stop=2*pi, length=1000); y = sin.(3 * x + 4 * cos.(2 * x));
# plot(x, y, color="red", linewidth=2.0, linestyle="--")
# title("A sinusoidally modulated sinusoid")
# # Display figure with Julia backend
# display(gcf())
# # Save as a Pdf
# savefig("sinusoid_test.pdf")
# #show()

# Index offset for asynchronus nodes
off = grid_size^2 * timedepth
# Choose asynchronus endusers
userpairs = [(1 + off, 100 + off), (50 + off, 81 + off), (87 + off, 22 + off)]
# Try heatmap
# Install numpy
using PyCall
np = pyimport("numpy")
kde = pyimport("scipy.stats.kde")
data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
Tdata = transpose(data)
x = Tdata[1, :]
y = Tdata[2, :]

net = GridNetwork(grid_size, grid_size)
T = QuNet.TemporalGraph(net, timedepth, memory_prob=1.0)
QuNet.add_async_nodes!(T, userpairs)
T_copy = deepcopy(T)
user_paths, dum1, dum2 = QuNet.greedy_multi_path!(T_copy, QuNet.purify, userpairs)
QuNet.plot_network(T.graph["Z"], user_paths, T.locs_x, T.locs_y)
end
# # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
# k = kde.gaussian_kde(Tdata)
# xi, yi = np.mgrid[minimum(x):maximum(x):nbins*1j, minimum(y):maximum(y):nbins*1j]
# zi = k(np.vstack([xi.flatten(), yi.flatten()]))

draw_network_routing()
fig, axs = plt.subplots(ncols=2, nrows=1, figsize=(21, 5))
# Everything starts with a Scatterplot
axs[0].set_title('Scatterplot')
axs[0].plot(x, y, 'ko')

# # plot a density
# axes[1].set_title('Calculate Gaussian KDE')
# axes[1].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto', cmap=plt.cm.BuGn_r)
#
display(gcf())

0 comments on commit 4842329

Please sign in to comment.