Commit 8d6b8417 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Add Detector class

parent 888f5284
import sys
from pycrystfel.bindings import Detector
det = Detector.from_filename(sys.argv[1])
print(f"{det.n_panels} panels:")
for name in det.panel_names:
print(f"- {name}")
......@@ -44,8 +44,6 @@ cdef featurelist_to_dataframe(crystfel.ImageFeatureList *flist):
arr.T, columns=['ss', 'fs', 'rx', 'ry', 'rz', 'intensity']
)
def find_peaks_single_panel(cnp.ndarray[cnp.npy_float, ndim=2] data, str geom_file):
cdef:
crystfel.beam_params beam
......@@ -67,3 +65,40 @@ def find_peaks_single_panel(cnp.ndarray[cnp.npy_float, ndim=2] data, str geom_fi
crystfel.free_detector_geometry(image.det)
return featurelist_to_dataframe(image.features)
# Pointer wrapper machinery based on Cython docs:
# https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#instantiation-from-existing-c-c-pointers
cdef class Detector:
cdef crystfel.detector* _ptr
cdef bint ptr_owner
def __cinit__(self):
self.ptr_owner = False
def __dealloc__(self):
# De-allocate if not null and flag is set
if self._ptr is not NULL and self.ptr_owner is True:
free(self._ptr)
self._ptr = NULL
@staticmethod
cdef Detector from_ptr(crystfel.detector* _ptr, bint owner=False):
# Call to __new__ bypasses __init__ constructor
cdef Detector det = Detector.__new__(Detector)
det._ptr = _ptr
det.ptr_owner = owner
return det
@classmethod
def from_filename(cls, path):
return Detector.from_ptr(
crystfel.get_detector_geometry(os.fsencode(path), NULL), owner=True
)
@property
def n_panels(self):
return self._ptr.n_panels if self._ptr is not NULL else 0
def panel_names(self):
return [self._ptr.panels[i].name for i in range(self.n_panels)]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment