Skip to main content

GitlabCIPipeline GitlabCICoverage Appveyor Pypi Downloads ReadTheDocs

Read the docs

https://kwarray.readthedocs.io

Gitlab (main)

https://gitlab.kitware.com/computer-vision/kwarray

Github (mirror)

https://github.com/Kitware/kwarray

Pypi

https://pypi.org/project/kwarray

The main webpage for this project is: https://gitlab.kitware.com/computer-vision/kwarray

The kwarray module implements a small set of pure-python extensions to numpy and torch.

The kwarray module 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:

from kwarray.arrayapi import ArrayAPI, dtype_info
from .algo_assignment import (maxvalue_assignment, mincost_assignment,
                              mindist_assignment,)
from .algo_setcover import (setcover,)
from .dataframe_light import (DataFrameArray, DataFrameLight, LocLight,)
from .fast_rand import (standard_normal, standard_normal32, standard_normal64,
                        uniform, uniform32,)
from .util_averages import (RunningStats, stats_dict,)
from .util_groups import (apply_grouping, group_consecutive,
                          group_consecutive_indices, group_indices,
                          group_items,)
from .util_misc import (FlatIndexer,)
from .util_numpy import (arglexmax, argmaxima, argminima, atleast_nd, boolmask,
                         isect_flags, iter_reduce_ufunc, normalize,)
from .util_random import (ensure_rng, random_combinations, random_product,
                          seed_global, shuffle,)
from .util_slices import (embed_slice, padded_slice,)
from .util_slider import (SlidingWindow, Stitcher,)
from .util_torch import (one_hot_embedding, one_hot_lookup,)

The ArrayAPI

On of the most useful features in kwarray is the kwarray.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 the kwarray.ArrayAPI.impl function. 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.

import torch
import numpy as np
data1 = torch.rand(10, 10)
data2 = data1.numpy()
# Method 1: grab the appropriate sub-impl
impl1 = ArrayAPI.impl(data1)
impl2 = ArrayAPI.impl(data2)
result1 = impl1.sum(data1, axis=0)
result2 = impl2.sum(data2, axis=0)
assert np.all(impl1.numpy(result1) == impl2.numpy(result2))
# Method 2: choose the impl on the fly
result1 = ArrayAPI.sum(data1, axis=0)
result2 = ArrayAPI.sum(data2, axis=0)
assert np.all(ArrayAPI.numpy(result1) == ArrayAPI.numpy(result2))

Other Notes:

The kwarray.ensure_rng function 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.

DataFrameArray and DataFrameLight implement a subset of the pandas API. They are less powerful, but orders of magnitude faster. The main drawback is that you lose loc, but iloc is available.

uniform32 and standard_normal32 are faster 32-bit random number generators (compared to their 64-bit numpy counterparts).

mincost_assignment is 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_embedding is a fast numpy / torch way to perform the often needed OHE deep-learning trick.

group_items is a fast way to group a numpy array by another numpy array. For fine grained control we also expose group_indices, which groups the indices of a numpy array, and apply_grouping, which partitions a numpy array by those indices.

boolmask effectively inverts np.where.

Usefulness:

This is the frequency that I’ve used various components of this library with in my projects:

Function name

Usefulness

kwarray.ensure_rng

239

kwarray.ArrayAPI

148

kwarray.atleast_nd

50

kwarray.DataFrameArray

43

kwarray.group_indices

40

kwarray.stats_dict

34

kwarray.normalize

28

kwarray.embed_slice

21

kwarray.shuffle

17

kwarray.padded_slice

14

kwarray.SlidingWindow

14

kwarray.isect_flags

12

kwarray.RunningStats

12

kwarray.standard_normal

10

kwarray.setcover

8

kwarray.robust_normalize

7

kwarray.boolmask

7

kwarray.one_hot_embedding

7

kwarray.uniform

6

kwarray.find_robust_normalizers

6

kwarray.Stitcher

6

kwarray.apply_grouping

6

kwarray.group_consecutive

5

kwarray.argmaxima

4

kwarray.seed_global

4

kwarray.FlatIndexer

3

kwarray.group_items

3

kwarray.arglexmax

2

kwarray.DataFrameLight

2

kwarray.group_consecutive_indices

1

kwarray.equal_with_nan

1

kwarray.dtype_info

1

kwarray.unique_rows

0

kwarray.uniform32

0

kwarray.standard_normal64

0

kwarray.standard_normal32

0

kwarray.random_product

0

kwarray.random_combinations

0

kwarray.one_hot_lookup

0

kwarray.mindist_assignment

0

kwarray.mincost_assignment

0

kwarray.maxvalue_assignment

0

kwarray.iter_reduce_ufunc

0

kwarray.generalized_logistic

0

kwarray.argminima

0

kwarray.apply_embedded_slice

0

kwarray.NoSupportError

0

kwarray.LocLight

0

Release files for kwarray 0.7.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kwarray 0.7.2
File Size Uploaded
kwarray-0.7.2.tar.gz 120.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kwarray 0.7.2
File Interpreter ABI Platform
kwarray-0.7.2-py3-none-any.whl Python 3 none any Details

Total release size: 237.8 kB

Release files / kwarray-0.7.2.tar.gz

Download URL kwarray-0.7.2.tar.gz
Size 120.6 kB
Tags Source
SHA-256 checksum
How to use checksums
370c6830141a35114c899fa6f5ef2a3272ea704f679a83177b74c6d9fe8bf162
BLAKE2b-256 checksum
How to use checksums
9dfca269954028b42841da26b248c10f8868c067ddc9fa75aaa7a5b61166ddb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / kwarray-0.7.2-py3-none-any.whl

Download URL kwarray-0.7.2-py3-none-any.whl
Size 117.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d34a8eb86c8c3a29ddc4cb626f4645eb928093e5c651baa2a65645db2a2be00b
BLAKE2b-256 checksum
How to use checksums
721d04fa3f608051f08b9edcaf15176659c1e7987d8f8cbe763fdd5dba73ec5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

0.7.2 This release

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.20

1 release file

0.6.19

1 release file

0.6.18

1 release file

0.6.17

1 release file

0.6.16

1 release file

0.6.15

1 release file

0.6.14

1 release file

0.6.13

1 release file

0.6.12

1 release file

0.6.11

1 release file

0.6.10

1 release file

0.6.9

1 release file

0.6.8

1 release file

0.6.7

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

3 release files

0.5.19

3 release files

0.5.18

3 release files

0.5.16

3 release files

0.5.15

3 release files

0.5.14

3 release files

0.5.12

2 release files

0.5.10

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.4.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page