Skip to content

Instantly share code, notes, and snippets.

@straypacket
Last active December 26, 2015 04:49
Show Gist options
  • Save straypacket/7096586 to your computer and use it in GitHub Desktop.
Save straypacket/7096586 to your computer and use it in GitHub Desktop.
Analysis of passers-by vs stable clients, over wifi mac monitoring
# Do this in a rails console @ footprint-tracker
# Weekend
we = [12, 13, 14, 19, 20]
#Workdays
wd = [11, 15, 16, 17, 18, 21, 22, 23]
#2D Matrices for weekend and worddays, Weekdays x Requests_per_distinct_mac
we_a = []
wd_a = []
we.each do |d|
we_a << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| Hierclust::Point.new(d,dd.last) }
end
we_a = we_a.flatten(1)
we_a.delete_if {|x| x == nil}
wd.each do |d|
wd_a << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| Hierclust::Point.new(d,dd.last) }
#wd_a << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| Hierclust::Point.new(20,dd.last) if dd.last < 100 }
end
wd_a = wd_a.flatten(1)
wd_a.delete_if {|x| x == nil}
#We'll cluster with a separation of 50 (stop the clustering process once all the items are at least these units apart)
#and a resolution of 10 (points that are within this distance from each other will not be hierarchically clustered)
clusterer = Hierclust::Clusterer.new(wd_a,150,10)
puts clusterer.clusters
# Viz
print %Q{<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="1500" height="600" version="1.1" xmlns="http://www.w3.org/2000/svg">
<meta charset="utf-8">
<style>
.axis text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 1500 - margin.left - margin.right,
height = 20 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0,width])
.range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(25)
.orient("bottom");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
</script>
}
@color = ['red', 'green', 'blue', 'yellow', 'brown', 'orange', 'grey', 'pink']
def plot(cluster,level)
if cluster.kind_of? Hierclust::Cluster
print %Q{
<circle level="#{level}" nitems="#{cluster.items.size}" cy="#{cluster.x*10}" cx="#{cluster.y}" r="#{cluster.radius}" fill="#{@color[level]}" stroke="black" stroke-width="1"/>
}
cluster.items.each {|item| plot(item,level+1)};nil
else
# If we alpha tthe fill-oppacity we will visually know where to discard
print %Q{
<circle cy="#{cluster.x*10}" cx="#{cluster.y}" r="2" fill="red" stroke="none" fill-opacity="0.01"/>
}
end
end
clusterer.clusters.each {|cluster| plot(cluster,0)};nil
}
# Do this in a rails console @ footprint-tracker
# Weekend
we = [12, 13, 14, 19, 20]
#Workdays
wd = [11, 15, 16, 17, 18, 21, 22, 23]
#2D Matrices for weekend and worddays, Weekdays x Requests_per_distinct_mac
we_ak = []
we.each do |d|
we_ak << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| [d,dd.last] }
end
we_ak = we_ak.flatten(1)
wd_ak = []
wd.each do |d|
wd_ak << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| [d,dd.last] }
end
wd_ak = wd_ak.flatten(1)
# We'll consider the search of two clusters, in and out of a premise
labels, error, nfound = Cluster.kcluster(wd_ak, :clusters => 2)
# We'll now map each point to a cluster
labels.length.times { |i| wd_ak[i] << labels[i] }
# Viz
k_colors = ['red', 'blue']
print %Q{<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="1500" height="600" version="1.1" xmlns="http://www.w3.org/2000/svg">
<meta charset="utf-8">
<style>
.axis text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 1500 - margin.left - margin.right,
height = 20 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0,width])
.range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(25)
.orient("bottom");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
</script>
}
wd_ak.each do |p|
print %Q{
<circle cy="#{p[0]*10}" cx="#{p[1]}" r="2" fill="#{k_colors[p[2]]}" stroke="none" fill-opacity="0.5"/>
}
end;nil
}
# Do this in a rails console @ footprint-tracker
# Weekend
we = [12, 13, 14, 19, 20]
#Workdays
wd = [11, 15, 16, 17, 18, 21, 22, 23]
#2D Matrices for weekend and worddays, Weekdays x Requests_per_distinct_mac
we_ak = []
we.each do |d|
we_ak << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| [d,dd.last] }
end
we_ak = we_ak.flatten(1)
wd_ak = []
wd.each do |d|
wd_ak << ArchivedWifiRequest.found_devices_at("2013-10-#{d}", 0).map{ |dd| [d,dd.last] }
end
wd_ak = wd_ak.flatten(1)
# We'll cut the tree at two levels
tree = Cluster.treecluster(wd_ak)
labels = tree.cut(2)
# We'll now map each point to a cluster
labels.length.times { |i| wd_ak[i] << labels[i] }
# Viz
k_colors = ['red', 'blue']
print %Q{<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="1500" height="600" version="1.1" xmlns="http://www.w3.org/2000/svg">
<meta charset="utf-8">
<style>
.axis text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 1500 - margin.left - margin.right,
height = 20 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0,width])
.range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(25)
.orient("bottom");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
</script>
}
wd_ak.each do |p|
print %Q{
<circle cy="#{p[0]*10}" cx="#{p[1]}" r="2" fill="#{k_colors[p[2]]}" stroke="none" fill-opacity="0.5"/>
}
end;nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment