Skip to content

Commit

Permalink
Testing relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
avishek-sen-gupta committed Oct 17, 2011
1 parent ba8ff10 commit 36457dd
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 7 deletions.
1 change: 0 additions & 1 deletion BoxPlot.rb
Expand Up @@ -16,7 +16,6 @@ def setup
smooth
background(0,0,0)
color_mode(HSB, 1.0)
metric = lambda {|r| r[:post_total]}

@responses = Response.find(:all)
plot(@screen_height) {|r| r[:post_total]}
Expand Down
131 changes: 131 additions & 0 deletions BoxPlotImprovement.rb
@@ -0,0 +1,131 @@
require 'rubygems'
Gem.clear_paths
ENV['GEM_HOME'] = '/home/avishek/jruby/jruby-1.6.4/lib/ruby/gems/1.8'
ENV['GEM_PATH'] = '/home/avishek/jruby/jruby-1.6.4/lib/ruby/gems/1.8'

require 'schema'
require 'basis_processing'

class MySketch < Processing::App
app = self
def setup
@screen_height = 900
@width = width
@height = height
no_loop
smooth
background(0,0,0)
color_mode(HSB, 1.0)

@responses = Response.find(:all)
plot(@screen_height/2) {|r| r.improvement}
end

def plot(y, &metric)
bins = {}
@responses.each do |r|
bins[r[:language]] = [] if bins[r[:language]].nil?
bins[r[:language]] << r
end

bins["ALL"] = @responses

bins.each_pair do |k,v|
begin
bins[k] = box(k, v, metric)
rescue => e
puts "Warning: Population of #{k} is statistically insignificant"
bins[k] = nil
end
end

@x_unit_vector = {:x => 1.0, :y => 0.0}
@y_unit_vector = {:x => 0.0, :y => 1.0}
@screen_transform = Transform.new({:x => 4.0, :y => -6.0}, {:x => 100, :y => y})
x_range = ContinuousRange.new({:minimum => 0.0, :maximum => 500.0})
y_range = ContinuousRange.new({:minimum => -60.0, :maximum => 60.0})
@c = CoordinateSystem.new(Axis.new(@x_unit_vector,x_range), Axis.new(@y_unit_vector,y_range), self, [[1,0],[0,1]])
@screen = Screen.new(@screen_transform, self, @c)
position = 10
box_width = 20
whisker_width = 10
f = createFont("Georgia", 24, true)
text_font(f,16)

stroke_weight(1)
stroke(0.7,0,0)
@screen.draw_axes(10, 10, {:x => ->(p){''}, :y => ->(p){p.to_i}})

stroke(0.7,0.3,1)
no_fill
bins.keys.sort.each do |k|
@screen.at({:x => position, :y => -5}) {|o,m,s| text(k, m[:x] - 15, m[:y] + 30)}
v = bins[k]
if (v.nil?)
position += 25
next
end
@screen.at(v) do |o,s|
s.in_basis do
stroke_weight(0.2)
rect(position - box_width/2, o[:q1], box_width, o[:q2] - o[:q1])
rect(position - box_width/2, o[:q2], box_width, o[:q3] - o[:q2])
line(position, o[:q3], position, o[:maximum])
line(position, o[:q1], position, o[:minimum])
line(position - whisker_width/2, o[:minimum], position + whisker_width/2, o[:minimum])
line(position - whisker_width/2, o[:maximum], position + whisker_width/2, o[:maximum])
end
end

@screen.at({:x => position, :y => v[:minimum]}) {|o,m,s| text(o[:y], m[:x] + 5, m[:y] + 14)}
@screen.at({:x => position, :y => v[:maximum]}) {|o,m,s| text(o[:y], m[:x] + 5, m[:y] - 14)}
@screen.at({:x => position, :y => v[:q1]}) {|o,m,s| text(o[:y], m[:x] + 5, m[:y] + 14)}
@screen.at({:x => position, :y => v[:q2]}) {|o,m,s| text(o[:y], m[:x] + 5, m[:y] + 14)}
@screen.at({:x => position, :y => v[:q3]}) {|o,m,s| text(o[:y], m[:x] + 5, m[:y] - 14)}

position += 25
end
end

def box(k, responses, metric)
cumulative_distribution = {}
responses.each do |r|
key = metric.call(r)
cumulative_distribution[key] = cumulative_distribution[key].nil? ? 1 : cumulative_distribution[key] + 1
end

maximum = cumulative_distribution.keys.max
minimum = cumulative_distribution.keys.min

keys = cumulative_distribution.keys.sort

keys.each_index do |index|
cumulative_distribution[keys[index]] = cumulative_distribution[keys[index]] + cumulative_distribution[keys[index - 1]] if index > 0
end

cumulative_distribution.each_pair do |k,v|
cumulative_distribution[k] = v/responses.count.to_f
end

q1 = quartile(1).call(cumulative_distribution)
q2 = quartile(2).call(cumulative_distribution)
q3 = quartile(3).call(cumulative_distribution)
maximum = keys.max
minimum = keys.min

box = {:minimum => minimum, :maximum => maximum, :q1 => q1, :q2 => q2, :q3 => q3}
end

def quartile(n)
lambda {|inputs| nth_quartile(n, inputs)}
end

def nth_quartile(n, cdf)
cdf.keys[cdf.keys.index {|k| (cdf[k] - (n/4.0)).abs < 0.04 }]
end
end

h = 1000
w = 1800
MySketch.new(:title => "My Sketch", :width => w, :height => h)

14 changes: 8 additions & 6 deletions ScatterPlot.rb
Expand Up @@ -13,7 +13,8 @@ class MySketch < Processing::App
def setup
@highlight_block = ->(o,m,s) do
rect_mode(CENTER)
rect(m[:x], m[:y], 10, 10)
rect(m[:x], m[:y], 8, 8)
text("(#{o[:x]}, #{o[:y]}) -> #{o[:value]}", m[:x] + 5, m[:y] + 5)
end
@screen_height = 900
@width = width
Expand All @@ -32,13 +33,13 @@ def setup

# @c = CoordinateSystem.standard({:minimum => 0.0, :maximum => 3.0}, {:minimum => 0.0, :maximum => 3.0}, self)
@c = CoordinateSystem.standard({:minimum => 0, :maximum => 60}, {:minimum => -60, :maximum => 60}, self)
@screen_transform = Transform.new({:x => 5.0, :y => -5.0}, {:x => 500, :y => @screen_height/2})
@screen_transform = Transform.new({:x => 8.0, :y => -8.0}, {:x => 500, :y => @screen_height/2 + 50})
@screen = Screen.new(@screen_transform, self, @c)

stroke(0.1,0.5,1)
fill(0.1,0.5,1)

plot_distribution(responses, ->(r) {r[:pre_total]}, ->(r) {r.improvement})
plot_distribution(responses, ->(r) {Math.log(r[:pre_total])}, ->(r) {r.improvement})
@screen.draw_axes(5, 5, :gridlines => false)
end

Expand All @@ -55,10 +56,11 @@ def plot_distribution(responses, x_metric, y_metric)
array.each_key do |r|
array[r].each_key do |c|
b = 500 * array[r][c]/28000.0
stroke(0.1,0.5,b)
stroke(0.5,0.5,0.07)
fill(0.1,0.5,b)
@screen.plot({:x => c, :y => r}, :track => true) do |o,m,s|
rect(m[:x], m[:y], 2, 2)
@screen.plot({:x => c, :y => r, :value => array[r][c]}) do |o,m,s|
rect_mode(CENTER)
rect(m[:x], m[:y], 8, 8)
end
end
end
Expand Down

0 comments on commit 36457dd

Please sign in to comment.