Skip to main content

Nifty solver

Project description

nifty-solve

Fit very flexible linear models using Fourier bases without ever constructing the design matrix.

Test Status Coverage Status

Install

uv add nifty-solve

If you plan to use JAX operators, you will need to use:

uv add nifty-solve[jax]

If that fails:

uv add "git+https://github.com/andycasey/nifty-solve"

Examples

1D real-valued signal with unknown uncertainties

import numpy as np
import matplotlib.pyplot as plt
from nifty_solve.operators import Finufft1DRealOperator

np.random.seed(1)

N = 128 # number of data points
K = 5 # number of Fourier modes

# Generate data
t = np.random.uniform(size=N)
y = np.random.uniform(size=3) @ np.array([t**2, t, np.ones(N)]) + np.random.normal(size=N) * 0.02

# Create a linear operator and solve the system
A = Finufft1DRealOperator(t, n_modes=K)
θ = A / y

# Make a plot
ti = np.linspace(0, 1, 1000)
Ai = Finufft1DRealOperator(ti, n_modes=K)

# Make a plot
fig, ax = plt.subplots()
ax.scatter(t, y, c="k")
ax.plot(ti, Ai @ θ)
ax.set_xlabel("t")
ax.set_ylabel("y")

figure1

[!TIP] The A / y is just syntactic sugar for finding the least-squares solution:

from scipy.sparse.linalg import lsqr

θ_1 = A / y
θ_2, *extras = lsqr(A, y)
assert np.allclose(θ_1, θ_2)

1D real-valued signal with uncertainties

import numpy as np
import matplotlib.pyplot as plt
from nifty_solve.operators import Finufft1DRealOperator
from pylops import Diagonal

np.random.seed(1)

N = 128 # number of data points
K = 5 # number of Fourier modes

# Generate data
t = np.random.uniform(size=N)
y_true = np.random.uniform(size=3) @ np.array([t**2, t, np.ones(N)])
y_err = 0.05 + np.abs(np.random.normal(size=N) * 0.02)
Y = y_true + y_err * np.random.normal(size=N)
C_inv = Diagonal(y_err**-2)

# Create a linear operator and solve the system
A = Finufft1DRealOperator(t, n_modes=K)
θ = (A.T @ C_inv @ A) / (A.T @ C_inv @ Y)

# Make a plot
ti = np.linspace(0, 1, 1000)
Ai = Finufft1DRealOperator(ti, n_modes=K)

# Make a plot
fig, ax = plt.subplots()
ax.errorbar(t, Y, yerr=y_err, c="k", fmt="o")
ax.plot(ti, Ai @ θ)
ax.set_xlabel("t")
ax.set_ylabel("y")

figure2

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

nifty_solve-0.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nifty_solve-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file nifty_solve-0.1.0.tar.gz.

File metadata

  • Download URL: nifty_solve-0.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nifty_solve-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bdc5915afa053e0b1b6d431bf32db3c9095f287c31c3efc5e3407bf6a79821e6
MD5 929587b1af04c920ce21758f2f8b0834
BLAKE2b-256 bc885dac503efc3a5f93728308610ff82375a2a3321085e60e675439d526fd3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for nifty_solve-0.1.0.tar.gz:

Publisher: publish.yml on andycasey/nifty-solve

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nifty_solve-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nifty_solve-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nifty_solve-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe2c3edb96fef2c705bc9883102d49cb5512e837cfce109f3e470d6b9630a7e0
MD5 88614ededaf97d6a158297a549fc13d5
BLAKE2b-256 ba31f8dc66c0da26ecd4b81d9bc101671ab05941b752d84fb6f7417260533cd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for nifty_solve-0.1.0-py3-none-any.whl:

Publisher: publish.yml on andycasey/nifty-solve

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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