Remap and renumber numpy arrays.
Project description
[![Build Status](https://travis-ci.org/seung-lab/fastremap.svg?branch=master)](https://travis-ci.org/seung-lab/fastremap) [![PyPI version](https://badge.fury.io/py/fastremap.svg)](https://badge.fury.io/py/fastremap)
# fastremap
Renumber and relabel Numpy arrays at C++ speed and physically convert rectangular Numpy arrays between C and Fortran order using an in-place transposition.
## `pip` Installation
```bash
pip install fastremap
```
*If not, a C++ compiler is required.*
```bash
pip install numpy
pip install fastremap --no-binary :all:
```
## Manual Installation
*A C++ compiler is required.*
```bash
sudo apt-get install g++ python3-dev
mkvirtualenv -p python3 fastremap
pip install numpy
# Choose one:
python setup.py develop
python setup.py install
```
## The Problem of Remapping
Python loops are slow, so Numpy is often used to perform remapping on large arrays (hundreds of megabytes or gigabytes). In order to efficiently remap an array in Numpy you need a key-value array where the index is the key and the value is the contents of that index.
```python
import numpy as np
original = np.array([ 1, 3, 5, 5, 10 ])
remap = np.array([ 0, -5, 0, 6, 0, 0, 2, 0, 0, 0, -100 ])
# Keys: 0 1 2 3 4 5 6 7 8 9 10
remapped = remap[ original ]
>>> [ -5, 6, 2, 2, -100 ]
```
If there are 32 or 64 bit labels in the array, this becomes impractical as the size of the array can grow larger than RAM. Therefore, it would be helpful to be able to perform this mapping using a C speed loop. Numba can be used for this in some circumstances. However, this library provides an alternative.
```python
import numpy as np
import fastremap
mappings = {
1: 100,
2: 200,
-3: 7,
}
arr = np.array([5, 1, 2, -5, -3, 10, 6])
# Custom remapping of -3, 5, and 6 leaving the rest alone
arr = fastremap.remap(arr, mappings, preserve_missing_labels=True)
# result: [ 5, 100, 200, -5, 7, 10, 6 ]
```
## The Problem of Renumbering
Sometimes a 64-bit array contains values that could be represented by an 8-bit array. However, similarly to the remapping problem, Python loops can be too slow to do this. Numpy doesn't provide a convenient way to do it either. Therefore this library provides an alternative solution.
```python
import fastremap
import numpy as np
arr = np.array([ 283732875, 439238823, 283732875, 182812404, 0 ], dtype=np.int64)
arr, remapping = fastremap.renumber(arr, preserve_zero=True) # Returns uint8 array
>>> arr = [ 1, 2, 1, 3, 0 ]
>>> remapping = { 0: 0, 283732875: 1, 439238823: 2, 182812404: 3 }
arr, remapping = fastremap.renumber(arr, preserve_zero=False) # Returns uint8 array
>>> arr = [ 1, 2, 1, 3, 4 ]
>>> remapping = { 0: 4, 283732875: 1, 439238823: 2, 182812404: 3 }
arr, remapping = fastremap.renumber(arr, preserve_zero=False, in_place=True) # Mutate arr to use less memory
>>> arr = [ 1, 2, 1, 3, 4 ]
>>> remapping = { 0: 4, 283732875: 1, 439238823: 2, 182812404: 3 }
```
## The Problem of In-Place Transposition
When transitioning between different media, e.g. CPU to GPU, CPU to Network, CPU to disk, it's often necessary to physically transpose multi-dimensional arrays to reformat as C or Fortran order. Tranposing matrices is also a common action in linear algebra, but often you can get away with just changing the strides.
An out-of-place transposition is easy to write, and often faster, but it will spike peak memory consumption. This library grants the user the option of performing an in-place transposition which trades CPU time for peak memory usage. In the special case of square or cubic arrays, the in-place transpisition is both lower memory and faster.
- **fastremap.asfortranarray:** Same as np.asfortranarray but will perform the transposition in-place for 1, 2, 3, and 4D arrays. 2D and 3D square matrices are faster to process than with Numpy.
- **fastremap.ascontiguousarray:** Same as np.ascontiguousarray but will perform the transposition in-place for 1, 2, 3, and 4D arrays. 2D and 3D square matrices are faster to process than with Numpy.
```python
import fastremap
import numpy as np
arr = np.ones((512,512,512), dtype=np.float32)
arr = fastremap.asfortranarray(x)
arr = np.ones((512,512,512), dtype=np.float32, order='F')
arr = fastremap.ascontiguousarray(x)
```
## All Available Functions:
- **renumber:** Relabel array from 1 to N which can often use smaller datatypes.
- **remap:** Custom relabeling of values in an array from a dictionary.
- **mask:** Zero out labels in an array specified by a given list.
- **remap_from_array:** Same as remap, but the map is an array where the key is the array index and the value is the value.
- **remap_from_array_kv:** Same as remap, but the map consists of two equal sized arrays, the first containing keys, the second containing values.
- **asfortranarray:** Perform an in-place matrix transposition for rectangular arrays if memory is contiguous, standard numpy otherwise.
- **ascontiguousarray:** Perform an in-place matrix transposition for rectangular arrays if memory is contiguous, standard numpy algorithm otherwise.
## C++ Usage
The in-place matrix transposition is implemented in ipt.hpp. If you're working in C++, you can also use it directly like so:
```cpp
#include "ipt.hpp"
int main() {
int sx = 128;
int sy = 124;
int sz = 103;
int sw = 3;
auto* arr = ....;
// All primitive number types supported
// The array will be modified in place,
// so these functions are void type.
ipt::ipt<int>(arr, sx, sy); // 2D
ipt::ipt<float>(arr, sx, sy, sz); // 3D
ipt::ipt<double>(arr, sx, sy, sz, sw); // 4D
return 0;
}
```
--
Made with <3
# fastremap
Renumber and relabel Numpy arrays at C++ speed and physically convert rectangular Numpy arrays between C and Fortran order using an in-place transposition.
## `pip` Installation
```bash
pip install fastremap
```
*If not, a C++ compiler is required.*
```bash
pip install numpy
pip install fastremap --no-binary :all:
```
## Manual Installation
*A C++ compiler is required.*
```bash
sudo apt-get install g++ python3-dev
mkvirtualenv -p python3 fastremap
pip install numpy
# Choose one:
python setup.py develop
python setup.py install
```
## The Problem of Remapping
Python loops are slow, so Numpy is often used to perform remapping on large arrays (hundreds of megabytes or gigabytes). In order to efficiently remap an array in Numpy you need a key-value array where the index is the key and the value is the contents of that index.
```python
import numpy as np
original = np.array([ 1, 3, 5, 5, 10 ])
remap = np.array([ 0, -5, 0, 6, 0, 0, 2, 0, 0, 0, -100 ])
# Keys: 0 1 2 3 4 5 6 7 8 9 10
remapped = remap[ original ]
>>> [ -5, 6, 2, 2, -100 ]
```
If there are 32 or 64 bit labels in the array, this becomes impractical as the size of the array can grow larger than RAM. Therefore, it would be helpful to be able to perform this mapping using a C speed loop. Numba can be used for this in some circumstances. However, this library provides an alternative.
```python
import numpy as np
import fastremap
mappings = {
1: 100,
2: 200,
-3: 7,
}
arr = np.array([5, 1, 2, -5, -3, 10, 6])
# Custom remapping of -3, 5, and 6 leaving the rest alone
arr = fastremap.remap(arr, mappings, preserve_missing_labels=True)
# result: [ 5, 100, 200, -5, 7, 10, 6 ]
```
## The Problem of Renumbering
Sometimes a 64-bit array contains values that could be represented by an 8-bit array. However, similarly to the remapping problem, Python loops can be too slow to do this. Numpy doesn't provide a convenient way to do it either. Therefore this library provides an alternative solution.
```python
import fastremap
import numpy as np
arr = np.array([ 283732875, 439238823, 283732875, 182812404, 0 ], dtype=np.int64)
arr, remapping = fastremap.renumber(arr, preserve_zero=True) # Returns uint8 array
>>> arr = [ 1, 2, 1, 3, 0 ]
>>> remapping = { 0: 0, 283732875: 1, 439238823: 2, 182812404: 3 }
arr, remapping = fastremap.renumber(arr, preserve_zero=False) # Returns uint8 array
>>> arr = [ 1, 2, 1, 3, 4 ]
>>> remapping = { 0: 4, 283732875: 1, 439238823: 2, 182812404: 3 }
arr, remapping = fastremap.renumber(arr, preserve_zero=False, in_place=True) # Mutate arr to use less memory
>>> arr = [ 1, 2, 1, 3, 4 ]
>>> remapping = { 0: 4, 283732875: 1, 439238823: 2, 182812404: 3 }
```
## The Problem of In-Place Transposition
When transitioning between different media, e.g. CPU to GPU, CPU to Network, CPU to disk, it's often necessary to physically transpose multi-dimensional arrays to reformat as C or Fortran order. Tranposing matrices is also a common action in linear algebra, but often you can get away with just changing the strides.
An out-of-place transposition is easy to write, and often faster, but it will spike peak memory consumption. This library grants the user the option of performing an in-place transposition which trades CPU time for peak memory usage. In the special case of square or cubic arrays, the in-place transpisition is both lower memory and faster.
- **fastremap.asfortranarray:** Same as np.asfortranarray but will perform the transposition in-place for 1, 2, 3, and 4D arrays. 2D and 3D square matrices are faster to process than with Numpy.
- **fastremap.ascontiguousarray:** Same as np.ascontiguousarray but will perform the transposition in-place for 1, 2, 3, and 4D arrays. 2D and 3D square matrices are faster to process than with Numpy.
```python
import fastremap
import numpy as np
arr = np.ones((512,512,512), dtype=np.float32)
arr = fastremap.asfortranarray(x)
arr = np.ones((512,512,512), dtype=np.float32, order='F')
arr = fastremap.ascontiguousarray(x)
```
## All Available Functions:
- **renumber:** Relabel array from 1 to N which can often use smaller datatypes.
- **remap:** Custom relabeling of values in an array from a dictionary.
- **mask:** Zero out labels in an array specified by a given list.
- **remap_from_array:** Same as remap, but the map is an array where the key is the array index and the value is the value.
- **remap_from_array_kv:** Same as remap, but the map consists of two equal sized arrays, the first containing keys, the second containing values.
- **asfortranarray:** Perform an in-place matrix transposition for rectangular arrays if memory is contiguous, standard numpy otherwise.
- **ascontiguousarray:** Perform an in-place matrix transposition for rectangular arrays if memory is contiguous, standard numpy algorithm otherwise.
## C++ Usage
The in-place matrix transposition is implemented in ipt.hpp. If you're working in C++, you can also use it directly like so:
```cpp
#include "ipt.hpp"
int main() {
int sx = 128;
int sy = 124;
int sz = 103;
int sw = 3;
auto* arr = ....;
// All primitive number types supported
// The array will be modified in place,
// so these functions are void type.
ipt::ipt<int>(arr, sx, sy); // 2D
ipt::ipt<float>(arr, sx, sy, sz); // 3D
ipt::ipt<double>(arr, sx, sy, sz, sw); // 4D
return 0;
}
```
--
Made with <3
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
fastremap-1.5.1.tar.gz
(227.0 kB
view details)
Built Distributions
File details
Details for the file fastremap-1.5.1.tar.gz
.
File metadata
- Download URL: fastremap-1.5.1.tar.gz
- Upload date:
- Size: 227.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9d972359056378ede395b799d0a922b6d3dcf02b90ed512589aed0c27326376 |
|
MD5 | 43c8484aa677f166b9c816287ce4c406 |
|
BLAKE2b-256 | 55eb0a1eb3135cb3b7dce335896cc81562cdd60f033301877a404ba7117779e8 |
File details
Details for the file fastremap-1.5.1-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: fastremap-1.5.1-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d12de5d232dd8f2520a5fc4f0aa4bee27d29e45548d8c024784071011f31acc |
|
MD5 | dcfe70155f4fa732f8a274c7ed33e733 |
|
BLAKE2b-256 | c8d143e24283c7c7e76857b8bcc58e1bce82dbdd2b781eba504105812b24ed60 |
File details
Details for the file fastremap-1.5.1-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: fastremap-1.5.1-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 200cefc3cd8b5632a6d7da3fc9a688c4f59c5498fb8cc80ef493dc918e414abe |
|
MD5 | adb69b4ddf27dd4d56e39e8706ab213c |
|
BLAKE2b-256 | 3d39857af8aa4d2f3ba433040668542fe27b9726184a90f384bcb04fe05bc692 |
File details
Details for the file fastremap-1.5.1-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: fastremap-1.5.1-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c09ac45023c5a4b13f389105c9c795e68584d87607adb6c1fe99e36bf4fb4c26 |
|
MD5 | 36413e3355d66963038068e5f71fbe17 |
|
BLAKE2b-256 | 45bcc6f0c9d0837993d78b4053a5d684349b97cd900c56dd577ee260082e0729 |
File details
Details for the file fastremap-1.5.1-cp27-cp27m-manylinux1_x86_64.whl
.
File metadata
- Download URL: fastremap-1.5.1-cp27-cp27m-manylinux1_x86_64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 2.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73b516f0bf134d56886c9fdc4e8959c2ac079654098e890a733c6f213b95dc9a |
|
MD5 | 97025df8c20b493b19d8c83218a01606 |
|
BLAKE2b-256 | dc03df42e734b2b08edf3611cdd8501191886763cef3fe1cff76860376665130 |