Commit a47486aa authored by Holger Niemann's avatar Holger Niemann
Browse files

Merge branch 'peter' into 'master'

Peter

See merge request !22
parents 96355a14 204811b3
......@@ -68,7 +68,7 @@ def bestimmtheitsmass_general(data,fit):
qam=quad_abweich_mittel(fit,mittel)
R=qam/(qam+quad_abweich(data,fit))
else:
print("Arrays must have same dimensions")
print("bestimmtheitsmass_general: Arrays must have same dimensions")
return R
def quad_abweich_mittel(data,mittel):
......@@ -83,7 +83,7 @@ def quad_abweich(data,fit):
for i in range(len(data)):
R=R+(data[i]-fit[i])**2
else:
print("Arrays must have same dimensions")
print("quad_abweich: Arrays must have same dimensions")
return R
def find_nearest(array,value):
......@@ -131,6 +131,8 @@ def check_coldframe(coldframe,references=None,threshold=0.5,plot_it=False):
return False,bestimmtheit
def check_coldframe_by_refframe(coldframe,reference_frame,threshold=0.8,plot_it=False):
'''
'''
references=[]
shapi=np.shape(reference_frame)
for i in [int(shapi[1]//5),int(shapi[1]//2),int(shapi[1]//4*3)]:
......@@ -185,8 +187,10 @@ def read_bad_pixels_from_file(port, shot_no=None, program=None,time_ns=None):
def find_outlier_pixels(frame,tolerance=3,worry_about_edges=True,plot_it=False):
# This function finds the bad pixels in a 2D dataset.
# Tolerance is the number of standard deviations used for cutoff.
'''
This function finds the bad pixels in a 2D dataset.
Tolerance is the number of standard deviations used for cutoff.
'''
frame = np.array(frame)#, dtype=int)
from scipy.ndimage import median_filter
blurred = median_filter(frame, size=9)
......@@ -222,26 +226,26 @@ def find_outlier_pixels(frame,tolerance=3,worry_about_edges=True,plot_it=False):
return bad_pixels
def correct_images(images,badpixels):
print('correct_images: New routine restore_bad_pixels() is used and can be called directly. Check out "help(restore_bad_pixels)"')
def correct_images(images,badpixels,verbose=0):
'''
'''
if type(badpixels)!=int:
if type(images) == list:
# return corrected images also as list of 2D arrays
# images = restore_bad_pixels(images, np.invert(badpixels==1))#.astype(np.float32)
# images = list(images)
for i in range(len(images)):
images[i]=restore_bad_pixels(images[i], np.invert(badpixels==1))
images[i]=restore_bad_pixels(images[i], np.invert(badpixels==1), verbose=verbose-1)
else:
# keep shape
images = restore_bad_pixels(images, np.invert(badpixels==1)).astype(np.float32)
images = restore_bad_pixels(images, np.invert(badpixels==1), verbose=verbose-1)
# for i in range(len(images)):
# images[i]=(restore_pixels(images[i],np.invert(badpixels==1))).astype(np.float32)
print("done")
if verbose>0:
print("correct_images: done")
return images
def restore_bad_pixels(frames, bad_pixel, by_list=True, check_neighbours=True, plot_it=False, verbose=0):
def restore_bad_pixels(frames, bad_pixel, by_list=True, check_neighbours=True, plot_it=False,verbose=0):
"""Restore bad pixel by interpolation of adjacent pixels. Optionally make
sure that adjacent pixels are not bad (time consuming). Default is to use
a list of bad pixels and a for loop. For many bad pixels consider using
......@@ -412,16 +416,20 @@ def restore_bad_pixels(frames, bad_pixel, by_list=True, check_neighbours=True, p
def generate_new_hot_image(cold,reference_cold,reference_hot):
'''
'''
if cold is None or reference_cold is None or reference_hot is None:
raise Exception("Cannot Calculate new Hot image, if images are missing!")
raise Exception("generate_new_hot_image: Cannot Calculate new Hot image, if images are missing!")
else:
return reference_hot+(cold-reference_cold)
def calculate_gain_offset_image_pix(cold_image,hot_image=None,reference_cold=None,reference_hot=None,bose=1):
def calculate_gain_offset_image_pix(cold_image,hot_image=None,reference_cold=None,reference_hot=None,verbose=0):
'''
'''
if hot_image is None:
hot_image=generate_new_hot_image(cold_image,reference_cold,reference_hot)
if bose>0:
print("calculate gain and offset")
if verbose>0:
print("calculate_gain_offset_image_pix: calculate gain and offset")
Sh_ref = hot_image[ ( np.int( np.shape(hot_image)[0] /2 ) ) ][np.int( (np.shape(hot_image)[1] /2 ) ) ]
Sc_ref = cold_image[ ( np.int( (np.shape(cold_image)[0]) /2 ) ) ][( np.int( (np.shape(cold_image)[1]) /2 ) ) ]
Gain_rel = ( Sh_ref - Sc_ref ) / ( hot_image - cold_image)
......@@ -434,7 +442,7 @@ def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,re
if hot_image is None:
hot_image=generate_new_hot_image(cold_image,reference_cold,reference_hot)
if verbose>0:
print("calculate gain and offset")
print("calculate_gain_offset_image: calculate gain and offset")
# Sh_ref = hot_image[ ( np.int( np.shape(hot_image)[0] /2 ) ) ][np.int( (np.shape(hot_image)[1] /2 ) ) ]
# Sc_ref = cold_image[ ( np.int( (np.shape(cold_image)[0]) /2 ) ) ][( np.int( (np.shape(cold_image)[1]) /2 ) ) ]
......@@ -455,30 +463,32 @@ def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,re
#%% functions from Yu Gao
""" functions by Yu Gao"""
def load_ref_images(port, exposuretime):
'''
load the reference cold and hot frame during calibration from local files.
@port: e.g. 'AEF10'
@exposuretime: int number.
'''
cameraname = portcamdict['OP1.2a'][port]
foldername = cameraname.split('_')[0] + '_' + cameraname.split('_')[2]
scanpath = join(IRCamRefImagespath, foldername)
coldref, hotref = [], []
for filename in glob.iglob(scanpath + '\*' + str(int(exposuretime)) + 'us.h5', recursive=True):
if 'hot' in filename:
print (filename)
with h5py.File(filename, 'r') as h5in:
hotref = h5in[basename(filename)].value
elif 'cold' in filename:
print (filename)
with h5py.File(filename, 'r') as h5in:
coldref = h5in[basename(filename)].value
return coldref, hotref
def load_ref_images(port, exposuretime, verbose=0):
'''
load the reference cold and hot frame during calibration from local files.
@port: e.g. 'AEF10'
@exposuretime: int number.
'''
cameraname = portcamdict['OP1.2a'][port]
foldername = cameraname.split('_')[0] + '_' + cameraname.split('_')[2]
scanpath = join(IRCamRefImagespath, foldername)
coldref, hotref = [], []
for filename in glob.iglob(scanpath + '\*' + str(int(exposuretime)) + 'us.h5', recursive=True):
if 'hot' in filename:
if verbose>0:
print('load_ref_images: read from ',filename)
with h5py.File(filename, 'r') as h5in:
hotref = h5in[basename(filename)].value
elif 'cold' in filename:
if verbose>0:
print('load_ref_images: read from ',filename)
with h5py.File(filename, 'r') as h5in:
coldref = h5in[basename(filename)].value
return coldref, hotref
def reconstruct_coldframe (exposuretime, sT, a, bnew, coldref):
cirebuild = a * sT + bnew * exposuretime + coldref
return cirebuild
cirebuild = a * sT + bnew * exposuretime + coldref
return cirebuild
#%% other functions
......
This diff is collapsed.
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