From eb1e2b4ce83e04e33fc92ba59b6236724c47cc97 Mon Sep 17 00:00:00 2001 From: Marco Cammarata Date: Fri, 25 Nov 2016 15:29:42 +0100 Subject: [PATCH] finalize npz save function and worked a bit on funcitons to make ratios --- xanes_analyzeRun.py | 76 +++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/xanes_analyzeRun.py b/xanes_analyzeRun.py index 2a12552..c6b7d2d 100644 --- a/xanes_analyzeRun.py +++ b/xanes_analyzeRun.py @@ -101,37 +101,8 @@ def showShots(im1,im2): a[0].plot(p1) a[1].plot(p2) -def ratioOfAverage(p1,p2,threshold=0.03): - """ - p1 and p2 are the energy spectrum. if 2D the first index has to be the shot number - calculate median ratio taking into account only regions where p1 and p2 are > 5% of the max """ - # check if they are 2D - if p1.ndim == 1: - p1 = p1[np.newaxis,:] - p2 = p2[np.newaxis,:] - # w1 and w2 are the weights - w1 = p1.copy(); w2 = p2.copy() - if threshold is not None: - # weights will be set to zero if intensity is smaller than 5% of max - # for each shots, get maximum - m1 = np.nanmax(p1,axis=1); m2 = np.nanmax(p2,axis=1) - # find where each spectrum is smaller than threshold*max_for_that_shot; they will be masked out - idx1 = p1 < (m1[:,np.newaxis]*threshold) - idx2 = p2 < (m2[:,np.newaxis]*threshold) - w1[idx1]=0 - w2[idx2]=0 - # using masked array because some pixel will have zero shots contributing - av1 = np.ma.average(p1,axis=0,weights=w1) - av1[av1.mask] = np.nan - av2 = np.ma.average(p2,axis=0,weights=w2) - av2[av2.mask] = np.nan - return av2/av1 - -def medianRatio(p1,p2,threshold=0.03): - """ - p1 and p2 are the energy spectrum. if 2D the first index has to be the shot number - calculate median ratio taking into account only regions where p1 and p2 are > 5% of the max """ - # check if they are 2D + +def maskLowIntensity(p1,p2,threshold=0.03,squeeze=True): if p1.ndim == 1: p1 = p1[np.newaxis,:] p2 = p2[np.newaxis,:] @@ -145,6 +116,28 @@ def medianRatio(p1,p2,threshold=0.03): idx = idx1 & idx2 p1.mask = idx p2.mask = idx + if squeeze: + p1 = np.squeeze(p1); + p2 = np.squeeze(p2) + return p1,p2 + +def ratioOfAverage(p1,p2,threshold=0.03): + """ + p1 and p2 are the energy spectrum. if 2D the first index has to be the shot number + calculate median ratio taking into account only regions where p1 and p2 are > 5% of the max """ + p1,p2 = maskLowIntensity(p1,p2,threshold=threshold,squeeze=False) + # using masked array because some pixel will have zero shots contributing + av1 = np.ma.average(p1,axis=0,weights=p1) + av2 = np.ma.average(p2,axis=0,weights=p2) + return av2/av1 + + + +def medianRatio(p1,p2,threshold=0.03): + """ + p1 and p2 are the energy spectrum. if 2D the first index has to be the shot number + calculate median ratio taking into account only regions where p1 and p2 are > 5% of the max """ + p1,p2 = maskLowIntensity(p1,p2,threshold=threshold,squeeze=False) ratio = p2/p1 return np.ma.average(ratio,axis=0,weights=p1) @@ -294,13 +287,30 @@ class AnalyzeRun(object): return ret def save(self,fname="auto",overwrite=False): - if len(self.results) == 0: print("self.results are empty, returning without saving") + if len(self.results) == 0: + print("self.results are empty, returning without saving") if not os.path.isdir(g_folder_out): os.makedirs(g_folder_out) if fname == "auto": - fname = g_folder_out+"/run%04d_analysis" % self.run + fname = g_folder_out+"/run%04d_analysis.npz" % self.run if os.path.exists(fname) and not overwrite: print("File %s exists, **NOT** saving, use overwrite=True is you want ..."%fname) return + h = dict() + h["roi1"] = (self.roi1.start,self.roi1.stop) + h["roi2"] = (self.roi2.start,self.roi2.stop) + if hasattr(self.data.scan,"scanmotor0"): + h["scanmot0"] = self.data.scan.scanmotor0 + else: + h["scanmot0"] = 'notascan' + h["scanpos0"] = self.data.scan.scanmotor0_values + if hasattr(self.data.scan,"scanmotor1"): + h["scanmot1"] = self.data.scan.scanmotor1 + h["scanpos1"] = self.data.scan.scanmotor1_values + h["results"] = self.results + h["E"] = self.E + np.savez(fname,**h) + #h["transform"] = self.initAlign + def load(self,fname="auto"): if fname == "auto": fname = g_folder_out+"/run%04d_analysis.npz" % self.run