Skip to content

Instantly share code, notes, and snippets.

@thorstenwagner
Last active December 8, 2023 22:43
Show Gist options
  • Save thorstenwagner/8ea99e8548a7df57470abb6f812414d7 to your computer and use it in GitHub Desktop.
Save thorstenwagner/8ea99e8548a7df57470abb6f812414d7 to your computer and use it in GitHub Desktop.
fast map array alternative
import numpy as np
from skimage.util import map_array
from time import perf_counter
from skimage.util import map_array
shape = (50,1024,1024)
total = shape[0]*shape[1]*shape[2]
NUM_LABELS=total*2
input_data = np.random.randint(NUM_LABELS,size=total).reshape(shape).astype("int64")
from_values = np.arange(NUM_LABELS)
to_values = np.arange(NUM_LABELS)+100
np.random.shuffle(to_values)
def map_array_new(label_image, label_list, predictionlist):
plist = np.zeros(
np.max(
[np.max(label_image),
np.max(predictionlist)]
)+1)
plist[label_list] = predictionlist
return plist[label_image]
def map_array_old(label_image, label_list, predictionlist):
return map_array(label_image, label_list, predictionlist)
t1 = perf_counter()
res_new = map_array_new(input_data, from_values, to_values)
t_new = perf_counter() - t1
print(t_new)
t1 = perf_counter()
res_old = map_array_old(input_data, from_values, to_values)
t_old = perf_counter() - t1
print(t_old)
print(f"Speedup {t_old/t_new}")
np.array_equal(res_new,res_old)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment