Skip to main content

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.

Metadata

Release files for npy-append-array 0.9.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for npy-append-array 0.9.19
File Size Uploaded
npy_append_array-0.9.19.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for npy-append-array 0.9.19
File Interpreter ABI Platform
npy_append_array-0.9.19-py3-none-any.whl Python 3 none any Details

Total release size: 22.4 kB

Release files / npy_append_array-0.9.19.tar.gz

Download URL npy_append_array-0.9.19.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7056e852cf597cca367d06b581068e99c18bb57499c524b4b2ffc7020f8f9477
BLAKE2b-256 checksum
How to use checksums
61fb7693333f5ce037a446ae2ed574b299c1cd4b64d320eb13afcd99b9ffc64c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release files / npy_append_array-0.9.19-py3-none-any.whl

Download URL npy_append_array-0.9.19-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c96838fd99cc14b99b323fc2fd7895ab58a5412a7398750e4a76bebbeafeb652
BLAKE2b-256 checksum
How to use checksums
ebf050da1b6c78e82546a58db20b64a340b26bd418559bba2009b6b9f62a524f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

0.9.19 This release

2 release files

0.9.17

2 release files

0.9.16

2 release files

0.9.15

2 release files

0.9.14

2 release files

0.9.13

2 release files

0.9.12

2 release files

0.9.11

2 release files

0.9.10

2 release files

0.9.9

2 release files

0.9.8

2 release files

0.9.7

2 release files

0.9.6

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page