Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 11, 2020 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/acfe06745aa27208c3efd512f1151351 to your computer and use it in GitHub Desktop.
Save vznvzn/acfe06745aa27208c3efd512f1151351 to your computer and use it in GitHub Desktop.
def dense(w, d)
w2 = w - 1
a = (0...w2).to_a
s = '0' * w2
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' }
return ('1' + s)
end
def len(ns, p)
l = ns.split(p)
l = [] if (l.nil?)
l.shift if (l[0] == '')
return l.map { |x| x.length }
end
def len1(ns)
return len(ns, /0+/)
end
def len0(ns)
return len(ns, /1+/)
end
def len01(ns)
return len1(ns), len0(ns)
end
def hist(l)
a = {}
l.each { |x| a[x] = a.fetch(x, 0) + 1 }
return a
end
def hdiff(h1, h2)
d = 0
(h1.keys | h2.keys).each { |x| d += (h1.fetch(x, 0) - h2.fetch(x, 0)).abs }
return d
end
def h2(ns)
l1, l0 = len01(ns)
return hist(l0), hist(l1)
end
def hdiff2(h2a, h2b)
return hdiff(h2a[0], h2b[0]) + hdiff(h2a[1], h2b[1])
end
def hdiff1(ns)
h2a = h2(ns)
t = 0
(c = 12).times \
{
ns2 = dense(ns.length, 0.5)
h2b = h2(ns2)
d = hdiff2(h2a, h2b)
t += d
}
return t.to_f / c
end
def ina()
l = (f = File.open('out10.txt')).readlines
f.close
k = l.shift.split
return l.map { |x| Hash[[k, x.split.map { |x| Kernel.eval(x) }].transpose] }
end
def outd(f, l)
f.puts('$dat << eof')
k = l[0].keys
f.puts(k.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.puts('eof')
return k
end
def outa(f, l, a = {}, t = '')
k = outd(f, l)
f.puts("set colors classic; set key top left opaque; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
k, ct = [k - ['t'], "(column('t')):"] if (k.member?('t'))
k.each \
{
|x|
next if (a.member?(x) && a[x].nil?)
opt = a.fetch(x, '')
opt += ' lw 2 ' if (!opt.include?('lw'))
opt += ' with line ' if (!opt.include?('with') && !opt.include?('pt'))
f.puts("'$dat' using #{ct}(column('#{x}')) #{opt} title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, a = {}, fn = nil)
outa(f = File.open(fn.nil? ? 'gnuplot.cmd' : fn, 'w'), l, a)
f.close
end
def axes2(x2)
return x2.inject({}) { |h, x| h.merge({x => 'axes x1y2' }) }
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
l = ina()
l.sort_by! { |x| x['nw'] }
l.each \
{
|a|
ns = a['ns'].to_s
hd1 = hdiff1(ns)
hd2 = hdiff1(dense(ns.length, 0.5))
a['hd1'] = hd1
a['hd2'] = hd2
hdr2 = (hd2 == 0) ? 1 : hd1 / hd2
a['hdr2'] = hdr2
a['d'] = d(ns)
a['e'] = e(ns)
a.delete('t')
}
#l.sort_by! { |x| x['hdr2'] }
outafn(l, {'ns' => nil}.merge(axes2(['cmr', 'cmcg', 'hdr', 'hdr2', 'd', 'e'])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment