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()
ttl_cache
from malib import ttl_cache
from time import sleep
@ttl_cache(ttl=1)
def f():
print("computing")
return "result"
print(f()) # prints "computing"
print(f()) # cached
sleep(1)
print(f()) # prints "computing"
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
Release history Release notifications | RSS feed
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.5.0.tar.gz
(3.7 kB
view details)
Built Distribution
malib-0.5.0-py3-none-any.whl
(5.0 kB
view details)
File details
Details for the file malib-0.5.0.tar.gz
.
File metadata
- Download URL: malib-0.5.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 672b33ceef7e900f68410a21c0730021cb1086c84e1947250bae8e4677d60577 |
|
MD5 | 853d410c990c80b289474876bb73e1a5 |
|
BLAKE2b-256 | 02ac67433180157a1bc83583a6aa2dea27ab5466f755e5ed248e9dad424bb369 |
File details
Details for the file malib-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: malib-0.5.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4de487aa71c91ac73e73383590abacb7aebffdba340a61682b843cc0512414d4 |
|
MD5 | 2cd8c644b37a7b18b7c935f7d0e39ee3 |
|
BLAKE2b-256 | 86f417bc4352929212b78f86f523e2daac583cca70a4ce6f240ecc39ba45c6f3 |