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

From source:

git clone git@github.com:andycasey/nifty-solve.git

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.1.tar.gz (7.3 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.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nifty_solve-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 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.1.tar.gz
Algorithm Hash digest
SHA256 a9894819535d44863ccea66ac6adf7c2e567de0dbc125f6eccc397c0007735ec
MD5 ffbb0363d32c65881061b8269f902b77
BLAKE2b-256 01815a2a7e1afbac6d39b7c10a7648fd0ff4bd75d99460c212ca5b6c92fbcdd4

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: nifty_solve-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 76992bfdff21769ec1bdd39de1feba8b21551b1653b6062a1d82f7acdd81058a
MD5 2016e1fb927ee4effa7d00c6d6dc0d82
BLAKE2b-256 db2b254990e5ba03dd3d9aede2fae8d181da5b527ff4f09985b40a30c33c9a56

See more details on using hashes here.

Provenance

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