Commit 5af39fb4 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Changes for API in CrystFEL master branch

parent 8db4c7ee
......@@ -16,65 +16,69 @@ def progress_bar(val, total, text):
def inspect_geom_file(path):
cdef crystfel.detector *det
cdef crystfel.beam_params beam
det = crystfel.get_detector_geometry(os.fsencode(path), &beam)
print("n_panels:", det.n_panels)
print("panel names:", [det.panels[i].name for i in range(det.n_panels)])
print("photon energy:", beam.photon_energy)
crystfel.DataTemplate* dt
crystfel.image* img
dt = crystfel.data_template_new_from_file(os.fsencode(path))
img = crystfel.image_create_for_simulation(dt)
print("n_panels:", img.detgeom.n_panels)
print("panel names:", [
img.detgeom.panels[i].name for i in range(img.detgeom.n_panels)
cdef featurelist_to_dataframe(crystfel.ImageFeatureList *flist):
int i, nfeat = crystfel.image_feature_count(flist)
crystfel.imagefeature *feat
cnp.ndarray arr
double[:] ss, fs, rx, ry, rz, it
double[:] ss, fs, it
arr = np.zeros((6, nfeat), dtype=np.double)
ss, fs, rx, ry, rz, it = arr
arr = np.zeros((3, nfeat), dtype=np.double)
ss, fs, it = arr
with cython.boundscheck(False), cython.wraparound(False):
for i in range(nfeat):
feat = crystfel.image_get_feature(flist, i)
ss[i] =
fs[i] = feat.fs
rx[i] = feat.rx
ry[i] = feat.ry
rz[i] = feat.rz
it[i] = feat.intensity
return pd.DataFrame(
arr.T, columns=['ss', 'fs', 'rx', 'ry', 'rz', 'intensity']
arr.T, columns=['ss', 'fs', 'intensity']
def find_peaks_single_panel(cnp.ndarray[cnp.npy_float, ndim=2] data, str geom_file):
crystfel.DataTemplate* dt
crystfel.beam_params beam
crystfel.image image
crystfel.image* image
image.dp[0] = <float*>
image.bad = NULL
image.det = crystfel.get_detector_geometry(os.fsencode(geom_file), &beam)
print(f"Loaded geometry with {image.det.n_panels} panels")
dt = crystfel.data_template_new_from_file(os.fsencode(geom_file))
image = crystfel.image_create_for_simulation(dt)
image.features = NULL
image.dp[0] = <float*>
print(f"Loaded geometry with {image.detgeom.n_panels} panels")
&image, max_n_peaks=200, threshold=20, min_snr=3, min_pix_count=2,
image, max_n_peaks=200, threshold=20, min_snr=3, min_pix_count=2,
max_pix_count=200, local_bg_radius=3, min_res=0, max_res=1200,
df = featurelist_to_dataframe(image.features)
return featurelist_to_dataframe(image.features)
# Pointer wrapper machinery based on Cython docs:
cdef class Detector:
cdef crystfel.detector* _ptr
cdef crystfel.image* _img
cdef bint ptr_owner
def __cinit__(self):
......@@ -82,27 +86,29 @@ cdef class Detector:
def __dealloc__(self):
# De-allocate if not null and flag is set
if self._ptr is not NULL and self.ptr_owner is True:
self._ptr = NULL
if self._img is not NULL and self.ptr_owner is True:
self._img = NULL
cdef Detector from_ptr(crystfel.detector* _ptr, bint owner=False):
cdef Detector from_image_ptr(crystfel.image* _img, bint owner=False):
# Call to __new__ bypasses __init__ constructor
cdef Detector det = Detector.__new__(Detector)
det._ptr = _ptr
det._img = _img
det.ptr_owner = owner
return det
def from_filename(cls, path):
return Detector.from_ptr(
crystfel.get_detector_geometry(os.fsencode(path), NULL), owner=True
cdef crystfel.DataTemplate* dt = \
cdef crystfel.image* img = crystfel.image_create_for_simulation(dt)
return Detector.from_image_ptr(img)
def n_panels(self):
return self._ptr.n_panels if self._ptr is not NULL else 0
return self._img.detgeom.n_panels if self._img is not NULL else 0
def panel_names(self):
return [self._ptr.panels[i].name.decode('utf-8') for i in range(self.n_panels)]
return [self._img.detgeom.panels[i].name.decode('utf-8') for i in range(self.n_panels)]
......@@ -30,20 +30,27 @@ cdef extern from "crystfel/index.h":
cdef extern from "crystfel/detector.h":
cdef struct panel:
char name[1024]
double coffset
double clen
cdef extern from "crystfel/detgeom.h":
cdef struct detgeom_panel:
char* name
double cnx
double cny
double cnz
# ...
cdef struct detector:
panel *panels
cdef struct detgeom:
detgeom_panel* panels
int n_panels
# ...
detector* get_detector_geometry(const char *filename, beam_params *beam)
void free_detector_geometry(detector *det)
void detgeom_free(detgeom* det)
cdef extern from "crystfel/datatemplate.h":
ctypedef struct DataTemplate:
pass # Opaque struct, used through functions
DataTemplate *data_template_new_from_file(const char* filename)
DataTemplate *data_template_new_from_string(const char* string_in)
void data_template_free(DataTemplate* dt)
cdef extern from "crystfel/spectrum.h":
ctypedef struct Spectrum:
......@@ -57,29 +64,25 @@ cdef extern from "crystfel/image.h":
double bandwidth
cdef struct imagefeature: # description of a peak
image *parent
panel *p
int pn # Panel number
double intensity
const char *name
double fs # Fast-scan & slow-scan coordinates, pixels from panel corner
double ss
double rx # Reciprocal space coordinates (m^-1)
double ry
double rz
ctypedef struct ImageFeatureList:
pass # Opaque struct, used through functions
ImageFeatureList* image_feature_list_new()
void image_feature_list_free(ImageFeatureList *flist)
void image_feature_list_free(ImageFeatureList* flist)
void image_add_feature(
ImageFeatureList *flist, double x, double y, panel *p, image *parent,
double intensity, const char *name
ImageFeatureList* flist, double x, double y, int pn, image* parent,
double intensity, const char* name
imagefeature* image_get_feature(ImageFeatureList *flist, int idx)
void image_remove_feature(ImageFeatureList *flist, int idx)
imagefeature* image_feature_closest(
ImageFeatureList *flist, double fs, double ss, panel *p, double *d, int *idx
ImageFeatureList* flist, double fs, double ss, int pn, double* d, int* idx
int image_feature_count(ImageFeatureList *flist)
......@@ -94,18 +97,22 @@ cdef extern from "crystfel/image.h":
int n_crystals
IndexingMethod indexed_by
int n_indexing_tries
detector *det
beam_params *beam
detgeom* detgeom
char* filename
char* ev # Event ID
double avg_clen
int id
int serial
Spectrum* spectrum
double lambda_ "lambda"
double div
double bw
double peak_resolution
ImageFeatureList *features
char* filename
double lambda_ "lambda" # Wavelength of the incident radiation, in metres
double div # Convergence angle of the incident ration, in radians (full angle)
double bw # Full-width half-maximum bandwidth as a fraction, applied to wavelength
double peak_resolution # Resolution estimate based on peaks
ImageFeatureList* features
image* image_new()
void image_free(image* image)
image* image_create_for_simulation(const DataTemplate* dtempl)
cdef extern from "crystfel/peaks.h":
int search_peaks_peakfinder8(
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