Skip to content

Commit

Permalink
Merge pull request #115 from slaclab/run_lint_format_script
Browse files Browse the repository at this point in the history
ENH: apply linting and formatting script's changes
  • Loading branch information
nstelter-slac authored Jun 12, 2024
2 parents 3053e90 + 4a73c07 commit 80bcd1e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 58 deletions.
6 changes: 2 additions & 4 deletions calibrationSuite/ancillaryMethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



29 changes: 15 additions & 14 deletions calibrationSuite/basicSuiteScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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__":
Expand Down
3 changes: 1 addition & 2 deletions calibrationSuite/detectorInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

21 changes: 15 additions & 6 deletions calibrationSuite/fitFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions config_files/archonSuiteConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
2 changes: 1 addition & 1 deletion suite_scripts/CalcNoiseAndMean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 5 additions & 3 deletions suite_scripts/DeltaTtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
49 changes: 29 additions & 20 deletions suite_scripts/LinearityPlotsParallelSlice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion suite_scripts/PersistenceCheckParallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions suite_scripts/simplePhotonCounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand All @@ -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()

0 comments on commit 80bcd1e

Please sign in to comment.