pynlme

Nonlinear Mixed-Effects Models for Python with Rust backend

https://github.com/willov/pynlme

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.3%) to scientific vocabulary

Keywords

ai-generated proof-of-concept
Last synced: 10 months ago · JSON representation ·

Repository

Nonlinear Mixed-Effects Models for Python with Rust backend

Basic Info
  • Host: GitHub
  • Owner: willov
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 416 KB
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
ai-generated proof-of-concept
Created about 1 year ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog Contributing License Citation

README.md

PyNLME - Nonlinear Mixed-Effects Models for Python

[![Tests](https://img.shields.io/badge/tests-48%2F48%20passing-brightgr## 🎯 Features

  • 🚀 Multi-Dimensional Input: Supports both stacked and grouped data formats
  • 🐍 Pythonic API: Modern fit_nlme() interface with method parameter
  • 🔄 MATLAB Compatible: Drop-in replacement for nlmefit/nlmefitsa
  • 🔥 Fast: Rust backend with automatic fallback to Python
  • 🧪 Robust: 48/48 tests passing, comprehensive error handling
  • 📊 Complete: Full diagnostics, residuals, and model statistics Rust Backend MATLAB Compatible Python

A high-performance Python implementation of nonlinear mixed-effects (NLME) models with MATLAB-compatible API and Rust backend for optimization.

AI Disclaimer

I (willov) asked Claude Sonnet 4 in GitHub Copilot agent mode to "create a Python NLME implementation with a rust backend with a similar call syntax to MATLAB's nlmefitsa". Please use the package with caution, it is very likely that the AI have hallucinated quirks not tested. If you find it useful, please assist in creating tests to test the reliability of the package.

At the current state, this should be seen as a proof-of-concept rather than production ready.

⚠️ Current Status

Proof of Concept: The package is functional with 48/48 tests passing, but requires thorough validation before production use. Both Rust and Python backends work, with automatic fallback capabilities. All core algorithms (MLE and SAEM) are implemented and appear to work correctly in test scenarios.

🚀 Quick Start

```python import numpy as np from pynlme import fit_nlme

Define your model

def exponential_decay(phi, t, v=None): return phi[0] * np.exp(-phi[1] * t.ravel())

Fit the model (uses optimized Rust backend with Python fallback)

beta, psi, stats, b = fitnlme(t, y, group, None, exponentialdecay, [10.0, 0.5]) print(f"Fixed effects: {beta}, Log-likelihood: {stats.logl}")

Alternative: specify algorithm explicitly

beta, psi, stats, b = fitnlme( t, y, group, None, exponentialdecay, [10.0, 0.5], method='SAEM' ) ```

🎯 Multi-Dimensional Input Format (New!)

PyNLME now supports both traditional "stacked" format and an intuitive "grouped" format:

```python

Traditional stacked format (MATLAB-style)

Xstacked = np.array([[1], [2], [3], [4], [1], [2], [3], [4]]) # All measurements ystacked = np.array([10, 7, 5, 3, 12, 8, 6, 4]) # All responses
group = np.array([0, 0, 0, 0, 1, 1, 1, 1]) # Group indicators

New grouped format (each row = one subject)

Xgrouped = np.array([[1, 2, 3, 4], [1, 2, 3, 4]]) # Row per subject ygrouped = np.array([[10, 7, 5, 3], [12, 8, 6, 4]]) # Row per subject

No group parameter needed!

Both work identically

beta1, psi1, , _ = fitnlme(Xstacked, ystacked, group, None, model, beta0) beta2, psi2, , _ = fitnlme(Xgrouped, ygrouped, None, None, model, beta0)

Results are identical: np.allclose(beta1, beta2) == True

```

MATLAB Compatibility

For users migrating from MATLAB, PyNLME provides identical function signatures:

```python

MATLAB-style usage (compatibility aliases)

from pynlme import nlmefit, nlmefitsa

Maximum Likelihood Estimation (same as fit_nlme with method='MLE')

beta, psi, stats, b = nlmefit(t, y, group, None, exponential_decay, [10.0, 0.5])

SAEM Algorithm (same as fit_nlme with method='SAEM')

beta, psi, stats, b = nlmefitsa(t, y, group, None, exponential_decay, [10.0, 0.5]) ```

📦 Installation

Option 1: Install from GitHub Release (Recommended)

Pre-built wheels are available for easy installation without requiring Rust:

```bash

Latest release (v0.3.0+):

Choose the appropriate wheel for your platform:

Linux (x86_64):

uv add https://github.com/willov/PyNLME/releases/download/v0.3.0/pynlme-0.3.0-cp311-abi3-linuxx8664.whl

Windows (x86_64):

uv add https://github.com/willov/PyNLME/releases/download/v0.3.0/pynlme-0.3.0-cp311-abi3-win_amd64.whl

macOS (Intel):

uv add https://github.com/willov/PyNLME/releases/download/v0.3.0/pynlme-0.3.0-cp311-abi3-macosx1012x8664.whl

macOS (Apple Silicon):

uv add https://github.com/willov/PyNLME/releases/download/v0.3.0/pynlme-0.3.0-cp311-abi3-macosx110_arm64.whl

Or using pip instead of uv:

pip install https://github.com/willov/PyNLME/releases/download/v0.3.0/pynlme-0.3.0-cp311-abi3-linuxx8664.whl

Or browse releases to find the exact wheel for your platform:

https://github.com/willov/PyNLME/releases/latest

```

Platform compatibility: - Python: 3.11, 3.12, 3.13+ (single wheel works for all) - Linux: x8664 (Intel/AMD 64-bit) - Windows: x8664 (Intel/AMD 64-bit)
- macOS: Intel x86_64 and Apple Silicon ARM64

Option 2: Install from source (for developers)

```bash

Requires Rust toolchain for building

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh source $HOME/.cargo/env

git clone https://github.com/willov/PyNLME cd PyNLME uv sync uv run maturin develop # Build Rust extension ```

Note: The cp311-abi3 wheels use Python's stable ABI and work with Python 3.11+ including future versions. You don't need separate wheels for each Python version. Option 2 is for developers who want to build from source or contribute to the project.

🎯 Features

  • � Pythonic API: Modern fit_nlme() interface with method parameter
  • 🔄 MATLAB Compatible: Drop-in replacement for nlmefit/nlmefitsa
  • 🔥 Fast: Rust backend with automatic fallback to Python
  • 🧪 Robust: 48/48 tests passing, comprehensive error handling
  • 📊 Complete: Full diagnostics, residuals, and model statistics

📚 Documentation

🧪 Quick Test

```bash

Run the full test suite (for developers)

uv run pytest tests/ -v # All 48 tests should pass

Verify installation works (for end users)

python -c " import pynlme print(f'PyNLME {pynlme.version} installed successfully!') "

Or use the comprehensive verification script

curl -sSL https://raw.githubusercontent.com/willov/PyNLME/main/scripts/verify-installation.py | python3 ```

💡 Important Note

When using UV for package management, run Python scripts with uv run python script.py to ensure proper access to installed packages.

📄 License

MIT License - see LICENSE file for details.

✅ Current Features (Working)

  • Multi-Dimensional Input Format: Support for both traditional stacked format and intuitive grouped format where each row represents a subject
  • Modern Python API: fit_nlme(), fit_mle(), and fit_saem() functions with clean interfaces
  • MATLAB-Compatible API: Identical interface to MATLAB's nlmefit and nlmefitsa
  • High-Performance Rust Backend: Core algorithms implemented in Rust with automatic Python fallback
  • Multiple Algorithms: Both MLE and SAEM (Stochastic Approximation EM) algorithms with full Rust backend support
  • Robust Implementation: Proper convergence checking and error handling
  • Comprehensive Statistics: AIC, BIC, log-likelihood, residuals, and more
  • Type Safety: Full type annotations and data validation
  • Real-World Tested: Validated with pharmacokinetic modeling scenarios

🚧 In Development

  • Advanced Error Models: Proportional, combined, and exponential error models
  • Parameter Transformations: Log, probit, and logit transformations
  • Enhanced Diagnostics: Additional model validation tools

📖 References

This implementation is based on the following papers:

  1. Lindstrom, M. J., and D. M. Bates. "Nonlinear mixed-effects models for repeated measures data." Biometrics. Vol. 46, 1990, pp. 673–687.
  2. Davidian, M., and D. M. Giltinan. Nonlinear Models for Repeated Measurements Data. New York: Chapman & Hall, 1995.
  3. Pinheiro, J. C., and D. M. Bates. "Approximations to the log-likelihood function in the nonlinear mixed-effects model." Journal of Computational and Graphical Statistics. Vol. 4, 1995, pp. 12–35.
  4. Demidenko, E. Mixed Models: Theory and Applications. Hoboken, NJ: John Wiley & Sons, Inc., 2004.

Owner

  • Name: William Lövfors
  • Login: willov
  • Kind: user
  • Location: Sweden
  • Company: Linköping university / Örebro University

Working with mechanistic modelling at Linköping University and Örebro University.

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
type: software
title: "PyNLME: Nonlinear Mixed-Effects Models for Python"
version: 0.3.0
date-released: 2025-06-16
url: "https://github.com/willov/PyNLME"
repository-code: "https://github.com/willov/PyNLME"
license: MIT
authors:
  - family-names: "Lövfors"
    given-names: "William"
keywords:
  - "nonlinear mixed-effects models"
  - "pharmacokinetics"
  - "pharmacodynamics"
  - "SAEM algorithm"
  - "Python"
  - "Rust"
abstract: >
  PyNLME is a Python package for fitting nonlinear mixed-effects models,
  commonly used in pharmacokinetic and pharmacodynamic analyses. It features
  a high-performance Rust backend implementing the SAEM algorithm with
  graceful fallback to pure Python implementations.

GitHub Events

Total
  • Release event: 2
  • Delete event: 2
  • Push event: 19
  • Create event: 8
Last Year
  • Release event: 2
  • Delete event: 2
  • Push event: 19
  • Create event: 8

Dependencies

.github/workflows/auto-release.yml actions
  • actions/checkout v4 composite
  • actions/github-script v7 composite
  • astral-sh/setup-uv v2 composite
.github/workflows/ci.yml actions
  • actions/cache v4 composite
  • actions/checkout v4 composite
  • actions/upload-artifact v4 composite
  • astral-sh/setup-uv v4 composite
  • dtolnay/rust-toolchain stable composite
.github/workflows/test.yml actions
  • actions/checkout v4 composite
  • actions/setup-python v5 composite
  • astral-sh/setup-uv v2 composite
  • dtolnay/rust-toolchain stable composite
.github/workflows/wheels.yml actions
  • actions/checkout v4 composite
  • actions/download-artifact v4 composite
  • actions/setup-python v5 composite
  • actions/upload-artifact v4 composite
  • dtolnay/rust-toolchain stable composite
  • softprops/action-gh-release v1 composite
Cargo.toml cargo
pyproject.toml pypi
  • numpy >=2.0
  • scipy >=1.7.0