Functions to efficiently rechunk multidimensional arrays
Project description
rechunkit
Functions to efficiently rechunk multidimensional arrays
Source Code: https://github.com/mullenkamp/rechunkit
Introduction
Rechunkit is a set of functions to allow efficient rechunking of multidimensional arrays that have been stored as chunks of numpy ndarrays. It allows for rechunking on-the-fly via python generators instead of requiring the user to save the full target array. It also contains several other handy tools for assisting the user as part of the rechunking process (e.g. estimating an optimal or ideal chunking size, iterating over chunks with a range-type function, etc).
Installation
pip install rechunkit
I can add it to conda-forge if there is demand.
Usage
Import the necessary modules and assign some parameters for the examples:
from rechunkit import guess_chunk_shape, chunk_range, calc_ideal_read_chunk_shape, calc_ideal_read_chunk_mem, calc_source_read_chunk_shape, calc_n_chunks, calc_n_reads_simple, calc_n_reads_rechunker, rechunker
source_shape = (31, 31, 31)
shape = source_shape
sel = (slice(3, 21), slice(11, 27), slice(7, 17))
source_chunk_shape = (5, 2, 4)
target_chunk_shape = (4, 5, 3)
max_mem = 2000 # smaller than the ideal chunk size
dtype = np.dtype('int32')
Preprocessing tools
We have defined our target_chunk_shape above, but rechunkit has a function to guess a good chunk shape given a user-defined amount of memory per chunk:
new_chunk_shape = guess_chunk_shape(source_shape, dtype, 400)
Chunks will be assigned to the highest composite number within the target_chunk_size. Using composite numbers will benefit the rehunking process as there is a very high likelihood that the least common multiple (LCM) of two composite numbers will be significantly lower than the product of those two numbers. The LCM is used to determine the ideal chunk size for the rechunking process.
Speaking of the ideal chunk size, we can determine the ideal chunk shape and size via a couple functions:
ideal_read_chunk_shape = calc_ideal_read_chunk_shape(source_chunk_shape, target_chunk_shape) # (20, 10, 12)
ideal_read_chunk_size = calc_ideal_read_chunk_mem(ideal_read_chunk_shape, dtype.itemsize) # 9600 bytes
If the ideal_read_chunk_size can comfortably fit in your memory, then you should use this value. Using the ideal chunk size will mean that you will only need to read all chunks in the source once. If the chunk size (called max_mem in the functions) is less than the ideal, then some chunks will need to be read multiple times.
To see how many reads are required if no optimization is performed during rechunking (i.e. every target chunk must iterate over every associated source chunk), you can use the calc_n_reads_simple function and compare it to the total number of chunks in the source:
n_chunks_source = calc_n_chunks(source_shape, source_chunk_shape) # 896
n_chunks_target = calc_n_chunks(source_shape, target_chunk_shape) # 616
n_reads_simple = calc_n_reads_simple(source_shape, source_chunk_shape, target_chunk_shape) # 3952
Using the simple brute force method requires one chunk to be read 4.4 times on average.
There's also a function to check the number of reads (and writes) using the optimized algorithm:
n_reads, n_writes = calc_n_reads_rechunker(source_shape, dtype, source_chunk_shape, target_chunk_shape, max_mem) # 2044, 616
In this case, we only require one chunk to be read 2.28 times on average. The more max_mem you give to the rechunker, the less reads per chunk is required (to a minium of 1 in the ideal case).
Rechunking
We need a source dataset to get data from. Rechunkit requires that the source input is a function/method that has a single parameter input of a tuple of slices. The slices contain the start and stop of the chunk to be read in the source.
For example, we can simply use a numpy array and it's __getitem__ method as the source:
source_data = np.arange(1, prod(source_shape) + 1, dtype=dtype).reshape(source_shape)
source = source_data.__getitem__
And again as a simple example, we can use a numpy array as the target:
target = np.zeros(source_shape, dtype=dtype)
We don't necessarily need the target as an array to be filled, because the rechunker function returns a generator that can be iterated over. The generator returns a tuple of slices (representing the target chunk) and the associated numpy array data:
for write_chunk, data in rechunker(source, source_shape, dtype, source_chunk_shape, target_chunk_shape, max_mem):
target[write_chunk] = data
assert np.all(source(()) == target) # Should pass!
Subsets of the source
There are many use-cases where you don't want the entire dataset. Rather you want a subset of the dataset, but you also want the subset rechunked. The rechunker function has a sel parameter which needs to be a tuple of slices of the number of dimensions.
n_reads, n_writes = calc_n_reads_rechunker(source_shape, dtype, source_chunk_shape, target_chunk_shape, max_mem, sel) # 288, 80
target = np.zeros(source_shape, dtype=dtype)[sel]
for write_chunk, data in rechunker(source, source_shape, dtype, source_chunk_shape, target_chunk_shape, max_mem, sel):
target[write_chunk] = data
assert np.all(source(sel) == target) # Should pass!
License
This project is licensed under the terms of the Apache Software License 2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rechunkit-0.1.2.tar.gz.
File metadata
- Download URL: rechunkit-0.1.2.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
770747bda12621932bffdf237f99de790390ed623d7c9305bae41622cf6d99d3
|
|
| MD5 |
17a4cfacb1713514aa4f9d6f471e5856
|
|
| BLAKE2b-256 |
aebb4bd8097782afa20e4b230e4dfc42cdbcf2c0a889c44a36a7a232e25cabd7
|
File details
Details for the file rechunkit-0.1.2-py3-none-any.whl.
File metadata
- Download URL: rechunkit-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b974ead70493acbeb0fcdff521a22976b04e97d4903d1b72ecad7998e9cac55
|
|
| MD5 |
2d6d0f3e6203b432a422b8974fe1b449
|
|
| BLAKE2b-256 |
fd32f8409e9e9c099b808b65d9e3a2518fe8df11dcf8ab5abf10c08be50f594e
|