nparraymapper 0.10
Mapping values in NumPy arrays (any shape!) with high speed - Cython -
License: MIT
Keywords: rgb,Cython


# Mapping values in NumPy arrays (any shape!) with high speed - Cython -

## pip install nparraymapper

### Tested against Python 3.11 / Windows 10

## Cython (and a C/C++ compiler) must be installed to use the optimized Cython implementation.

This module provides functions for mapping values in NumPy arrays based on a specified mapping dictionary. It works on any shape and with almost all dtypes (OBJECT NOT!!!)

It always returns a copy! The original data doesn't change!

```python
import numpy as np

from nparraymapper import map_numpy_array, map_array_with_strings

np.random.seed(0)
a = np.random.randint(1, 9, (10000, 10000, 4))
ds = map_numpy_array(a, mapdict={4: 400000, 3: 1021, 2: -1}, keepnotmapped=True)
print(a)
print(ds)
print('----------------------------')
a = np.random.randint(2, 9, (100, 100, 3), dtype=np.uint32)
ds = map_numpy_array(a, mapdict={4: 4000.1232, 3: 1021.32}, keepnotmapped=True)
print(a)
print(ds)
print('----------------------------')
a = np.random.randint(2, 9, (1000, 100, 8)).astype(np.float32)

ds = map_numpy_array(a, mapdict={4: 40, 3: 1021}, keepnotmapped=False)

print(a)
print(ds)
print('----------------------------')

subsdi = {110: 'babça', 14: 'bsdfvd', 9: 'çbba'}
src = np.random.randint(1, 12, (1000, 1000, 3))
print(src)
out = map_array_with_strings(src, mapdict=subsdi)
print(out)

# [[[5 8 6 1]
#   [4 4 4 8]
#   [2 4 6 3]
#   ...
#   [6 4 4 4]
#   [2 5 8 2]
#   [3 7 5 5]]
#  [[6 1 4 1]
#   [1 2 5 8]
#   [1 7 6 5]
#   ...
#   [6 7 7 1]
#   [5 5 4 7]
#   [5 8 7 6]]
#  [[1 2 8 5]
#   [2 2 8 6]
#   [1 3 4 4]
#   ...
#   [3 4 3 2]
#   [4 6 8 5]
#   [4 7 6 4]]
#  ...
#  [[2 7 5 5]
#   [2 2 8 8]
#   [8 8 7 8]
#   ...
#   [8 7 5 8]
#   [4 6 5 8]
#   [3 8 5 8]]
#  [[6 4 1 5]
#   [1 7 7 1]
#   [2 4 5 3]
#   ...
#   [1 5 5 4]
#   [1 8 6 7]
#   [4 5 8 4]]
#  [[4 2 3 5]
#   [5 7 8 2]
#   [1 3 4 6]
#   ...
#   [3 8 1 5]
#   [7 8 4 3]
#   [6 8 3 4]]]
# [[[     5      8      6      1]
#   [400000 400000 400000      8]
#   [    -1 400000      6   1021]
#   ...
#   [     6 400000 400000 400000]
#   [    -1      5      8     -1]
#   [  1021      7      5      5]]
#  [[     6      1 400000      1]
#   [     1     -1      5      8]
#   [     1      7      6      5]
#   ...
#   [     6      7      7      1]
#   [     5      5 400000      7]
#   [     5      8      7      6]]
#  [[     1     -1      8      5]
#   [    -1     -1      8      6]
#   [     1   1021 400000 400000]
#   ...
#   [  1021 400000   1021     -1]
#   [400000      6      8      5]
#   [400000      7      6 400000]]
#  ...
#  [[    -1      7      5      5]
#   [    -1     -1      8      8]
#   [     8      8      7      8]
#   ...
#   [     8      7      5      8]
#   [400000      6      5      8]
#   [  1021      8      5      8]]
#  [[     6 400000      1      5]
#   [     1      7      7      1]
#   [    -1 400000      5   1021]
#   ...
#   [     1      5      5 400000]
#   [     1      8      6      7]
#   [400000      5      8 400000]]
#  [[400000     -1   1021      5]
#   [     5      7      8     -1]
#   [     1   1021 400000      6]
#   ...
#   [  1021      8      1      5]
#   [     7      8 400000   1021]
#   [     6      8   1021 400000]]]
# ----------------------------
# [[[2 8 2]
#   [3 4 3]
#   [2 7 7]
#   ...
#   [4 6 4]
#   [3 7 5]
#   [7 7 8]]
#  [[4 4 4]
#   [2 7 4]
#   [2 7 2]
#   ...
#   [2 7 8]
#   [4 6 8]
#   [8 8 7]]
#  [[6 5 3]
#   [5 5 5]
#   [8 6 3]
#   ...
#   [4 3 6]
#   [4 6 7]
#   [2 8 7]]
#  ...
#  [[3 3 4]
#   [8 4 4]
#   [5 5 2]
#   ...
#   [4 2 5]
#   [7 3 7]
#   [6 5 2]]
#  [[3 3 2]
#   [4 6 5]
#   [8 8 4]
#   ...
#   [7 7 5]
#   [6 6 2]
#   [7 5 4]]
#  [[5 8 2]
#   [8 5 4]
#   [6 7 6]
#   ...
#   [4 8 6]
#   [2 8 3]
#   [2 8 4]]]
# [[[2.0000000e+00 8.0000000e+00 2.0000000e+00]
#   [1.0213200e+03 4.0001232e+03 1.0213200e+03]
#   [2.0000000e+00 7.0000000e+00 7.0000000e+00]
#   ...
#   [4.0001232e+03 6.0000000e+00 4.0001232e+03]
#   [1.0213200e+03 7.0000000e+00 5.0000000e+00]
#   [7.0000000e+00 7.0000000e+00 8.0000000e+00]]
#  [[4.0001232e+03 4.0001232e+03 4.0001232e+03]
#   [2.0000000e+00 7.0000000e+00 4.0001232e+03]
#   [2.0000000e+00 7.0000000e+00 2.0000000e+00]
#   ...
#   [2.0000000e+00 7.0000000e+00 8.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 8.0000000e+00]
#   [8.0000000e+00 8.0000000e+00 7.0000000e+00]]
#  [[6.0000000e+00 5.0000000e+00 1.0213200e+03]
#   [5.0000000e+00 5.0000000e+00 5.0000000e+00]
#   [8.0000000e+00 6.0000000e+00 1.0213200e+03]
#   ...
#   [4.0001232e+03 1.0213200e+03 6.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 7.0000000e+00]
#   [2.0000000e+00 8.0000000e+00 7.0000000e+00]]
#  ...
#  [[1.0213200e+03 1.0213200e+03 4.0001232e+03]
#   [8.0000000e+00 4.0001232e+03 4.0001232e+03]
#   [5.0000000e+00 5.0000000e+00 2.0000000e+00]
#   ...
#   [4.0001232e+03 2.0000000e+00 5.0000000e+00]
#   [7.0000000e+00 1.0213200e+03 7.0000000e+00]
#   [6.0000000e+00 5.0000000e+00 2.0000000e+00]]
#  [[1.0213200e+03 1.0213200e+03 2.0000000e+00]
#   [4.0001232e+03 6.0000000e+00 5.0000000e+00]
#   [8.0000000e+00 8.0000000e+00 4.0001232e+03]
#   ...
#   [7.0000000e+00 7.0000000e+00 5.0000000e+00]
#   [6.0000000e+00 6.0000000e+00 2.0000000e+00]
#   [7.0000000e+00 5.0000000e+00 4.0001232e+03]]
#  [[5.0000000e+00 8.0000000e+00 2.0000000e+00]
#   [8.0000000e+00 5.0000000e+00 4.0001232e+03]
#   [6.0000000e+00 7.0000000e+00 6.0000000e+00]
#   ...
#   [4.0001232e+03 8.0000000e+00 6.0000000e+00]
#   [2.0000000e+00 8.0000000e+00 1.0213200e+03]
#   [2.0000000e+00 8.0000000e+00 4.0001232e+03]]]
# ----------------------------
# [[[2. 2. 4. ... 7. 5. 4.]
#   [7. 3. 4. ... 4. 7. 3.]
#   [5. 2. 3. ... 8. 6. 6.]
#   ...
#   [4. 2. 2. ... 5. 5. 4.]
#   [4. 5. 8. ... 4. 2. 2.]
#   [5. 2. 6. ... 6. 2. 8.]]
#  [[7. 5. 7. ... 2. 4. 8.]
#   [6. 4. 6. ... 8. 5. 4.]
#   [4. 8. 7. ... 6. 6. 8.]
#   ...
#   [5. 7. 4. ... 5. 8. 7.]
#   [3. 2. 8. ... 7. 6. 4.]
#   [5. 6. 2. ... 3. 4. 4.]]
#  [[2. 2. 3. ... 3. 2. 3.]
#   [7. 2. 2. ... 6. 7. 8.]
#   [8. 5. 7. ... 3. 6. 3.]
#   ...
#   [3. 8. 5. ... 6. 8. 5.]
#   [6. 8. 2. ... 2. 3. 4.]
#   [2. 7. 4. ... 2. 5. 2.]]
#  ...
#  [[4. 6. 3. ... 7. 6. 2.]
#   [4. 4. 2. ... 5. 8. 4.]
#   [6. 7. 8. ... 4. 2. 6.]
#   ...
#   [7. 3. 6. ... 2. 7. 4.]
#   [2. 6. 7. ... 3. 5. 3.]
#   [5. 4. 8. ... 3. 4. 5.]]
#  [[8. 7. 8. ... 5. 8. 2.]
#   [7. 3. 2. ... 5. 4. 8.]
#   [4. 8. 8. ... 2. 2. 5.]
#   ...
#   [6. 3. 2. ... 4. 6. 7.]
#   [7. 7. 6. ... 2. 7. 3.]
#   [8. 4. 3. ... 3. 6. 8.]]
#  [[6. 3. 4. ... 2. 7. 7.]
#   [2. 3. 6. ... 3. 5. 6.]
#   [7. 6. 2. ... 7. 6. 8.]
#   ...
#   [4. 3. 2. ... 3. 4. 3.]
#   [6. 2. 5. ... 2. 5. 5.]
#   [7. 8. 4. ... 7. 7. 8.]]]
# [[[   0    0   40 ...    0    0   40]
#   [   0 1021   40 ...   40    0 1021]
#   [   0    0 1021 ...    0    0    0]
#   ...
#   [  40    0    0 ...    0    0   40]
#   [  40    0    0 ...   40    0    0]
#   [   0    0    0 ...    0    0    0]]
#  [[   0    0    0 ...    0   40    0]
#   [   0   40    0 ...    0    0   40]
#   [  40    0    0 ...    0    0    0]
#   ...
#   [   0    0   40 ...    0    0    0]
#   [1021    0    0 ...    0    0   40]
#   [   0    0    0 ... 1021   40   40]]
#  [[   0    0 1021 ... 1021    0 1021]
#   [   0    0    0 ...    0    0    0]
#   [   0    0    0 ... 1021    0 1021]
#   ...
#   [1021    0    0 ...    0    0    0]
#   [   0    0    0 ...    0 1021   40]
#   [   0    0   40 ...    0    0    0]]
#  ...
#  [[  40    0 1021 ...    0    0    0]
#   [  40   40    0 ...    0    0   40]
#   [   0    0    0 ...   40    0    0]
#   ...
#   [   0 1021    0 ...    0    0   40]
#   [   0    0    0 ... 1021    0 1021]
#   [   0   40    0 ... 1021   40    0]]
#  [[   0    0    0 ...    0    0    0]
#   [   0 1021    0 ...    0   40    0]
#   [  40    0    0 ...    0    0    0]
#   ...
#   [   0 1021    0 ...   40    0    0]
#   [   0    0    0 ...    0    0 1021]
#   [   0   40 1021 ... 1021    0    0]]
#  [[   0 1021   40 ...    0    0    0]
#   [   0 1021    0 ... 1021    0    0]
#   [   0    0    0 ...    0    0    0]
#   ...
#   [  40 1021    0 ... 1021   40 1021]
#   [   0    0    0 ...    0    0    0]
#   [   0    0   40 ...    0    0    0]]]
# ----------------------------
# [[[ 3  4 11]
#   [ 2  9  6]
#   [ 5  5  5]
#   ...
#   [11  9  3]
#   [ 9  3 10]
#   [ 5  5  4]]
#  [[ 5  6  8]
#   [ 7  8  9]
#   [ 9  8  9]
#   ...
#   [ 5  6 11]
#   [ 7  5  9]
#   [ 1 11  3]]
#  [[ 3  3 10]
#   [ 4  7  2]
#   [ 5  1  9]
#   ...
#   [11  8  5]
#   [ 6 11 11]
#   [ 2 10  1]]
#  ...
#  [[ 1  4  6]
#   [ 8  7  6]
#   [10  3  2]
#   ...
#   [10  8  3]
#   [ 7  7 11]
#   [ 7  7  7]]
#  [[ 3  8  6]
#   [ 5  5  6]
#   [ 7  7  7]
#   ...
#   [ 1  9  9]
#   [ 9  3  9]
#   [10  9  3]]
#  [[ 2  9 11]
#   [ 7  7  9]
#   [10 11  3]
#   ...
#   [ 6  7 11]
#   [11  3  4]
#   [ 5  3  2]]]
# [[['' '' '']
#   ['' 'çbba' '']
#   ['' '' '']
#   ...
#   ['' 'çbba' '']
#   ['çbba' '' '']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' 'çbba']
#   ['çbba' '' 'çbba']
#   ...
#   ['' '' '']
#   ['' '' 'çbba']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' '']
#   ['' '' 'çbba']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]
#  ...
#  [['' '' '']
#   ['' '' '']
#   ['' '' '']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]
#  [['' '' '']
#   ['' '' '']
#   ['' '' '']
#   ...
#   ['' 'çbba' 'çbba']
#   ['çbba' '' 'çbba']
#   ['' 'çbba' '']]
#  [['' 'çbba' '']
#   ['' '' 'çbba']
#   ['' '' '']
#   ...
#   ['' '' '']
#   ['' '' '']
#   ['' '' '']]]

```