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

Uploaded Source

Built Distributions

fastremap-1.9.2-cp38-cp38-win_amd64.whl (333.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.8

fastremap-1.9.2-cp38-cp38-macosx_10_9_x86_64.whl (442.4 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

fastremap-1.9.2-cp37-cp37m-win_amd64.whl (328.0 kB view details)

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.7m

fastremap-1.9.2-cp37-cp37m-macosx_10_9_x86_64.whl (436.6 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

fastremap-1.9.2-cp36-cp36m-win_amd64.whl (327.5 kB view details)

Uploaded CPython 3.6m Windows x86-64

fastremap-1.9.2-cp36-cp36m-manylinux1_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.6m

fastremap-1.9.2-cp36-cp36m-macosx_10_9_x86_64.whl (449.4 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

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

Uploaded CPython 3.5m

fastremap-1.9.2-cp35-cp35m-macosx_10_6_intel.whl (797.6 kB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

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

Uploaded CPython 2.7m

fastremap-1.9.2-cp27-cp27m-macosx_10_14_intel.whl (416.6 kB view details)

Uploaded CPython 2.7m macOS 10.14+ intel

File details

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

File metadata

  • Download URL: fastremap-1.9.2.tar.gz
  • Upload date:
  • Size: 298.1 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.2.tar.gz
Algorithm Hash digest
SHA256 789d1764e6ed42ddec8150683f3f32c8d08b28ccd40c53dcd905d444efaa7ef8
MD5 8a59718922c9625d24f5f8c095173c48
BLAKE2b-256 81e8e4c775466c61e2974b8ba8e0533eb1d3f3e75c6969fca7c73b667981ade9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 333.9 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.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 82961551b82bf57bc2efe97db563be0462b6979b4f1203302a66d0815e2e1bab
MD5 1ca9a731b6f2a366cf6050cb564970dc
BLAKE2b-256 b57055f20849fe8dc179fed8302f09ff992b2b373f50bff8e9c99ba31f4acfcf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-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.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ddc54e47178cc5b3fac6723bfd446b0e56af836b315e22a892f96de72ca86718
MD5 25e06cf2f96e0abda23b2dc496d1d61f
BLAKE2b-256 0c2112d3f7f6127148da4898f798e974c71f3c2906e99fa442bf1280b016bb94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 442.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.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 65697c14e02a26798dc9db550bbfd8add2f3a31688877c2e8a9e1fc983dacea2
MD5 aa678c5fd5df1c3a82fa18bb88f49940
BLAKE2b-256 cb493d319de14b86e09ddcd91284664513db2d642bbae5d0a4dc4457c719ae89

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 328.0 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.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 352019a55b144f022711e11a085e43e9e36daecefaa4bacc4e397f97e56bc73d
MD5 964023d38507033dc94a03e76fea37ef
BLAKE2b-256 1601f0e9ab170957f9ad295aabfdb76f644347983e7a69b904c30be52efe73a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-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.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 02c7a72a18396c8178911bde48dd8b5d55f48e5953eb0051bc79a5dfc88b4a03
MD5 4208ee3f81ccaafdf121bae3e308d5e1
BLAKE2b-256 444af7f2b1478decd6f124fa0a181f1fef42014ad635c65dd38a38a8ba66bc9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 436.6 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.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b29ce049f2c34ad7e378b9ca5c8e4c450afd69b5cea708320745cd221b7db704
MD5 79fa074006c9d8c78e105313dc43209c
BLAKE2b-256 7ef89c7c0232e12a61e66e0f43ce03aac49b0329f862ab842d4d6a0e1730e5f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 327.5 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.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 3d0238b12374c4c29bbfed0d9ec67bb8a18114769b0ef2a23f0c60a27673af55
MD5 639303afa5747c658c1bd0d6a16e7a8f
BLAKE2b-256 be6c2177426994f4a2a860c07b87673e27ff3f5f55ed6e6bd268994eeae8be9d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.1 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.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7c926bcb822096792dc5d76513b99346368afdc2b8557bbe511f29a178a3f762
MD5 8b8cc4585936fd0b79968d37591d8f70
BLAKE2b-256 154e7727b165e4036a91b6b27b2ef6e25b04625d4281084db5f2298ff979a0bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 449.4 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.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e6853a5cbeb8346daccc1573d60ffcca1d468b5db1358a58667f47cf509153f0
MD5 5f7fe637b786fc04ec1aec5d3d259353
BLAKE2b-256 72993cf5f098321c3f4c7e574ec327d9354702596ac7e996b5f6981fab7a3259

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-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.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ed14f172f29ab86ca1873240c97e5c5eb426b0642644d54164aa7802d389aa08
MD5 3017e4f4f8b7925d38a091fa5a12967a
BLAKE2b-256 9f3db3ad45065bc3a56994f02c250d4323c51fce46da8cf778729eb0a2c29236

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp35-cp35m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 797.6 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.2-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 4c65267f2cad07d54a411317c71d7ac002f8ee7fdef88c0f63ee369faaac3e6c
MD5 6406fd51e0e2ab860bd35f67a9efd01f
BLAKE2b-256 897c1372b384ae7de1aae2b92591b666435925268c5abd45c937aa2ecab5de7f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-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.2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fccb2747fd2f9ed02a2e6b6a6cb19a375ec0ed08bbec024b5cd8c8883d9ea5fa
MD5 21f84d954e59079dc44f2221a0088ef7
BLAKE2b-256 d37ae1cdf36d33840571e8609cb467313b50560ddad191999e7215301bd04439

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastremap-1.9.2-cp27-cp27m-macosx_10_14_intel.whl
  • Upload date:
  • Size: 416.6 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.2-cp27-cp27m-macosx_10_14_intel.whl
Algorithm Hash digest
SHA256 a1262f88e5140586408b677de145e8df454e94cfc29a1d01942a4caae08db339
MD5 dafb917bf7f013d819e3e9939e12f096
BLAKE2b-256 2cfc8e12bd126547b7d4faf51a9e41a616f311579128bb365836652cc4cd10bc

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