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