Included Memory Shift

This commit is contained in:
gac-x11ma
2022-11-09 15:03:09 +01:00
parent d6803e82ba
commit 2ff79ee1c5
71 changed files with 1530 additions and 177 deletions
+135
View File
@@ -0,0 +1,135 @@
MAX_SHIFT = 300
SLEEP_MOVE = 3.0
CALIBRATE_RANGE = 20.0
shift_ref = shift_ref_x = shift_ref_y = None
def get_image_shift(cur_data=None, former_data=None, roi=None):
global shift_ref
if roi is None:
roi=get_focus_scan_roi()
if roi is None:
roi = Rectangle(128,128,256,256)
if former_data is None:
former_data = shift_ref
if former_data is None:
raise Exception("Image reference not available")
if cur_data is None:
cur_data = get_cur_data()
print roi
calc_roi = Roi(roi.x,roi.y,roi.width, roi.height)
try:
xoff, yoff, error, diffphase, _ = calculate_shift(former_data,cur_data, calc_roi)
print "Calculated shift: ", xoff, yoff, error, diffphase
if ( abs(xoff) <= MAX_SHIFT) and (abs(yoff) <= MAX_SHIFT):
return -xoff, -yoff
else:
raise Exception("Error - shift too big: " + str(xoff) + "," + str(yoff))
except:
raise Exception("Error calculating shift: " + str(sys.exc_info()[1]))
def get_cur_data():
return image.data
def grab_shift_ref():
global shift_ref, shift_ref_x, shift_ref_y
#TODO: read from device
obj_align_x.write(0.0);obj_align_y.write(0.0)
time.sleep(SLEEP_MOVE)
shift_ref_x = shift_ref_y = 0.0
shift_ref = get_cur_data()
def calibrate_image_shift():
#TODO: Must find way to read current OBJDX and OBJDY values
obj_align_y.write(0.0)
obj_align_x.write(-CALIBRATE_RANGE)
time.sleep(SLEEP_MOVE)
grab_shift_ref()
obj_align_x.write(CALIBRATE_RANGE)
time.sleep(SLEEP_MOVE)
xdx, xdy = get_image_shift()
obj_align_x.write(0.0)
obj_align_y.write(-CALIBRATE_RANGE)
time.sleep(SLEEP_MOVE)
grab_shift_ref()
obj_align_y.write(CALIBRATE_RANGE)
time.sleep(SLEEP_MOVE)
ydx, ydy = get_image_shift()
obj_align_x.write(0.0)
obj_align_y.write(0.0)
time.sleep(SLEEP_MOVE)
#TODO calcs are wrong
#s = hipot(mdy,mdx) / hipot(idt,idx)
# cos beta = mdx/mdy
# th alpha = idy/idx
#teta = alpha - beta
ixt, iyt = math.hypot(xdx, xdy), math.hypot(ydx, ydy) #image move
mdx= mdy = 2 * 10.0 #Total motor move
sx,sy = ixt/mdx, iyt/mdy #Scale
s = mean ((sx,sy)) #supposing x and y scale equal
#angle
t1 = math.acos((-xdx/sx)/mdx)
t2 = math.asin((xdy/sx)/mdx)
t3 = math.acos((ydy/sy)/mdy)
t4 = math.asin((ydx/sy)/mdy)
theta = mean((t1,t2,t3,t4))
set_setting("image_shift_angle", theta)
set_setting("image_shift_scale", s)
print s, math.degrees(theta)
def correct_image_shift():
angle = get_setting("image_shift_angle")
scale = get_setting("image_shift_scale")
if (angle is None) or (scale is None) :
raise Exception("Image shift not calibrated")
angle, scale = float(angle), float(scale)
idx, idy = get_image_shift()
#idx, idy = idx/scale, idy/scale
it = math.hypot(idx, idy)
#mdx = idx / math.cos(angle)
#mdy = idy / math.sin(angle)
mt = it/scale
#mdx = mt * math.sin(angle)
#mdy = mt * math.cos(angle)
try:
t=math.atan(idy/idx)
t2 = t+angle
#mdx = it * math.cos(-t2)
#mdy = it * math.sin(-t2)
mdx = idx * math.cos(t2)
mdy = idy * math.sin(t2)
#mdx = mt * math.cos(t2)
#mdy = mt * math.sin(t2)
except:
#ZeroDivisionError
mdx = mdy = 0.0
mdx=mdx+shift_ref_x
mdy=mdy+shift_ref_y
print "Moving " , mdx, ", ", mdy
def translate(shift, image):
#shifts= load_shifts("{images}/TestObjAligner/shifts.mat")
stack = load_test_stack(show=True)
r=translate(stack, shifts, show=True)
#
#grab_shift_ref()
#print get_image_shift()
#calibrate_image_shift()
+3 -3
View File
@@ -6,7 +6,7 @@ run ("imaging/shifts")
roi=Roi(256,0,128,128)
stack = load_test_stack(show=False, size=9)
ipr, ipi = complex_edge_filtering(stack, show=False)
shifts = calculate_shifts(ipr, ipi, roi, java_code=True)
shifts = (ipr, ipi, roi, java_code=True)
#shifts= load_shifts("{images}/TestObjAligner/shifts.mat")
#stack = load_test_stack(show=True)
r=translate(stack, shifts, show=True)
@@ -33,7 +33,7 @@ def eval_shift(roi, obj=None, stiga=None, stigb=None, wait=3.0):
time.sleep(wait)
image.waitNext(10000)
img = image.data
xoff, yoff, error, diffphase = calculate_shift(ref,img, roi)
xoff, yoff, error, diffphase, _ = calculate_shift(ref,img, roi)
print "xoff: ", xoff
print "yoff: ", yoff
print "error: ", error
@@ -56,7 +56,7 @@ def eval_shift_obj(roi, obj1, obj2, wait=3.0):
time.sleep(wait)
image.waitNext(10000)
img = image.data
xoff, yoff, error, diffphase = calculate_shift(ref,img, roi)
xoff, yoff, error, diffphase, _ = calculate_shift(ref,img, roi)
print "Ref obj: ", obj1, " img obj: ", obj2
print "xoff: ", xoff
print "yoff: ", yoff
+160
View File
@@ -0,0 +1,160 @@
def init_eiger(exposure=None, check=True, retries=2):
"""
Set Eiger scan mode
"""
global chrono_eiger
chrono_eiger.waitTimeout(CONTINOUS_MODE_MIN_TIME)
for i in range(retries):
try:
stop_eiger() #Set mode single
eiger.setNumImages(1)# Is it relevant?]
caputq("X11MA-ES1-SD1:cam1:NumImages",1);
set_eiger_number_of_frames(1)
caget("X11MA-ES1-SD1:cam1:NumFrames_RBV") and compares before writing (therefore does nothing)
if exposure:
set_exposure_time(exposure, check)
apply_averaging_detector(is_averaging_detector())
caput("X11MA-ES1-SD1:Proc1:EnableFilter", False)
caput("X11MA-ES1-SD1:Proc1:NumFilter", 1)
caput("X11MA-ES1-SD1:cam1:NumCycles", 1)
break
except:
if i==(retries-1):
raise
else:
print "Error initializing Eiger, retrying: " + str(sys.exc_info()[1])
def restore_eiger(check=True, retries=2, exposure_time = 0.2):
"""
Set Eiger default mode
"""
global chrono_eiger
for i in range(retries):
try:
stop_eiger()
eiger.setNumImages(1)# Is it relevant?
caputq("X11MA-ES1-SD1:cam1:NumImages",1);
set_eiger_number_of_frames(1, check)
set_exposure_time(exposure_time, check)
apply_averaging_detector(False)
caput("X11MA-ES1-SD1:Proc1:EnableFilter", False)
caput("X11MA-ES1-SD1:Proc1:NumFilter", 1)
caput("X11MA-ES1-SD1:cam1:NumCycles", 1)
eiger.grabMode=eiger.GrabMode.Continuous
caputq("X11MA-ES1-SD1:cam1:ImageMode", Single)
eiger.start()
caputq("X11MA-ES1-SD1:cam1:Acquire", 1)
chrono_eiger = Chrono()
break
except:
if i==(retries-1):
raise
else:
print "Error restoring Eiger, retrying " + str(sys.exc_info()[1])
def set_exposure_time(value, check = True, retries=5):
if value == eiger.getExposure():
caget("X11MA-ES1-SD1:cam1:AcquireTime"), if equals retuem
return
started = eiger.isStarted()
caget("X11MA-ES1-SD1:cam1:Acquire"),
if started:
stop_eiger()
for i in range(retries):
try:
eiger.setExposure(value)
caputq("X11MA-ES1-SD1:cam1:AcquireTime", value)
if check:
readback=get_eiger_exposure_readback()
caget("X11MA-ES1-SD1:cam1:AcquireTime_RBV")
if abs(value - readback) > 0.01:
raise Exception("Error changing Eiger exposure time to %f: readback=%f" % (value, readback))
except:
if i==(retries-1):
raise
else:
print "Error changing Eiger exposure time: retrying"
time.sleep(0.3)
if started:
if eiger.grabMode==eiger.GrabMode.Continuous:
caget("X11MA-ES1-SD1:cam1:ImageMode")
eiger.start()
caputq("X11MA-ES1-SD1:cam1:Acquire", 1)
def set_eiger_number_of_frames(value, check = True):
if value == get_eiger_number_of_frames():
caget("X11MA-ES1-SD1:cam1:NumFrames_RBV",'i')
return
started = eiger.isStarted()
caget("X11MA-ES1-SD1:cam1:Acquire")
if started:
stop_eiger()
caput("X11MA-ES1-SD1:cam1:NumFrames",value)
if check:
readback = get_eiger_number_of_frames()
caget("X11MA-ES1-SD1:cam1:NumFrames_RBV",'i')
if value != readback:
raise Exception("Error changing Eiger number of frames to %d: readback=%d" % (value, readback))
if started:
if eiger.grabMode==eiger.GrabMode.Continuous:
caget("X11MA-ES1-SD1:cam1:ImageMode")
eiger.start()
caputq("X11MA-ES1-SD1:cam1:Acquire", 1)
def stop_eiger():
global chrono_eiger
chrono_eiger.waitTimeout(CONTINOUS_MODE_MIN_TIME)
started = eiger.isStarted()
caget("X11MA-ES1-SD1:cam1:Acquire")
if started:
eiger.stop()
caputq("X11MA-ES1-SD1:cam1:Acquire", 0)
eiger.grabMode=eiger.GrabMode.Single
caputq("X11MA-ES1-SD1:cam1:ImageMode", Single)
eiger.stop()
caputq("X11MA-ES1-SD1:cam1:Acquire", 0)
eiger.waitReady(5000)
wait until monitor of X11MA-ES1-SD1:cam1:Acquire == 0 is received
else:
eiger.grabMode=eiger.GrabMode.Single
caputq("X11MA-ES1-SD1:cam1:ImageMode", Single)
time.sleep(0.1)
def average_eiger_frames(samples, roi=None, wait_next=False, sleep=0, outliers_threshold=None, outliers_mask=None, retries=None, timeout=None):
global eiger_averaging_number_of_samples #, chrono_eiger
sample = int(samples)
if is_averaging_detector():
#Not now
else:
ret = grab_frames(image, samples, roi, wait_next, sleep, outliers_threshold, outliers_mask, retries, timeout)
if wait_next:
eiger.waitReady(5000)
eiger.start()
elif str(eiger.grabMode)=="Continuous":
if not eiger.isStarted():
eiger.start()
try:
#eiger.getDataArray().waitCacheChange(timeout)
source.waitNext(timeout)
breakzxZZy
except java.util.concurrent.TimeoutException:
if try_count == (retries-1):
raise
msg = "Eiger timeout - retrying #" + str(try_count)
print msg
log(msg)
return average_frames(ret) if samples > 1 else ret[0]
+5 -1
View File
@@ -1 +1,5 @@
otf2(start=500, end=510, time=1, delay=10, mode='LINEAR', alpha = 0.0, offset=0.0, name='test')
#otf2(start=778, end=785, time=1, delay=10, mode='LINEAR', alpha = 0.0, offset=-3.8, name='test_Co')
#otf2(start=772, end=805, time=2, delay=10, mode='LINEAR', alpha = 0.0, offset=-3.8, name='test')
#otf2(start=845, end=885, time=2, delay=10, mode='LINEAR', alpha = 0.0, offset=-3.8, name='test_Ni')
#otf2(start=520, end=570, time=2, delay=10, mode='LINEAR', alpha = 0.0, offset= 0, name='oXAS-VS01-OCP_O_test')
otf2(start=520, end=595, time=2, delay=10, mode='LINEAR', alpha = 0.0, offset= 0, name='oXAS-VS01-OCP_O_test')