mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-09-07 11:42:37 +02:00
118 lines
3.8 KiB
Python
118 lines
3.8 KiB
Python
# SPDX-License-Identifier: MPL-2.0
|
|
from . import _aare
|
|
import numpy as np
|
|
|
|
_supported_cluster_sizes = [(2,2), (3,3), (5,5), (7,7), (9,9),]
|
|
|
|
def _type_to_char(dtype):
|
|
if dtype == np.int32:
|
|
return 'i'
|
|
elif dtype == np.float32:
|
|
return 'f'
|
|
elif dtype == np.float64:
|
|
return 'd'
|
|
elif dtype == np.int16:
|
|
return 'i16'
|
|
else:
|
|
raise ValueError(f"Unsupported dtype: {dtype}. Only np.int32, np.float32, and np.float64 are supported.")
|
|
|
|
def _get_class(name, cluster_size, dtype):
|
|
"""
|
|
Helper function to get the class based on the name, cluster size, and dtype.
|
|
"""
|
|
try:
|
|
class_name = f"{name}_Cluster{cluster_size[0]}x{cluster_size[1]}{_type_to_char(dtype)}"
|
|
cls = getattr(_aare, class_name)
|
|
except AttributeError:
|
|
raise ValueError(f"Unsupported combination of type and cluster size: {dtype}/{cluster_size} when requesting {class_name}")
|
|
return cls
|
|
|
|
|
|
|
|
def ClusterFinder(image_size, cluster_size=(3,3), n_sigma=5, dtype = np.int32, capacity = 1024):
|
|
"""
|
|
Factory function to create a ClusterFinder object. Provides a cleaner syntax for
|
|
the templated ClusterFinder in C++.
|
|
"""
|
|
cls = _get_class("ClusterFinder", cluster_size, dtype)
|
|
return cls(image_size, n_sigma=n_sigma, capacity=capacity)
|
|
|
|
|
|
|
|
def ClusterFinderMT(image_size, cluster_size = (3,3), dtype=np.int32, n_sigma=5, capacity = 1024, n_threads = 3):
|
|
"""
|
|
Factory function to create a ClusterFinderMT object. Provides a cleaner syntax for
|
|
the templated ClusterFinderMT in C++.
|
|
"""
|
|
|
|
cls = _get_class("ClusterFinderMT", cluster_size, dtype)
|
|
return cls(image_size, n_sigma=n_sigma, capacity=capacity, n_threads=n_threads)
|
|
|
|
|
|
def ClusterCollector(clusterfindermt, dtype=np.int32):
|
|
"""
|
|
Factory function to create a ClusterCollector object. Provides a cleaner syntax for
|
|
the templated ClusterCollector in C++.
|
|
"""
|
|
|
|
cls = _get_class("ClusterCollector", clusterfindermt.cluster_size, dtype)
|
|
return cls(clusterfindermt)
|
|
|
|
def ClusterFileSink(clusterfindermt, cluster_file, dtype=np.int32):
|
|
"""
|
|
Factory function to create a ClusterCollector object. Provides a cleaner syntax for
|
|
the templated ClusterCollector in C++.
|
|
"""
|
|
|
|
cls = _get_class("ClusterFileSink", clusterfindermt.cluster_size, dtype)
|
|
return cls(clusterfindermt, cluster_file)
|
|
|
|
|
|
def ClusterFile(fname, cluster_size=(3,3), dtype=np.int32, chunk_size = 1000, mode = "r"):
|
|
"""Create a reader or writer for a legacy binary cluster file.
|
|
|
|
Parameters
|
|
----------
|
|
fname : path-like
|
|
Cluster file to open.
|
|
cluster_size : tuple[int, int], default=(3, 3)
|
|
Cluster dimensions stored in the file.
|
|
dtype : numpy dtype, default=numpy.int32
|
|
Data type of the cluster values stored in the file.
|
|
chunk_size : int, default=1000
|
|
Maximum number of selected clusters returned by each iterator step.
|
|
mode : {"r", "w", "a"}, default="r"
|
|
Open for reading, truncate and write, or append, respectively.
|
|
|
|
Returns
|
|
-------
|
|
ClusterFile
|
|
The compiled ClusterFile specialization matching ``cluster_size`` and
|
|
``dtype``.
|
|
|
|
Notes
|
|
-----
|
|
The file format contains no cluster shape or data-type metadata. Supplying
|
|
values that do not match the file causes its bytes to be interpreted
|
|
incorrectly. Iterator chunks may combine frames, so their frame number is
|
|
not reliable per-cluster metadata.
|
|
|
|
Examples
|
|
--------
|
|
|
|
.. code-block:: python
|
|
|
|
from aare import ClusterFile
|
|
|
|
with ClusterFile(
|
|
"clusters.clust", cluster_size=(3, 3), dtype=np.int32
|
|
) as cf:
|
|
for clusters in cf:
|
|
# Process clusters in chunks of at most 1000.
|
|
...
|
|
|
|
"""
|
|
|
|
cls = _get_class("ClusterFile", cluster_size, dtype)
|
|
return cls(fname, chunk_size=chunk_size, mode=mode)
|