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


Groups > comp.lang.python > #32010

Re: Fast forward-backward (write-read)

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <vs@it.uu.se>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
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; 'stored': 0.10; 'def': 0.10; 'assume': 0.11; '2):': 0.16; 'arrays.': 0.16; 'benjamin': 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; 'input': 0.18; 'to:name:python-list@python.org': 0.20; 'written': 0.20; 'import': 0.21; 'assuming': 0.22; 'finally,': 0.22; 'external': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '---': 0.26; 'wrote': 0.26; 'charset:iso-8859-15': 0.26; '(e.g.': 0.27; 'question': 0.27; '>>>>': 0.29; 'arrays': 0.29; 'efficiently': 0.29; 'thus,': 0.29; 'array': 0.29; 'performing': 0.30; 'file': 0.32; 'ram': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'saved': 0.35; 'data.': 0.36; "i'll": 0.36; 'subject: (': 0.36; 'october': 0.37; 'being': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'files': 0.38; 'some': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'subject:-': 0.40; 'save': 0.61; 'first': 0.61; 'more.': 0.62; '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
X-SENDER-IP [213.112.50.224]
X-LISTENER [smtp.bredband.net]
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Aj4mAEeUh1DVcDLgPGdsb2JhbAANN4pOt0YBAQEBOIJSAQEBAQM4QBELGAkWDwkDAgECATEUBg0GAgEBr0+TRYtghmsDkkGWXA
X-IronPort-AV E=Sophos;i="4.80,639,1344204000"; d="scan'208";a="432634866"
Date Wed, 24 Oct 2012 09:12:29 +0200
From Virgil Stokes <vs@it.uu.se>
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/17.0 Thunderbird/17.0
MIME-Version 1.0
To "python-list@python.org" <python-list@python.org>
Subject Re: Fast forward-backward (write-read)
References <5086AA35.4000509@it.uu.se> <CAHVvXxRRSwV=A9Sx6odkpG2wOYwito7=OD6YPatcAutoBuAcgw@mail.gmail.com>
In-Reply-To <CAHVvXxRRSwV=A9Sx6odkpG2wOYwito7=OD6YPatcAutoBuAcgw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding 7bit
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.2733.1351062745.27098.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351062745 news.xs4all.nl 6982 [2001:888:2000:d::a6]:50554
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32010

Show key headers only | View raw


On 24-Oct-2012 02:06, Oscar Benjamin wrote:
> 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
Ok Oscar,
Thanks for the tip and I will look into this more.

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


Thread

Re: Fast forward-backward (write-read) Virgil Stokes <vs@it.uu.se> - 2012-10-24 09:12 +0200

csiph-web