diff --git a/calibrationSuite/ancillaryMethods.py b/calibrationSuite/ancillaryMethods.py index 8acf3a4..21d3353 100644 --- a/calibrationSuite/ancillaryMethods.py +++ b/calibrationSuite/ancillaryMethods.py @@ -107,16 +107,14 @@ def getClusterEnergies(clusters): ##print(clusters) return clusters[:, 0] + class Histogram_1d(object): def __init__(self, data=[], nBins=None, xRange=None): self.nBins = nBins self.xRange = xRange self.hist, edges = np.histogram(data, bins=self.nBins, range=self.xRange) - self.bins = (edges[:-1] + edges[1:]) / 2. + self.bins = (edges[:-1] + edges[1:]) / 2.0 def fill(self, value): hist, _ = np.histogram([value], bins=self.nBins, range=self.xRange) self.hist += hist - - - diff --git a/calibrationSuite/basicSuiteScript.py b/calibrationSuite/basicSuiteScript.py index 116b115..5a6df4e 100755 --- a/calibrationSuite/basicSuiteScript.py +++ b/calibrationSuite/basicSuiteScript.py @@ -56,7 +56,7 @@ def __init__(self, analysisType="scan"): pass self.ROIfileNames = None try: - ##if True: + ##if True: self.ROIfileNames = self.experimentHash["ROIs"] self.ROIs = [] for f in self.ROIfileNames: @@ -66,7 +66,7 @@ def __init__(self, analysisType="scan"): except Exception: pass ##if False: - except: + except Exception: if self.ROIfileNames is not None: print("had trouble finding", self.ROIfileNames) for currName in self.ROIfileNames: @@ -94,7 +94,7 @@ def __init__(self, analysisType="scan"): if self.detectorInfo.dimension == 2: self.regionSlice = self.regionSlice[0], self.regionSlice[2] print("remapping regionSlice to handle 1d case") - + try: self.fluxSource = self.experimentHash["fluxSource"] try: @@ -110,8 +110,8 @@ def __init__(self, analysisType="scan"): try: self.ignoreEventCodeCheck = self.experimentHash["ignoreEventCodeCheck"] - self.fakeBeamCode = True ## just in case - except: + self.fakeBeamCode = True ## just in case + except Exception: self.ignoreEventCodeCheck = False self.fakeBeamCode = False @@ -124,13 +124,12 @@ def __init__(self, analysisType="scan"): self.daqCode = 281 self.beamCode = 283 ## per Matt ##self.beamCode = 281 ## don't see 283... - if not self.fakeBeamCode: ## defined in ignoreEventCodeCheck + if not self.fakeBeamCode: ## defined in ignoreEventCodeCheck if self.special is not None: self.fakeBeamCode = "fakeBeamCode" in self.special - print("ignoring event code check, faking beam code:", - self.ignoreEventCodeCheck, self.fakeBeamCode) - + print("ignoring event code check, faking beam code:", self.ignoreEventCodeCheck, self.fakeBeamCode) + ##mymodule = importlib.import_module(full_module_name) ## for standalone analysis @@ -329,7 +328,8 @@ def colCommonModeCorrection(self, frame, arbitraryCut=1000): return frame def isBeamEvent(self, evt): - if self.ignoreEventCodeCheck: return True + if self.ignoreEventCodeCheck: + return True ec = self.getEventCodes(evt) ##print(ec[280], ec[281], ec[282], ec[283], ec[284], ec[285] ) if ec[self.runCode]: @@ -385,11 +385,12 @@ def addFakePhotons(self, frames, occupancy, E, width): shape = frames.shape occ = np.random.random(shape) fakes = np.random.normal(E, width, shape) - fakes[occ>occupancy] = 0 - return frames + fakes, (fakes>0).sum() - + fakes[occ > occupancy] = 0 + return frames + fakes, (fakes > 0).sum() + def getNswitchedPixels(self, data, region=None): - return (((data>=self.g0cut)*1).sum()) + return ((data >= self.g0cut) * 1).sum() + """ if __name__ == "__main__": diff --git a/calibrationSuite/detectorInfo.py b/calibrationSuite/detectorInfo.py index 99821d9..f6684d3 100644 --- a/calibrationSuite/detectorInfo.py +++ b/calibrationSuite/detectorInfo.py @@ -99,6 +99,5 @@ def setup_rixsCCD(self, mode="1d", version=0): else: self.nRows = 1200 self.clusterShape = [3, 5] ## maybe - self.g0cut = 1<<16 + self.g0cut = 1 << 16 self.dimension = 2 - diff --git a/calibrationSuite/fitFunctions.py b/calibrationSuite/fitFunctions.py index 48690e4..b952d45 100644 --- a/calibrationSuite/fitFunctions.py +++ b/calibrationSuite/fitFunctions.py @@ -125,25 +125,33 @@ def fitLinearUnSaturatedData(x, y, saturated=False): ##, gainMode, label): return popt, pcov, y_fit, r2 +# not in use atm, and a + ds not def'd +""" def fitMatrixOfLinearFits(x, y): if len(x.shape) != 1: - print("y data shape is %dd, not 1d" %(len(s))) + print("y data shape is %dd, not 1d" % (len(s))) raise Exception yShape = y.shape if len(yShape) != 4: - print("y data shape is %dd, not 4d" %(len(yShape))) + print("y data shape is %dd, not 4d" % (len(yShape))) raise Exception - slopesAndIntercepts = [fitLinearUnSaturatedData(a, ds[m, r, c, :])[0] for m in range(yShape[0]) for r in range(yShape[1]) for c in range(yShape[2])] + slopesAndIntercepts = [ + fitLinearUnSaturatedData(a, ds[m, r, c, :])[0] + for m in range(yShape[0]) + for r in range(yShape[1]) + for c in range(yShape[2]) + ] return np.array(slopesAndIntercepts) +""" def linearFitTest(): a = np.linspace(0, 9, 10) - d0 = np.random.normal(1, 1, [3,5]) + d0 = np.random.normal(1, 1, [3, 5]) d = np.array([d0 + i for i in range(10)]) ds = np.stack(d, axis=-1) print("single pixel fit:") - print(np.ravel(fitLinearUnSaturatedData(a, ds[2,2,:])[0])) + print(np.ravel(fitLinearUnSaturatedData(a, ds[2, 2, :])[0])) print("array fit:") dss = ds.shape @@ -159,7 +167,8 @@ def linearFitTest(): ##print("matrix call:") ##print(fitMatrixOfLinearFits(a, ds)) - + + def twoGaussSilvermanModeTest(x0, x1): a = np.random.normal(0, 1, 1000) b = np.random.normal(x0, 1, 500) diff --git a/config_files/archonSuiteConfig.py b/config_files/archonSuiteConfig.py index 9c35f63..6478ac9 100755 --- a/config_files/archonSuiteConfig.py +++ b/config_files/archonSuiteConfig.py @@ -27,12 +27,12 @@ [0, 1000], [0, 2000], ], - "ignoreEventCodeCheck":True, + "ignoreEventCodeCheck": True, # 'ROIs':['module0', 'module2', 'module4', 'module6', 'module10','module12', 'module14'] # 'ROIs':['roiFromSwitched_e557_rmfxx1005021'] # 'ROIs':['allHRasicPixels', 'goodboxROI']#'roiAbove7k_raw_r123'] # "ROIs": ["../data/XavierV4_2", "../data/OffXavierV4_2"], - "regionSlice": np.s_[0:1,0:1, 0:4800] ##[1d] + "regionSlice": np.s_[0:1, 0:1, 0:4800], ##[1d] } # more complex approach allowing run ranges # fluxHash = {1:['MFX-USR-DIO', 11]} diff --git a/suite_scripts/CalcNoiseAndMean.py b/suite_scripts/CalcNoiseAndMean.py index 688a492..537a04e 100755 --- a/suite_scripts/CalcNoiseAndMean.py +++ b/suite_scripts/CalcNoiseAndMean.py @@ -104,7 +104,7 @@ if bss.getPingPongParity(frames[0][144:224, 0:80]) == ("negative" in bss.special): continue ##print(frames) - + if frames is None: print("None frames on beam event, should not happen") logger.info("None frames on beam event") diff --git a/suite_scripts/DeltaTtest.py b/suite_scripts/DeltaTtest.py index 3767960..834429c 100644 --- a/suite_scripts/DeltaTtest.py +++ b/suite_scripts/DeltaTtest.py @@ -94,7 +94,7 @@ def plotData(self, data, pixels, eventNumbers, dPulseId, label): plt.close() # unused and not working atm - ''' + """ def analyzeData(self, delays, data, label): edge = np.zeros(data.shape[0]) for m in range(data.shape[1]): @@ -106,7 +106,7 @@ def analyzeData(self, delays, data, label): coeff, var = curve_fit(f, delays, d, p0=p0) edge[m, r, c] = coeff[1] return edge - ''' + """ def analyze_h5(self, dataFile, label): import h5py @@ -151,7 +151,9 @@ def analyze_h5(self, dataFile, label): esp.setupPsana() - smd = esp.ds.smalldata(filename="%s/%s_c%d_r%d_n%d.h5" % (esp.outputDir, esp.className, esp.camera, esp.run, esp.size)) + smd = esp.ds.smalldata( + filename="%s/%s_c%d_r%d_n%d.h5" % (esp.outputDir, esp.className, esp.camera, esp.run, esp.size) + ) esp.fluxTS = 0 esp.nGoodEvents = 0 diff --git a/suite_scripts/LinearityPlotsParallelSlice.py b/suite_scripts/LinearityPlotsParallelSlice.py index b7bcadd..8a33dd1 100755 --- a/suite_scripts/LinearityPlotsParallelSlice.py +++ b/suite_scripts/LinearityPlotsParallelSlice.py @@ -18,7 +18,6 @@ import matplotlib.pyplot as plt import numpy as np from calibrationSuite.basicSuiteScript import BasicSuiteScript -from scipy.optimize import curve_fit # for logging from current file logger = logging.getLogger(__name__) @@ -101,7 +100,7 @@ def plotAutorangingData_profile(self, g0s, g1s, g0Fluxes, g1Fluxes, label, parti ##try:## using truncate instead ##yMaxPlot = max(y.max(), yMaxPlot) ##except: - ##yMaxPlot = y.max() + ##yMaxPlot = y.max() sns.regplot( x=x, y=y, x_bins=40, marker=".", ax=ax, order=order, truncate=True ) ##add fit_reg=None for no plot @@ -207,12 +206,12 @@ def analyze_h5_slice(self, dataFile, label): for module in [1, 2]: for i in range(rows): # so we can end early on testing runs: - if self.special is not None and 'testing' in self.special and i >= self.maxNevents: + if self.special is not None and "testing" in self.special and i >= self.maxNevents: break for j in range(cols): # so we can end early on testing runs: - if self.special is not None and 'testing' in self.special and j >= self.maxNevents: + if self.special is not None and "testing" in self.special and j >= self.maxNevents: break iDet, jDet = self.sliceToDetector(i, j) @@ -235,9 +234,11 @@ def analyze_h5_slice(self, dataFile, label): logger.info("empty profile for %d, %d" % (i, j)) continue if x is not None: - fitPar, covar, fitFunc, r2 = fitFunctions.fitLinearUnSaturatedData(x, y, saturated=self.saturated) + fitPar, covar, fitFunc, r2 = fitFunctions.fitLinearUnSaturatedData( + x, y, saturated=self.saturated + ) if True: - if i%10==0 and j%10==0: + if i % 10 == 0 and j % 10 == 0: print(i, j, fitPar, r2, 0) ##np.save("temp_r%dc%d_x.py" %(i,j), fluxes[g0]) ##np.save("temp_r%dc%d_y.py" %(i,j), y) @@ -316,7 +317,16 @@ def analyze_h5_slice(self, dataFile, label): if self.residuals: plt.figure(2) - figFileName = "%s/%s_slice_m%d_r%d_c%d_r%d_c%d_residuals_%s.png" % (self.outputDir, self.className, module, i, j, self.run, self.camera, label) + figFileName = "%s/%s_slice_m%d_r%d_c%d_r%d_c%d_residuals_%s.png" % ( + self.outputDir, + self.className, + module, + i, + j, + self.run, + self.camera, + label, + ) plt.savefig(figFileName) logger.info("Wrote file: " + figFileName) plt.close() @@ -332,16 +342,15 @@ def analyze_h5_slice(self, dataFile, label): print("have built an LPP") logger.info("have built an LPP") lpp.useNswitchedAsFlux = False - lpp.fluxLabel = "wave8 flux (ADU)" - if lpp.special is not None and 'useNswitchedAsFlux' in lpp.special: + lpp.fluxLabel = "wave8 flux (ADU)" + if lpp.special is not None and "useNswitchedAsFlux" in lpp.special: lpp.useNswitchedAsFlux = True lpp.fluxLabel = "number of low-gain pixels" - noSwitchedOnly = lpp.special is not None and 'noSwitchedOnly' in lpp.special + noSwitchedOnly = lpp.special is not None and "noSwitchedOnly" in lpp.special + + print("using switched pixels as flux? only events with no switch?", lpp.useNswitchedAsFlux, noSwitchedOnly) - print("using switched pixels as flux? only events with no switch?", - lpp.useNswitchedAsFlux, noSwitchedOnly) - if lpp.file is not None: print("using flux label:", lpp.fluxLabel) lpp.fitInfo = None @@ -359,7 +368,6 @@ def analyze_h5_slice(self, dataFile, label): print("not doing Kaz events") logger.info("not doing Kaz events") - lpp.setupPsana() size = 666 @@ -433,7 +441,7 @@ def analyze_h5_slice(self, dataFile, label): if nSwitched > 0: ##print('nSwitched: %d' %(nSwitched)) continue - + roiMeans = [] for i, roi in enumerate(lpp.ROIs): ##m = np.multiply(roi, frames).mean() @@ -453,12 +461,13 @@ def analyze_h5_slice(self, dataFile, label): for j, p in enumerate(lpp.singlePixels): singlePixelData.append([int(rawFrames[tuple(p)] >= lpp.g0cut), rawFrames[tuple(p)] & lpp.gainBitsMask]) - eventDict = {'fluxes':flux, - 'rois':np.array(roiMeans), - 'pixels':np.array(singlePixelData), - 'slice':rawFrames[lpp.regionSlice] + eventDict = { + "fluxes": flux, + "rois": np.array(roiMeans), + "pixels": np.array(singlePixelData), + "slice": rawFrames[lpp.regionSlice], } - + smd.event( evt, eventDict, diff --git a/suite_scripts/PersistenceCheckParallel.py b/suite_scripts/PersistenceCheckParallel.py index c42a953..daac168 100755 --- a/suite_scripts/PersistenceCheckParallel.py +++ b/suite_scripts/PersistenceCheckParallel.py @@ -56,7 +56,9 @@ def analyze_h5(self, dataFile, label): sys.exit(0) pcp.setupPsana() - smd = pcp.ds.smalldata(filename="%s/%s_c%d_r%d_n%d.h5" % (pcp.outputDir, pcp.className, pcp.camera, pcp.run, pcp.size)) + smd = pcp.ds.smalldata( + filename="%s/%s_c%d_r%d_n%d.h5" % (pcp.outputDir, pcp.className, pcp.camera, pcp.run, pcp.size) + ) nEvent = -1 nGoodEvents = 0 diff --git a/suite_scripts/simplePhotonCounter.py b/suite_scripts/simplePhotonCounter.py index f21efe3..2eb9b16 100755 --- a/suite_scripts/simplePhotonCounter.py +++ b/suite_scripts/simplePhotonCounter.py @@ -32,7 +32,7 @@ thresholded = None energyHistogramBins = 1000 energyHistogram = np.zeros(energyHistogramBins) - energyHistogramRange = [-100,900]## should get this from somewhere + energyHistogramRange = [-100, 900] ## should get this from somewhere gain = None if spc.fakePedestal is not None: if spc.detectorInfo is not None: @@ -77,8 +77,8 @@ if False and gain is not None: for e in frames.flatten(): - energyHistogram[((e-energyHistogramRange[0])/energyHistogramBins).clip(0, energyHistogramBins)] += 1 - + energyHistogram[((e - energyHistogramRange[0]) / energyHistogramBins).clip(0, energyHistogramBins)] += 1 + nGoodEvents += 1 if nGoodEvents % 100 == 0: print("n good events analyzed: %d" % (nGoodEvents)) @@ -104,8 +104,14 @@ logger.info("total photons in detector using cut %0.2f is %0.3f" % (spc.photonCut, (thresholded).sum())) if False: - spectrumFileName = "%s/%s_%s_r%d_c%d_%s_spectrum.npy" % (spc.outputDir, scriptType, spc.label, spc.run, spc.camera, spc.exp) + spectrumFileName = "%s/%s_%s_r%d_c%d_%s_spectrum.npy" % ( + spc.outputDir, + scriptType, + spc.label, + spc.run, + spc.camera, + spc.exp, + ) np.save(spectrumFileName, energyHistogram) - spc.dumpEventCodeStatistics()