Package: imops
ImopsEfficient parallelizable algorithms for multidimensional arrays to speed up your data pipelines.DocumentationBenchmarksInstallpipinstallimops# default install with Cython backendpipinstallimops[numba]# additionally install Numba backendHow fast is it?Time comparisons (ms) for Intel(R) Xeon(R) Silver 4114 CPU @ 2.20GHz using 8 threads. All inputs are C-contiguous NumPy arrays. For morphology functionsbooldtype is used andfloat64for all others.function / backendScipy()Cython(fast=False)Cython(fast=True)Numba()zoom(..., order=0)207211148673590zoom(..., order=1)65275965753757interp1d780149146420radon5971159824837-inverse_radon5292882546535-binary_dilation2207310298-binary_erosion2296326304-binary_closing4158544469-binary_opening4410567522-center_of_mass22376464-We useairspeed velocityto benchmark our code. For detailed results visitbenchmark page.FeaturesFast Radon transformfromimopsimportradon,inverse_radonFast 0/1-order zoomfromimopsimportzoom,zoom_to_shape# fast zoom with optional fallback to scipy's implementationy=zoom(x,2,axis=[0,1])# a handy function to zoom the array to a given shape# without the need to compute the scale factorz=zoom_to_shape(x,(4,120,67))Works faster only forndim<=4, dtype=float32 or float64 (and bool-int16-32-64 if order == 0), output=None, order=0 or 1, mode='constant', grid_mode=FalseFast 1d linear interpolationfromimopsimportinterp1d# same as `scipy.interpolate.interp1d`Works faster only forndim<=3, dtype=float32 or float64, order=1Fast 2d linear interpolationimportnumpyasnpfromimops.interp2dimportLinear2DInterpolatorn,m=1024,2points=np.random.randint(low=0,high=1024,size=(n,m))points=np.unique(points,axis=0)x_points=points[:n//2]values=np.random.uniform(low=0.0,high=1.0,size=(len(x_points),))interp_points=points[n//2:]num_threads=-1# will be equal to num of CPU cores# You can optionally pass your own triangulation as an np.array of shape [num_triangles, 3], element at (i, j) position is an index of a point from x_pointsinterpolator=Linear2DInterpolator(x_points,values,num_threads=num_threads,triangles=None)# Also you can pass values to __call__ and rewrite the ones that were passed to __init__interp_values=interpolator(interp_points,values+1.0,fill_value=0.0)Fast binary morphologyfromimopsimportbinary_dilation,binary_erosion,binary_opening,binary_closingThese functions mimicscikit-imagecounterpartsPaddingfromimopsimportpad,pad_to_shapey=pad(x,10,axis=[0,1])# `ratio` controls how much padding is applied to left side:# 0 - pad from right# 1 - pad from left# 0.5 - distribute the padding equallyz=pad_to_shape(x,(4,120,67),ratio=0.25)Croppingfromimopsimportcrop_to_shape# `ratio` controls the position of the crop# 0 - crop from right# 1 - crop from left# 0.5 - crop from the middlez=crop_to_shape(x,(4,120,67),ratio=0.25)Labelingfromimopsimportlabel# same as `skimage.measure.label`labeled,num_components=label(x,background=1,return_num=True)BackendsFor all heavy image routines exceptlabelyou can specify which backend to use. Backend can be specified by a string or by an instance ofBackendclass. The latter allows you to customize some backend options:fromimopsimportCython,Numba,Scipy,zoomy=zoom(x,2,backend='Cython')y=zoom(x,2,backend=Cython(fast=False))# same as previousy=zoom(x,2,backend=Cython(fast=True))# -ffast-math compiled cython backendy=zoom(x,2,backend=Scipy())# use scipy original implementationy=zoom(x,2,backend='Numba')y=zoom(x,2,backend=Numba(parallel=True,nogil=True,cache=True))# same as previousAlso backend can be specified globally or locally:fromimopsimportimops_backend,set_backend,zoomset_backend('Numba')# sets Numba as default backendwithimops_backend('Cython'):# sets Cython backend via context managerzoom(x,2)Note that forNumbabackend settingnum_threadsargument has no effect for now and you should useNUMBA_NUM_THREADSenvironment variable.
Available backends:function / backendScipyCythonNumbazoom✓✓✓interp1d✓✓✓radon✗✓✗inverse_radon✗✓✗binary_dilation✓✓✗binary_erosion✓✓✗binary_closing✓✓✗binary_opening✓✓✗center_of_mass✓✓✗AcknowledgementsSome parts of our code for radon/inverse radon transform as well as the code for linear interpolation are inspired by
the implementations fromscikit-imageandscipy.
Also we usedfastremapandcc3dout of the box.