Projects / PersonDetection / perdet / utils /

# encoding: utf-8
# Author: Nicholas Piël
# A fast compressor and decompressor that will load and an array to file

import ctypes
import numpy
from perdet.config import *

FLZ = ctypes.cdll.LoadLibrary(binfile(''))
#FLZ = ctypes.cdll.LoadLibrary('/Users/nlp/Documents/school/Afstuderen/PersonDetection/Resources/bin/')

# We will be using the following header: 
#   dtype  size  shape_sizes

def load_compressed_array(filename):
    ''' Simply load the compressed array from filename
        use ctypes pointers so we do not need to copy the data
    f = open(filename, 'rb')
    header = f.readline().strip().split()
    dtype = header[0]
    size = int(header[1])
    shape = tuple([int(i) for i in header[2:]])
    data = decompress(, size-1)
    return numpy.fromstring(data, dtype=dtype).reshape(shape)

def save_compressed_array(filename, array):
    ''' Compress the array and save it to file
        XXX: could use ctypes pointers
    f = open(filename, 'wb')
    shape = ' '.join([str(i) for i in array.shape])
    header = "%s %s %s\n" % (array.dtype, array.nbytes, shape)

def compress(data):
    ''' This will compress the datastring and return the compressed 
    datalen = len(data)
    output = ctypes.create_string_buffer('\000' * int(datalen * 1.05))
    count = FLZ.fastlz_compress(data, datalen, output)
    return output[:count]

def decompress(data, size=307200):
    ''' This will decompress the datastring and return the uncompressed
    output = ctypes.create_string_buffer('\000' * size)
    FLZ.fastlz_decompress(data, size+1, output, len(output) )
    return output.raw

if __name__ == "__main__":
    import numpy
    a = numpy.ones((10,10)).astype('uint8')
    print "Saving..."
    save_compressed_array('array.flz', a)
    print "loading"
    b = load_compressed_array('array.flz')
    print b.shape, b.dtype, b