Skip to main content

Kdict: dict with multi-dimensional, sliceable keys

CI

kdict is like dict for multi-dimensional keys. With kdict, you can easily filter and slice your dictionary by key dimensions.

Example: machine learning model evaluation. Suppose you're evaluating several models on three cross validation folds, each with a training set and a test set.

Before kdict, you might store evaluation scores in a nested dictionary. But that's cumbersome and error-prone. Here's what it would take to get the mean accuracy for a particular model across all folds:

# To access inner nested data without kdict, you'd need to write iterators like this:
import numpy as np
np.mean(
    [
        data[fold_id][fold_label]["lasso"]
        for fold_id in data.keys()
        for fold_label in data[fold_id].keys()
    ]
)

kdict makes storing and accessing this type of data a breeze. No more nesting:

# Store data in a three-dimensional kdict.
# Dimensions: fold ID, fold label, model name
data = kdict(...)

# Slice the kdict to get lasso model's mean accuracy across all folds:
# data[:, :, 'lasso'] is a subset of the full dictionary
np.mean(list(data[:, :, 'lasso'].values()))

In this example, data is a three-dimensional kdict that you can slice along any dimension. So how did we make this kdict?

from kdict import kdict
data = kdict() # make a blank kdict
for fold_id in range(3):
    for fold_label in ['train', 'test']:
        for model_name in ['lasso', 'randomforest']:
            # add an entry for each fold ID, fold label, and model name
            data[fold_id, fold_label, model_name] = get_model_score(
                fold_id,
                fold_label,
                model_name
            )

The syntax, in a nutshell:

  • Read or write a single element by accessing [key_dimension_1, key_dimension_2] and so on.
  • Or get a subset of the dictionary by slicing, e.g. [:, key_dimension_2].

Installation

pip install kdict

Usage

Create a kdict

Import: from kdict import kdict

Create a blank kdict: data = kdict(). Or initialize from an existing dict: data = kdict(existing_dict). You can also use a dict comprehension there, such as:

data = kdict({
    (fold_id, fold_label, model_name): get_model_score(fold_id, fold_label, model_name)
    for model_name in ['lasso', 'randomforest']
    for fold_label in ['train', 'test']
    for fold_id in range(3)
})

Slice a kdict

Access an individual item with data[0, 'train', 'lasso'].

Or get a subset of the dictionary with slices: data[0, :, :] will have all items where the first dimension of the key is 0. This slice is also a kdict, so you can keep slicing and filtering further.

You can also iterate over specific key dimensions:

# get final dimension of the keys
available_models = data.keys(dimensions=2)

# or get all pairs of first two dimensions
for fold_id, fold_label in data.keys(dimensions=[0, 1]):
    ... # now do something with data[fold_id, fold_label, :]

Eject

A kdict behaves just like a dict, except all keys must have the same number of dimensions.

To get a raw dict back, call data.eject().

Development

Submit PRs against develop branch, then make a release pull request to master.

# Install requirements
pip install --upgrade pip wheel
pip install -r requirements_dev.txt

# Install local package
pip install -e .

# Install pre-commit
pre-commit install

# Run tests
make test

# Run lint
make lint

# bump version before submitting a PR against master (all master commits are deployed)
bump2version patch # possible: major / minor / patch

# also ensure CHANGELOG.md updated

Changelog

0.0.1

  • First release on PyPI.

Release files for kdict 0.1.0

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

Source distribution (sdist)

Source distribution for kdict 0.1.0
File Size Uploaded
kdict-0.1.0.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kdict 0.1.0
File Interpreter ABI Platform
kdict-0.1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size:19.2 kB

Release files / kdict-0.1.0.tar.gz

Download URL kdict-0.1.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bcf332c2ffd039d87a7a58e483137825b39cf1fb3c24f1bb5ddfa33f5a2b6058
BLAKE2b-256 checksum
How to use checksums
9790002e727ff4ffde1c678c07fbc6ddc091ad58dc336e92a6545d8a1f9bc54e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / kdict-0.1.0-py2.py3-none-any.whl

Download URL kdict-0.1.0-py2.py3-none-any.whl
Size 6.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
04c5fc751fa5f2958b309df4dc5d1c41e15281d1ef72ec5b7ac3ae464f347555
BLAKE2b-256 checksum
How to use checksums
1855a89315d830e93f4400e5ca41085a6760d37159ffc82a0270b21bccd970c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.1

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