interpax: Interpolation and Function Approximation with JAX

repository·main·Indexed 43 days ago

https://github.com/f0uriest/interpax

A JAX-based library for high-performance interpolation and function approximation. It supports linear, cubic, and Fourier interpolation across 1D, 2D, and 3D dimensions, featuring SciPy-compatible classes such as CubicSpline, PchipInterpolator, and Akima1DInterpolator, as well as functional interfaces like interp1d, interp2d, and interp3d.

Tokens
8.9K
Snippets
25
Records
42
Agent score
53%

What's inside interpax

  1. Overview of interpax interpolation methods

    main

    interpax provides several interpolation schemes across different dimensions:

    • 1D, 2D, and 3D: Nearest neighbor, linear, and several cubic interpolation schemes.
    • 1D and 2D: Fourier interpolation for periodic functions.

    Planned features include spline interpolation for rectilinear grids in N-dimensions, RBF interpolation for unstructured data in N-dimensions, and smoothing splines for noisy data.

  2. Python coding style and standards

    main

    When contributing Python code, follow these guidelines:

    • Formatting: Use ruff. Run ruff format interpax/ tests/ before committing. Use ruff check interpax/ tests/ to check for errors.
    • Docstrings: Use Numpy Style Docstrings. At minimum, include a description of inputs, outputs, and a summary of the function/method. Code snippets are strongly encouraged.
    • Naming: Use descriptive, clear names. Avoid cryptic abbreviations (e.g., avoid ma00ab). If a shorthand is necessary, use a # notation: comment at the start of the function to explain it.
    • Complexity: If a function has more than ~5 inputs or return values, consider using a dictionary or custom class. Keep functions modular and focused on doing one thing well.
    • Readability: Prioritize readability and usability over speed. If a block of code is complex, add a comment explaining it.
  3. Run tests and check coverage

    main

    The testing suite uses pytest.

    Run all unit tests:

    pytest -m unit

    Run specific tests by name:

    pytest -k <substring>

    Check test coverage: Use the --cov flag to report coverage. Detailed information is saved to a .coverage file. You can generate an HTML report using:

    coverage html

    This creates a htmlcov/ directory which can be viewed in a browser.

    pytest -m unit
    pytest -k foo
    pytest --cov
    coverage html
  4. Set up the development environment

    main

    To contribute code, you should install the development dependencies and linting tools.

    Install linting tools: Install the pinned version of ruff from the lint extra:

    pip install -e ".[lint]"

    Install pre-commit hooks: To automatically run checks during commits:

    pip install pre-commit
    pre-commit install

    Install development dependencies: To install the full suite including pytest and other dev tools:

    pip install -e ".[dev]"
    pip install -e ".[lint]"
    pip install pre-commit
    pre-commit install
    pip install -e ".[dev]"
  5. Ask a question about interpax

    main
    If you have a question that doesn't require a formal bug report or enhancement suggestion, the simplest method is to create a GitHub issue. To ensure it is prioritized correctly, begin the subject line with Question:. This makes the question visible to all developers and allows the answer to be viewed by other users.
  6. Git commit message conventions

    main

    Follow these rules for commit messages to maintain project history quality:

    • Template: Use the provided .gitmessagetemplate. You can set it as your default with: git config commit.template .gitmessagetemplate
    • Structure: Separate the subject line from the body with a single blank line.
    • Subject Line:
      • Limit to 50 characters or less.
      • Capitalize the first letter.
      • Use the imperative mood (e.g., "Add feature" or "Fix issue", not "Added" or "Fixed").
    • Body:
      • Wrap lines at 72 characters.
      • Explain what and why the change was made, rather than how (implementation details belong in the code).
      • Reference issues and pull requests (e.g., Resolves: #123).
    git config commit.template .gitmessagetemplate
  7. Submit a bug report

    main

    Bugs are tracked via GitHub issues. A high-quality bug report should include:

    • Clear Title: A descriptive title identifying the problem.
    • Reproduction Steps: Detailed, step-by-step instructions on how to reproduce the issue (explain how you did it, not just what you did).
    • Examples: Copy-pasteable code snippets using Markdown code blocks or links to relevant files.
    • Observed vs. Expected Behavior: Describe what actually happened and what you expected to see instead.
    • Context:
      • Did this start recently (e.g., after an update)?
      • Can it be reproduced in older versions?
      • Is it reliably reproducible?
    • Environment Details:
      • interpax version
      • JAX and other dependency versions
      • OS name and version
      • Hardware details (CPU, GPU, RAM, or cluster resource allocations)
    • Visuals: Include plots if the results are visually incorrect.
  8. Suggest an enhancement

    main

    Before suggesting an enhancement, please:

    1. Check the official documentation to see if the feature already exists.
    2. Search existing GitHub issues to see if it has already been suggested. If it has, comment on the existing issue instead of opening a new one.

    If you proceed with a new suggestion, include:

    • A clear, descriptive title.
    • A step-by-step description of the suggested enhancement.
    • Specific examples using Markdown code blocks.
    • A description of current behavior vs. expected behavior.
    • A justification of why this enhancement is useful.
    • References to relevant physics papers if applicable.
  9. Interpolation methods for 3D splines

    main

    The interpolation logic supports several methods, which determine how the function is reconstructed from the grid:

    • nearest: Returns the value of the nearest grid point. Derivatives for this method are either zero or computed via a neighbor-search approach.
    • linear: Performs multi-linear interpolation between grid points.
    • Cubic Methods: A set of methods (defined in CUBIC_METHODS) that use higher-order polynomials. These require pre-computed or approximated derivatives (fx, fy, fz, fxy, etc.) to ensure smoothness.
  10. Perform 1D interpolation with interp1d

    main

    Use interp1d to interpolate a 1D function. It requires the query points (xq), the sample points (xp), and the function values at the sample points (fp). You can specify the interpolation scheme using the method argument (e.g., "cubic").

    import jax.numpy as jnp
    import numpy as np
    from interpax import interp1d
    
    # Define sample points and function values
    xp = jnp.linspace(0, 2 * np.pi, 100)
    f = lambda x: jnp.sin(x)
    fp = f(xp)
    
    # Define query points
    xq = jnp.linspace(0, 2 * np.pi, 10000)
    
    # Perform cubic interpolation
    fq = interp1d(xq, xp, fp, method="cubic")
    
    # Verify results
    np.testing.assert_allclose(fq, f(xq), rtol=1e-6, atol=1e-5)