Skip to main content

Remap, mask, renumber, and in-place transpose numpy arrays.

Project description

Build Status PyPI version

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.

import fastremap

uniq, cts = fastremap.unique(labels, return_counts=True) # may be much faster than np.unique
labels = fastremap.renumber(labels, in_place=True) # relabel values from 1 and refit data type

labels = fastremap.refit(labels) # resize the data type of the array to fit extrema
labels = fastremap.refit(labels, value=-35) # resize the data type to fit the value provided

# remap all occurances of 1 -> 2
labels = fastremap.remap(labels, { 1: 2 }, preserve_missing_labels=True, in_place=True)

labels = fastremap.mask(labels, [1,5,13]) # set all occurances of 1,5,13 to 0
labels = fastremap.mask_except(labels, [1,5,13]) # set all labels except 1,5,13 to 0

fastremap.transpose(labels) # physically transpose labels in-place
fastremap.ascontiguousarray(labels) # try to perform a physical in-place transposition to C order
fastremap.asfortranarray(labels) # try to perform a physical in-place transposition to F order

minval, maxval = fastremap.minmax(labels) # faster version of (np.min(labels), np.max(labels))

# computes number of matching adjacent pixel pairs in an image
num_pairs = fastremap.pixel_pairs(labels) 

All Available Functions

  • unique: Faster implementation of np.unique.
  • renumber: Relabel array from 1 to N which can often use smaller datatypes.
  • remap: Custom relabeling of values in an array from a dictionary.
  • refit: Resize the data type of an array to the smallest that can contain the most extreme values in it.
  • mask: Zero out labels in an array specified by a given list.
  • mask_except: Zero out all labels except those specified in 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.
  • minmax: Compute the min and max of an array in one pass.
  • pixel_pairs: Computes the number of adjacent matching memory locations in an image. A quick heuristic for understanding if the image statistics are roughly similar to a connectomics segmentation.

pip Installation

pip install fastremap

If not, a C++ compiler is required.

pip install numpy
pip install fastremap --no-binary :all:

Manual Installation

A C++ compiler is required.

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.

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.

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.

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.
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)

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:

#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


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.9.1.tar.gz (296.9 kB view details)

Uploaded Source

Built Distributions

fastremap-1.9.1-cp38-cp38-win_amd64.whl (332.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

fastremap-1.9.1-cp38-cp38-manylinux1_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.8

fastremap-1.9.1-cp38-cp38-macosx_10_9_x86_64.whl (438.4 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fastremap-1.9.1-cp37-cp37m-win_amd64.whl (325.2 kB view details)

Uploaded CPython 3.7m Windows x86-64

fastremap-1.9.1-cp37-cp37m-manylinux1_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.7m

fastremap-1.9.1-cp37-cp37m-macosx_10_9_x86_64.whl (430.5 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

fastremap-1.9.1-cp36-cp36m-win_amd64.whl (324.9 kB view details)

Uploaded CPython 3.6m Windows x86-64

fastremap-1.9.1-cp36-cp36m-manylinux1_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.6m

fastremap-1.9.1-cp36-cp36m-macosx_10_9_x86_64.whl (443.3 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

fastremap-1.9.1-cp35-cp35m-manylinux1_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.5m

fastremap-1.9.1-cp35-cp35m-macosx_10_6_intel.whl (793.0 kB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

fastremap-1.9.1-cp27-cp27m-manylinux1_x86_64.whl (1.8 MB view details)

Uploaded CPython 2.7m

fastremap-1.9.1-cp27-cp27m-macosx_10_14_intel.whl (414.5 kB view details)

Uploaded CPython 2.7m macOS 10.14+ intel

File details

Details for the file fastremap-1.9.1.tar.gz.

File metadata

  • Download URL: fastremap-1.9.1.tar.gz
  • Upload date:
  • Size: 296.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for fastremap-1.9.1.tar.gz
Algorithm Hash digest
SHA256 8ca45c8bc0c938012533f34553f60affe6439d19cc0b8a2652c83d04ad018735
MD5 907ca7681f6ce8a0524296b2d6c07ddb
BLAKE2b-256 5645236266ccf4bedc3e588fc9ce45ea573ae51f924560f423013e9836f041ec

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 332.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.4

File hashes

Hashes for fastremap-1.9.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d7f17cc21781a33f80c24ce5a2a00201984987f2aeae4a20af3ca53bd9096b5d
MD5 5ee45d4aae02c56b09262d08a6c90cee
BLAKE2b-256 9167553abe86908dd12a393b57b9cc4123b93ff407301877b684afef76c40c74

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.1 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for fastremap-1.9.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a2c88e3a02e9fe24c60227f49f99be7061cfde05011057a1e4c33bc4789ccddd
MD5 522a6a32a6d6b034bb98e4678b65e68c
BLAKE2b-256 d01a75a6de3630c3ba3cff4d5c6c25f6bf8c831015e7265af817b3b55b9eb71a

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 438.4 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for fastremap-1.9.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 695f3cd03cb993c44b5ed083ce221814c68bc73a2c4846eef4fa2d3d1cd4319e
MD5 661daa3ad6efa501714e4bc7b003bd13
BLAKE2b-256 7363da6252928d49283a12e19e0803cf795b77b87951be4740becb906615d5b2

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 325.2 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.4

File hashes

Hashes for fastremap-1.9.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 bc7f4082581dec877e924f538b154fe8740bf9bc86fdf74f6e2748ae54df6870
MD5 741e4e70923166dd4380be6c53bee050
BLAKE2b-256 dc3c6abf748e594d440c26e74f9a57922763b6e0c078828f5ceff96d211b5e89

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for fastremap-1.9.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 47616a19892c0f87579b8784211f8441f432b1aa3922df4b0ef6f863edd9da06
MD5 313612c99b7892659197b8137436a47a
BLAKE2b-256 0813a7238453af0bac4bd471b624e19e9d6a4e74aaf9c719db9bffdfcedd3d33

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 430.5 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for fastremap-1.9.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 679d90a6915aa6c976a0da5f472ab56349379ad7688fb0f2b1d5cdc8f9aa447f
MD5 d8221fa3bd85df1edfe2f679e75f1645
BLAKE2b-256 c568d1c509286120f5a64cd9737af7d0f6bba01cf3af6fd75ef765edeb597553

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 324.9 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.4

File hashes

Hashes for fastremap-1.9.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 81d533004314dba3a38f43bdb5fb2e8979d5922fe0538388a3c358ecd4625f34
MD5 995a0ac8c5c181dd3b5ec0303f408ed2
BLAKE2b-256 4bf98dc63f6fa48ff1421be7c00ccb3053d6d57fcb7aa2d23f297c5ac7a7d310

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for fastremap-1.9.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0335022fc0dd4ec09ca8673a0111a8380743627dae104b3f0022a3aaf65024e1
MD5 bb5aed6079d2ba76ed3bf4f90b75416c
BLAKE2b-256 e84da5ec7f2c591dff631411666e60b944c8b3da71a2fadb4b050dc004088aef

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 443.3 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for fastremap-1.9.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7c2280a2e2f0f4948cc7a2d40d514316731eecb141ea8067ab5882d3d47e98dc
MD5 c316a35f0bcbb5595cb5b5610b28f315
BLAKE2b-256 1ffe784d8d6a41e737ebdb04d3e9232700969853f4cb2aa80eb199264651e93a

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for fastremap-1.9.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 407cd5420326650ffa653282ecd9e8af4da4508471ee5fbbedd585020bc68035
MD5 86d5f80bc5f8a0c9ddd547e246c37f5c
BLAKE2b-256 6fdf5f549fdec718cb731363255998539f0f446f3bd0bfbb51177a27c00b20a8

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp35-cp35m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 793.0 kB
  • Tags: CPython 3.5m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for fastremap-1.9.1-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 0dfba4ed9fd49f8f6487beec96e0156633115c1244541b62c34cd66ea112eca9
MD5 1a96e80842265d1b1d7538e4f188fe35
BLAKE2b-256 518980d00deffef737bf2fdb104636601eccd537f3ceff3247e7e173dfe99b71

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 2.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for fastremap-1.9.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff708314feda9900fa06905c2728d2f6cdefe21bab044da0eeb988e271264a36
MD5 48d441db6edec1e899e69c3e17166ea4
BLAKE2b-256 01e7b5f4881b0e6d16cc3197b6efc66562f927e04f7af19ad41e7e320c201792

See more details on using hashes here.

File details

Details for the file fastremap-1.9.1-cp27-cp27m-macosx_10_14_intel.whl.

File metadata

  • Download URL: fastremap-1.9.1-cp27-cp27m-macosx_10_14_intel.whl
  • Upload date:
  • Size: 414.5 kB
  • Tags: CPython 2.7m, macOS 10.14+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for fastremap-1.9.1-cp27-cp27m-macosx_10_14_intel.whl
Algorithm Hash digest
SHA256 a29c8c4880dc9a3c7054369e3bf5f24f515a8bc298cc2811a8dd91f17088d345
MD5 3aad50cbdd8f09b37e005387644e8d13
BLAKE2b-256 de4e0f4b2c324eb2356fa910b6ceb2460836f4855ed98032c179ae7b2dbfff58

See more details on using hashes here.

Supported by

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