Skip to main content

Create Numpy .npy files by appending on the growth axis

Project description

NpyAppendArray

Create Numpy .npy files by appending on the growth axis (0 for C order, -1 for Fortran order). It behaves like numpy.concatenate with the difference that the result is stored out-of-memory in a .npy file and can be reused for further appending. After creation, the file can then be read with memory mapping (e.g. by adding mmap_mode="r") which altogether allows to create and read files (optionally) larger than the machine's main memory.

Some possible applications:

  1. efficiently create large .npy (optionally database-like) files
    • Handling of offsets not included, can be done in an extra array
    • Large legacy files can be made appendable by calling ensure_appendable
      • can (optionally) be performed in-place to minimize disk space usage
  2. create binary log files (optionally on low-memory embedded devices)
    • Check the option rewrite_header_on_append=False for extra efficiency
    • Binary log files can be accessed very efficiently without parsing
    • Incomplete files can be recovered efficiently by calling recover

Another feature of this library is the (above mentioned) recover function, which makes incomplete .npy files readable by numpy.load again, no matter whether they should be appended to or not.

Incomplete files can be the result of broken downloads or unfinished writes. Recovery works by rewriting the header and inferring the growth axis (see above) by the file size. As the data length may not be evenly divisible by the non-append-axis shape, incomplete entries can either be ignored (zerofill_incomplete=False), which probably makes sense in most scenarios. Alternatively, to squeeze out the as much information from the file as possible, zerofill_incomplete=True can be used, which fills the incomplete last append axis item with zeros.

Raises ValueError instead of TypeError since version 0.9.14 to be more consistent with Numpy.

NpyAppendArray can be used in multithreaded environments.

Installation

conda install -c conda-forge npy-append-array

or

pip install npy-append-array

Usage

from npy_append_array import NpyAppendArray
import numpy as np

arr1 = np.array([[1,2],[3,4]])
arr2 = np.array([[1,2],[3,4],[5,6]])

filename = 'out.npy'

with NpyAppendArray(filename, delete_if_exists=True) as npaa:
    npaa.append(arr1)
    npaa.append(arr2)
    npaa.append(arr2)
    
data = np.load(filename, mmap_mode="r")

print(data)

Concurrency

Concurrency can be achieved by multithreading: A single NpyAppendArray object (per file) needs to be created. Then, append can be called from multiple threads and locks will ensure that file writes do not happen in parallel. When using with a with statement, make sure the join happens within it, compare test.py.

Multithreaded writes are not the pinnacle of what is technically possible with modern operating systems. It would be highly desirable to use async file writes. However, although modules like aiofile exist, this is currently not supported natively by Python or Numpy, compare

https://github.com/python/cpython/issues/76742

Implementation Details

NpyAppendArray contains a modified, partial version of format.py from the Numpy package. It ensures that array headers are created with 21 (=len(str(8*2**64-1))) bytes of spare space. This allows to fit an array of maxed out dimensions (for a 64 bit machine) without increasing the array header size. This allows to simply rewrite the header as we append data to the end of the .npy file.

Supported Systems

Tested with Ubuntu Linux, macOS and Windows.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

npy_append_array-0.9.19.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

npy_append_array-0.9.19-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file npy_append_array-0.9.19.tar.gz.

File metadata

  • Download URL: npy_append_array-0.9.19.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for npy_append_array-0.9.19.tar.gz
Algorithm Hash digest
SHA256 7056e852cf597cca367d06b581068e99c18bb57499c524b4b2ffc7020f8f9477
MD5 c4d73ef622e7d6a9b588699433bdd161
BLAKE2b-256 61fb7693333f5ce037a446ae2ed574b299c1cd4b64d320eb13afcd99b9ffc64c

See more details on using hashes here.

File details

Details for the file npy_append_array-0.9.19-py3-none-any.whl.

File metadata

File hashes

Hashes for npy_append_array-0.9.19-py3-none-any.whl
Algorithm Hash digest
SHA256 c96838fd99cc14b99b323fc2fd7895ab58a5412a7398750e4a76bebbeafeb652
MD5 8b91d8b225b827ef13ee6310d6a4bec1
BLAKE2b-256 ebf050da1b6c78e82546a58db20b64a340b26bd418559bba2009b6b9f62a524f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page