2

I don't have access to a PC for the next couple of days but I can't get this problem off my mind. I am just playing around with compression algorithms, created one of my own for audio and I am stuck at the output to file step. So here are my questions, hopefully I can figure out an answer before I am back or else this will eat my mind.

1) If I have a numpy X array with some integers (say int16), if I open a file object and do file.write(X) what will the file output be like? Numbers? Or ASCII of numbers? Or binary?

2) Depending on the above answer, how do I read this file into a numpy array X?

Essentially my compression does some wavelet and fft transforms, does some filtering here and there and returns an array with some numbers, I know the format of this array and I already achieve a high % of compression here, the next step is to first dump this array into a binary file. Once I achieve this my next goal is to implement some kind of entropy coding of the file/vector.

Any input appreciated.

Lee Taylor
7,99416 gold badges38 silver badges53 bronze badges
asked Nov 4, 2012 at 17:12

1 Answer 1

4

1) Writing:

In [1]: f = open('ints','wb')
In [2]: x = numpy.int16(array([1,2,3]))
Out[2]: array([1, 2, 3], dtype=int16)
In [3]: f.write(x)
In [4]: f.close()

2) Reading:

In [5]: f = open('ints','wb')
In [6]: x = f.read()
In [7]: x
Out[7]: '\x01\x00\x02\x00\x03\x00'
In [8]: numpy.fromstring(x, dtype=np.uint16, count=3)
Out[8]: array([1, 2, 3], dtype=uint16)

Update:

As J.F.Sebastian suggested there are better ways to do this, like using:

or as Janne Karila suggested using:

answered Nov 4, 2012 at 17:25
Sign up to request clarification or add additional context in comments.

4 Comments

there are numpy.save, .savez, etc that store metadata in the file
@J.F.Sebastian yes, but the question was referring to file.write(X), nevertheless good advice
Thank you, this seems to be working. If anyone manages to read this comment, when I do f=open(...), do I keep the entire content of the file in memory pointed at f? Or do I only link it to some file object and (I suspect) stuff won't be loaded into memory untill I actually do something like "for line in f: ...."?
File content is loaded only when you call f.read() and assign its output (object) to some target (name). If you iterate with for line in f only one segment of file (line) is loaded at a time.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.