Skip to content

Instantly share code, notes, and snippets.

@ZGainsforth
Last active November 13, 2015 23:16
Show Gist options
  • Select an option

  • Save ZGainsforth/cf339f6761cfdb6e930d to your computer and use it in GitHub Desktop.

Select an option

Save ZGainsforth/cf339f6761cfdb6e930d to your computer and use it in GitHub Desktop.
Go through a diffraction map and produce a sum image, average image, stdev image, and a real space residual image.
# Created 2015, Zack Gainsforth
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
from matplotlib.image import imsave
import numpy as np
import sys
import math
import os
from numba import jit
from tifffile import imread
def ComputeFileScalar(FileName):
Arr = imread(FileName)
return np.std(Arr)
### EDIT ME FOR EACH DIFFRACTION MAP!!!! ###
x_pixels = 114
y_pixels = 19
FileFormatStr = '/DanteACI2_05 10 keV/DanteACI2_05 10 keV/DanteACI2_05_%05d.tif'
### DONE EDITING! ###
print 'First pass: compute the average and sum images.'
# Get the first image, and therefore the size of the image.
FileName = os.getcwd() + FileFormatStr % (1)
Arr = imread(FileName).astype('float')
# Get a sum image.
SumImg = np.zeros(Arr.shape).astype('float')
SkippedFiles = 0
for i in range(y_pixels*x_pixels):
FileName = os.getcwd() + FileFormatStr % (i+1)
if not os.path.isfile(FileName):
print "File %s doesn't exist." % FileName
SkippedFiles += 1
continue
print 'Pass 1: ' + FileName
Arr = imread(FileName).astype('float')
SumImg += Arr
np.savetxt('SumImage.txt', SumImg)
AvgImg = np.copy(SumImg) / (y_pixels*x_pixels-SkippedFiles)
np.savetxt('AvgImage.txt', AvgImg)
print 'Second pass: compute the stdev and residual images.'
StDevImage = np.zeros(AvgImg.shape).astype('float')
R = np.zeros((x_pixels,y_pixels), dtype=(float))
S = np.zeros((x_pixels,y_pixels), dtype=(float))
for y in range(y_pixels):
for x in range(x_pixels):
FileName = FileFormatStr % (x + y*x_pixels + 1)
FileName = os.getcwd() + FileName
if not os.path.isfile(FileName):
R[x,y] = 0
S[x,y] = 0
print "File %s doesn't exist. R=0" % FileName
continue
print 'Pass 2: ' + FileName
Arr = imread(FileName)
StDevArr = (Arr - AvgImg)
R[x,y] = np.sum(np.sum(StDevArr))
S[x,y] = np.std(Arr)
StDevImage += StDevArr**2
# Save the residual image.
np.savetxt('ResidualImage.txt', R)
np.savetxt('SpikeImage.txt', S)
# Save the stdev image
np.savetxt('StDevImage.txt', StDevImage/(x_pixels*y_pixels-SkippedFiles))
print 'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment