Skip to content

Instantly share code, notes, and snippets.

View mortenjohs's full-sized avatar

Morten Johannes Ervik mortenjohs

  • International Agency for Research on Cancer
  • Lyon, France
View GitHub Profile
class Point
def initialize(coords)
@coords = coords
end
def distance_to(point)
Math.sqrt( @coords.keys.inject(0) { |sum, key| sum + (@coords[key] - point.send(key))**2 } )
end
def to_s
"#{@coords.values.join(", ")}"
end
class Point2D < Point
def initialize(x, y = nil)
y.nil? ? super(x) : super({x: x, y: y})
end
end
class Cluster
attr_reader :center
def initialize(center)
@center = center
@points = []
@moved = true
end
def add_point(point)
@points << point
end
def k_means (points, k = 5, delta = 0.001, plot_on = false)
k = points.length if points.length < k
clusters = []
k.times { clusters << Cluster.new(points.sample) }
iterations = 0
while (clusters.any?(&:moved?))
def cluster_plot_2D(clusters, seq = 1, title = "Random noise", get_x = 'x', get_y = 'y')
if defined? Gnuplot
Gnuplot.open do |gp|
Gnuplot::Plot.new(gp) do |plot|
plot.terminal "png"
plot.output File.expand_path("../outfiles/clusters-#{seq}.png", __FILE__)
plot.title title
# Plot each cluster's points
clusters.each do |cluster|
# parameters
k = 7
number_of_points = 10000
delta = 0.001
plot_on = false
require './k-means.rb'
points = []
number_of_points.times do
@mortenjohs
mortenjohs / index.html
Last active December 12, 2015 07:49
D3 Morphing Map Projections
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<style>
.background {
fill: #a4bac7;
}
@mortenjohs
mortenjohs / index.html
Last active December 12, 2015 08:18
D3 Map Projections Morphing
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script>
<style>
.background {
fill: none;
pointer-events: all;
}
@mortenjohs
mortenjohs / root-certs-rvm.sh
Created February 26, 2013 16:14
Ruby, rvm, and root certificates
mkdir $rvm_path/usr/ssl
cd $rvm_path/usr/ssl
curl -O http://curl.haxx.se/ca/cacert.pem # download certificates
mv cacert.pem cert.pem # move them in place
touch ~/.bashrc # create .bashrc if it does not exist
echo 'export SSL_CERT_FILE=~/.rvm/usr/ssl/cert.pem' >> ~/.bashrc # add key to environment
source ~/.bashrc # or close your consoles
@mortenjohs
mortenjohs / videocalc.js
Created October 20, 2013 21:03
Add overall lesson timing to video listings on Coursera
function pad2(number){
if (number<10) {
return "0" + parseInt(number);
} else {
return parseInt(number);
}
}
var sectionList = $(".course-item-list-section-list"),
headerList = $(".course-item-list-header"),