Remove outliers

This commit is contained in:
gac-x11ma
2020-02-03 16:57:46 +01:00
parent 2670bb1f56
commit a0b61220e9
5 changed files with 173 additions and 95 deletions

View File

@@ -1,2 +1,2 @@
#Fri Jan 24 14:20:38 CET 2020
FileSequentialNumber=213
#Mon Feb 03 16:51:48 CET 2020
FileSequentialNumber=327

View File

@@ -1,4 +1,4 @@
#Wed Jan 15 09:14:50 CET 2020
#Mon Feb 03 16:50:13 CET 2020
spatialCalOffsetY=NaN
spatialCalOffsetX=NaN
colormapLogarithmic=false
@@ -6,7 +6,7 @@ scale=1.0
grayscale=false
spatialCalScaleX=NaN
spatialCalScaleY=NaN
colormapMax=3300.457
colormapMax=100.0
rescaleOffset=0.0
roiWidth=-1
colormap=Grayscale

View File

@@ -86,7 +86,7 @@ def average_frames(source, samples=1, roi=None, wait_next=False, sleep=0, as_flo
def integrate_frames(source, samples=1, roi=None, wait_next=False, sleep=0, as_float=True):
return integrate_ips(grab_frames(source, samples, roi, wait_next, sleep), as_float)
"""
def grab_frame(source, roi=None, wait_next=False):
def grab_frame(source, roi=None, wait_next=False, rmv_outliers=True):
if wait_next:
if str(eiger.grabMode)=="Single":
eiger.start()
@@ -98,14 +98,16 @@ def grab_frame(source, roi=None, wait_next=False):
if roi is not None:
data = data.getRoi(Rectangle(roi[0], roi[1], roi[2], roi[3]))
#ret = load_image(img)
if rmv_outliers:
data = remove_outliers(data)
return data
def grab_frames(source, samples, roi=None, wait_next=False, sleep=0):
def grab_frames(source, samples, roi=None, wait_next=False, sleep=0, rmv_outliers=True):
frames = []
for i in range(samples):
if (i>0) and (sleep>0):
time.sleep(sleep)
aux = grab_frame(source, roi, wait_next)
aux = grab_frame(source, roi, wait_next, rmv_outliers)
frames.append(aux)
return frames
@@ -123,6 +125,19 @@ def average_frames(frames):
ret.div(len(frames))
return ret
def remove_outliers(imgdata, threshold = 1e9, replace_value=None):
if replace_value is None:
if str(imgdata.getType()) in ('float', 'double'):
replace_value = float('NaN')
else:
replace_value = 0
data = imgdata.copy()
for i in range(data.width):
for j in range(data.height):
if data.getElementDbl(j,i, False) >=threshold:
data.setElement(j*data.width + i, replace_value)
return data
def save_as_tiff(data, filename, check=False, show = False):
if type(data) == Data:
ip = load_array(data.matrix)

View File

@@ -13,8 +13,8 @@ if get_exec_pars().source == CommandSource.ui:
OFFSET_ID_2 = 0.0
ROI = {"ROI1": [50,50,200, 100]}
METHOD = "Two_Energies"
MEASUREMENTS = 2
METHOD = "Two_Pol"
MEASUREMENTS = 4
CONTRAST = 0
AUTO_SAVE = True
AS_SEQUENCE = False
@@ -50,8 +50,11 @@ data_path = get_context().setup.expandPath(path)
def get_image_file_name(cycle=-1, frame_index=0, short_name=False):
root = "" if short_name else (data_path + "/")
if cycle < 0:
return root + ("%04d" % cycle) + ".tif"
if cycle < 0:
ret = root + "i"+"{seq}%03d" + "_" + ("%d" % frame_index) + ".tif"
else:
ret= root + ("%d/" % frame_index) + "i"+"{seq}%03d" + "_" + str(cycle) + ("_%d" % frame_index) + ".tif"
ret = get_context().setup.expandPath(ret)
return ret
@@ -61,6 +64,9 @@ def getLEEM():
startvoltage=getStartvoltage()
objective=getObjective()
LEEMtemp=getLEEMtemp()
"""
set_exec_pars(path= path, format="txt", layout="table", open=True)
data_path = get_context().setup.expandPath(path)
@@ -82,7 +88,17 @@ def assert_status_ok():
Close
Exit Sub
"""
#Initialize vartiables
if not DRY_RUN:
open_vg10()
if ID=='ID1':
caput('X11PHS-E:OPT',1)
elif ID=='ID2':
caput('X11PHS-E:OPT',2)
elif ID =='ID1_ID2':
caput('X11PHS-E:OPT',3)
active_id = 1
@@ -90,24 +106,26 @@ polID1=None
polID1=None
polID2=None
#rbkEnergy=energy_rbk.read()
if METHOD == "Two_Pol":
if ID == "ID1":
current_pol=POL_IDS[POL_ID_1] #get_id_pol(1)
put_id_pol(1,current_pol)
elif ID == "ID2":
current_pol=POL_IDS[POL_ID_2]#get_id_pol(2)
put_id_pol(2,current_pol)
elif ID == "ID1_ID2":
polID1=POL_IDS[POL_ID_1]
polID2=POL_IDS[POL_ID_2]
put_id_pol(1,polID1)
put_id_pol(2,polID2)
current_pol=polID1
if SWITCHING == "Tune_Detune":
put_id_offset(2, OFFSET_ID_2-40) #detuneID2
put_id_offset(1, OFFSET_ID_1) #tuneID2
elif SWITCHING == "Chopper":
if METHOD == "Two_Pol":
if not DRY_RUN:
if ID == "ID1":
current_pol=POL_IDS[POL_ID_1] #get_id_pol(1)
put_id_pol(1,current_pol)
elif ID == "ID2":
current_pol=POL_IDS[POL_ID_2]#get_id_pol(2)
put_id_pol(2,current_pol)
elif ID == "ID1_ID2":
polID1=POL_IDS[POL_ID_1]
polID2=POL_IDS[POL_ID_2]
put_id_pol(1,polID1)
put_id_pol(2,polID2)
current_pol=polID1
if SWITCHING == "Tune_Detune":
put_id_offset(2, OFFSET_ID_2-40) #detuneID2
put_id_offset(1, OFFSET_ID_1) #tuneID2
elif SWITCHING == "Chopper":
set_chopper(0)
wait_channel("X11PHS:alldone", 1)
def imageinfo(info):
@@ -181,69 +199,74 @@ def save_image_file(frame, cycle=-1, frame_index=0):
log(filename)
print filename
# log("SV:"+Format(startvoltage,"0.000")+" OB:"+Format(objective,"0.00")+" ST:"+Format(LEEMtemp,"0.0"))
eiger.stop()
eiger.grabMode=eiger.GrabMode.Single
#Do the measurement loop
for cycle in range(1, MEASUREMENTS + 1):
frames = []
if MEASUREMENTS > 1:
log("")
log("nround = " + str(cycle) + " / " + str(MEASUREMENTS))
if METHOD == "Two_Energies":
change_energy(ENERGY_1)
###??? SETAR POLARIZATION?
time.sleep(1)
for i in range(2):
assert_status_ok()
eiger.setExposure(EXPOSURE[i])
if AS_SEQUENCE:
raise Exception("As sequence not implemented")
# TODO
else:
ret = grab_frames(image, AVERAGE[i], roi=None, wait_next=True)
frames.append(average_frames(ret) if AVERAGE[i] > 1 else ret[0])
imageinfo("I")
save_image_file(frames[i], cycle, i)
if METHOD == "Take_Image":
break
try:
#Do the measurement loop
for cycle in range(1, MEASUREMENTS + 1):
frames = []
if MEASUREMENTS > 1:
log("")
log("nround = " + str(cycle) + " / " + str(MEASUREMENTS))
if METHOD == "Two_Energies":
change_energy(ENERGY_1)
###??? SETAR POLARIZATION?
time.sleep(1)
for i in range(2):
assert_status_ok()
eiger.setExposure(EXPOSURE[i])
if AS_SEQUENCE:
raise Exception("As sequence not implemented")
# TODO
else:
ret = grab_frames(image, AVERAGE[i], roi=None, wait_next=True)
frames.append(average_frames(ret) if AVERAGE[i] > 1 else ret[0])
imageinfo("I")
save_image_file(frames[i], cycle, i)
if METHOD == "Take_Image":
break
if METHOD == "Two_Pol":
switch_pol()
if METHOD == "Two_Energies":
change_energy(ENERGY_2)
time.sleep(1)
if (METHOD == "Two_Pol") or (METHOD == "Two_Energies"):
if (METHOD == "Two_Energies") or ((current_pol == 2) or (current_pol==3)):
###??? autocontrast???
frames.append(frames[0].copy())
frames[2].div(frames[1])
else:
frames.append(frames[1].copy())
frames[2].div(frames[0])
change_energy(ENERGY_2)
time.sleep(1)
# objective = getObjective()
# temp = getLEEMtemp()
# log(0 , "Temp : " + Format(temp,"0.00") + " OB : " + Format(objective,"0.00") + " StigmaA : " + " StigmaB : " )
# Auto average and save
if MEASUREMENTS > 1 and AUTO_SAVE == 1:
for i in range(len(frames)):
measures = []
for cycle in range(1, MEASUREMENTS + 1):
filename = get_image_file_name(cycle, i)
ip = open_image(filename)
#measures.append(Data(get_ip_array(ip)))
measures.append(ip)
#av = average_frames(measures) #Result is transposed???
av=average_ips (measures, roi=None, as_float=True)
save_image_file(av, -1, i)
if (METHOD == "Two_Pol") or (METHOD == "Two_Energies"):
if (METHOD == "Two_Energies") or ((current_pol == 2) or (current_pol==3)):
###??? autocontrast???
frames.append(frames[0].copy())
frames[2].div(frames[1])
else:
frames.append(frames[1].copy())
frames[2].div(frames[0])
save_image_file(frames[2],cycle, 2)
# objective = getObjective()
# temp = getLEEMtemp()
# log(0 , "Temp : " + Format(temp,"0.00") + " OB : " + Format(objective,"0.00") + " StigmaA : " + " StigmaB : " )
# Auto average and save
if MEASUREMENTS > 1 and AUTO_SAVE == 1:
for i in range(len(frames)):
measures = []
for cycle in range(1, MEASUREMENTS + 1):
filename = get_image_file_name(cycle, i)
ip = open_image(filename)
#measures.append(Data(get_ip_array(ip)))
measures.append(ip)
#av = average_frames(measures) #Result is transposed???
av=average_ips (measures, roi=None, as_float=True)
save_image_file(av, -1, i)
finally:
if not DRY_RUN:
close_vg10()
eiger.grabMode=eiger.GrabMode.Continuous

View File

@@ -1,6 +1,6 @@
#If running from editor
if get_exec_pars().source == CommandSource.ui:
ID = "ID1"
ID = "ID2"
SWITCHING = "Normal"
POL_ID_1 = "Circ_Plus"
ALPHA_ID_1 = 0.0
@@ -19,13 +19,15 @@ if get_exec_pars().source == CommandSource.ui:
EXPOSURE = 1.0
AVERAGE = 2
NUMBER_SCANS =1
RANGES = [[500.0, 1000.0, 100.0]]
RANGES = [[500.0, 1000.0, 100.0]]
DRY_RUN=False
if len(RANGES) == 0:
raise Exception("No scan range defined")
eiger.stop()
eiger.grabMode=eiger.GrabMode.Single
eiger.setExposure(EXPOSURE)
set_exec_pars(path="{data}/{year}_{month}/{date}/AbsortionSpectrum_{seq}%03d", format="csv", layout="table")
@@ -63,8 +65,9 @@ def grab_image(position, scan):
global av
frames = grab_frames(image, AVERAGE, roi=None, wait_next=True)
av = average_frames(frames)
if SAVE_IMAGES:
filename = get_exec_pars().path + "/" + ("%05d.tif" % scan.recordIndex)
if SAVE_IMAGES:
Scan_Nr = (scan.recordIndex-1)/NUMBER_SCANS
filename = get_exec_pars().path + "/" + ("%02d" % Scan_Nr) + "/s" + "{seq}%03d" + "_" + ("%03d.tif" % scan.recordIndex)
print filename
save_as_tiff(av, filename)
@@ -78,6 +81,41 @@ class Average(ReadableMatrix):
averager=Average()
set_device_alias(averager, "Image") #Set display name
sensors.append(averager) #sensors.append(eiger.getDataMatrix())
#Initialize vartiables
open_vg10()
if ID=='ID1':
caput('X11PHS-E:OPT',1)
elif ID=='ID2':
caput('X11PHS-E:OPT',2)
elif ID =='ID1_ID2':
caput('X11PHS-E:OPT',3)
put_id_offset(1, OFFSET_ID_1) #offset on ID1
put_id_offset(2, OFFSET_ID_2) #offset on ID2
active_id = 1
current_pol=None
polID1=None
polID2=None
if ID == "ID1":
current_pol=POL_IDS[POL_ID_1] #get_id_pol(1)
put_id_pol(1,current_pol)
caput('X11MA-ID2-GAP:SET',100) #open Gap ID2
elif ID == "ID2":
current_pol=POL_IDS[POL_ID_2]#get_id_pol(2)
put_id_pol(2,current_pol)
caput('X11MA-ID1-GAP:SET',100) #open Gap ID1
elif ID == "ID1_ID2":
polID1=POL_IDS[POL_ID_1]
polID2=POL_IDS[POL_ID_2]
put_id_pol(1,polID1)
put_id_pol(2,polID2)
wait_channel("X11PHS:alldone", 1)
try:
#tscan(rois, 10, 0.1, format="csv", before_read=grab_image, passes = NUMBER_SCANS, tag=tag)
@@ -86,4 +124,6 @@ finally:
if AVERAGE>1:
for i in range(len(rois)):
rois[i].parent.monitored = False # Remove listeners on the image
close_vg10()
eiger.grabMode=eiger.GrabMode.Continuous
eiger.start()