2

I saved my numpy array to a binary file as np.save("image_mean.npy", averaged_mean). When I open the file, observed the bianry file's header as “NUMPY V {'descr': '<f8', 'fortran_order': False, 'shape': (3L, 704L, 1248L), }.

My query is what NUMPY V refers to? If I want it as NUMPY F “NUMPY F {'descr': '<f8', 'fortran_order': False, 'shape': (3L, 704L, 1248L), }, how can I change in np.save API?

3
  • Why do you want F? What do you think that will change in the file format? I think that the V is referring to version myself. Here is the documentation on .npy Commented Sep 22, 2015 at 16:44
  • If you're wanting to save a numpy array to a "raw" binary file, don't use np.save. numpy.save will save the file in "npy" format, which is not a raw binary file (thus the header). Instead, it has a header, etc. Instead, use your_array.tofile(filename). To change the order to a fortran ordered array, you'd use your_array.ravel(order='F').tofile(filename). Commented Sep 22, 2015 at 16:50
  • documentation on .npy format is now at scipy.org numpy.lib.format Commented Oct 20, 2019 at 22:44

1 Answer 1

4

V is the length of the header data (include space paddings and the terminating newline).

As given in the documentation -

  1. The first 6 bytes are a magic string: exactly “x93NUMPY”.

  2. The next 1 byte is an unsigned byte: the major version number of the file format, e.g. x01.

  3. The next 1 byte is an unsigned byte: the minor version number of the file format, e.g. x00. Note: the version of the file format is not tied to the version of the numpy package.

  4. The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN.

  5. The next HEADER_LEN bytes form the header data describing the array’s format. It is an ASCII string which contains a Python literal expression of a dictionary. It is terminated by a newline (‘n’) and padded with spaces (‘x20’) to make the total length of the magic string + 4 + HEADER_LEN be evenly divisible by 16 for alignment purposes.

The length of the header data from your example (including a single newline) comes to 71 . So that makes magic_string + 4 + HEADER_LEN equal 81 , which is not divisible by 16 , so the next divisible number is 96 , hence the header data is padded with 15 spaces so that that total length becomes equal to 96 . This makes the header length to be - 86 . Which is V .

>>> chr(86)
'V'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.