|
| 1 | +import nose |
| 2 | +from nose import with_setup |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +import os, sys |
| 6 | + |
| 7 | +def create_test_file(): |
| 8 | + global xbed, xstore, xgroup |
| 9 | + xbed = "testtable.tab" |
| 10 | + xstore = 'tempstore.h5' |
| 11 | + xgroup = "x" |
| 12 | + |
| 13 | + col_nums = [0] |
| 14 | + df = pd.DataFrame({"V1":["a","b","c","d","e", "aaaah!!!"], |
| 15 | + "W":["c","d","c","d","c","c"], |
| 16 | + "ZZZ":np.arange(6)}) |
| 17 | + df.set_index(["V1","W"], inplace = True) |
| 18 | + df.to_csv( xbed, sep = "\t") |
| 19 | + |
| 20 | + |
| 21 | +def clear_files(): |
| 22 | + os.remove(xbed) |
| 23 | + os.remove(xstore) |
| 24 | + |
| 25 | +def write_hdf5_11364(indexcols): |
| 26 | + sep = "\t" |
| 27 | + chunksize=5 |
| 28 | + try: |
| 29 | + os.remove(xstore) |
| 30 | + except OSError: |
| 31 | + pass |
| 32 | + # create a store |
| 33 | + with pd.HDFStore(xstore) as store: |
| 34 | + for nn, chunk in enumerate(pd.read_table(xbed, chunksize=chunksize, sep = sep, index_col= indexcols if not indexcols==["index"] else 0)): |
| 35 | + #print(chunk.index.names) |
| 36 | + store.append(xgroup, chunk, format = "table", min_itemsize = \ |
| 37 | + #{"index":32} if len(indexcols)==1 else \ |
| 38 | + dict(zip(chunk.index.names, [32]*len(chunk.index.names)))) |
| 39 | + print("chunk #" , nn, file = sys.stderr) |
| 40 | + |
| 41 | + print("index columns:", indexcols, file = sys.stderr) |
| 42 | + assert True |
| 43 | + |
| 44 | +def read_hdf5_11364(indexcols): |
| 45 | + with pd.HDFStore(xstore) as store: |
| 46 | + df = store.get(xgroup) |
| 47 | + print(df.shape) |
| 48 | + assert (df.shape==(6,3 - len(indexcols))), "wrong shape" |
| 49 | + |
| 50 | +@with_setup(create_test_file, clear_files ) |
| 51 | +def test_write_read_hdf5_11364_indexcol(): |
| 52 | + indexcols = ["index"] |
| 53 | + write_hdf5_11364(indexcols) |
| 54 | + read_hdf5_11364(indexcols) |
| 55 | + return |
| 56 | + |
| 57 | +@with_setup(create_test_file, clear_files ) |
| 58 | +def test_write_read_hdf5_11364_1col(): |
| 59 | + indexcols =[0] |
| 60 | + write_hdf5_11364(indexcols) |
| 61 | + read_hdf5_11364(indexcols) |
| 62 | + return |
| 63 | + |
| 64 | +@with_setup(create_test_file, clear_files ) |
| 65 | +def test_write_read_hdf5_11364_2col(): |
| 66 | + indexcols =[0,1] |
| 67 | + write_hdf5_11364(indexcols) |
| 68 | + read_hdf5_11364(indexcols) |
| 69 | + return |
| 70 | + |
| 71 | + |
0 commit comments