A collection of tools and utilities that I use in my work.
Project description
LuxTools
A collection of tools and utilities that I often use in my work.
Features
- Scientific
- Error propagation for numerical calculations
- Pretty printing of numerical results with uncertainties
- Functional
- Function composition utilities
- Partial function application
- Overload function definitions
Installation
pip install luxtools
Usage
Scientific
Error Propagation
import torch
from luxtools import get_error
# Create tensors with uncertainties
x = torch.tensor([1.0, 3.0], dtype=torch.float32, requires_grad=True)
x.sigma = torch.tensor([0.1, 0.2], dtype=torch.float32)
y = torch.tensor([2.0, 4.0], dtype=torch.float32, requires_grad=True)
y = Variable(y, torch.tensor([0.2, 0.3]))
# Perform calculations
f = x * y
# Get propagated error
error = get_error(f)
# tensor([0.2828, 1.2042])
Numeric Result Formatting
from luxtools import NumericResult
# Create a measurement with uncertainty
result = NumericResult(1.234, 0.193)
print(result)
# (1.2 ± 0.2)
# Scientific notation
result = NumericResult(234.23424, 10)
print(result)
# (2.3 ± 0.1)*10^(2)
# LaTeX output
print(result.latex())
# (2.3 \pm 0.1)\cdot 10^{2}
Functional
Function Composition
from luxtools import chain
# Compose functions
f = lambda x: x + 1
g = lambda x: x * 2
h = lambda x: x ** 2
# Create a new function that applies f, then g, then h
composed = chain(f, g, h)
result = composed(3) # ((3 + 1) * 2) ** 2 = 64
Partial Application
Allows you to partially apply arguments to a function, creating a new function with fewer arguments. See article for discussion.
from luxtools import partial
@partial
def greet(greeting, name):
return f"{greeting}, {name}!"
# Create a new function with 'Hello' fixed as the greeting
say_hello = greet("Hello")
result = say_hello("World") # "Hello, World!"
Overload function definitions
Allows you to have multiple function definitions for the same function name. It uses typehints to determine which function to call. See article for discussion.
from luxtools import overload
class Email:
def __init__(self, email: str):
self.email = email
def __str__(self) -> str:
return self.email
class PhoneNumber:
def __init__(self, phone_number: str):
self.phone_number = phone_number
def __str__(self) -> str:
return self.phone_number
@overload
def get_user(email: Email):
print("Email:", email)
return email
@overload
def get_user(phone_number: PhoneNumber):
print("Phone:", phone_number)
return phone_number
get_user(Email("test@example.com")) # prints: Email: test@example.com
get_user(PhoneNumber("123-456-789")) # prints: Phone: 123-456-789
Caveat, if the function is defined in a non-global scope such as a class, or inside a function, then you need to pass the local scope to the decorator.
def local_scope():
@overload(scope=locals())
def get_user(email: Email):
print("Email:", email)
@overload(scope=locals())
def get_user(phone_number: PhoneNumber):
print("Phone:", phone_number)
This is necessary because the parent stack frame doesn't exist inside the overload function, so it has to be passed explicitly. See tests.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file luxtools-0.1.1.tar.gz.
File metadata
- Download URL: luxtools-0.1.1.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6716cb2106e7fe58e6f6c33ed72ed98fb1138077c808f975726f012f694c47af
|
|
| MD5 |
8e03cc70a271dbe2407b1b2f1d8ee30b
|
|
| BLAKE2b-256 |
ed923fa60675434708c65442107180376aed70531af7c32d3c8631b2f470a116
|
File details
Details for the file luxtools-0.1.1-py3-none-any.whl.
File metadata
- Download URL: luxtools-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30fd8d869098739a165943dd63eb5f668d9f05e516f7cf9cdf8d63a9745472c8
|
|
| MD5 |
2ebcdd5a3fbba2ef75ef2e2565288c80
|
|
| BLAKE2b-256 |
a82514e2e13ab36d0427562165af393ec12e232118aea44f5d237426bfd91a93
|