Skip to content

Commit

Permalink
Merge pull request #16 from nstelter-slac/formatting_linting
Browse files Browse the repository at this point in the history
apply formatting/linting
  • Loading branch information
nstelter-slac authored Jan 30, 2024
2 parents 6841550 + 05093bb commit 72d0da9
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 139 deletions.
106 changes: 78 additions & 28 deletions scripts/AnalyzeH5.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
class FileNamingInfo:
def __init__(self, outputDir, className, run, camera, label):
self.outputDir = outputDir
self.className = className
self.className = className
self.run = run
self.camera = camera
self.label = label


# Setup logging.
# Log file gets appended to each new run, can manually delete for fresh log.
# Could change so makes new unique log each run or overwrites existing log.
logging.basicConfig(
filename='analyze_h5.log',
level=logging.INFO, # For full logging set to INFO which includes ERROR logging too
format='%(asctime)s - %(levelname)s - %(message)s' # levelname is log severity (ERROR, INFO, etc)
filename="analyze_h5.log",
level=logging.INFO, # For full logging set to INFO which includes ERROR logging too
format="%(asctime)s - %(levelname)s - %(message)s", # levelname is log severity (ERROR, INFO, etc)
)


Expand Down Expand Up @@ -48,7 +49,7 @@ def __init__(self):
parser.add_argument("-a", "--analysis_mode", type=int, help="choose analysis mode 1 or 2")
args = parser.parse_args()

if args.files == None:
if args.files is None:
print("No input files specified, quitting...")
logging.info("No input files specified, quitting...")
exit(1)
Expand All @@ -58,12 +59,18 @@ def __init__(self):
self.highEnergyCut = 30 # fix - should be 1.5 photons or something
self.sliceEdges = None
if args.slice_edges is not None:
self.sliceEdges = args.slice_edges.split(',')
self.sliceEdges = args.slice_edges.split(",")
self.sliceEdges = [int(curr) for curr in self.sliceEdges]
self.nBins = 100
self.shiftEnergy = False if args.shift_energy_bits == None else True
self.analysisNum = 1 if args.analysis_mode == None else int(args.analysis_mode)
self.fileNameInfo = FileNamingInfo(args.path, self.__class__.__name__, args.run, 0, args.label,)
self.shiftEnergy = False if args.shift_energy_bits is None else True
self.analysisNum = 1 if args.analysis_mode is None else int(args.analysis_mode)
self.fileNameInfo = FileNamingInfo(
args.path,
self.__class__.__name__,
args.run,
0,
args.label,
)
print("Output dir: " + self.fileNameInfo.outputDir)
logging.info("Output dir: " + self.fileNameInfo.outputDir)

Expand All @@ -86,7 +93,7 @@ def identifyAnalysis(self):
# do something useful here, maybe
# but for now
self.analysisType = "cluster"
if self.sliceEdges == None: # set if not already by cmdline args
if self.sliceEdges is None: # set if not already by cmdline args
self.sliceEdges = [288 - 270, 107 - 59]
self.sliceCoordinates = [[270, 288], [59, 107]]

Expand All @@ -99,15 +106,14 @@ def analyze(self):
logging.error(errorString)

def clusterAnalysis(self):

#energyHist = None
# energyHist = None
clusters = np.concatenate([h5["clusterData"][()] for h5 in self.h5Files])

# concat never works here since h5 undefined
try:
# meant to do similar thing as clusters above?
energyHist = None #np.concatenate(energyHist, h5["energyHistogram"][()])
#self.plotEnergyHist(energyHist, self.fileNameInfo)
pass # np.concatenate(energyHist, h5["energyHistogram"][()])
# self.plotEnergyHist(energyHist, self.fileNameInfo)
except Exception as e:
print(f"An exception occurred: {e}")
logging.error(f"An exception occurred: {e}")
Expand All @@ -122,18 +128,30 @@ def clusterAnalysis(self):
def plotEnergyHist(self, energyHist, fileInfo):
_, bins = np.histogram(energyHist, 250, [-5, 45])

plt.hist(bins[:-1], bins, weights=energyHist) #, log=True)
plt.hist(bins[:-1], bins, weights=energyHist) # , log=True)
plt.grid(which="major", linewidth=0.5)
plt.title = "All pixel energies in run after common mode correction"
plt.xlabel = "energy (keV)"
print("I hate matplotlib so much")
logging.info("I hate matplotlib so much")

fileNamePlot = "%s/%s_r%d_c%d_%s_energyHistogram.png" % (fileInfo.outputDir, fileInfo.className, fileInfo.run, fileInfo.camera, fileInfo.label)
fileNamePlot = "%s/%s_r%d_c%d_%s_energyHistogram.png" % (
fileInfo.outputDir,
fileInfo.className,
fileInfo.run,
fileInfo.camera,
fileInfo.label,
)
logging.info("Writing plot: " + fileNamePlot)
plt.savefig(fileNamePlot)

fileNameNpy = "%s/%s_r%d_c%d_%s_energyHistogram.npy" % (fileInfo.outputDir, fileInfo.className, fileInfo.run, fileInfo.camera, fileInfo.label)
fileNameNpy = "%s/%s_r%d_c%d_%s_energyHistogram.npy" % (
fileInfo.outputDir,
fileInfo.className,
fileInfo.run,
fileInfo.camera,
fileInfo.label,
)
logging.info("Writing npy: " + fileNameNpy)
np.save(fileNameNpy, energyHist)
plt.close()
Expand All @@ -145,17 +163,31 @@ def plot_overall_energy_distribution(self, energy, fileInfo):
print("mean energy above 0:", energy[energy > 0].mean())
logging.info("mean energy above 0:" + str(energy[energy > 0].mean()))

ax.hist(energy[energy > 0], 100) # 100 bins
ax.hist(energy[energy > 0], 100) # 100 bins
plt.xlabel = "energy (keV)"
plt.title = "All pixels"

fileName = "%s/%s_r%d_c%d_%s_E.png" % (fileInfo.outputDir, fileInfo.className, fileInfo.run, fileInfo.camera, fileInfo.label)
fileName = "%s/%s_r%d_c%d_%s_E.png" % (
fileInfo.outputDir,
fileInfo.className,
fileInfo.run,
fileInfo.camera,
fileInfo.label,
)
logging.info("Writing plot: " + fileName)
plt.savefig(fileName)
plt.close()

def save_fit_information(self, fitInfo, rows, cols, fileInfo):
fileName = "%s/%s_r%d_c%d_r%d_c%d_%s_fitInfo.npy" % (fileInfo.outputDir, fileInfo.className, fileInfo.run, fileInfo.camera, rows-1, cols-1, fileInfo.label)
fileName = "%s/%s_r%d_c%d_r%d_c%d_%s_fitInfo.npy" % (
fileInfo.outputDir,
fileInfo.className,
fileInfo.run,
fileInfo.camera,
rows - 1,
cols - 1,
fileInfo.label,
)
logging.info("Writing npy: " + fileName)
np.save(fileName, fitInfo)

Expand All @@ -167,38 +199,56 @@ def plot_gain_distribution(self, fitInfo, fileInfo, fitIndex):
ax.hist(goodGains, 100)
ax.set_xlabel("energy (keV)")
ax.set_title("pixel single photon fitted energy")
fileName = "%s/%s_r%d_c%d_%s_gainDistribution.png" % (fileInfo.outputDir, fileInfo.className, fileInfo.run, fileInfo.camera, fileInfo.label)
fileName = "%s/%s_r%d_c%d_%s_gainDistribution.png" % (
fileInfo.outputDir,
fileInfo.className,
fileInfo.run,
fileInfo.camera,
fileInfo.label,
)
logging.info("Writing plot: " + fileName)
plt.savefig(fileName)

def analyzeSimpleClusters(self, clusters):
energy = clusters[:, :, 0] #.flatten()
energy = clusters[:, :, 0] # .flatten()
if self.shiftEnergy:
energy *= 2 # temporary, due to bit shift
rows = self.sliceEdges[0]
cols = self.sliceEdges[1]
fitInfo = np.zeros((rows, cols, 4)) # mean, std, mu, sigma
fitInfo = np.zeros((rows, cols, 4)) # mean, std, mu, sigma
fitIndex = 0

self.plot_overall_energy_distribution(energy, self.fileNameInfo)

print("Analysis Mode: " + str(self.analysisNum))
logging.info("Analysis Mode: " + str(self.analysisNum))
if self.analysisNum == 1:
fitIndex = 2
fitInfo = pixelAnalysis.analysis_one(clusters, energy, rows, cols, fitInfo, self.lowEnergyCut, self.highEnergyCut, self.fileNameInfo)
fitInfo = pixelAnalysis.analysis_one(
clusters, energy, rows, cols, fitInfo, self.lowEnergyCut, self.highEnergyCut, self.fileNameInfo
)
else:
fitIndex = 3
fitInfo = pixelAnalysis.analysis_two(clusters, self.nBins, self.sliceCoordinates, rows, cols, fitInfo, self.lowEnergyCut, self.highEnergyCut, self.fileNameInfo)
fitInfo = pixelAnalysis.analysis_two(
clusters,
self.nBins,
self.sliceCoordinates,
rows,
cols,
fitInfo,
self.lowEnergyCut,
self.highEnergyCut,
self.fileNameInfo,
)

self.save_fit_information(fitInfo, rows, cols, self.fileNameInfo)
self.plot_gain_distribution(fitInfo, self.fileNameInfo, fitIndex)


if __name__ == "__main__":
print ("Starting new run!")
print("Starting new run!")
logging.info("Starting new run!")
ah5 = AnalyzeH5()
ah5.getFiles()
ah5.identifyAnalysis()
ah5.analyze()
ah5.analyze()
7 changes: 4 additions & 3 deletions scripts/EventScanParallelSlice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
rank = comm.Get_rank()
size = comm.Get_size()


class EventScanParallel(BasicSuiteScript):
def __init__(self):
super().__init__() ##self)
Expand Down Expand Up @@ -93,9 +94,9 @@ def analyzeData(self, delays, data, label):
for c in range(data.shape[3]):
d = data[:, m, r, c]
# where are these funcs defined?, ignoring from ruff for now
p0 = estimateFineScanPars(delays, d) # noqa: F821
f = fineScanFunc # noqa: F821
coeff, var = curve_fit(f, delays, d, p0=p0) # noqa: F821
p0 = estimateFineScanPars(delays, d) # noqa: F821
f = fineScanFunc # noqa: F821
coeff, var = curve_fit(f, delays, d, p0=p0) # noqa: F821
edge[m, r, c] = coeff[1]
return edge

Expand Down
30 changes: 18 additions & 12 deletions scripts/ancillaryMethods.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binned_statistic


def makeProfile(x, y, bins, range=None, spread=False):
## NaN for empty bins are suppressed
## using mean root(N) for non-empty bins to calculate 0 var weights
##
## spread=True to return standard deviation instead of standard error

meansObj = binned_statistic(x, [y, y**2], bins=bins, range=range, statistic='mean')
meansObj = binned_statistic(x, [y, y**2], bins=bins, range=range, statistic="mean")
means, means2 = meansObj.statistic
countsObj = binned_statistic(x, [y, y**2], bins=bins, range=(0,1), statistic='count')
countsObj = binned_statistic(x, [y, y**2], bins=bins, range=(0, 1), statistic="count")
bin_N = countsObj.statistic
yErr = np.sqrt(means2 - means**2)
if not spread:
root_N = np.sqrt(bin_N)
root_N[root_N==0] = root_N[root_N>0].mean()
yErr = yErr/root_N
root_N[root_N == 0] = root_N[root_N > 0].mean()
yErr = yErr / root_N
##yErr = yErr.clip(0, 6666666.)
bin_edges = means_result.bin_edges
bin_centers = (bin_edges[:-1] + bin_edges[1:])/2.
usefulBins = bin_N>0
# where is means_result defined?? (ignore from ruff for now)
bin_edges = means_result.bin_edges # noqa: F821
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.0
usefulBins = bin_N > 0
return bin_centers[usefulBins], means[usefulBins], yErr[usefulBins]


def plotProfile(x, y, yErr):
plt.errorbar(x=x, y=y, yerr=yErr, linestyle='none', marker='.')
plt.errorbar(x=x, y=y, yerr=yErr, linestyle="none", marker=".")


def selectedClusters(clusters, row, col, lowEnerygCut, highEnergyCut, nPixelCut=4, isSquare=1):
pass


def goodClusters(clusters, row, col, nPixelCut=4, isSquare=None):
##print(clusters)
pixelRowCol = np.bitwise_and((clusters[:,:,1] == row),
(clusters[:,:,2] == col))
pixelRowCol = np.bitwise_and((clusters[:, :, 1] == row), (clusters[:, :, 2] == col))
if isSquare is None:
small = clusters[:,:,3]<nPixelCut
small = clusters[:, :, 3] < nPixelCut
else:
small = np.bitwise_and((clusters[:,:,3]<nPixelCut), (clusters[:,:,4]==isSquare))
small = np.bitwise_and((clusters[:, :, 3] < nPixelCut), (clusters[:, :, 4] == isSquare))
return clusters[np.bitwise_and(small, pixelRowCol)]


def getClusterEnergies(clusters):
##print(clusters)
return clusters[:, 0]
4 changes: 3 additions & 1 deletion scripts/basicSuiteScript.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import numpy as np
import os
import os
from rixSuiteConfig import experimentHash

##from mfxRixSuiteConfig import *
Expand All @@ -12,9 +12,11 @@
print("psana2")
from psana2Base import PsanaBase


def sortArrayByList(a, data):
return [x for _, x in sorted(zip(a, data), key=lambda pair: pair[0])]


class BasicSuiteScript(PsanaBase):
def __init__(self, analysisType="scan"):
super().__init__()
Expand Down
Loading

0 comments on commit 72d0da9

Please sign in to comment.