| 1 | """ |
|---|
| 2 | Tests for `bx.binned_array`. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from numpy import * |
|---|
| 6 | from binned_array import * |
|---|
| 7 | |
|---|
| 8 | random = random.random |
|---|
| 9 | |
|---|
| 10 | # Bigger values take longer, but excercise more bins |
|---|
| 11 | CHUNK_SIZE_RANDOM=945 |
|---|
| 12 | CHUNK_SIZE_ZEROS=897 |
|---|
| 13 | ##CHUNK_SIZE_RANDOM=9456 |
|---|
| 14 | ##CHUNK_SIZE_ZEROS=8972 |
|---|
| 15 | |
|---|
| 16 | source = target = None |
|---|
| 17 | |
|---|
| 18 | def setup(): |
|---|
| 19 | global source |
|---|
| 20 | global target |
|---|
| 21 | source = [] |
|---|
| 22 | for i in range( 13 ): |
|---|
| 23 | if random() < 0.5: |
|---|
| 24 | source = concatenate( ( source, random( CHUNK_SIZE_RANDOM ) ) ) |
|---|
| 25 | else: |
|---|
| 26 | source = concatenate( ( source, zeros( CHUNK_SIZE_ZEROS, 'f' ) ) ) |
|---|
| 27 | source = source.astype( 'f' ) |
|---|
| 28 | # Set on target |
|---|
| 29 | target = BinnedArray( 128, NaN, len( source ) ) |
|---|
| 30 | for i in range( len( source ) ): |
|---|
| 31 | # if not isNaN( source[i] ): |
|---|
| 32 | target[i] = source[i] |
|---|
| 33 | return source, target |
|---|
| 34 | |
|---|
| 35 | def test_simple(): |
|---|
| 36 | # Verify |
|---|
| 37 | for i in range( len( source ) ): |
|---|
| 38 | assert source[i] == target[i], "No match, index: %d, source: %f, target: %f, len( source ): %d" % ( i, source[i], target[i], len( source ) ) |
|---|
| 39 | # Verify with slices |
|---|
| 40 | for i in range( 10 ): |
|---|
| 41 | a = int( random() * len( source ) ) |
|---|
| 42 | b = int( random() * len( source ) ) |
|---|
| 43 | if b < a: a, b = b, a |
|---|
| 44 | assert allclose( source[a:b], target[a:b] ), "No match, index: %d:%d, source: %s, target: %s" % \ |
|---|
| 45 | ( a, b, ",".join( map( str, source[a:a+10] ) ), ",".join( map( str, target[a:a+10] ) ) ) |
|---|
| 46 | |
|---|
| 47 | def test_file(): |
|---|
| 48 | # With a file (zlib) |
|---|
| 49 | target.to_file( open( "/tmp/foo", "w" ) ) |
|---|
| 50 | target2 = FileBinnedArray( open( "/tmp/foo" ) ) |
|---|
| 51 | for i in range( len( source ) ): |
|---|
| 52 | assert source[i] == target2[i], "No match, index: %d, source: %d, target: %d" % ( i, source[i], target2[i] ) |
|---|
| 53 | # Verify with slices |
|---|
| 54 | target2 = FileBinnedArray( open( "/tmp/foo" ) ) |
|---|
| 55 | for i in range( 10 ): |
|---|
| 56 | a = int( random() * len( source ) ) |
|---|
| 57 | b = int( random() * len( source ) ) |
|---|
| 58 | if b < a: a, b = b, a |
|---|
| 59 | assert allclose( source[a:b], target[a:b] ), "No match, index: %d:%d, source: %s, target: %s" % \ |
|---|
| 60 | ( a, b, ",".join( map( str, source[a:a+10] ) ), ",".join( map( str, target2[a:a+10] ) ) ) |
|---|
| 61 | |
|---|
| 62 | def test_file_lzo(): |
|---|
| 63 | # With a file (lzo) |
|---|
| 64 | target.to_file( open( "/tmp/foo3", "w" ), comp_type="lzo" ) |
|---|
| 65 | target3 = FileBinnedArray( open( "/tmp/foo3" ) ) |
|---|
| 66 | # Verify |
|---|
| 67 | for i in range( len( source ) ): |
|---|
| 68 | assert source[i] == target3[i], "No match, index: %d, source: %d, target: %d" % ( i, source[i], target3[i] ) |
|---|
| 69 | # Verify with slices |
|---|
| 70 | target3 = FileBinnedArray( open( "/tmp/foo3" ) ) |
|---|
| 71 | for i in range( 10 ): |
|---|
| 72 | a = int( random() * len( source ) ) |
|---|
| 73 | b = int( random() * len( source ) ) |
|---|
| 74 | if b < a: a, b = b, a |
|---|
| 75 | assert allclose( source[a:b], target3[a:b] ), "No match, index: %d:%d, source: %s, target: %s" % \ |
|---|
| 76 | ( a, b, ",".join( map( str, source[a:a+10] ) ), ",".join( map( str, target3[a:a+10] ) ) ) |
|---|
| 77 | |
|---|
| 78 | def test_binned_array_writer(): |
|---|
| 79 | # Test with ba writer |
|---|
| 80 | o = open( "/tmp/foo4", "w" ) |
|---|
| 81 | w = BinnedArrayWriter( o, 128, comp_type='lzo' ) |
|---|
| 82 | for val in source: |
|---|
| 83 | w.write( val ) |
|---|
| 84 | w.finish() |
|---|
| 85 | o.close() |
|---|
| 86 | # Verify |
|---|
| 87 | target4 = FileBinnedArray( open( "/tmp/foo4" ) ) |
|---|
| 88 | for i in range( len( source ) ): |
|---|
| 89 | assert allclose( source[i], target4[i] ), "No match, index: %d, source: %d, target: %d" % ( i, source[i], target4[i] ) |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|