This commit is contained in:
root
2025-08-12 11:34:04 +02:00
parent 7dbbfd6676
commit 098a550abc
17 changed files with 174 additions and 124 deletions

View File

@@ -6,8 +6,8 @@
3429,
2751
],
"reference_marker_width": 30000.0,
"reference_marker_height": 30000.0,
"reference_marker_width": 20000.0,
"reference_marker_height": 20000.0,
"angle_horizontal": 0.0,
"angle_vertical": 0.0
},

View File

@@ -6,8 +6,8 @@
1400,
1101
],
"reference_marker_width": 30000.0,
"reference_marker_height": 30000.0,
"reference_marker_width": 20000.0,
"reference_marker_height": 20000.0,
"angle_horizontal": 0.0,
"angle_vertical": 0.0
},

View File

@@ -6,8 +6,8 @@
72,
72
],
"reference_marker_width": 500.0,
"reference_marker_height": 500.0,
"reference_marker_width": 170.0,
"reference_marker_height": 170.0,
"angle_horizontal": 0.0,
"angle_vertical": 0.0
},

View File

@@ -6,8 +6,8 @@
2077,
2077
],
"reference_marker_width": 4280.0,
"reference_marker_height": 4280.0,
"reference_marker_width": 2140.0,
"reference_marker_height": 2140.0,
"angle_horizontal": 0.0,
"angle_vertical": 0.0
},

View File

@@ -1,13 +1,13 @@
{
"camera_calibration": {
"reference_marker": [
348,
348,
354,
354,
1200,
1200
],
"reference_marker_width": 2080.0,
"reference_marker_height": 2080.0,
"reference_marker_width": 1040.0,
"reference_marker_height": 1040.0,
"angle_horizontal": 0.0,
"angle_vertical": 0.0
},

View File

@@ -1,3 +1,3 @@
{
"#source_h5": "source_h5"
"source_h5": "source_h5"
}

View File

@@ -11,22 +11,24 @@
"camera_name": "X01DD-ES-CAM1",
"function": "sp_pipolar",
"fit": {
"delta_height": 50,
"height": 2181,
"width": 2832,
"delta_height": 40000,
"bg_range_low": [
0,
200
],
"bg_range_high": [
840,
1040
2631,
2831
],
"peak_search_rel_range": [
-1,
2
-2,
3
],
"valley_search_rel_range": [
-1,
2
-2,
3
]
},
"copy": true

View File

@@ -2,18 +2,17 @@
"image_background_enable": false,
"image_background": null,
"image_threshold": null,
"image_region_of_interest": [
941,
633,
945,
572
],
"image_good_region": null,
"image_region_of_interest": null,
"image_good_region": {
"threshold": 0.4,
"gfscale": 3.0
},
"image_slices": null,
"pipeline_type": "processing",
"camera_name": "X01DD-FE-CAM1",
"function": "epics_forwarder.py",
"function": "epics_forwarder_gr.py",
"name": "X01DD-FE-CAM1_proc",
"max_frame_rate": 10.1,
"throw_epics_errors": true
"throw_epics_errors": true,
"reload": true
}

View File

@@ -2,13 +2,11 @@
"image_background_enable": false,
"image_background": null,
"image_threshold": null,
"image_region_of_interest": [
941,
633,
945,
572
],
"image_good_region": null,
"image_region_of_interest": null,
"image_good_region": {
"threshold": 0.4,
"gfscale": 3.0
},
"image_slices": null,
"pipeline_type": "processing",
"camera_name": "X01DD-FE-CAM1",

View File

@@ -3,12 +3,16 @@
"image_background": null,
"image_threshold": null,
"image_region_of_interest": null,
"image_good_region": null,
"image_good_region": {
"threshold": 0.4,
"gfscale": 3.0
},
"image_slices": null,
"pipeline_type": "processing",
"camera_name": "X08DB-FE-CAM1",
"function": "epics_forwarder.py",
"function": "epics_forwarder_gr.py",
"name": "X08DB-FE-CAM1_proc",
"max_frame_rate": 10.1,
"throw_epics_errors": true
"throw_epics_errors": true,
"reload": true
}

View File

@@ -3,7 +3,10 @@
"image_background": null,
"image_threshold": null,
"image_region_of_interest": null,
"image_good_region": null,
"image_good_region": {
"threshold": 0.4,
"gfscale": 3.0
},
"image_slices": null,
"pipeline_type": "processing",
"camera_name": "X08DB-FE-CAM1",

View File

@@ -29,6 +29,5 @@
-1,
2
]
},
"reload": true
}
}

View File

@@ -1,6 +1,7 @@
{
"#ALBDI-SM-2_emittance": "ALBDI-SM-2_emittance",
"#ALIDI-SM-5_proc": "ALIDI-SM-5_proc",
"#X01DD-FE-CAM1_sp": "X01DD-FE-CAM1_sp",
"#simulation_proc_sp": "simulation_proc_sp",
"#simulation_sp1": "simulation_sp",
"ABODI-SM-1S1_proc": "ABODI-SM-1S1_proc",
@@ -25,6 +26,7 @@
"X01DD-ES-CAM1_emittance": "X01DD-ES-CAM1_emittance",
"X01DD-ES-CAM1_sp_proc": "X01DD-ES-CAM1_sp_proc",
"X01DD-FE-CAM1_proc": "X01DD-FE-CAM1_proc",
"X01DD-SC-CAM1_proc": "X01DD-SC-CAM1_proc",
"X08DB-FE-CAM1_proc": "X08DB-FE-CAM1_proc",
"emittance_pipolar": "emittance_pipolar"
}

View File

@@ -38,9 +38,12 @@
"instances": [
"ALIDI-SM-5_sp_proc",
"ALBDI-SM-2_sp_proc",
"#X01DD-FE-CAM1_sp",
"ALBDI-SM-2_emittance:9001",
"X01DD-ES-CAM1_emittance",
"X01DD-ES-CAM1_sp_proc"
"X01DD-FE-CAM1_proc",
"X01DD-ES-CAM1_sp_proc",
"X01DD-SC-CAM1_proc"
]
},
"http://sls-daqsync-43.psi.ch:8881": {

View File

@@ -11,6 +11,7 @@ mean_x_name, mean_y_name, sig_x_name, sig_y_name = None, None, None, None
ampl_x_pv, ampl_y_pv, integ_x_pv, integ_y_pv = None, None, None, None
ampl_x_name, ampl_y_name, integ_x_name, integ_y_name = None, None, None, None
sent_pid = -1
throw_epics_errors = False
initialized = False
def initialize(parameters):

View File

@@ -12,6 +12,7 @@ camera_name = None
sent_pid = -1
initialized = False
throw_epics_errors = False
def initialize(parameters, data):

View File

@@ -22,8 +22,50 @@ class PpolPeakValley():
self.x, self.y = self.ppol_interpol()
def ppol_interpol(self, plot_flag=False):
ipv = [32.976,20.848,15.306,12.121,10.0499899,8.595,7.5176,6.6871,6.0276,5.491,5.0471,4.673,4.353,4.0778,3.837,3.439,3.122,2.865,2.652,2.473,2.32]
sig = [3.659, 5.175, 6.338, 7.318, 8.182, 8.963, 9.681, 10.350, 10.978, 11.572, 12.136, 12.676, 13.194, 13.692, 14.172, 15.087, 15.950, 16.769, 17.549, 18.296, 19.014]
# ipv = [32.98,20.85,15.31,12.12,10.05,8.60,7.52,6.69,6.028,5.49,5.047,4.67,4.35,4.078,3.84,3.44,3.12,2.87,2.65,2.47,2.32]
ipv = [0.032771623, 0.045653159, 0.058377743, 0.070933335, 0.083347283, 0.095648417, 0.107858216, 0.119990516,
0.132053213, 0.144050065, 0.155982387, 0.167849814, 0.179651077, 0.19138435, 0.203047604, 0.22615558,
0.248958238, 0.271440041, 0.293587065, 0.315386908, 0.33682858]
sig = [3.66, 5.18, 6.34, 7.32, 8.18, 8.96, 9.68, 10.35, 10.98, 11.57, 12.14, 12.68, 13.19, 13.69, 14.17, 15.09,
15.95, 16.77, 17.55, 18.30, 19.014]
sig2 = [element * element for element in sig]
x = np.linspace(0, 999999, 999999)
x = [(1 + val) / 1000000 * max(ipv) for val in x]
interpfunc = interpolate.interp1d(ipv, sig2, kind='quadratic')
inter_x = interpfunc(x)
sqrt_val = [math.sqrt(val) for val in inter_x]
y = [0]
y.extend(sqrt_val)
y.append(max(sig))
xf = [0]
xf.extend(x)
xf.append(max(ipv))
return xf, y
def get_emittance(self, ratio):
emittance = 0
for i in range(0, len(self.x)):
if self.x[i] > ratio:
self.y[i - 1]
return self.y[i - 1]
# For peak search - delta(h) to max peak value
DELTA_HEIGHT = 400 # 400
BG_XRANGE_LOW = [340, 400] # 100 160 [340, 400]
BG_XRANGE_HIGH = [460, 520] # 840 900 [460, 520]
PEAK_SEARCH_REL_RANGE = [-1, 2]
VALLEY_SEARCH_REL_RANGE = [-2, 3]
class PpolPeakValley():
def __init__(self):
self.x, self.y = self.ppol_interpol()
def ppol_interpol(self, plot_flag=False):
ipv = [0.0, 32.976, 20.848, 15.306, 12.121, 10.050, 8.595, 7.518, 6.687, 6.028, 5.491, 5.047, 4.673, 4.353, 4.078, 3.837, 3.439, 3.122, 2.865, 2.652, 2.473, 2.320]
sig = [0.0, 3.659, 5.175, 6.338, 7.318, 8.182, 8.963, 9.681, 10.350, 10.978, 11.572, 12.136, 12.676, 13.194, 13.692, 14.172, 15.087, 15.950, 16.769, 17.549, 18.296, 19.014]
sig2 = [element * element for element in sig]
x = np.linspace(0, 999999, 999999)
x = [(1+val)/1000000*max(ipv) for val in x]
@@ -46,17 +88,16 @@ class PpolPeakValley():
return self.y[i-1]
#For peak search - delta(h) to max peak value
DELTA_HEIGHT = 400 # 400
BG_XRANGE_LOW = [340, 400] #100 160 [340, 400]
BG_XRANGE_HIGH = [460, 520] #840 900 [460, 520]
DELTA_HEIGHT = 400
BG_XRANGE_LOW = [340, 400] #100 160
BG_XRANGE_HIGH = [460, 520] #840 900
PEAK_SEARCH_REL_RANGE = [-1, 2]
VALLEY_SEARCH_REL_RANGE = [-2, 3]
ppol = PpolPeakValley()
#abreviations
# abreviations
BX1 = BG_XRANGE_LOW[0]
BX2 = BG_XRANGE_LOW[1]
BX3 = BG_XRANGE_HIGH[0]
@@ -68,16 +109,15 @@ ydata = []
def calculate_emittance(image, fit_pars):
global ydata # array of indexes
global ydata # array of indexes
DELTA_HEIGHT = fit_pars['delta_height']
BG_XRANGE_LOW = fit_pars['bg_range_low']
BG_XRANGE_HIGH = fit_pars['bg_range_high']
PEAK_SEARCH_REL_RANGE = fit_pars['peak_search_rel_range']
VALLEY_SEARCH_REL_RANGE = fit_pars['valley_search_rel_range']
w,h=len(image[0]),len(image)
if len(ydata)!=h:
w, h = len(image[0]), len(image)
if len(ydata) != h:
H = []
for i in range(0, h):
H.append(i)
@@ -90,63 +130,63 @@ def calculate_emittance(image, fit_pars):
image -= image.min()
projy = np.sum(image, axis=1)
projx = np.sum(image, axis=0)
#find peaks
# find peaks
max_element = np.amax(projy)
max_indices = np.where(projy == max_element)
peaks, _ = find_peaks(projy, height=(max_element - DELTA_HEIGHT))
_logger.debug("max indices /peaks " + str(max_indices) + " " + str(peaks))
_logger.debug("max indices /peaks " + str(max_indices) + " " + str(peaks))
if len(peaks) != 2:
mess = "Too few peaks found! " if len(peaks) < 2 else \
"Too many peaks found "
_logger.debug(mess + str(peaks))
peaks_buffer = []
for val in peaks:
### COMMENTED BY ALEX
#if val > 567 and val < 590:
peaks_buffer.append(val)
mess = "Too few peaks found! " if len(peaks) < 2 else \
"Too many peaks found "
_logger.debug(mess + str(peaks))
peaks_buffer = []
for val in peaks:
### COMMENTED BY ALEX
# if val > 567 and val < 590:
peaks_buffer.append(val)
#if len(peaks_buffer) ==3:
# peaks = [None] * 2
# peaks[0] = peaks_buffer[0]
# peaks[1] = peaks_buffer[2]
if len(peaks_buffer) !=2:
return (-1.0, -2.0)
else:
peaks = peaks_buffer
# if len(peaks_buffer) ==3:
# peaks = [None] * 2
# peaks[0] = peaks_buffer[0]
# peaks[1] = peaks_buffer[2]
if len(peaks_buffer) != 2:
return (-1.0, -2.0)
else:
peaks = peaks_buffer
if (peaks[1] - peaks[0]) < 6:
_logger.debug("Peaks are too close: " + str(peaks[1] - peaks[0]))
raise Exception("Peaks are too close")
#peaks =[569, 577]
#Distance to minimum
# peaks =[569, 577]
# Distance to minimum
min_element = np.amin(projy[peaks[0]:peaks[1]])
min_indices = np.where(projy == min_element)
min_idx_value = 0 #min_indices[0][0]
min_idx_value = 0 # min_indices[0][0]
for val in min_indices[0]:
if val > peaks[0] and val < peaks[1]:
min_idx_value = val
break
min_idx_value = val
break
if min_idx_value == 0:
raise Exception("min_idx_value == 0")
for i in range (0, len(peak_array)):
peak_array[i] = ydata[peaks[i]+plr[0] : peaks[i]+plr[1]]
valley_array = ydata[min_idx_value+vlr[0] : min_idx_value+vlr[1]]
for i in range(0, len(peak_array)):
peak_array[i] = ydata[peaks[i] + plr[0]: peaks[i] + plr[1]]
valley_array = ydata[min_idx_value + vlr[0]: min_idx_value + vlr[1]]
#print("peaks", peaks, flush=True)
#print(np.subtract(peaks, h)*(-1))
#print("projections peak, valley", projy[(peaks[0]-1):(peaks[1]+2)], projy[valley_array])
#print(peak_array, valley_array, flush=True)
#x_bg_center = min_indices[0][0]
# print("peaks", peaks, flush=True)
# print(np.subtract(peaks, h)*(-1))
# print("projections peak, valley", projy[(peaks[0]-1):(peaks[1]+2)], projy[valley_array])
# print(peak_array, valley_array, flush=True)
# x_bg_center = min_indices[0][0]
#background
bg_y1 = projy[BX1 : BX2]
bg_y2 = projy[BX3 : BX4]
# background
bg_y1 = projy[BX1: BX2]
bg_y2 = projy[BX3: BX4]
bg_yS = np.concatenate((bg_y1, bg_y2))
bg_xS = list(range(BX1, BX2)) + list(range(BX3, BX4))
@@ -156,57 +196,57 @@ def calculate_emittance(image, fit_pars):
for x, y in zip(bg_xS, bg_yS):
### COMMENTED BY ALEX
#if y > 800 and y < 1000:
bg_x.append(x)
bg_y.append(y)
# if y > 800 and y < 1000:
bg_x.append(x)
bg_y.append(y)
#print(bg_x, flush=True)
#print(bg_y, flush=True)
#fit
# print(bg_x, flush=True)
# print(bg_y, flush=True)
# fit
poly_bg = np.polyfit(bg_x, bg_y, deg=1)
array_bg = np.linspace(0, h, 10400)
val_bg = np.polyval(poly_bg, array_bg)
for i in range (0, len(proj_peak_array)):
proj_peak_array[i] = projy[peaks[i]+plr[0] : peaks[i]+plr[1]]
#proj_peak_array[1] = projy[peaks[1]-1 : peaks[1]+2]
proj_valley = projy[min_idx_value+vlr[0] : min_idx_value+vlr[1]]
for i in range(0, len(proj_peak_array)):
proj_peak_array[i] = projy[peaks[i] + plr[0]: peaks[i] + plr[1]]
# proj_peak_array[1] = projy[peaks[1]-1 : peaks[1]+2]
proj_valley = projy[min_idx_value + vlr[0]: min_idx_value + vlr[1]]
#peaks
# peaks
for i in range(0, 2):
poly = np.polyfit(peak_array[i], proj_peak_array[i], deg=2)
idx = -poly[1]/2/poly[0]
idx = -poly[1] / 2 / poly[0]
peak_value[i] = np.polyval(poly, idx)
peak_bg[i] = val_bg[int(idx)]
#valley
# valley
poly2 = np.polyfit(valley_array, proj_valley, deg=2)
#Only works for deg=2
minv_idx = -poly2[1]/2/poly2[0]
# Only works for deg=2
minv_idx = -poly2[1] / 2 / poly2[0]
poly = np.polyfit(valley_array, proj_valley, deg=4)
valley_subarray = np.linspace(valley_array[0], valley_array[-1], 800)
poly_array = np.polyval(poly, valley_subarray)
poly_array = np.polyval(poly, valley_subarray)
valley_fitted_value = min(poly_array)
#print("peak value", peak_value, "valley_fitted value", valley_fitted_value, flush=True)
# print("peak value", peak_value, "valley_fitted value", valley_fitted_value, flush=True)
valley_bg = val_bg[int(minv_idx)]
#print("valley background", valley_bg, "peak background", peak_bg[0], peak_bg[1], flush=True)
# print("valley background", valley_bg, "peak background", peak_bg[0], peak_bg[1], flush=True)
### COMMENTED BY ALEX
#if valley_fitted_value < valley_bg:
# if valley_fitted_value < valley_bg:
# valley_fitted_value = min( projy[valley_array])
# if valley_fitted_value < valley_bg:
# raise Exception("valley_fitted_value < valley_bg")
# #return
ratio_corrected = 2*(valley_fitted_value-valley_bg)/(
abs((peak_value[0]-peak_bg[0])+(peak_value[1]-peak_bg[1])))
ratio_corrected = 2 * (valley_fitted_value - valley_bg) / (
abs((peak_value[0] - peak_bg[0]) + (peak_value[1] - peak_bg[1])))
idx = 0 if abs(peak_value[0]-peak_bg[0]) > abs(peak_value[1]-peak_bg[1]) else 1
ratio_max = (valley_fitted_value-valley_bg)/abs(peak_value[idx]-peak_bg[idx])
idx = 0 if abs(peak_value[0] - peak_bg[0]) > abs(peak_value[1] - peak_bg[1]) else 1
ratio_max = (valley_fitted_value - valley_bg) / abs(peak_value[idx] - peak_bg[idx])
emittance = ppol.get_emittance(ratio_corrected)
emittance2 = ppol.get_emittance(ratio_max)
@@ -215,7 +255,6 @@ def calculate_emittance(image, fit_pars):
return (emittance, emittance2)
def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata=None):
channel_prefix = parameters["camera_name"]
emittance = emittance2 = float("NaN")
@@ -223,19 +262,18 @@ def process_image(image, pulse_id, timestamp, x_axis, y_axis, parameters, bsdata
try:
ret = calculate_emittance(image, parameters['fit'])
if ret is not None:
emittance,emittance2=ret
emittance, emittance2 = ret
except Exception as e:
status="Error: "
exc_type, exc_obj, exc_tb = sys.exc_info()
while exc_tb is not None:
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
status = status + fname + " line " + str(exc_tb.tb_lineno) + ": " + str(e) + " | "
exc_tb = exc_tb.tb_next
ret ={}
status = "Error: "
exc_type, exc_obj, exc_tb = sys.exc_info()
while exc_tb is not None:
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
status = status + fname + " line " + str(exc_tb.tb_lineno) + ": " + str(e) + " | "
exc_tb = exc_tb.tb_next
ret = {}
ret[channel_prefix + ":emmitance"] = emittance
ret[channel_prefix + ":emmitance2"] = emittance2
ret[channel_prefix + ":status"] = str(status)
ret[channel_prefix + ":status"] = str(status)
return ret