Module: Score

Defined in:
lib/MARQ/score.rb

Constant Summary collapse

COLORS =
{
  :red =>   PNG::Color::Red,
  :green => PNG::Color::Green,
  :white => PNG::Color::White,
  :black => PNG::Color::Black,

}

Class Method Summary collapse

Class Method Details

.average(list) ⇒ Object



17
18
19
20
# File 'lib/MARQ/score.rb', line 17

def self.average(list)
  clean = list.compact
  clean.inject(0){|acc, e| acc += e}.to_f / clean.length
end

.combine(up, down) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/MARQ/score.rb', line 5

def self.combine(up, down)
  return down if up == 0
  return up if down == 0

  return up - down
  if (up > 0) == (down > 0)
    return 0
  else
    up - down
  end
end

.draw_hits(hits, total, filename = nil, options = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/MARQ/score.rb', line 316

def self.draw_hits(hits, total, filename = nil, options = {})

  size = options[:size] || total
  bg_color = options[:bg_color] || :white
  width = options[:width] || 20
  sections = options[:sections] || []

  size = [size, total].min

  hits = hits.collect{|h| h - 1}
  if size < total
    hits = hits.collect{|h| (h.to_f * size / total).to_i}
  end

  canvas = PNG::Canvas.new size, width, COLORS[bg_color]

  sections.each{|color, info|
    start = info[0]
    finish = info[1]
    (start..finish).each{|x|
      (0..width - 1).each{|y|
        canvas[x,y] = COLORS[color]
      }
    }
  }

  hits.each{|hit|
    canvas.line hit, 0, hit , width - 1, PNG::Color::Black
  }

  png = PNG.new canvas

  if filename
    png.save filename
  else
    png.to_blob
  end
end

.permutations(genes, total, times = 10000) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/MARQ/score.rb', line 286

def self.permutations(genes, total, times = 10000)
  scores = []
  times.times{
    positions = Array.new(genes){ (rand * total).to_i }
    scores << score(positions, total)[:score]
  }
  scores
end

.pvalues(scores, up, down, total, options = {}) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/MARQ/score.rb', line 295

def self.pvalues(scores, up, down, total, options = {})
  times = options[:times]|| 1000

  permutations_up = permutations(up, total, times)
  permutations_down = permutations(down, total, times )
  permutations = permutations_up.zip(permutations_down).collect{|p| combine(*p).abs }.sort

  scores.collect{|score| 
    num = permutations.count_smaller(score.abs)
    (times - num).to_f / times
  }
end

.scale_score1(positions, platform_entries) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/MARQ/score.rb', line 85

def self.scale_score1(positions, platform_entries)

  mean = platform_entries/2
  max_top = 0
  max_bottom = 0

  top_list = []
  bottom_list = []

  weights = positions.sort.collect{|position| 
    rel_pos = ((position - mean).abs.to_f / mean);
    0.3 * rel_pos + 0.7 * Math::exp(30*rel_pos)/Math::exp(30)
  }
  weights.unshift(0)
  total_weights = weights.inject(0){|v,acc| acc += v}
  weights.collect!{|v| v / total_weights}

  rel_qt = 0
  rel_qb = 0
  positions.sort.each_with_index{|position, idx|

    rel_qt += weights[idx + 1] 
    rel_qb += weights[idx]     
    rel_p   = position.to_f    / platform_entries
 
    top    = (rel_qt - rel_p);
    bottom = (rel_p - rel_qb);

    top_list << top
    bottom_list << bottom

    if (top > max_top) 
      max_top = top;
    end
    if (bottom > max_bottom) 
      max_bottom = bottom;
    end
  }

  p [top_list, bottom_list]
  if (max_top > max_bottom)
    return max_top;
  else
    return -max_bottom;
  end           
end

.score_area(positions, platform_entries, missing = 0) ⇒ Object



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
# File 'lib/MARQ/score.rb', line 22

def self.score_area(positions, platform_entries, missing = 0)
  return {:score => 0, :top => 0, :bottom => 0} if positions.nil? || positions.empty? || positions.compact.empty?

  clean_positions = positions.compact.sort

  total_tags      = positions.length + missing
  extra           = total_tags - clean_positions.length

  top = 0
  bottom = 0

  clean_positions.each_with_index{|p,i|
    rel_qt = (i + 1).to_f / total_tags
    rel_qb = ( i + extra ).to_f / total_tags
    rel_p  = p.to_f / platform_entries


    top    += rel_qt - rel_p if rel_qt > rel_p
    bottom += rel_p - rel_qb if rel_p > rel_qb
  }


  {
    :top => top,
    :bottom => bottom,
    :score => top > bottom ? top.to_f / total_tags : - bottom.to_f / total_tags, 
  }
end

.score_max(positions, platform_entries, missing = 0) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/MARQ/score.rb', line 246

def self.score_max(positions, platform_entries, missing = 0)
  return {:score => 0, :top => 0, :bottom => 0} if positions.nil? || positions.empty? || positions.compact.empty?

  clean_positions = positions.compact.sort

  extra           = missing + (positions.length - clean_positions.length)
  total_tags      = extra + clean_positions.length


  values = [0]

  clean_positions.each_with_index{|p,i| 
    
    up   = (i + 1).to_f / total_tags
    down = p.to_f       / platform_entries

    values << up - down
  }

  top    = values.max
  bottom = values.min + ((extra - 1).to_f/ total_tags)


  {
    :score => top.abs > bottom.abs ? top : bottom, 
  }
end

.score_max_norm(positions, platform_entries, missing = 0) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/MARQ/score.rb', line 51

def self.score_max_norm(positions, platform_entries, missing = 0)
  return {:score => 0, :top => 0, :bottom => 0} if positions.nil? || positions.empty? || positions.compact.empty?

  clean_positions = positions.compact.sort

  extra           = missing + (positions.length - clean_positions.length)
  total_tags      = extra + clean_positions.length

  mean = platform_entries / 2

  values_top = [0]
  values_bottom = [0]

  clean_positions.each_with_index{|p,i| 
    rel_qt = (i + 1).to_f / total_tags
    rel_qb = ( i + extra ).to_f / total_tags
    rel_p  = p.to_f / platform_entries


    values_top << (rel_qt - rel_p) * ((p - mean).abs.to_f / mean)**2
    values_bottom << (rel_p - rel_qb) * ((p - mean).abs.to_f / mean)**2
  }

  top    = values_top.max
  bottom = values_bottom.max 


  {
    :score => top > bottom ? top : -bottom, 
  }


end

.score_norm_fast(positions, platform_entries, missing = 0) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/MARQ/score.rb', line 233

def self.score_norm_fast(positions, platform_entries, missing = 0)
  return {:score => 0, :top => 0, :bottom => 0} if positions.nil? || positions.empty? || positions.compact.empty?

  clean_positions = positions.compact.sort

  extra           = missing + (positions.length - clean_positions.length)
  total_tags      = extra + clean_positions.length

  {
    :score => fast_norm_score(clean_positions, total_tags, extra, platform_entries)
  }
end

.score_scale_fast(positions, platform_entries, missing = 0) ⇒ Object Also known as: score



221
222
223
224
225
226
227
228
229
230
# File 'lib/MARQ/score.rb', line 221

def self.score_scale_fast(positions, platform_entries, missing=0)
  return {:score => 0, :top => 0, :bottom => 0} if positions.nil? || positions.empty? || positions.compact.empty?

  clean_positions = positions.compact.sort
  missing = missing + positions.length - clean_positions.length

  {
    :score => fast_score_scale(clean_positions,  platform_entries, missing)
  }
end

.score_up_down(up, down, total, missing_up = 0, missing_down = 0) ⇒ Object



279
280
281
282
283
284
# File 'lib/MARQ/score.rb', line 279

def self.score_up_down(up, down, total, missing_up = 0, missing_down = 0)
  up = score(up, total, missing_up)
  down = score(down, total, missing_down)

  {:up =>  up[:score], :down => down[:score], :score => combine(up[:score], down[:score])}
end