GithubHelp home page GithubHelp logo

lua_gzipfile's Introduction

gzfile

Conveniently (and with decent performance) read and write data from gzip files directly. Useful for text or very large files.

Implemented as an FFI wrapper for zlib, including wrappers for C FILE functions like fscanf and fwrite. More can be easily added.

Install:

luarocks install https://raw.githubusercontent.com/bshillingford/lua_gzipfile/master/gzfile-scm-1.rockspec

Example:

Read 200 floats from a gzipped file directly into a torch tensor:

require 'torch'
local GZFile = require 'gzfile.GZFile'

local tensor = torch.FloatTensor(200)
local f = GZFile('floats.gz', 'rb')
f:readbuf(tensor:data(), 200*4)  -- sizeof(float)=4; read 200*4 bytes
f:close()
-- now do stuff with the tensor

Note: compressing floats can be useful for neural nets, since similar values at similar orders of magnitude will often results in repeated byte patterns.

To access the underlying FILE* handle, use the handle property of GZFile.

Functions implemented:

  • Constructor: file = GZFile(filename, mode) Opens the file for reading or writing using the given mode. See zlib gzopen() documentation for supported modes.
  • :close() Closes the file. Later operations will fail.
  • :write(str) Writes a lua string. Doesn't allocate memory, just casts using ffi. Calls fwrite and returns its ret val, i.e. number of bytes written.
  • :read(nbytes) Reads to a buffer then turns into a lua string. Allocates memory on each call, so slightly inefficient if you do many reads.
  • :writebuf(ptr, nbytes) Writes data from the given buffer to the file. Returns number of bytes written.
  • :readbuf(ptr, nbytes) Reads into the given location in memory. Returns number of bytes read.
  • :flush()
  • :peek()
  • :tell() Returns the position in the file using ftell.
  • :seek(offset, origin) Seek using fseek, relative to beginning of file. Note the argument order matches C's rather than Lua's io. Returns new position from ftell.
  • :getc()
  • :scanf(fmt, typestring) Calls fscanf, but only for reading a single field e.g. %s. Allocates memory automatically, this memory will be gc'd by lua. Typestring examples: 'float[1]', 'uint8_t[1]', 'char[16]'.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.