avg = sum(s)*1.0/len(s)
    variance = map(lambda x: (x-avg)**2, s)
    return math.sqrt(average(variance))
    
def average(x):
    average = sum(x)*1.0/len(x)
    return average

IJ.log("Results for " + path.basename(stackpath) + ":")    
IJ.log("Average intensity: " + str(average(means))) 
IJ.log("Standard deviation: " + str(stdev(means)))

# now, use the subtracted image to output the flat field image and plot

# get the plot out of the way to bring back the stack
WM.putBehind()
#average all frames
IJ.run("Z Project...", "projection=[Average Intensity]")

# plot the diagonal profile
imp = IJ.getImage()
IJ.run("Line Width...", "line=3")
IJ.makeLine(0, 0, imp.width, imp.height)
IJ.run("Plot Profile")

# bring back the projection, run Measure to get max and min
WM.putBehind()
IJ.run("Measure")  #this method reports the actual max and min, not averaged across 3 px...
rt = ResultsTable.getResultsTable()
max = rt.getValue("Max", 0)
min = rt.getValue("Min", 0)