Python API

The rustpix Python package provides thin wrappers around the high-performance Rust core. Data is returned as NumPy arrays or PyArrow Tables for seamless integration with the scientific Python ecosystem.

Overview

FunctionDescription
read_tpx3_hitsRead all hits from a TPX3 file
stream_tpx3_hitsStream hits in batches
process_tpx3_neutronsProcess hits into neutron events
stream_tpx3_neutronsStream neutron events in batches
cluster_hitsCluster an existing HitBatch

Data Types

HitBatch

Contains raw detector hits with the following fields:

FieldTypeDescription
xuint16X coordinate (pixels)
yuint16Y coordinate (pixels)
tofuint32Time-of-flight (25ns ticks)
totuint16Time-over-threshold (charge proxy)
timestampuint32Raw timestamp
chip_iduint8Detector chip ID
cluster_idint32Cluster assignment (-1 if unclustered)

Note: The tof field is stored in 25ns tick units for efficiency. To convert to nanoseconds: tof_ns = tof * 25

NeutronBatch

Contains processed neutron events:

FieldTypeDescription
xfloat64Centroid X (sub-pixel resolution)
yfloat64Centroid Y (sub-pixel resolution)
tofuint32Time-of-flight (25ns ticks)
totuint16Total charge (sum of hit ToT)
n_hitsuint16Number of hits in cluster
chip_iduint8Detector chip ID

Note: The tof field is stored in 25ns tick units. To convert to nanoseconds: tof_ns = tof * 25

Output Formats

Both HitBatch and NeutronBatch support:

# Convert to NumPy dict of arrays
data = batch.to_numpy()

# Convert to PyArrow Table (requires pyarrow)
table = batch.to_arrow()

Algorithms

Three clustering algorithms are available:

AlgorithmDescriptionBest For
absAdjacency-Based Search (8-connectivity)General use, balanced
dbscanDensity-based spatial clusteringNoisy data
gridParallel grid-based clusteringLarge datasets

Specify with the algorithm keyword argument:

neutrons = rustpix.process_tpx3_neutrons(
    "data.tpx3",
    algorithm="abs"  # or "dbscan", "grid"
)

Next Steps