Skip to content

Commit

Permalink
ENH: Add logging to commonly used scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nstelter-slac committed Mar 7, 2024
1 parent 5205914 commit 24a11d0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
41 changes: 27 additions & 14 deletions suite_scripts/CalcNoiseAndMean.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from calibrationSuite.basicSuiteScript import *
from calibrationSuite.Stats import *
import calibrationSuite.loggingSetup as ls

# for logging from current file
logger = logging.getLogger(__name__)
# log to file named <curr script name>.log
currFileName = os.path.basename(__file__)
ls.setupScriptLogging(currFileName[:-3] + ".log", logging.INFO) # change to logging.INFO for full logging output


class CalcNoise(BasicSuiteScript):
def __init__(self):
super().__init__("dark") ##self)
super().__init__("dark")


if __name__ == "__main__":
cn = CalcNoise()
print("have built a", cn.className, "class")
print("have built a" + cn.className + "class")
logger.info("have built a" + cn.className + "class")

cn.setupPsana()
if cn.special is not None and "skip281" in cn.special:
Expand All @@ -18,7 +26,7 @@ def __init__(self):
skip281 = False

stepGen = cn.getStepGen()
## for nstep, step in enumerate (cn.ds.steps()):
##for nstep, step in enumerate (cn.ds.steps()):
for nstep, step in enumerate(stepGen):
statsArray = [None for i in cn.singlePixels]
for nevt, evt in enumerate(step.events()):
Expand Down Expand Up @@ -57,12 +65,14 @@ def __init__(self):
continue

print("empty non-None frames")
logger.info("empty non-None frames")
continue
else:
##print(ec)
continue
if frames is None:
print("no frame")
logger.info("no frame")
continue
for i, p in enumerate(cn.singlePixels):
try:
Expand All @@ -75,23 +85,26 @@ def __init__(self):
means = stats.mean()
if cn.special is not None and "slice" in cn.special:
noise = noise[cn.regionSlice]
print("mean, median noise:", noise.mean(), np.median(noise))
print("mean, median noise:" + str(noise.mean()) + str(np.median(noise)))
logger.info("mean, median noise:" + str(noise.mean()) + str(np.median(noise)))
means = means[cn.regionSlice]
else:
pass
np.save("%s/CalcNoiseAndMean_rms_r%d_step%s.npy" % (cn.outputDir, cn.run, nstep), noise)
np.save("%s/CalcNoiseAndMean_mean_r%d_step%s.npy" % (cn.outputDir, cn.run, nstep), means)

meanRmsFileName = "%s/CalcNoiseAndMean_rms_r%d_step%s.npy" % (cn.outputDir, cn.run, nstep)
np.save(meanRmsFileName, noise)
meanFileName = "%s/CalcNoiseAndMean_mean_r%d_step%s.npy" % (cn.outputDir, cn.run, nstep)
np.save(meanFileName, means)
logger.info("Wrote file: " + meanRmsFileName)
logger.info("Wrote file: " + meanFileName)

for i, p in enumerate(cn.singlePixels):
try:
np.save(
"%s/CalcNoiseAndMean_correlation_pixel_%d_%d_%s_r%d_step%s.npy"
% (cn.outputDir, p[1], p[2], cn.label, cn.run, nstep),
statsArray[i].corr(statsArray[i].mean()[p[1], p[2]], statsArray[i].rms()[p[1], p[2]])[
cn.regionSlice
],
)
meanCorrelationFileName = "%s/CalcNoiseAndMean_correlation_pixel_%d_%d_%s_r%d_step%s.npy" % (cn.outputDir, p[1], p[2], cn.label, cn.run, nstep),
np.save(meanCorrelationFileName, statsArray[i].corr(statsArray[i].mean()[p[1], p[2]], statsArray[i].rms()[p[1], p[2]])[cn.regionSlice],)
logging.info("Wrote file: " + meanCorrelationFileName)
except:
## probably rms = 0.
continue

cn.dumpEventCodeStatistics()
cn.dumpEventCodeStatistics()
58 changes: 40 additions & 18 deletions suite_scripts/EventScanParallel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import os
import h5py
from calibrationSuite.basicSuiteScript import *
import calibrationSuite.loggingSetup as ls
from matplotlib.ticker import AutoMinorLocator

# for logging from current file
logger = logging.getLogger(__name__)
# log to file named <curr script name>.log
currFileName = os.path.basename(__file__)
ls.setupScriptLogging(currFileName[:-3] + ".log", logging.INFO) # change to logging.INFO for full logging output

class EventScanParallel(BasicSuiteScript):
def __init__(self):
super().__init__() ##self)
super().__init__()

def plotData(self, data, pixels, eventNumbers, label):
if "timestamp" in label:
Expand All @@ -28,10 +37,10 @@ def plotData(self, data, pixels, eventNumbers, label):
minor_locator = AutoMinorLocator(5)
ax.xaxis.set_minor_locator(minor_locator)
plt.grid(which="minor", linewidth=0.5)
plt.savefig(
"%s/%s_r%d_c%d_%s_ROI%d.png"
% (self.outputDir, self.__class__.__name__, self.run, self.camera, label, i)
)

figFileName = "%s/%s_r%d_c%d_%s_ROI%d.png" % (self.outputDir, self.__class__.__name__, self.run, self.camera, label, i)
plt.savefig(figFileName)
logger.info("Wrote file: " + figFileName)
plt.clf()

for i, roi in enumerate(self.ROIs):
Expand All @@ -46,9 +55,10 @@ def plotData(self, data, pixels, eventNumbers, label):
plt.ylabel("Mean (ADU)")
##plt.yscale('log')
plt.legend(loc="upper right")
plt.savefig(
"%s/%s_r%d_c%d_%s_All%d.png" % (self.outputDir, self.__class__.__name__, self.run, self.camera, label, i)
)

figFileName = "%s/%s_r%d_c%d_%s_All%d.png" % (self.outputDir, self.__class__.__name__, self.run, self.camera, label, i)
plt.savefig(figFileName)
logger.info("Wrote file: " + figFileName)
plt.clf()
# plt.show()

Expand All @@ -60,10 +70,10 @@ def plotData(self, data, pixels, eventNumbers, label):
##ax.scatter(eventNumbers, pixels[i], marker='.', s=1, label=str(p))
plt.xlabel(xlabel)
plt.ylabel("Pixel ADU")
plt.savefig(
"%s/%s_r%d_c%d_%s_pixel%d.png"
% (self.outputDir, self.__class__.__name__, self.run, self.camera, label, i)
)

figFileName = "%s/%s_r%d_c%d_%s_pixel%d.png" % (self.outputDir, self.__class__.__name__, self.run, self.camera, label, i)
plt.savefig(figFileName)
logger.info("Wrote file: " + figFileName)
plt.clf()

def analyzeData(self, delays, data, label):
Expand All @@ -79,15 +89,17 @@ def analyzeData(self, delays, data, label):
return edge

def analyze_h5(self, dataFile, label):
import h5py

data = h5py.File(dataFile)

ts = data["timestamps"][()]
print(ts)

pixels = data["pixels"][()]
rois = data["rois"][()]
pixels = sortArrayByList(ts, pixels)
rois = sortArrayByList(ts, rois)

# get time differences
ts.sort()
ts = ts - ts[0]
##ts = ts/np.median(ts[1:]-ts[0:-1])
Expand All @@ -105,15 +117,19 @@ def analyze_h5(self, dataFile, label):

if __name__ == "__main__":
esp = EventScanParallel()
print("have built a", esp.className, "class")
print("have built a" + esp.className + "class")
logger.info("have built a" + esp.className + "class")

if esp.file is not None:
esp.analyze_h5(esp.file, esp.label)
print("done with standalone analysis of %s, exiting" % (esp.file))
logger.info("done with standalone analysis of %s, exiting" % (esp.file))
sys.exit(0)

esp.setupPsana()

smd = esp.ds.smalldata(filename="%s/%s_c%d_r%d_n%d.h5" % (esp.outputDir, esp.className, esp.camera, esp.run, size))
h5FileName = "%s/%s_c%d_r%d_n%d.h5" % (esp.outputDir, esp.className, esp.camera, esp.run, size)
smd = esp.ds.smalldata(filename=h5FileName)

esp.nGoodEvents = 0
roiMeans = [[] for i in esp.ROIs]
Expand Down Expand Up @@ -149,14 +165,20 @@ def analyze_h5(self, dataFile, label):
esp.nGoodEvents += 1
if esp.nGoodEvents % 100 == 0:
print("n good events analyzed: %d" % (esp.nGoodEvents))
logger.info("n good events analyzed: %d" % (esp.nGoodEvents))

if esp.nGoodEvents > esp.maxNevents:
break

np.save("%s/means_c%d_r%d_%s.npy" % (esp.outputDir, esp.camera, esp.run, esp.exp), np.array(roiMeans))
np.save("%s/eventNumbers_c%d_r%d_%s.npy" % (esp.outputDir, esp.camera, esp.run, esp.exp), np.array(eventNumbers))
npMeansFileName = "%s/means_c%d_r%d_%s.npy" % (esp.outputDir, esp.camera, esp.run, esp.exp)
np.save(npMeansFileName, np.array(roiMeans))
npEventFileName = "%s/eventNumbers_c%d_r%d_%s.npy" % (esp.outputDir, esp.camera, esp.run, esp.exp)
np.save(npEventFileName, np.array(eventNumbers))
logger.info("Wrote file: " + npMeansFileName)
logger.info("Wrote file: " + npEventFileName)
esp.plotData(roiMeans, pixelValues, eventNumbers, "foo")

##if smd.summary:
##smd.save_summary(
smd.done()
logger.info("Wrote file: " + h5FileName)
26 changes: 12 additions & 14 deletions suite_scripts/EventScanParallelSlice.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from calibrationSuite.basicSuiteScript import *
import calibrationSuite.loggingSetup as ls
import logging
import h5py
import os

# for logging from current file
logger = logging.getLogger(__name__)

import calibrationSuite.loggingSetup as ls
import os

# log to file named <curr script name>.log
currFileName = os.path.basename(__file__)
ls.setupScriptLogging(currFileName[:-3] + ".log", logging.ERROR) # change to logging.INFO for full logging output


class EventScanParallel(BasicSuiteScript):
def __init__(self):
super().__init__("misc") ##self)
Expand Down Expand Up @@ -132,17 +130,17 @@ def analyzeData(self, delays, data, label):
return edge

def analyze_h5(self, dataFile, label):
import h5py

data = h5py.File(dataFile)
print(data.keys())
logger.info("keys: " + str(data.keys))

ts = data["timestamps"][()]
print(ts)
print("ts: ", ts)

pulseIds = data["pulseIds"][()]
pixels = data["pixels"][()]
rois = data["rois"][()]

# get summedBitSlice and save it to a numpy file
try:
bitSlice = data["summedBitSlice"][()]
npyFileName = "%s/bitSlice_c%d_r%d_%s.npy" % (self.outputDir, self.camera, self.run, self.exp)
Expand All @@ -151,30 +149,30 @@ def analyze_h5(self, dataFile, label):
except:
pass

# sort and save pulseIds to a numpy file
pulseIds.sort()

npyFileName = "%s/pulseIds_c%d_r%d_%s.npy" % (self.outputDir, self.camera, self.run, self.exp)
logger.info("Wrote file: " + npyFileName)
np.save(npyFileName, np.array(pulseIds))
dPulseId = pulseIds[1:] - pulseIds[0:-1]

# sort pixels and rois based on timestamps
pixels = sortArrayByList(ts, pixels)
rois = sortArrayByList(ts, rois)


ts.sort()
ts = ts - ts[0]
##ts = ts/np.median(ts[1:]-ts[0:-1])
print("ts: ", ts)

self.plotData(np.array(rois).T, np.array(pixels).T, ts, dPulseId, "timestamps" + label)


if __name__ == "__main__":
commandUsed = sys.executable + " " + " ".join(sys.argv)
logger.info("Ran with cmd: " + commandUsed)

esp = EventScanParallel()
print("have built a" + esp.className + "class")
logger.info("have built a" + esp.className + "class")

if esp.file is not None:
esp.analyze_h5(esp.file, esp.label)
print("done with standalone analysis of %s, exiting" % (esp.file))
Expand Down
2 changes: 1 addition & 1 deletion suite_scripts/simplePhotonCounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self):
break
if not spc.isBeamEvent(evt):
continue

print ("HELLO")
gain = None
if spc.special is not None and "fakePedestal" in spc.special:
if "FH" in spc.special:
Expand Down

0 comments on commit 24a11d0

Please sign in to comment.