Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31980

Re: Fast forward-backward (write-read)

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'binary': 0.05; 'ascii': 0.07; "'rb')": 0.09; 'backwards': 0.09; 'bytes)': 0.09; 'chunks': 0.09; 'cc:addr:python-list': 0.10; 'stored': 0.10; 'def': 0.10; 'assume': 0.11; '2):': 0.16; 'arrays.': 0.16; 'f.tell()': 0.16; 'inputs': 0.16; 'iterator': 0.16; 'numpy': 0.16; 'processed.': 0.16; 'reload': 0.16; 'reversed': 0.16; 'sequential': 0.16; 'written.': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'yield': 0.17; '>>>': 0.18; 'input': 0.18; 'written': 0.20; 'import': 0.21; 'assuming': 0.22; 'finally,': 0.22; 'cc:2**0': 0.23; 'external': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '---': 0.26; 'wrote': 0.26; '(e.g.': 0.27; 'question': 0.27; 'received:209.85.210.46': 0.27; 'message-id:@mail.gmail.com': 0.27; 'arrays': 0.29; 'efficiently': 0.29; 'thus,': 0.29; 'array': 0.29; 'performing': 0.30; 'file': 0.32; 'ram': 0.33; 'received:google.com': 0.34; 'saved': 0.35; 'received:209.85': 0.35; 'data.': 0.36; "i'll": 0.36; 'subject: (': 0.36; 'october': 0.37; 'being': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'files': 0.38; 'some': 0.38; 'several': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'save': 0.61; 'first': 0.61; 'mentioned': 0.63; 'series': 0.63; 'skip:n 10': 0.63; 'reverse': 0.65; 'forward': 0.66; 'manner': 0.74; 'oscar': 0.84; 'subject:Fast': 0.84; 'subject:read': 0.84; 'subject:write': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=szO1zl4gg2A/rs7cHf8HEJpQhGf7ZFoLtQadl+MiBkc=; b=T45oQ7YVFyoRDSkDuzW4hnhrMvm0JedEgDfZWaFYn5qeYCclRHXW38UmD/alSStonU PhqZFcSR9NuQRK5AYuhZCrRDkzyTsp0/N6EPIQEclkGZLupx3NisZWgBq/mVMxfpUVK3 AP6NE80nuHHR5jP8oAwrbdvitprW9mJtZL4eRkyTBc6FK5lw5X0m68jwEenAujHkKQeU guugoYcI2is4+Oqcl1fGtsurcsxs7KqG9l1uGxwidr5S7h8uzDm2hr6mGy4b4dTv/9qj XaZlDvCoJ9UUAl9R8LJibLFwfFrgquUXF27j2VYEL+SxlJU/IiqaGRTatFsFWJbh0RL5 Iw4w==
MIME-Version 1.0
In-Reply-To <5086AA35.4000509@it.uu.se>
References <5086AA35.4000509@it.uu.se>
Date Wed, 24 Oct 2012 01:06:13 +0100
Subject Re: Fast forward-backward (write-read)
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
To Virgil Stokes <vs@it.uu.se>
Content-Type text/plain; charset=ISO-8859-1
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2706.1351037177.27098.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351037177 news.xs4all.nl 6854 [2001:888:2000:d::a6]:55861
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31980

Show key headers only | View raw


On 23 October 2012 15:31, Virgil Stokes <vs@it.uu.se> wrote:
> I am working with some rather large data files (>100GB) that contain time
> series data. The data (t_k,y(t_k)), k = 0,1,...,N are stored in ASCII
> format. I perform various types of processing on these data (e.g. moving
> median, moving average, and Kalman-filter, Kalman-smoother) in a sequential
> manner and only a small number of these data need be stored in RAM when
> being processed. When performing Kalman-filtering (forward in time pass, k =
> 0,1,...,N) I need to save to an external file several variables (e.g. 11*32
> bytes) for each (t_k, y(t_k)). These are inputs to the Kalman-smoother
> (backward in time pass, k = N,N-1,...,0). Thus, I will need to input these
> variables saved to an external file from the forward pass, in reverse order
> --- from last written to first written.
>
> Finally, to my question --- What is a fast way to write these variables to
> an external file and then read them in backwards?

You mentioned elsewhere that you are using numpy. I'll assume that the
data you want to read/write are numpy arrays.

Numpy arrays can be written very efficiently in binary form using
tofile/fromfile:

>>> import numpy
>>> a = numpy.array([1, 2, 5], numpy.int64)
>>> a
array([1, 2, 5])
>>> with open('data.bin', 'wb') as f:
...   a.tofile(f)
...

You can then reload the array with:

>>> with open('data.bin', 'rb') as f:
...   a2 = numpy.fromfile(f, numpy.int64)
...
>>> a2
array([1, 2, 5])

Numpy arrays can be reversed before writing or after reading using;

>>> a2
array([1, 2, 5])
>>> a2[::-1]
array([5, 2, 1])

Assuming you wrote the file forwards you can make an iterator to yield
the file in chunks backwards like so (untested):

def read_backwards(f, dtype, chunksize=1024 ** 2):
    dtype = numpy.dtype(dtype)
    nbytes = chunksize * dtype.itemsize
    f.seek(0, 2)
    fpos = f.tell()
    while fpos > nbytes:
        f.seek(fpos, 0)
        yield numpy.fromfile(f, dtype, chunksize)[::-1]
        fpos -= nbytes
    yield numpy.fromfile(f, dtype)[::-1]


Oscar

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Fast forward-backward (write-read) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-10-24 01:06 +0100

csiph-web