Skip to main content

Fast RIM (raking) survey weighting with narwhals - supports polars and pandas

Project description

rimpy — Rust-accelerated RIM engine

Architecture

rimpy/
├── Cargo.toml                    # Rust crate definition
├── pyproject.toml                # Python package (maturin build)
├── src/                          # Rust source
│   ├── lib.rs                    # PyO3 bindings + module exports
│   └── engine.rs                 # Core RIM algorithm (pure Rust)
├── python/                       # Python source (maturin mixed layout)
│   └── rimpy/
│       ├── __init__.py           # Public API (unchanged)
│       ├── _engine.py            # Shim: tries Rust → falls back to Python
│       ├── _engine_py.py         # Pure Python/NumPy fallback
│       ├── _rake.py              # Narwhals orchestration (unchanged)
│       └── _loaders.py           # Scheme loaders (unchanged)
├── tests/
│   └── test_backend_parity.py    # Validates Rust == Python results
└── benchmarks/
    └── bench_engine.py           # Performance comparison

What changed, what didn't

File Status Why
_rake.py Unchanged Orchestration layer, not a bottleneck
_loaders.py Unchanged I/O bound, no benefit from Rust
__init__.py Unchanged Public API stays the same
_engine.py Now a shim Tries rimpy_engine (Rust), falls back to _engine_py
_engine_py.py Renamed original Pure Python fallback for portability
src/engine.rs New Core RIM loop in Rust (raw slices, zero alloc)
src/lib.rs New PyO3 bindings accepting NumPy arrays

Why this design

  • Zero API changes: _rake.py still does from ._engine import RakeResult, rim_iterate. Users see no difference.
  • Graceful fallback: No Rust compiler? No problem — pure Python still works.
  • Check which backend: rimpy._engine.get_backend() returns "rust" or "python".

Building

Prerequisites

  • Rust toolchain: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • uv (already installed)

Development build

# uv reads [build-system] → maturin, handles everything
uv pip install -e ".[dev]"

# Verify
uv run python -c "from rimpy._engine import get_backend; print(get_backend())"
# → "rust"

Release build (wheel)

uv build
# Output: dist/rimpy-0.2.0-cp312-cp312-*.whl

Publish to PyPI

uv publish

Run benchmarks

uv run python benchmarks/bench_engine.py

Run tests

uv run pytest tests/ -v

Rust engine design decisions

Why raw Vec<f64> instead of ndarray or Arrow?

RIM's core operation is indexed gather/scatter on a 1D array:

  • Gather: sum weights[indices] for each category
  • Scatter: weights[indices] *= multiplier

This is trivially expressed with plain slices. ndarray would add abstraction overhead and allocate on fancy indexing. Arrow is a columnar format, not a compute engine.

Raw slices let rustc + LLVM auto-vectorize with SIMD, and the hot loop does zero heap allocations (the old_weights buffer is pre-allocated and reused via copy_from_slice).

Why Rayon for grouped raking?

Each country/segment gets its own weights vector — no shared mutable state. This is embarrassingly parallel. rayon::par_iter() gives near-linear speedup across CPU cores with zero synchronization overhead.

Data ingress

Currently accepts NumPy arrays via pyo3-numpy (near-zero-copy for contiguous arrays).

Future: Arrow FFI path for zero-copy from Polars. The engine itself stays the same — only the ingress layer changes.

Integration with existing code

Your _rake.py and _loaders.py remain identical. The only structural change is:

# Before (in _rake.py):
from ._engine import RakeResult, rim_iterate

# After (same import — _engine.py is now a shim):
from ._engine import RakeResult, rim_iterate  # tries Rust, falls back to Python

The shim handles everything transparently.

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

rimpy-0.1.0.tar.gz (38.1 kB view details)

Uploaded Source

Built Distributions

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

rimpy-0.1.0-cp314-cp314-win_amd64.whl (233.4 kB view details)

Uploaded CPython 3.14Windows x86-64

rimpy-0.1.0-cp314-cp314-manylinux_2_34_x86_64.whl (326.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

rimpy-0.1.0-cp314-cp314-macosx_11_0_arm64.whl (288.7 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

rimpy-0.1.0-cp313-cp313-win_amd64.whl (233.3 kB view details)

Uploaded CPython 3.13Windows x86-64

rimpy-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl (326.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

rimpy-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (288.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

rimpy-0.1.0-cp312-cp312-win_amd64.whl (233.6 kB view details)

Uploaded CPython 3.12Windows x86-64

rimpy-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl (326.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

rimpy-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (289.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file rimpy-0.1.0.tar.gz.

File metadata

  • Download URL: rimpy-0.1.0.tar.gz
  • Upload date:
  • Size: 38.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rimpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 20d8095b78a38b28f45b8038c06bc8de497b43bf3c11d67ff236f0691dbe28b9
MD5 75f6828e6c1adece8f1c4fda1b489976
BLAKE2b-256 3bf972d31a7dd8aff591286fe1db072dbb495497fba348887c6bf4af9d7f3ead

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0.tar.gz:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: rimpy-0.1.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 233.4 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rimpy-0.1.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 b2ada4dd74242bbae639d25d0778d7f0c9be86346ea9277f921e29fd5a555554
MD5 b4a0bec5fe56ae6d48fb3b3716aec994
BLAKE2b-256 42999b1ba4239ebbb2afe3b04bbaeffe7f92536f48f47e78ace40c9441315bdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp314-cp314-win_amd64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rimpy-0.1.0-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 86bc39ab2ce218b384cd48cd51691b86c6718b48dc2bd5c24b10d70eec06f686
MD5 00e8a26eea23a7832742e9cb11eb813f
BLAKE2b-256 2a64512981c5d8bac0e592889f51cf61a93037e374c4e01ac322a2ce95c37ebb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp314-cp314-manylinux_2_34_x86_64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rimpy-0.1.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9e6125cc7441c75cf5cff83cf40b6358bad71091a97f5c882808675bf10110b
MD5 a466110f8d2413b9a7193bdc395e5714
BLAKE2b-256 e25a3d5e9a3b84fa92c9d52b737fb2ef97b5b9c7d1fee4cf3dc255aa616854a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: rimpy-0.1.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 233.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rimpy-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 48e6b8a08be4586b639140ee0fe751d8314eed462d921ca6850dfbe9aeffa287
MD5 c6799d8e3b3c3f57163275445509bc86
BLAKE2b-256 7efd20f12302ef5d36f8109d34781b9a7271523ff9d3697b4ea38dabc6c889b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp313-cp313-win_amd64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rimpy-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 66a1fbce9133fdb7c751b3a562ba0817f1374a12d1f28ec7656002151d644ced
MD5 bf60defd9c582ca010da76a3e683734a
BLAKE2b-256 3136572e6c31dec379acf18074ada37b13f420973a3ab70368f2703184401cbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rimpy-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0d512a178b54c97f17406b229d6968b63528d42fd36ffb9ec5e06873c6133cf9
MD5 648d28ce195cb27c199eb18a7ebb5dd4
BLAKE2b-256 48d761e12d54d161fe7c213599dbe138f81c1694f123c1890ca300f51c2ae43b

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: rimpy-0.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 233.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rimpy-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e46ce690988c90092ff90db7f3cc265b04fdbcef0904515a6a752876c0150372
MD5 bdb9c70ced725c6059a49b49da9e8e35
BLAKE2b-256 710f7316e236081c46d26f4585a894faab7cb83da4468b6d54594d8133d6c161

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rimpy-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 10ff0813bd2aff6fbc78f0e61da54327d44a939a83a19391039df1bbaafa6d8f
MD5 39b67aff332fbde5c61f4b0589874559
BLAKE2b-256 dbc1f5a382f6b77b0e2c10c59892d1ab8a6264afff80cbad8f508482ebb48c78

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: release.yml on albertxli/rimpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rimpy-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rimpy-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 263c6ee13fa2dc8aae1c225a3dca3aa74439c4c4b23458c3b4d44a8f012eac3a
MD5 ac183f80dc3e269df5845b4f424bb7cc
BLAKE2b-256 975ec7e027045ad51cb5d8d6883ab176c22aea65ad5a1768f4255c2ceff7ff18

See more details on using hashes here.

Provenance

The following attestation bundles were made for rimpy-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on albertxli/rimpy

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