Debian Codegraph @

scripts/heri-split

raw
1 #!/usr/bin/env ruby
2
3 # Copyright (c) 2015 Alexandra Figlovskaya <[email protected]>
4 # Copyright (c) 2015-2019 Aleksey Cheusov <[email protected]>
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 require 'optparse'
26
27 $options = {}
28 $fold_cnt = nil
29 $ratio = nil
30 $tmp_dir = nil
31 $seed = Random.new_seed
32 $stratified = true
33
34 OptionParser.new do |opts|
35 opts.banner = <<EOF
36 heri-split splits the given dataset (in svmlight format)
37 into training and testing sets for further evaluation.
38 If option -c is specified, test<N>.txt and train<N>.txt files
39 (also in svmlight format) are created, where N is the number of fold.
40 If option -R is specified, test.txt and train.txt files
41 are created for the same purposes.
42 Also testing_fold.txt file is created, where for each object (one per line)
43 its testing fold number is specified if oprion -c is applied.
44 The file testing_fold.txt contain either 1 for testing set and 0
45 for training set, if option -R is applied.
46 Usage:
47 heri-split [OPTIONS] dataset1 [dataset2...]
48 OPTIONS:
49 EOF
50
51 opts.on('-h', '--help','display this message and exit') do
52 puts opts
53 exit 0
54 end
55
56 opts.on("-cFOLD_CNT", "--folds=FOLD_CNT", "A number if folds (mandatory option)") do |c|
57 $fold_cnt = c.to_i
58 end
59
60 opts.on("-dDIR", "--output-dir=DIR", "Output directory (mandatory option)") do |d|
61 $tmp_dir = d
62 end
63
64 opts.on("-sSEED", "--seed=SEED", "Seed for pseudo-random number generator") do |s|
65 if s != "" then
66 $seed = s.to_i
67 end
68 end
69
70 opts.on("-r", "--random", "Use random split instead of stratified") do
71 $stratified = false
72 end
73
74 opts.on("-R", "--ratio=RATIO", "Split input dataset into training and testing sets
75 with the specified RATIO (in percents)") do |r|
76 $ratio = r.to_i
77 $fold_cnt = 0
78 end
79
80 opts.separator " "
81 end.parse!
82
83 if $tmp_dir == nil or ($fold_cnt == nil and $ratio == nil) then
84 STDERR.puts("Options -c/-R and -d are mandatory, see heri-split -h for details")
85 exit(1)
86 end
87
88 $rnd = Random.new($seed)
89
90 # same as in StratifiedSplitter
91 $files_test = []
92 $files_train = []
93 $testing_fold = File.open($tmp_dir+"/testing_fold.txt", 'w:ASCII-8BIT')
94 (1..$fold_cnt).each do |i|
95 name_train = "train" + "#{i.to_i}"
96 name_test = "test" + "#{i.to_i}"
97 $files_test << File.open($tmp_dir+"/"+name_test+".txt", 'w:ASCII-8BIT')
98 $files_train << File.open($tmp_dir+ "/"+ name_train+".txt", 'w:ASCII-8BIT')
99 end
100 if $ratio != nil
101 $files_test << File.open($tmp_dir+"/test.txt", 'w:ASCII-8BIT')
102 $files_train << File.open($tmp_dir+ "/train.txt", 'w:ASCII-8BIT')
103 end
104
105 def random_split_cv()
106 nums = []
107 curr_number = 0
108 ARGV.each do |fn|
109 File.open(fn, "r:ASCII-8BIT").each_line do |line|
110 if line =~ /^([^\s]+)\s/
111 nums << curr_number % $fold_cnt
112 curr_number += 1
113 end
114 end
115 end
116
117 nums.shuffle!(random: $rnd)
118
119 curr_number = 0
120 ARGV.each do |fn|
121 File.open(fn, "r:ASCII-8BIT").each_line do |line|
122 if line =~ /^([^\s]+)\s/
123 fold_num = nums[curr_number]
124 $fold_cnt.times do |n|
125 if fold_num == n
126 $files_test[n].puts line
127 $testing_fold.puts n+1
128 else
129 $files_train[n].puts line
130 end
131 end
132
133 curr_number += 1
134 end
135 end
136 end
137 end
138
139 def random_split_holdout()
140 nums = []
141 threshold = 1
142 line_count = 0
143 ARGV.each do |fn|
144 File.open(fn, "r:ASCII-8BIT").each_line do |line|
145 if line =~ /^([^\s]+)\s/
146 line_count += 1
147 nums << $rnd.rand()
148 end
149 end
150 end
151
152 threshold = (line_count.to_f * $ratio / 100).to_i
153 if threshold == 0
154 threshold = 1
155 end
156 threshold = nums.sort[threshold]
157
158 curr_number = 0
159 ARGV.each do |fn|
160 File.open(fn, "r:ASCII-8BIT").each_line do |line|
161 if line =~ /^([^\s]+)\s/
162 if nums[curr_number] < threshold
163 $files_train[0].puts line
164 $testing_fold.puts '0'
165 else
166 $files_test[0].puts line
167 $testing_fold.puts '1'
168 end
169 curr_number += 1
170 end
171 end
172 end
173 end
174
175 def random_split()
176 if $fold_cnt != nil and $fold_cnt > 0
177 random_split_cv()
178 else
179 random_split_holdout()
180 end
181 end
182
183 def stratified_split()
184 classes = Hash.new(0)
185 ARGV.each do |fn|
186 File.open(fn, "r:ASCII-8BIT").each_line do |line|
187 if line =~ /^([^\s]+)\s/
188 classes[$1] += 1
189 end
190 end
191 end
192 classes_arr = {}
193 classes.each do |x, y|
194 arr = []
195 y.times do |i|
196 arr << i
197 end
198 arr.shuffle!(random: $rnd)
199 classes_arr [x] = {}
200 arr.each_index do |i|
201 if $ratio != nil
202 cnt = (y.to_f * $ratio / 100).to_i
203 fold_train = (arr[i] < cnt)
204 classes_arr[x][i] = fold_train
205 else
206 fold_train = (i * $fold_cnt.to_f / arr.size).to_i
207 classes_arr[x][arr[i]] = fold_train
208 end
209 end
210 end
211
212 num_line = Hash.new(0)
213 ARGV.each do |fn|
214 File.open(fn, "r:ASCII-8BIT").each_line do |line|
215 if line =~ /^([^\s]+)\s/
216 curr_number = num_line[$1]
217 if $ratio != nil
218 if classes_arr[$1][curr_number]
219 $files_train[0].puts line
220 $testing_fold.puts "0"
221 else
222 $files_test[0].puts line
223 $testing_fold.puts "1"
224 end
225 else
226 $fold_cnt.times do |n|
227 if classes_arr[$1][curr_number] == n
228 $files_test[n].puts line
229 $testing_fold.puts n+1
230 else
231 $files_train[n].puts line
232 end
233 end
234 end
235 num_line[$1] += 1
236 end
237 end
238 end
239 end
240
241 if $stratified
242 stratified_split()
243 else
244 random_split()
245 end
246
247 $files_test.each { |x|
248 x.close
249 }
250 $files_train.each { |x|
251 x.close
252 }
253 $testing_fold.close