Skip to main content

pybbhash

PyPI License: 3-Clause BSD

This is a Python (Cython) wrapper for the BBHash codebase for building minimal perfect hash functions.

Right now, this is supporting k-mer-based hashing needs from spacegraphcats, using hash values generated (mostly) by murmurhash, e.g. from khmer's Nodetable and sourmash hashing. As such, I am focused on building MPHF for 64-bit hashes and am wrapping only that bit of the interface; the rest should be ~straightforward (hah!).

I've also added a Python-accessible "values table", BBHashTable, in the bbhash_table module. This is a table that supports a dictionary-like feature where you can associate a hash with a value, and then query the table with the hash to retrieve the value. The only tricky bit here is that unlike the bbhash module, this table supports queries with hashes that are not in the MPHF.

Thoughts for further improvement.

  • I would like to be able to use generic Python iterators in the PyMPHF construction. Right now there is a round of memory-inefficient copying of hashes, which is bad when you have a lot of k-mers!

  • I would like to be able to save to/load from strings, not just files.

I also need to investigate thread safety.

Usage

Usage of core bbhash functionality:

import bbhash

# some collection of 64-bit (or smaller) hashes
uint_hashes = [10, 20, 50, 80]

num_threads = 1 # hopefully self-explanatory :)
gamma = 1.0     # internal gamma parameter for BBHash

mph = bbhash.PyMPHF(uint_hashes, len(uint_hashes), num_threads, gamma)

for val in uint_hashes:
    print('{} now hashes to {}'.format(val, mph.lookup(val)))

# can also use 'mph.save(filename)' and 'mph = bbhash.load_mphf(filename)'.

Usage of BBHashTable

import random
from collections import defaultdict
from bbhash_table import BBHashTable

all_hashes = [ random.randint(100, 2**32) for i in range(200) ]
half_hashes = all_hashes[:100]

table = BBHashTable()

# hash the first 100 of the hashes
table.initialize(half_hashes)

# store associated values
for hashval, value in zip(half_hashes, [ 1, 2, 3, 4, 5 ] *20):
   table[hashval] = value
   
# retrieve & count for all (which will include hashes not in MPHF)
d = defaultdict(int)
for hashval in all_hashes:
   value = table[hashval]
   d[value] += 1

assert d[1] == 20
assert d[None] == 100

The last for loop can be done quickly, in Cython, using

d = table.get_unique_values(all_hashes)

Motivation: the table is a useful way to (just for one hypothetical example :) store a mapping from k-mers to compact De Bruijn graph node IDs. (We use this in several places in spacegraphcats!)


CTB Oct 2020

Release files for bbhash 0.6.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 bbhash 0.6.0
File Size Uploaded
bbhash-0.6.0.tar.gz 21.2 kB Details

Release files / bbhash-0.6.0.tar.gz

Download URL bbhash-0.6.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2a7d39c4d92713ea6b0423d939882350ba1f94900e8b0d49859e66f2628cd7d5
BLAKE2b-256 checksum
How to use checksums
3cd5fc3b23c9e93d89eb60e05bc646a442b2dfe7e8a06a55c75b4cac26d9f20e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.6.0 This release

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.3

1 release file

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