Getting started with the network diffusion tutorial in Julia

The setup.jl script that we include below does a few things to install packages that we will need including the MatrixNetworks.jl package that Huda Nassar has been building to make working with graphs easy in Julia (in a way that interoperates with matrix operations).

It also configures a few plotting packages PyPlot and Gadfly


In [5]:
include("setup.jl")


INFO: Nothing to be done
INFO: METADATA is out-of-date — you may not have the latest version of Gadfly
INFO: Use `Pkg.update()` to get the latest versions of your packages
INFO: Nothing to be done
INFO: METADATA is out-of-date — you may not have the latest version of PyPlot
INFO: Use `Pkg.update()` to get the latest versions of your packages
INFO: Cloning cache of Interact from git://github.com/JuliaLang/Interact.jl.git
INFO: Cloning cache of Reactive from git://github.com/JuliaLang/Reactive.jl.git
INFO: Installing Interact v0.2.1
INFO: Installing Reactive v0.2.4
INFO: Package database updated
INFO: METADATA is out-of-date — you may not have the latest version of Interact
INFO: Use `Pkg.update()` to get the latest versions of your packages
INFO: Cloning MatrixNetworks from https://github.com/nassarhuda/MatrixNetworks.jl.git
LoadError: LoadError: MatrixNetworks already exists
while loading /Users/dgleich/Dropbox/lectures/diffusion-tutorial/setup.jl, in expression starting on line 4
while loading In[5], in expression starting on line 1

Now we are going to test the packages themselves to make sure things are working. If this step fails, many of the other ideas will not work either.


In [3]:
using MatrixNetworks
using PyPlot
using Gadfly
using Compose
using Interact


LoadError: ArgumentError: Interact not found in path
while loading In[3], in expression starting on line 5

 in require at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib

In [4]:
PyPlot.plot(randn(50))


Out[4]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x3125c5210>

In [5]:
# E.g.
Gadfly.plot(x=collect(1:100), y=sort(rand(100)))


Out[5]:
x -150 -100 -50 0 50 100 150 200 250 -100 -95 -90 -85 -80 -75 -70 -65 -60 -55 -50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 -100 0 100 200 -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 y

In [9]:
@manipulate for phi=0:pi/16:4*pi
    Gadfly.plot(x -> sin(x + phi), 0, 25)
end


Out[9]:
x -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 -25 0 25 50 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 f(x)

In [14]:
matrix_network_datasets()
(A,xy) = load_matrix_network_metadata("bfs_example")


Out[14]:
(
8x8 sparse matrix with 18 Int64 entries:
	[2, 1]  =  1
	[5, 1]  =  1
	[1, 2]  =  1
	[6, 2]  =  1
	[4, 3]  =  1
	[6, 3]  =  1
	[7, 3]  =  1
	[3, 4]  =  1
	[8, 4]  =  1
	[1, 5]  =  1
	[2, 6]  =  1
	[3, 6]  =  1
	[7, 6]  =  1
	[3, 7]  =  1
	[6, 7]  =  1
	[8, 7]  =  1
	[4, 8]  =  1
	[7, 8]  =  1,

8x2 Array{Float64,2}:
 0.0  1.0
 1.0  1.0
 2.0  1.0
 3.0  1.0
 0.0  0.0
 1.0  0.0
 2.0  0.0
 3.0  0.0,

1x1 Array{Any,2}:
 "rstuvwxy")

In [120]:
function graphplot(A,xy)
    (ei,ej) = findnz(triu(A))
    lx = [xy[ei,1]';xy[ej,1]';NaN*ones(1,length(ei))]
    ly = [xy[ei,2]';xy[ej,2]';NaN*ones(1,length(ei))]
    lines = PyPlot.plot(lx,ly)
    PyPlot.axis("off")
    PyPlot.setp(lines,alpha=0.5,color=[0.,0.,0.],zorder=0)
end
graphplot(A,xy)
PyPlot.scatter(xy[:,1],xy[:,2],25*rand(size(A,1)),edgecolors="none",zorder=2)


Out[120]:
PyObject <matplotlib.collections.PathCollection object at 0x7f889525bd90>

In [127]:
(A,xy) = load_matrix_network_metadata("airports")
graphplot(A,xy)
PyPlot.scatter(xy[:,1],xy[:,2],25*rand(size(A,1)),edgecolors="none",zorder=2)


Out[127]:
PyObject <matplotlib.collections.PathCollection object at 0x7f8846060d50>

In [82]:
(ei,ej) = findnz(triu(A))
glines = [ [(xy[ei[i],1],xy[ei[i],2]), (xy[ej[i],1],xy[ej[i],2])] 
                for i=1:length(ei)]'
#glines = Vector{Vector{Tuple{Float64,Float64}}}()

@show glines
@show typeof(glines)
compose(context(), line(glines), stroke("black"))
#compose(context(), line([[(0,0), (1,1)]; [(0,1), (1,0)]]), stroke("black"))


glines = Any[[(0.0,1.0) (1.0,1.0)] [(2.0,1.0) (3.0,1.0)] [(0.0,1.0) (0.0,0.0)] [(1.0,1.0) (1.0,0.0)] [(2.0,1.0) (1.0,0.0)] [(2.0,1.0) (2.0,0.0)] [(1.0,0.0) (2.0,0.0)] [(3.0,1.0) (3.0,0.0)] [(2.0,0.0) (3.0,0.0)]]
Out[82]:
typeof(glines) = Array{Any,2}

In [123]:
using Gadfly,Reactive,Interact,PyPlot
myfig = figure()
function myplot(data)
    withfig(myfig) do
        PyPlot.plot(data[1], data[2])
        axis([0,1,-.3,.3])
    end
end
x = collect(linspace(0,1,100))
myinput=Input((x,0*x))
lift(myplot, myinput)


Out[123]:

In [125]:
x = collect(linspace(0,1,100))
for t = -1:.1:1
    y = t * x .*(1-x)
    push!(myinput,(x, y))
    sleep(0.25)
end



In [99]:
using Gadfly,Reactive,Interact
function myplot2(data)
    Gadfly.plot(x=data[1], y=data[2])
end
x = collect(linspace(0,1,100))
myinput=Input((x,0*x))
lift(myplot2, myinput)


Out[99]:
x -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 0 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 y

In [100]:
x = collect(linspace(0,1,100))
for t = -1:.1:1
    y = t * x .*(1-x)
    push!(myinput,(x, y))
end