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.2.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.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nifty_solve-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 081f8edf8a505e473a8049fdb555689261ac391737bb88308ecf90b565014bb4
MD5 71e314715ba6d5643a63cd635783ca2b
BLAKE2b-256 b9df9cd2b76ed57e6649cf73fac6db05e3f47bab7262f74524c6eb9ca1311cf9

See more details on using hashes here.

Provenance

The following attestation bundles were made for nifty_solve-0.1.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: nifty_solve-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b5f206709e03c517e53a7e19f5eded4581ff9e6281cf9f360953788836fb8d5
MD5 a1ffb6703c1b10891ea947b8ca771b02
BLAKE2b-256 62b56d0c4fefb650ce0f1ef4761498121f8b89124ddbfca97ee9e99c4be31592

See more details on using hashes here.

Provenance

The following attestation bundles were made for nifty_solve-0.1.2-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