forked from munibanust/febrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyrandomselect.py
More file actions
202 lines (161 loc) · 6.85 KB
/
pyrandomselect.py
File metadata and controls
202 lines (161 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
50
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
84
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# =============================================================================
# pyRandomSelect.py - Auxiliary program to randomly select records
#
# Freely extensible biomedical record linkage (Febrl) Version 0.1
# See http://datamining.anu.edu.au/projects/linkage.html
#
# =============================================================================
# AUSTRALIAN NATIONAL UNIVERSITY OPEN SOURCE LICENSE (ANUOS LICENSE)
# VERSION 1.0
#
# The contents of this file are subject to the ANUOS License Version 1.0 (the
# "License"); you may not use this file except in compliance with the License.
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# The Original Software is "pyRandomSelect.py".
# The Initial Developers of the Original Software are Dr Peter Christen
# (Department of Computer Science, Australian National University), Dr Tim
# Churches (Centre for Epidemiology and Research, New South Wales Department
# of Health) and Drs Markus Hegland, Stephen Roberts and Ole Nielsen
# (Mathematical Sciences Insitute, Australian National University). Copyright
# (C) 2002 the Australian National University and others. All Rights Reserved.
# Contributors:
#
# =============================================================================
"""Module pyRandomSelect.py - Auxiliary program to randomly select records.
USAGE:
pyRandomSelect.py [in_file] [out_file] -perc [percentage_value]
or
pyRandomSelect.py [in_file] [out_file] -num [num_records]
ARGUMENTS:
in_file Name of the input file with the original data records
out_file Name of the output file where the randomly selected records are
written to
Either '-perc' or '-num' (plus a value) has to be given as third command
line argument:
-perc [percentage_value] Set the percentage value of how many records
should be selected randomly as a floating-point
number. The percentage value must be between
0.0 and 100.0.
For example '-perc 0.1' selects 0.1% of all
records.
-num [num_records] Alternatively, the absolute number of records
to be selected randomly can be given as an
argument. The value must be positive.
DESCRIPTION:
This program reads in a data file and randomly selects record according
to the selected optional argument. It writes these records unchanged into
the output file.
"""
# -----------------------------------------------------------------------------
import sys
import random
# -----------------------------------------------------------------------------
def selectrandom():
"""Main routine, open file, read lines, select randomly, write if select.
USAGE:
selectrandom()
ARGUMENTS:
None
DESCRIPTION:
Main routine, see description of module above.
"""
# Process command line arguments and check for correctness - - - - - - - - -
#
if (len(sys.argv) != 5):
print '***** Error: %s needs three arguments:'% (sys.argv[0])
print '***** - Name of the original input data file'
print '***** - Name of the output file with the selected records'
print '***** Then either'
print '***** - -perc [percentage_value]'
print '***** or'
print '***** - -num [num_records]'
raise Exception()
if (sys.argv[1] == sys.argv[2]):
print '***** Error: Input and output files must differ'
print '***** Input file name: ', sys.argv[1]
print '***** Output file name:', sys.argv[2]
raise Exception()
in_file_name = sys.argv[1]
out_file_name = sys.argv[2]
if (sys.argv[3][:2] == '-p'):
select_mode = 'perc'
elif(sys.argv[3][:2] == '-n'):
select_mode = 'num'
else:
print '***** Error: Illegal random selection argument:', sys.argv[3]
print '***** Possible are:'
print '***** - -perc [percentage_value]'
print '***** or'
print '***** - -num [num_records]'
raise Exception()
if (select_mode == 'perc'):
perc_val = float(sys.argv[4])
if (perc_val <= 0.0) or (perc_val >= 100.0):
print '***** Error: Illegal value for random percentage:', sys.argv[4]
print '***** Value must be between 0.0 and 100.0'
raise Exception()
else: # Number of records given
num_rec = int(sys.argv[4])
if (num_rec <= 0):
print '***** Error: Illegal value for number of records:', sys.argv[4]
print '***** Value must be positive.'
raise Exception()
# Open input file and check number of available records - - - - - - - - - - -
#
try:
f_in = open(in_file_name,'r')
except:
print '***** Error: Can not open input file:', in_file_name
raise IOError()
print "Counting lines in source file..."
line_count = 0
for line in f_in.xreadlines():
line_count += 1
f_in.close()
if (select_mode == 'num') and (num_rec > line_count): # Illegal value
print '***** Error: Illegal values for number of records:', num_rec
print '***** File only contains',line_count, 'lines/records'
raise Exception()
# Flag records which have been selected randomly - - - - - - - - - - - - - -
#
if (select_mode == 'perc'): # Compute number of records to be selected
num_rec = int(perc_val * float(line_count) / 100.0)
# Create a sequence of bit flags, one for each record in the file
#
record_flags = [0] * line_count
selected_rec_num = 0
while (selected_rec_num < num_rec):
random_rec_num = random.randrange(0,line_count,1)
if (record_flags[random_rec_num] == 0):
record_flags[random_rec_num] = 1
selected_rec_num += 1
print 'Selected', selected_rec_num, 'records...'
# Open files, read lines and write selected records to output file - - - - -
f_in = open(in_file_name,'r')
try:
f_out = open(out_file_name,'w')
except:
print '***** Error: Can not write to output file:', out_file_name
f_in.close()
raise IOError()
increment = int(float(line_count) / 100.0)
print 'Processing source file...' # - - - - - - - - - - - - - - - - - - - -
line_count = 0
percent_complete = 0
for line in f_in.xreadlines():
if (record_flags[line_count] == 1): # This line is selected
f_out.write(line)
line_count += 1
if ((line_count % increment) == 0):
percent_complete += 1
print '%3d%% completed' %(percent_complete),'\r',
f_in.close()
f_out.close()
print
# ----------------------------------------------------------------------------
# Start main routine
#
selectrandom()
# ----------------------------------------------------------------------------