Metadata-Version: 2.4
Name: goldsluice
Version: 0.1.2
Requires-Dist: numpy>=1.26
Requires-Dist: goldpan[parquet]>=0.3 ; extra == 'batch'
Requires-Dist: goldpan[parquet,root]>=0.3 ; extra == 'root'
Requires-Dist: pytest ; extra == 'test'
Provides-Extra: batch
Provides-Extra: root
Provides-Extra: test
License-File: LICENSE
Summary: Fast pulse-reduction kernels in Rust, with a Python mirror of every algorithm
License-Expression: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/villano-lab/goldsluice/blob/main/CHANGELOG.md
Project-URL: Documentation, https://villano-lab.github.io/goldsluice/
Project-URL: Homepage, https://github.com/villano-lab/goldsluice
Project-URL: Repository, https://github.com/villano-lab/goldsluice

# goldsluice

Fast pulse-reduction kernels in Rust, with a **Python mirror of every algorithm**. It is the reduction half of the pair that
[`goldpan`](https://github.com/villano-lab/goldpan) (analysis) belongs to. It was `goldsluice/` of [NSDF-Data](https://github.com/villano-lab/NSDF-Data) until 2026-11-05 (the history is kept), and `goldsluice` and `goldpan` are published on PyPI. **Install it:** `python -m pip install goldsluice` (a wheel for Windows, macOS and Linux; no Rust needed), with `goldsluice[batch]` for batch mode. **Documentation:** https://villano-lab.github.io/goldsluice/ (the reference is read from the docstrings). To work on it: `CONTRIBUTING.md`. It is in its first steps: ten algorithms, `peak`, `max_time`, `baseline`, `integral`, `window_integral`, `excursion`, `rise_time` and `rise_crossing` (issue 155), which reduce a trace to numbers, and two that return a trace, `subtract_line` and `lowpass`.

```python
from goldsluice import run, compare

traces = ...                  # float array, shape (n_events, n_samples): one channel
out = run("peak", traces, glitch_samples=10, pretrigger_samples=1000, amps_per_adc=1.5e-9)
out["adc_max"]                # largest raw ADC value of each trace
out["current_max"]            # largest current above the pretrigger baseline of each trace, in amps

run("peak", traces, backend="python", ...)     # the same through the Python mirror (slow; for development)
print(compare("peak", traces, ...))            # time both backends and the speedup
```

Nothing about the readout is assumed: `amps_per_adc`, `glitch_samples` and `pretrigger_samples` have no defaults.

## Batch mode: a dataset definition, processed in one command

The developer quick-sheet `goldsluice-batch.pdf` (in `docs/developers/` of NSDF-Data) is the guide to this section.

Datasets are defined once, in a TOML file that `goldpan` and `goldsluice` both read (`goldpan.dataset`; its docstring is the
specification and `examples/r76.dataset.toml` an example): the raw files and how to read them (any `goldpan` adapter, the
readout), and the processing steps with every parameter written down.

```
pip install goldsluice[batch]                          # batch mode reads the data with goldpan (from PyPI)
python -m goldsluice check r76.dataset.toml          # read the definition, list the raw files
python -m goldsluice run r76.dataset.toml            # process what is new or changed (--chunk, --force, --workers N, --readahead N, --replace, --backend python)
```

```python
from goldpan.dataset import Dataset

ds = Dataset.open("r76.dataset.toml")
ev = ds.read()                                  # the pulses with the processed quantities attached (one Events)
q = ds.processed()                              # only the processed quantities: small, no pulses
ev.quantities["Side2pA"]["current_max"]
```

`run` reads each raw file in chunks, applies every step to the channels it names with the Rust kernels and writes the quantities (one
record per channel, keyed by series, dump and event; no pulses) into the `output` of the definition, a goldpan Parquet dataset. A
ledger (`_goldsluice.json`) records what was done for each raw file, so a second run does only what changed (a definition, a raw file, or an algorithm or reader it uses whose `REVISION` was raised), and every output file
carries its provenance in its metadata. The report gives the time of every phase: reading is usually most of it, the Rust kernels
the least (6000 events of 4096 samples, 3 channels: 1.2 s reading, 0.05 s in `peak`, 0.1 s writing).

## How it is laid out

| Where | What |
|---|---|
| `src/<algorithm>.rs` | the kernel in pure Rust: no Python types, unit tests next to it |
| `src/lib.rs` | the Python bindings (`goldsluice._core`): one function per algorithm, same name and keyword arguments as the mirror |
| `python/goldsluice/reference/<algorithm>.py` | the **Python mirror**: the readable definition, the place to develop and to define the standard inputs |
| `python/goldsluice/algorithms.py` | `run(name, traces, backend=..., **params)` |
| `python/goldsluice/profile.py` | `compare(...)`: the timing of both backends |
| `python/goldsluice/batch.py`, `__main__.py` | batch mode: `python -m goldsluice run\|check\|algorithms` |
| `benches/kernels.rs` | `cargo bench`: the Rust kernels alone, with no Python |
| `tests/test_parity.py` | every algorithm has both versions, they agree on the standard inputs and refuse bad parameters alike |

## Build and test

You need Rust (<https://rustup.rs>; on Windows also the Visual Studio *Build Tools* with "Desktop development with C++") and Python
with `numpy`, `pytest` and `maturin`. From this folder, in your Python environment:

```
python -m pip install maturin pytest numpy
maturin develop --release
python -m pytest tests
cargo test
cargo bench
```

`maturin develop --release` builds the Rust and installs `goldsluice` in place. Always use `--release` when timing: a debug build is
slower by a factor of ten or more.

## Writing an algorithm

Write the Python mirror first, then port it to Rust, then register both. The developer quick-sheet `goldsluice-algorithm.pdf` (in `docs/developers/` of NSDF-Data) walks through it with an example; **`PORTING.md`** is the reference, with the rules the Python
must follow so that the port is mechanical, a table of the equivalent Python and Rust, and a checklist.

## Timing

Two levels, on purpose:

* `cargo bench` times the kernel alone (no Python, no copy). It is the number to optimise.
* `goldsluice.compare` times what a user sees: the call from Python, the conversion of the input, the output arrays, against the
  Python mirror (timed on a sample of the traces and scaled, because it is slow).

Rust kernels run over the traces in parallel (`rayon`); `RAYON_NUM_THREADS=1` gives the single-thread time.

