Skip to main content

No project description provided

Project description

malib

A few utilities that I find useful.

RateLimiter

from malib import RateLimiter

# call a function at most 10 times per minute
rl = RateLimiter(max_calls=10, period=60) 
# call .wait() every time before calling the function
rl.wait()

Exact cover

Code inspired by this blog post.

from malib import exact_cover

piece_to_constraints = {"A": {1}, "B": {2, 4}, "C": {2, 3, 5}, "D": {3, 5}}
next(exact_cover(piece_to_constraints))
# ("A", "B", "D")

PyTorch bivariate normal cdf

Provide two functions to compute a differentiable cumulative distribution function of a bivariate normal distribution.

Requires scipy and pytorch.

import torch
from malib import standard_bivariate_normal_cdf, bivariate_normal_cdf

# standard bivariate normal cdf
x = torch.tensor([0.0, 0.0], requires_grad=True)
cor = 0.5
y = standard_bivariate_normal_cdf(x, cor)
print(y)
# tensor(0.3333, grad_fn=<StandardBivariateNormalCDFBackward>)
y.backward()
print(x.grad)
# tensor([0.1995, 0.1995])

# bivariate_normal_cdf
x = torch.tensor([0.0, 0.0], requires_grad=True)
mean = torch.tensor([0.0, 0.0])
cov = torch.tensor([[1.0, 0.5], [0.5, 1.0]])
y = bivariate_normal_cdf(x, mean, cov)
print(y)
# tensor(0.3333, grad_fn=<BivariateNormalCDFBackward>)
y.backward()
print(x.grad)
# tensor([0.1995, 0.1995])

PyTorch interpolation

import torch
from malib import interp

x = torch.tensor([0.0, 1.0, 2.0])
y = torch.tensor([0.0, 1.0, 4.0])
interp(torch.tensor([0.5, 1.5]), x, y)
# tensor([0.5000, 2.5000])

Testing

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

malib-0.4.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

malib-0.4.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file malib-0.4.0.tar.gz.

File metadata

  • Download URL: malib-0.4.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for malib-0.4.0.tar.gz
Algorithm Hash digest
SHA256 062cdd4a0d709df429cf8366d426e94912b7a19c2061defc8d75c23c6e2d330f
MD5 7bb4557d1ec5089206d61796da0187ec
BLAKE2b-256 6d5ac167c52ce53503958f27832647c514165511844dcafe3c5f36e7c1ac36a7

See more details on using hashes here.

File details

Details for the file malib-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: malib-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for malib-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5f6baa7b9ebd0cb0183a1cea405ab3a0183b58c758d5abde916660f7a523e9c
MD5 e1eebbaf7ad6b0a2675b3fc5646cce88
BLAKE2b-256 378932a20424395bc2b949d4007c528e5b558f166bbbd8375b6353c3e07208b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page