swak 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/swak.rb +2 -154
- data/lib/swak/table.rb +96 -0
- data/lib/swak/toplevel.rb +153 -0
- metadata +4 -2
data/lib/swak.rb
CHANGED
@@ -1,157 +1,5 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'swak', 'toplevel')
|
1
2
|
require File.join(File.dirname(__FILE__), 'swak', 'logger')
|
2
3
|
require File.join(File.dirname(__FILE__), 'swak', 'interval')
|
3
4
|
require File.join(File.dirname(__FILE__), 'swak', 'crypt')
|
4
|
-
|
5
|
-
class String
|
6
|
-
AnsiMap = {"k" => 0,
|
7
|
-
"r" => 1,
|
8
|
-
"g" => 2,
|
9
|
-
"y" => 3,
|
10
|
-
"b" => 4,
|
11
|
-
"m" => 5,
|
12
|
-
"c" => 6,
|
13
|
-
"w" => 7,
|
14
|
-
"d" => 9}
|
15
|
-
|
16
|
-
def color(color, fg=true)
|
17
|
-
raise "Illegal color" unless AnsiMap.include?(color)
|
18
|
-
color_int = AnsiMap[color]
|
19
|
-
return "[#{fg ? 3 : 4}#{color_int}m#{self}[#{fg ? 3 : 4}9m"
|
20
|
-
end
|
21
|
-
|
22
|
-
# Wraps long string by lines, using whitespace boundaries
|
23
|
-
def wrap(max_chars_per_line=80)
|
24
|
-
return gsub(/\n/," ").scan(/\S.{0,#{max_chars_per_line-2}}\S(?=\s|$)|\S+/).join("\n")
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
#######################################################
|
30
|
-
#######################################################
|
31
|
-
|
32
|
-
class Object
|
33
|
-
def is_i?
|
34
|
-
true if Integer(self) rescue false
|
35
|
-
end
|
36
|
-
|
37
|
-
def is_f?
|
38
|
-
true if Float(self) rescue false
|
39
|
-
end
|
40
|
-
|
41
|
-
def to_i_strict
|
42
|
-
if is_i?
|
43
|
-
return to_i
|
44
|
-
else
|
45
|
-
raise "String '#{self}' cannot be converted to an int"
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
def to_f_strict
|
51
|
-
if is_f?
|
52
|
-
return to_f
|
53
|
-
else
|
54
|
-
raise "String '#{self}' cannot be converted to a float"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class NilClass
|
60
|
-
undef_method(:is_i?)
|
61
|
-
undef_method(:is_f?)
|
62
|
-
undef_method(:to_i_strict)
|
63
|
-
undef_method(:to_f_strict)
|
64
|
-
undef_method(:to_f)
|
65
|
-
undef_method(:to_i)
|
66
|
-
end
|
67
|
-
|
68
|
-
class Float
|
69
|
-
def is_i?
|
70
|
-
return self == self.to_i
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
|
75
|
-
#######################################################
|
76
|
-
#######################################################
|
77
|
-
|
78
|
-
|
79
|
-
class Array
|
80
|
-
|
81
|
-
def hist
|
82
|
-
h = {}
|
83
|
-
for item in self
|
84
|
-
h[item] ||= 0
|
85
|
-
h[item] += 1
|
86
|
-
end
|
87
|
-
return h
|
88
|
-
end
|
89
|
-
|
90
|
-
def shuffle!
|
91
|
-
sort_by {rand}
|
92
|
-
return self
|
93
|
-
end
|
94
|
-
|
95
|
-
def shuffle
|
96
|
-
return dup().sort_by {rand}
|
97
|
-
end
|
98
|
-
|
99
|
-
def sum
|
100
|
-
total = 0
|
101
|
-
for item in self
|
102
|
-
total += item
|
103
|
-
end
|
104
|
-
return total
|
105
|
-
end
|
106
|
-
|
107
|
-
def avg
|
108
|
-
return sum.to_f / self.length
|
109
|
-
end
|
110
|
-
|
111
|
-
# Sample variance
|
112
|
-
def variance
|
113
|
-
average = avg()
|
114
|
-
variance = 0
|
115
|
-
for item in self
|
116
|
-
diff = item - average
|
117
|
-
variance += diff * diff
|
118
|
-
end
|
119
|
-
return variance / (self.length - 1)
|
120
|
-
end
|
121
|
-
|
122
|
-
def stddev
|
123
|
-
return Math.sqrt(self.variance())
|
124
|
-
end
|
125
|
-
|
126
|
-
def mean
|
127
|
-
return self.avg
|
128
|
-
end
|
129
|
-
|
130
|
-
def argmax
|
131
|
-
max_i = 0
|
132
|
-
max_val = self[max_i]
|
133
|
-
|
134
|
-
self.each_with_index do |x, i|
|
135
|
-
if x > max_val
|
136
|
-
max_val = x
|
137
|
-
max_i = i
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
return max_i
|
142
|
-
end
|
143
|
-
|
144
|
-
def argmin
|
145
|
-
min_i = 0
|
146
|
-
min_val = self[min_i]
|
147
|
-
|
148
|
-
self.each_with_index do |x, i|
|
149
|
-
if x < min_val
|
150
|
-
min_val = x
|
151
|
-
min_i = i
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
return min_i
|
156
|
-
end
|
157
|
-
end
|
5
|
+
require File.join(File.dirname(__FILE__), 'swak', 'table')
|
data/lib/swak/table.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module Swak
|
2
|
+
module Table
|
3
|
+
def Table.read(io_or_fn, opts={:has_header => false, :type => String, :delim => "\t"})
|
4
|
+
if io_or_fn.is_a?(String)
|
5
|
+
io = File.new(io_or_fn)
|
6
|
+
else
|
7
|
+
io = io_or_fn
|
8
|
+
end
|
9
|
+
if opts[:has_header]
|
10
|
+
header = io.gets
|
11
|
+
opts[:header] = header.chomp
|
12
|
+
end
|
13
|
+
|
14
|
+
table = []
|
15
|
+
delim = opts[:delim] || "\t"
|
16
|
+
|
17
|
+
if (opts[:type] == Integer)
|
18
|
+
for line in io
|
19
|
+
line.chomp!
|
20
|
+
f = line.split(delim)
|
21
|
+
table << f.map{|x| x.to_i_strict}
|
22
|
+
end
|
23
|
+
elsif (opts[:type] == Float)
|
24
|
+
for line in io
|
25
|
+
line.chomp!
|
26
|
+
f = line.chomp.split(delim)
|
27
|
+
table << f.map{|x| x.to_f_strict}
|
28
|
+
end
|
29
|
+
else # Assume String
|
30
|
+
for line in io
|
31
|
+
line.chomp!
|
32
|
+
table << line.split(delim)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
return table
|
37
|
+
end
|
38
|
+
|
39
|
+
def Table.write(table, io_or_fn, opts={:header => nil, :fmt => nil, :delim => "\t"})
|
40
|
+
raise "Illegal fmt in Swak::Table.write" if !opts[:fmt].nil? && !opts[:fmt].is_a?(String)
|
41
|
+
|
42
|
+
delim = opts[:delim] || "\t"
|
43
|
+
|
44
|
+
header = opts[:header]
|
45
|
+
header = header.join(delim) if header.is_a?(Array) && header.size > 0
|
46
|
+
|
47
|
+
if io_or_fn.is_a?(String)
|
48
|
+
io = File.new(io_or_fn, "w")
|
49
|
+
else
|
50
|
+
io = io_or_fn
|
51
|
+
end
|
52
|
+
|
53
|
+
io.puts header if !header.nil?
|
54
|
+
|
55
|
+
if opts[:fmt]
|
56
|
+
fmt = opts[:fmt]
|
57
|
+
for row in table
|
58
|
+
io.puts row.map{|str| fmt % str }.join(delim)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
for row in table
|
62
|
+
io.puts row.join(delim)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
io.close if io_or_fn.is_a?(String)
|
67
|
+
end
|
68
|
+
|
69
|
+
def Table.transpose(table)
|
70
|
+
raise "Error in Swak::Table.transpose(): Cannot transpose nil table" if table.nil?
|
71
|
+
return [] if table.size == 0
|
72
|
+
|
73
|
+
num_rows = table.size
|
74
|
+
num_cols = table[0].size
|
75
|
+
|
76
|
+
out_mat = Array.new(num_cols)
|
77
|
+
|
78
|
+
for row in table
|
79
|
+
raise "Error in Swak::Table.transpose(): Doesn't support jagged table" if row.size != num_cols
|
80
|
+
end
|
81
|
+
|
82
|
+
c = 0
|
83
|
+
while c < num_cols
|
84
|
+
out_mat[c] = Array.new(num_rows)
|
85
|
+
r = 0
|
86
|
+
while r < num_rows
|
87
|
+
out_mat[c][r] = table[r][c]
|
88
|
+
r += 1
|
89
|
+
end
|
90
|
+
c += 1
|
91
|
+
end
|
92
|
+
|
93
|
+
return out_mat
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
class String
|
2
|
+
AnsiMap = {"k" => 0,
|
3
|
+
"r" => 1,
|
4
|
+
"g" => 2,
|
5
|
+
"y" => 3,
|
6
|
+
"b" => 4,
|
7
|
+
"m" => 5,
|
8
|
+
"c" => 6,
|
9
|
+
"w" => 7,
|
10
|
+
"d" => 9}
|
11
|
+
|
12
|
+
def color(color, fg=true)
|
13
|
+
raise "Illegal color" unless AnsiMap.include?(color)
|
14
|
+
color_int = AnsiMap[color]
|
15
|
+
return "[#{fg ? 3 : 4}#{color_int}m#{self}[#{fg ? 3 : 4}9m"
|
16
|
+
end
|
17
|
+
|
18
|
+
# Wraps long string by lines, using whitespace boundaries
|
19
|
+
def wrap(max_chars_per_line=80)
|
20
|
+
return gsub(/\n/," ").scan(/\S.{0,#{max_chars_per_line-2}}\S(?=\s|$)|\S+/).join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
#######################################################
|
26
|
+
#######################################################
|
27
|
+
|
28
|
+
class Object
|
29
|
+
def is_i?
|
30
|
+
true if Integer(self) rescue false
|
31
|
+
end
|
32
|
+
|
33
|
+
def is_f?
|
34
|
+
true if Float(self) rescue false
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_i_strict
|
38
|
+
if is_i?
|
39
|
+
return to_i
|
40
|
+
else
|
41
|
+
raise "String '#{self}' cannot be converted to an int"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_f_strict
|
47
|
+
if is_f?
|
48
|
+
return to_f
|
49
|
+
else
|
50
|
+
raise "String '#{self}' cannot be converted to a float"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class NilClass
|
56
|
+
undef_method(:is_i?)
|
57
|
+
undef_method(:is_f?)
|
58
|
+
undef_method(:to_i_strict)
|
59
|
+
undef_method(:to_f_strict)
|
60
|
+
undef_method(:to_f)
|
61
|
+
undef_method(:to_i)
|
62
|
+
end
|
63
|
+
|
64
|
+
class Float
|
65
|
+
def is_i?
|
66
|
+
return self == self.to_i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
#######################################################
|
72
|
+
#######################################################
|
73
|
+
|
74
|
+
|
75
|
+
class Array
|
76
|
+
|
77
|
+
def hist
|
78
|
+
h = {}
|
79
|
+
for item in self
|
80
|
+
h[item] ||= 0
|
81
|
+
h[item] += 1
|
82
|
+
end
|
83
|
+
return h
|
84
|
+
end
|
85
|
+
|
86
|
+
def shuffle!
|
87
|
+
sort_by {rand}
|
88
|
+
return self
|
89
|
+
end
|
90
|
+
|
91
|
+
def shuffle
|
92
|
+
return dup().sort_by {rand}
|
93
|
+
end
|
94
|
+
|
95
|
+
def sum
|
96
|
+
total = 0
|
97
|
+
for item in self
|
98
|
+
total += item
|
99
|
+
end
|
100
|
+
return total
|
101
|
+
end
|
102
|
+
|
103
|
+
def avg
|
104
|
+
return sum.to_f / self.length
|
105
|
+
end
|
106
|
+
|
107
|
+
# Sample variance
|
108
|
+
def variance
|
109
|
+
average = avg()
|
110
|
+
variance = 0
|
111
|
+
for item in self
|
112
|
+
diff = item - average
|
113
|
+
variance += diff * diff
|
114
|
+
end
|
115
|
+
return variance / (self.length - 1)
|
116
|
+
end
|
117
|
+
|
118
|
+
def stddev
|
119
|
+
return Math.sqrt(self.variance())
|
120
|
+
end
|
121
|
+
|
122
|
+
def mean
|
123
|
+
return self.avg
|
124
|
+
end
|
125
|
+
|
126
|
+
def argmax
|
127
|
+
max_i = 0
|
128
|
+
max_val = self[max_i]
|
129
|
+
|
130
|
+
self.each_with_index do |x, i|
|
131
|
+
if x > max_val
|
132
|
+
max_val = x
|
133
|
+
max_i = i
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
return max_i
|
138
|
+
end
|
139
|
+
|
140
|
+
def argmin
|
141
|
+
min_i = 0
|
142
|
+
min_val = self[min_i]
|
143
|
+
|
144
|
+
self.each_with_index do |x, i|
|
145
|
+
if x < min_val
|
146
|
+
min_val = x
|
147
|
+
min_i = i
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
return min_i
|
152
|
+
end
|
153
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: swak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jesse Rodriguez
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-07-
|
13
|
+
date: 2012-07-16 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Random tools and mixins
|
@@ -24,7 +24,9 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- lib/swak.rb
|
26
26
|
- lib/swak/crypt.rb
|
27
|
+
- lib/swak/toplevel.rb
|
27
28
|
- lib/swak/interval.rb
|
29
|
+
- lib/swak/table.rb
|
28
30
|
- lib/swak/logger.rb
|
29
31
|
homepage: http://rubygems.org/gems/swak
|
30
32
|
licenses: []
|