Skip to main content

Geometry-aware spline smoothing and geodesic modeling for spherical data in Python

Project description

spheresmooth (Python)

A Python port of the spheresmooth R package for spherical geometry and spline smoothing on the unit sphere.
The library provides utilities for coordinate transforms, geodesics, and penalized spherical smoothing using piecewise geodesic splines.

This project is a Python implementation of the original R package, with core components rewritten in a clean, modular, and extensible Python design.


Documentation

Full documentation is available at:

https://seyoung-230.github.io/spheresmooth-python/

Authors

Motivation

Spherical data arise naturally in many scientific fields, including geophysics, meteorology, biomechanics, computer vision, and robotics. Standard Euclidean smoothing methods often fail to respect the intrinsic geometry of the sphere, leading to distorted trajectories and inaccurate inference.

spheresmooth aims to provide geometry-aware tools for analyzing spherical data directly on the unit sphere, avoiding ad hoc projections to Euclidean space.


License

GPL-3


Installation

pip install spheresmooth

Requirements

spheresmooth requires the following dependencies:

Core dependencies

Package Version (recommended) Description
Python 3.9+ Core language requirement
NumPy >= 1.24 Numerical computations
pandas >= 1.5 Internal data loading and handling (import pandas as pd)
importlib.resources stdlib (Python ≥ 3.9) Access to packaged data files

Optional dependencies (examples and visualization)

Package Version (recommended) Description
matplotlib >= 3.7 Plotting and visualization in example scripts (import matplotlib.pyplot as plt)
geopandas >= 0.13 Geographic data handling and map-based examples (import geopandas as gpd)

The core library uses pandas and importlib.resources solely for internal data loading.
importlib.resources is part of the Python standard library (Python ≥ 3.9) and does not need to be installed separately.

Visualization-related dependencies (matplotlib, geopandas) are required only for running example scripts and generating figures. They are not needed for using the core functionality of the library.

If you install spheresmooth via pip, the core dependencies will be installed automatically:

pip install spheresmooth

To install the optional dependencies for examples and visualization, use:

pip install spheresmooth[viz]

Source code structure

The source code of the package is contained in the spheresmooth/ directory, following standard Python packaging conventions.

Features

Coordinate Transformations

  • Convert Cartesian ↔ Spherical coordinates
  • Batch processing with NumPy
  • Consistent handling of row-wise/column-wise inputs

Geometry Utilities

  • Compute geodesics on the sphere
  • Normalize vectors
  • Spherical distance functions
  • Projection and gradient operators

Smoothing Functions (In Progress)

  • Penalized piecewise geodesic spline smoothing
  • Python implementation of the structure of the R function penalized_linear_spherical_spline()
  • Full Riemannian optimization is currently under development

Example

import numpy as np
from spheresmooth import cartesian_to_spherical

points = np.array([
    [1/np.sqrt(3), 1/np.sqrt(3), 1/np.sqrt(3)],
    [-1/np.sqrt(3), 1/np.sqrt(3), -1/np.sqrt(3)],
])

theta_phi = cartesian_to_spherical(points)
print(theta_phi)

APW Spherical Spline Example

The following example demonstrates how to fit a penalized spherical spline to the Apparent Polar Wander (APW) path and visualize the result on a world map.

The APW dataset consists of time-indexed observations on the unit sphere, represented in spherical coordinates ((theta, phi)).

Workflow Overview

  1. Load spherical APW data ((theta, phi))
  2. Convert spherical coordinates to Cartesian coordinates on the unit sphere
  3. Select knot locations using quantiles of the time variable
  4. Fit a penalized piecewise geodesic spline using BIC-based model selection
  5. Convert fitted control points back to spherical coordinates
  6. Evaluate the fitted geodesic curve and visualize it on a world map

Example Code (Simplified)

import spheresmooth as ss
import numpy as np

# Load APW data: columns = (t, theta, phi)
apw = ss.load_apw()
t = apw.iloc[:, 0].values
spherical = apw.iloc[:, 1:3].values

# Spherical → Cartesian
y = ss.spherical_to_cartesian(spherical)

# Knot selection
dimension = 15
knots = ss.knots_quantile(t, dimension)
lambdas = np.exp(np.linspace(np.log(1e-7), np.log(1), 40))

# Penalized spherical spline fit
fit = ss.penalized_linear_spherical_spline(
    t=t,
    y=y,
    dimension=dimension,
    initial_knots=knots,
    lambdas=lambdas
)

APW Spherical Spline Example

APW spherical spline example

Interpretation

The fitted model represents the APW trajectory as a sequence of connected great-circle segments on the unit sphere. A sparsity-inducing penalty controls changes in velocity between segments, resulting in a smooth yet geometry-respecting trajectory.

The smoothing parameter lambda is selected using the Bayesian Information Criterion (BIC), balancing goodness-of-fit and model complexity.

This example illustrates how spheresmooth performs intrinsic smoothing directly on the sphere, avoiding ad hoc Euclidean projections.

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

spheresmooth-0.1.2.tar.gz (243.4 kB view details)

Uploaded Source

Built Distribution

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

spheresmooth-0.1.2-py3-none-any.whl (255.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spheresmooth-0.1.2.tar.gz
  • Upload date:
  • Size: 243.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for spheresmooth-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2bcadaa9baf42d5794b7afb39f64a3cae841d624f51058fe3b8b1c9d9ef3600f
MD5 dfd91aaabe1a969648c566ae77fd77cc
BLAKE2b-256 5a3ede1891ab288f8cea0898d2d0febccc0c56026ff156cd6507f323b7104fc3

See more details on using hashes here.

File details

Details for the file spheresmooth-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: spheresmooth-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 255.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for spheresmooth-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe91b3b6473cf989502dfd2dcd0f4357c8076d81b4911620af7ba3c7519bab95
MD5 3e2004f5b4787fa9ebd164ce7f922cb7
BLAKE2b-256 b6cf3b6b2d527cfc05f382adc99c8a4541101752cd12df36330877f8fdcf228a

See more details on using hashes here.

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