From 1f652ab162bc12ad04ebabfbe4a0d82df786da6a Mon Sep 17 00:00:00 2001 From: Marco Cammarata Date: Tue, 10 Jan 2017 22:43:22 +0100 Subject: [PATCH] more improvements (including some needed to work after background subtraction, see example on salen --- xray/azav.py | 5 +- xray/dataReduction.py | 12 +++-- xray/example_main_salen.py | 106 +++++++++++++++++++++++++++++++++++++ xray/id9.py | 13 +++-- xray/mask.py | 5 +- xray/storage.py | 4 +- xray/utils.py | 4 +- 7 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 xray/example_main_salen.py diff --git a/xray/azav.py b/xray/azav.py index 4e0cabe..a28039a 100644 --- a/xray/azav.py +++ b/xray/azav.py @@ -1,7 +1,8 @@ from __future__ import print_function,division -import logging as log -log.basicConfig(level=log.INFO) +import logging +log = logging.getLogger(__name__) + import numpy as np np.seterr(all='ignore') diff --git a/xray/dataReduction.py b/xray/dataReduction.py index ab72c28..c960195 100644 --- a/xray/dataReduction.py +++ b/xray/dataReduction.py @@ -1,7 +1,8 @@ from __future__ import print_function,division -import logging as log -log.basicConfig(level=log.INFO) +import logging +log = logging.getLogger(__name__) + import numpy as np np.seterr(all='ignore') @@ -40,7 +41,7 @@ def subtractReferences(i,idx_ref, useRatio = False): # normal reference for an on chi, the weighted average iref[_i] = weight_before*ref_before + weight_after*ref_after if _i>=idx_ref_after: _ref += 1 - log.debug("For image %d : %d-%d"%(_i,idx_ref_before,idx_ref_after)) + log.debug("SubtractRederence For image %d : %d-%d"%(_i,idx_ref_before,idx_ref_after)) if useRatio: i /= iref else: @@ -170,6 +171,9 @@ def errorMask(data,threshold=5): # sqrt(len(temp)) = sqrt(numOfDiffs); it is needed to estimate error of single Diff idx = np.abs(temp-np.median(temp,axis=0)) > threshold*data.err[iscan]*np.sqrt(len(temp)) idx_mask.append( idx ) + + log.debug("errorMask mask, scanpoint: %s, fraction of q points filtered out (average) %.4e [max %.4e])"%\ + (data.scan[iscan],idx.sum()/idx.size,max(np.sum(idx,axis=1)/idx.shape[1])) ) if "masks" not in data: data['masks'] = dict() data['masks']['error'] = idx_mask return data @@ -188,6 +192,8 @@ def chi2Mask(data,threshold=2): # expand along other axis (q ...) idx = utils.reshapeToBroadcast(idx,data.diffsInScanPoint[iscan]) idx_mask.append(idx) + log.debug("Chi2 mask, scanpoint: %s, curves filtereout out %d/%d (%.2f%%)"%\ + (data.scan[iscan],idx.sum(),len(idx),idx.sum()/len(idx)*100) ) if "masks" not in data: data['masks'] = dict() data['masks']['chi2'] = idx_mask return data diff --git a/xray/example_main_salen.py b/xray/example_main_salen.py new file mode 100644 index 0000000..e130607 --- /dev/null +++ b/xray/example_main_salen.py @@ -0,0 +1,106 @@ +from __future__ import print_function,division +import os + + +# prepare logging +import logging +logfname = os.path.splitext(__file__)[0] + ".log" +logging.basicConfig(level=logging.DEBUG, + format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', + datefmt='%y-%m-%d %H:%M:%S', + filename=logfname, + filemode='w') +import numpy as np +import pylab as plt +import mcutils as mc +import mcutils.xray as xray +from mcutils.xray import id9 +#id9 = xray.id9 + +from dualtree import dualtree + +# use npz files (they can handle more stuff (list of arrays,unicode) than h5py) +id9.default_extension = '.npz' +#id9.default_extension = '.h5' + +g_default_mask = '../masks/fesalen1_run8_careful.edf' + + +def prepareLog(): + """ It allows printing to terminal on top of logfile """ + # define a Handler which writes INFO messages or higher to the sys.stderr + console = logging.StreamHandler() + console.setLevel(logging.INFO) + # set a format which is simpler for console use + formatter = logging.Formatter('%(message)s') + # tell the handler to use this format + console.setFormatter(formatter) + # add the handler to the root logger + logging.getLogger('').addHandler(console) + +prepareLog() + +def findCenter(): + files = xray.utils.getEdfFiles("../fesalen/fesalen1/run8/",nFiles=100) + img = xray.azav.read(files).mean(axis=0) - 10. + xray.azav.find_center(img) + + +def azav(folder,nQ=1500,force=False,saveChi=True,mask=g_default_mask): + if isinstance(mask,int): + files = xray.utils.getFiles(folder,"*.edf*") + img = xray.azav.pyFAIread(files[0]) + temp = np.ones_like(img,dtype=bool) + temp[:mask] = False + mask = temp + return id9.doFolder_azav(folder,nQ=nQ,force=force,mask=mask,saveChi=saveChi) + + +def removeBaseline(folder,qlims=(0.6,3),max_iter=30): + data = azav(folder) + idx = (data.q>qlims[0]) & (data.qr[0]) & (data.qqlims[0]) & (data.q