Package: kwarray
Read the docshttps://kwarray.readthedocs.ioGitlab (main)https://gitlab.kitware.com/computer-vision/kwarrayGithub (mirror)https://github.com/Kitware/kwarrayPypihttps://pypi.org/project/kwarrayThe main webpage for this project is:https://gitlab.kitware.com/computer-vision/kwarrayThekwarraymodule implements a small set of pure-python extensions to numpy and torch.Thekwarraymodule started as extensions for numpy + a simplified
pandas-like DataFrame object with much faster item row and column access. But
it also include an ArrayAPI, which is a wrapper that allows 100%
interoperability between torch and numpy. It also contains a few algorithms
like setcover and mincost_assignment.The top-level API is:fromkwarray.arrayapiimportArrayAPI,dtype_infofrom.algo_assignmentimport(maxvalue_assignment,mincost_assignment,mindist_assignment,)from.algo_setcoverimport(setcover,)from.dataframe_lightimport(DataFrameArray,DataFrameLight,LocLight,)from.fast_randimport(standard_normal,standard_normal32,standard_normal64,uniform,uniform32,)from.util_averagesimport(RunningStats,stats_dict,)from.util_groupsimport(apply_grouping,group_consecutive,group_consecutive_indices,group_indices,group_items,)from.util_miscimport(FlatIndexer,)from.util_numpyimport(arglexmax,argmaxima,argminima,atleast_nd,boolmask,isect_flags,iter_reduce_ufunc,normalize,)from.util_randomimport(ensure_rng,random_combinations,random_product,seed_global,shuffle,)from.util_slicesimport(embed_slice,padded_slice,)from.util_sliderimport(SlidingWindow,Stitcher,)from.util_torchimport(one_hot_embedding,one_hot_lookup,)The ArrayAPIOn of the most useful features inkwarrayis thekwarray.ArrayAPI— a
class that helps bridge between numpy and torch. This class consists of static
methods that implement part of the numpy API and operate equivalently on either
torch.Tensor or numpy.ndarray objects.This works because every function call checks if the input is a torch tensor or
a numpy array and then takes the appropriate action.As you can imagine, it can be slow to validate your inputs on each function
call. Therefore the recommended way of using the array API is via thekwarray.ArrayAPI.implfunction. This function does the check once and then
returns another object that directly performs the correct operations on
subsequent data items of the same type.The following example demonstrates both modes of usage.importtorchimportnumpyasnpdata1=torch.rand(10,10)data2=data1.numpy()# Method 1: grab the appropriate sub-implimpl1=ArrayAPI.impl(data1)impl2=ArrayAPI.impl(data2)result1=impl1.sum(data1,axis=0)result2=impl2.sum(data2,axis=0)assertnp.all(impl1.numpy(result1)==impl2.numpy(result2))# Method 2: choose the impl on the flyresult1=ArrayAPI.sum(data1,axis=0)result2=ArrayAPI.sum(data2,axis=0)assertnp.all(ArrayAPI.numpy(result1)==ArrayAPI.numpy(result2))Other Notes:Thekwarray.ensure_rngfunction helps you properly maintain and control local
seeded random number generation. This means that you wont clobber the random
state of another library / get your random state clobbered.DataFrameArrayandDataFrameLightimplement a subset of the pandas API.
They are less powerful, but orders of magnitude faster. The main drawback is
that you loseloc, butilocis available.uniform32andstandard_normal32are faster 32-bit random number generators
(compared to their 64-bit numpy counterparts).mincost_assignmentis the Munkres / Hungarian algorithm. It solves the
assignment problem.setcover- solves the minimum weighted set cover problem using either an
approximate or an exact solution.one_hot_embeddingis a fast numpy / torch way to perform the often needed OHE
deep-learning trick.group_itemsis a fast way to group a numpy array by another numpy array.  For
fine grained control we also exposegroup_indices, which groups the indices
of a numpy array, andapply_grouping, which partitions a numpy array by those
indices.boolmaskeffectively invertsnp.where.Usefulness:This is the frequency that I’ve used various components of this library with in
my projects:Function nameUsefulnesskwarray.ensure_rng239kwarray.ArrayAPI148kwarray.atleast_nd50kwarray.DataFrameArray43kwarray.group_indices40kwarray.stats_dict34kwarray.normalize28kwarray.embed_slice21kwarray.shuffle17kwarray.padded_slice14kwarray.SlidingWindow14kwarray.isect_flags12kwarray.RunningStats12kwarray.standard_normal10kwarray.setcover8kwarray.robust_normalize7kwarray.boolmask7kwarray.one_hot_embedding7kwarray.uniform6kwarray.find_robust_normalizers6kwarray.Stitcher6kwarray.apply_grouping6kwarray.group_consecutive5kwarray.argmaxima4kwarray.seed_global4kwarray.FlatIndexer3kwarray.group_items3kwarray.arglexmax2kwarray.DataFrameLight2kwarray.group_consecutive_indices1kwarray.equal_with_nan1kwarray.dtype_info1kwarray.unique_rows0kwarray.uniform320kwarray.standard_normal640kwarray.standard_normal320kwarray.random_product0kwarray.random_combinations0kwarray.one_hot_lookup0kwarray.mindist_assignment0kwarray.mincost_assignment0kwarray.maxvalue_assignment0kwarray.iter_reduce_ufunc0kwarray.generalized_logistic0kwarray.argminima0kwarray.apply_embedded_slice0kwarray.NoSupportError0kwarray.LocLight0