From 940bb8d73805da09c8f90af767dbeef1ad89cbfa Mon Sep 17 00:00:00 2001 From: Daniel Blankenberg Date: Sun, 25 Sep 2011 13:27:14 -0400 Subject: [PATCH] Some random lines tool tweaks. --- tools/filters/random_lines_two_pass.py | 40 +++++++++++++++++++------- tools/filters/randomlines.xml | 2 +- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/tools/filters/random_lines_two_pass.py b/tools/filters/random_lines_two_pass.py index 7d0b2bb7758..25ba02d1c1a 100644 --- a/tools/filters/random_lines_two_pass.py +++ b/tools/filters/random_lines_two_pass.py @@ -2,9 +2,25 @@ #Dan Blankenberg #Selects N random lines from a file and outputs to another file, maintaining original line order #allows specifying a seed -#does two passes to determine line counts and offsets, and then to output contents +#does two passes to determine line offsets/count, and then to output contents -import optparse, random +import optparse, random + +def get_random_by_subtraction( line_offsets, num_lines ): + while len( line_offsets ) > num_lines: + del line_offsets[ random.randint( 0, len( line_offsets ) - 1 ) ] + return line_offsets + +def get_random_by_sample( line_offsets, num_lines ): + line_offsets = random.sample( line_offsets, num_lines ) + line_offsets.sort() + return line_offsets + +def get_random( line_offsets, num_lines ): + if num_lines > ( len( line_offsets ) / 2 ): + return get_random_by_subtraction( line_offsets, num_lines ) + else: + return get_random_by_sample( line_offsets, num_lines ) def __main__(): #Parse Command Line @@ -14,7 +30,7 @@ def __main__(): assert len( args ) == 3, "Invalid command line specified." - input = open( args[0] ) + input = open( args[0], 'rb' ) output = open( args[1], 'wb' ) num_lines = int( args[2] ) assert num_lines > 0, "You must select at least one line." @@ -24,10 +40,13 @@ def __main__(): #get line offsets line_offsets = [] + teller = input.tell + readliner = input.readline + appender = line_offsets.append while True: - offset = input.tell() - if input.readline(): - line_offsets.append( offset ) + offset = teller() + if readliner(): + appender( offset ) else: break @@ -35,13 +54,14 @@ def __main__(): assert num_lines <= total_lines, "Error: asked to select more lines (%i) than there were in the file (%i)." % ( num_lines, total_lines ) #get random line offsets - while len( line_offsets ) > num_lines: - line_offsets.pop( random.randint( 0, len( line_offsets ) - 1 ) ) + line_offsets = get_random( line_offsets, num_lines ) #write out random lines + seeker = input.seek + writer = output.write for line_offset in line_offsets: - input.seek( line_offset ) - output.write( input.readline() ) + seeker( line_offset ) + writer( readliner() ) input.close() output.close() print "Kept %i of %i total lines." % ( num_lines, total_lines ) diff --git a/tools/filters/randomlines.xml b/tools/filters/randomlines.xml index 1089d73a613..526c66d4b59 100644 --- a/tools/filters/randomlines.xml +++ b/tools/filters/randomlines.xml @@ -1,4 +1,4 @@ - + from a file random_lines_two_pass.py "${input}" "${out_file1}" "${num_lines}" #if str( $seed_source.seed_source_selector ) == "set_seed":