Add loading data file from c extension for Furka

This commit is contained in:
root
2023-06-07 17:00:38 +02:00
parent 103adcab03
commit 850baa5816
21 changed files with 226 additions and 123 deletions

View File

@@ -66,8 +66,8 @@ def edge(filter_name, backgrounds, signals, peakback):
# interpolate to get evenly sampled in frequency space
sig_interp = interpolate(nus, nus_new, sig_norm)
# Fourier filter
#sig_filtered = fourier_filter(sig_interp, ffilter)
sig_filtered = sig_interp
sig_filtered = fourier_filter(sig_interp, ffilter)
#sig_filtered = sig_interp
# interpolate to get unevenly sampled in frequency space (back to original wavelength space)
sig_uninterp = interpolate(nus_new, nus, sig_filtered[..., 0:2048]) + 1
# peak via the derivative

View File

@@ -14,7 +14,7 @@ def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata
image_buffer.append(image)
try:
frames = numpy.array(image_buffer)
image = numpy.average(frames, 0)
image = numpy.average(frames, 0)*averaging
#_logger.info("Averaged: %d" % len(image_buffer))
except:
# Different shapes

View File

@@ -25,6 +25,81 @@ void setArrayToValue(double array[], int size, double value) {
}
}
//Initialization: Threshold & background
initialized = 0;
//const char *THRESHOLD_FILE = "/configuration/user_scripts/lib/Thresh_2D_start600_size800.npy";
const char *THRESHOLD_FILE = "/configuration/user_scripts/lib/threshold_2d_start600_800.txt";
int threshold_num_elements=-1;
double *threshold= NULL;
double *background= NULL;
const int MAX_LINE_LENGTH = 50000;
int initialize(int size_x, int size_y, PyObject *pars){
//background (all matrices are indexed in 1d)
background = malloc(size_x*size_y*sizeof(double));
for(int i=0; i<size_y;i++) {
for(int j=0;j<size_x;j++) {
background[i*size_x+j]=0.0;
}
}
threshold = (double *)malloc(size_x*size_y*sizeof(double));
int ret = 1;
if (THRESHOLD_FILE!=NULL){
FILE *file = fopen(THRESHOLD_FILE, "rb");
if (file == NULL) {
printf("Failed to open file.\n");
return -1;
}
char line[MAX_LINE_LENGTH];
int x = 0;
int y = 0;
while (fgets(line, sizeof(line), file) != NULL) {
char* token = strtok(line, " ");
while (token != NULL) {
threshold[y * size_x + x] = atof(token);
token = strtok(NULL, " ");
x++;
}
y++;
if (x != size_x){
printf("Invalid threshold file: wrong number of columns\n");
ret = -2;
break;
}
x = 0;
}
if (y != size_y){
printf("Invalid threshold file: wrong number of rows\n");
ret = -3;
}
fclose(file);
if (ret<0){
free(threshold); threshold = NULL;
}
} else {
PyObject* threshold_obj = PyDict_GetItemString(pars, "threshold");
double threshold_val = 60000.0;
if (threshold_obj!=NULL){
if (PyFloat_Check(threshold_obj)) {
threshold_val = PyFloat_AsDouble(threshold_obj);
} else if (PyLong_Check(threshold_obj)) {
threshold_val = PyLong_AsDouble(threshold_obj);
}
}
for(int i=0; i<size_y;i++) {
for(int j=0;j<size_x;j++) {
threshold[i*size_x+j]=100.0;//threshold_val; #FIX ME
}
}
}
return ret;
}
//def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata=None):
PyObject *process(PyObject *self, PyObject *args)
{
@@ -55,33 +130,16 @@ PyObject *process(PyObject *self, PyObject *args)
unsigned short* img_data = (unsigned short*)image->data;
//Initialization
if (initialized==0){
initialized = initialize(size_x, size_y, pars);
}
int i,j,l;
int i_dim=size_y;
int j_dim=size_x;
PyObject* threshold_obj = PyDict_GetItemString(pars, "threshold");
double threshold_val = 60000.0;
if (threshold_obj!=NULL){
if (PyFloat_Check(threshold_obj)) {
threshold_val = PyFloat_AsDouble(threshold_obj);
} else if (PyLong_Check(threshold_obj)) {
threshold_val = PyLong_AsDouble(threshold_obj);
}
}
double *threshold = malloc(i_dim*j_dim*sizeof(double));
for(i=0; i<i_dim;i++) {
for(j=0;j<j_dim;j++) {
threshold[i*j_dim+j]=100.0;//threshold_val;
}
}
//background (all matrices are indexed in 1d)
double *background = malloc(i_dim*j_dim*sizeof(double));
for(i=0; i<i_dim;i++) {
for(j=0;j<j_dim;j++) {
background[i*j_dim+j]=0.0;
}
}
double *frameBKsub = malloc(i_dim*j_dim*sizeof(double));
for(i=0; i<i_dim;i++) {
@@ -89,7 +147,10 @@ PyObject *process(PyObject *self, PyObject *args)
frameBKsub[i*j_dim+j]=(double)img_data[i*j_dim+j] - background[i*j_dim+j];
}
}
int evnt_num = func_ph_1d_double( frameBKsub, i_dim, j_dim, threshold);
// int evnt_num = func_ph_1d_double( (double)img_data, i_dim, j_dim, threshold);
//Create return dictionary
PyObject *ret = PyDict_New();
@@ -111,8 +172,6 @@ PyObject *process(PyObject *self, PyObject *args)
//}
//free(evns1d.evnt_ijc);
free(frameBKsub);
free(background);
free(threshold);
return ret;
}

View File

@@ -1,5 +1,7 @@
import sys
import json
from functools import lru_cache
import numpy as np
def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata=None):
@@ -16,12 +18,17 @@ def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata
project_axis = parameters.get("project_axis", 0)
threshold = parameters.get("threshold")
roi_radial = parameters.get("roi_radial")
radial_x0 = parameters.get("radial_x0")
radial_y0 = parameters.get("radial_y0")
# maintain the structure of processing_parameters
background_shape = None
# maintain the structure of res
projection_signal = projection_background = None
projection_radial_bins = projection_radial_counts = None
try:
@@ -40,6 +47,9 @@ def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata
if roi_background is not None:
projection_background = get_roi_projection(image, roi_background, project_axis)
if roi_radial is not None:
projection_radial_bins, projection_radial_counts = get_radial_projection(image, roi_radial, radial_x0, radial_y0)
except Exception as e:
lineno = sys.exc_info()[2].tb_lineno
tn = type(e).__name__
@@ -60,6 +70,10 @@ def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata
"project_axis": project_axis,
"threshold": threshold,
"roi_radial": roi_radial,
"radial_x0": radial_x0,
"radial_y0": radial_y0,
"status": status
}
@@ -69,7 +83,10 @@ def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata
camera_name + ".processing_parameters": processing_parameters,
camera_name + ".projection_signal": projection_signal,
camera_name + ".projection_background": projection_background
camera_name + ".projection_background": projection_background,
camera_name + ".projection_radial_bins": projection_radial_bins,
camera_name + ".projection_radial_counts": projection_radial_counts
}
return res
@@ -83,3 +100,28 @@ def get_roi_projection(image, roi, axis):
return project
def get_radial_projection(image, roi, x0, y0):
x_start, x_stop, y_start, y_stop = roi
image = image[x_start:x_stop, y_start:y_stop]
r, norm, ur = calc_r_norm_ur(image.shape, x0, y0)
image = image.ravel()
count = np.bincount(r, image)
res = count / norm
return ur, res
@lru_cache
def calc_r_norm_ur(shape, x0, y0):
r = calc_r(shape, x0, y0)
norm = np.bincount(r)
ur = np.unique(r)
return r, norm, ur
def calc_r(shape, x0, y0):
y, x = np.indices(shape)
r = np.sqrt((x - x0)**2 + (y - y0)**2)
r = r.ravel().astype(int)
return r