This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunstable.py
executable file
·299 lines (275 loc) · 11.6 KB
/
unstable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
import numpy as N
import h5py as H
import glob as G
import matplotlib
import matplotlib.pyplot as P
import sys, os, re, shutil, subprocess
from myModules import extractDetectorDist as eDD
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-r", "--run", action="store", type="string", dest="runNumber", help="run number you wish to view", metavar="xxxx", default="")
parser.add_option("-W", "--weakHitsTreatment", action="store", type="int", dest="weakHitsTreatment", help="(default)0, stores ang_avg.h5 file names as rxxxx_weakAvgFiles.txt;\n1, also shows averages both ang_avg and 2D patterns (slow);", metavar="0 or 1", default=0)
parser.add_option("-S", "--strongHitsTreatment", action="store", type="int", dest="strongHitsTreatment", help="(default)0, stores ang_avg.h5 filenames as rxxxx_strongAvgFiles.txt in output dir;\n1, also shows averages both ang_avg and 2D patterns (slow);", metavar="0 or 1", default=0)
parser.add_option("-c", "--copyFiles", action="store_true", dest="store_files", help="copy *.angavg.h5 files into output directory",default=False)
parser.add_option("-o", "--outputdir", action="store", type="string", dest="outputDir", help="output directory (default: output_rxxxx)", metavar="myOutputDir", default="output")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="prints out the frame number as it is processed", default=False)
(options, args) = parser.parse_args()
########################################################
# Edit this variable accordingly
# Files are read for source_dir/runtag and
# written to write_dir/runtag.
# Be careful of the trailing "/";
# ensure you have the necessary read/write permissions.
########################################################
source_dir = "/reg/d/psdm/cxi/cxi25410/scratch/cleaned_hdf5/"
ang_avg_dir = "/reg/d/psdm/cxi/cxi25410/scratch/cleaned_hdf5/"
runtag = "r%s"%(options.runNumber)
write_dir = options.outputDir + '_' + runtag + '/'
canonicalOrderedHitsFN = write_dir + runtag + "_orderedFiles.txt"
canonicalOrderedIntensInHitsFN = write_dir + runtag + "_orderedIntens.txt"
h5files= []
integratedIntens = []
orderedH5Files = []
foundFiles = []
#Check if write_dir already exists, and which angavg files have already
#been copied over.
if not os.path.exists(write_dir):
os.mkdir(write_dir)
else:
write_anomaly_dir = write_dir
originaldir=os.getcwd()
foundTypes=[write_dir]
anomalousTypes=G.glob(write_anomaly_dir+"type[1-9]")
if (len(anomalousTypes)>0):
foundTypes+=anomalousTypes
numTypes=len(foundTypes)
for cDir in foundTypes:
os.chdir(cDir)
foundFiles+=G.glob("LCLS*angavg.h5")
os.chdir(originaldir)
print "Found %d types (including type0) with %d files" % (numTypes, len(foundFiles))
if(options.verbose):
print "detector at distance: %lf" % eDD.get_detector_dist_in_meters(runtag)
#Always check the angavg files in searchDir.
searchDir = ang_avg_dir + runtag
print "Now examining H5 files in %s/ ..."%(searchDir)
searchstring="[a-zA-Z0-9\_]+"+runtag+"[a-z0-9\_]+-angavg.h5"
h5pattern = re.compile(searchstring)
h5files = [x for x in os.listdir(searchDir) if h5pattern.findall(x)]
numFiles = len(h5files)
#Check if file sorting has already been done.
if(os.path.exists(canonicalOrderedHitsFN) and os.path.exists(canonicalOrderedIntensInHitsFN)):
print "Found sorted filenames and intens lists."
f = open(canonicalOrderedHitsFN, 'r')
orderedH5Files = N.array(f.read().split())
f.close()
f = open(canonicalOrderedIntensInHitsFN, 'r')
integratedIntens = N.array([float(i) for i in f.read().split()])
f.close()
#Check if presorted files are identical to those in searchDir:
#Re-sort only if different.
if(len(set(orderedH5Files) - set(h5files)) == 0):
print "Number of pre-sorted filenames matches those in %s" % (searchDir)
P.plot(integratedIntens)
P.title("Note the strong/weak hits cutoff")
P.xlabel("sorted frame number")
P.ylabel("integrated radial intensities of each run")
P.show()
else:
print "Found %d angavg files, sorting them now.."%(numFiles)
integratedIntens = N.zeros(numFiles)
for i in range(numFiles):
fullFilePath = ang_avg_dir+runtag+"/"+h5files[i]
f = H.File(fullFilePath, 'r')
integratedIntens[i] = N.abs(N.array(f['/data/data'])).sum()
f.close()
ordering = integratedIntens.argsort()
orderedH5Files = N.array(h5files)[ordering]
P.plot(integratedIntens[ordering])
P.title("Note the strong/weak hits cutoff")
P.xlabel("sorted frame number")
P.ylabel("integrated radial intensities of each run")
P.show()
orderedH5Files.tofile(canonicalOrderedHitsFN, sep="\n")
(integratedIntens[ordering]).tofile(canonicalOrderedIntensInHitsFN, sep="\n")
print "wrote to files %s and %s"%(canonicalOrderedIntensInHitsFN, canonicalOrderedHitsFN)
colmax=1000
colmin=0
########################################################
# Imaging class modified from Ingrid Ofte's pyana_misc code
########################################################
class img_class (object):
def __init__(self, inarr, inangavg , filename, meanWaveLengthInAngs=eDD.nominalWavelengthInAngs):
self.inarr = inarr*(inarr>0)
self.filename = filename
self.inangavg = inangavg
self.HIceQ ={}
for i,j in eDD.iceHInvAngQ.iteritems():
self.HIceQ[i] = eDD.get_pix_from_invAngsQ(runtag,j, meanWaveLengthInAngs)
global colmax
global colmin
def on_keypress(self,event):
global colmax
global colmin
if event.key == 'p':
if not os.path.exists(write_dir):
os.mkdir(write_dir)
pngtag = write_dir +"%s.png" % (self.filename)
print "saving image as " + pngtag
P.savefig(pngtag)
if event.key == 'r':
colmin = self.inarr.min()
colmax = self.inarr.max()
P.clim(colmin, colmax)
P.draw()
def on_click(self, event):
global colmax
global colmin
if event.inaxes:
lims = self.axes.get_clim()
colmin = lims[0]
colmax = lims[1]
range = colmax - colmin
value = colmin + event.ydata * range
if event.button is 1 :
if value > colmin and value < colmax :
colmin = value
elif event.button is 2 :
colmin = self.inarr.min()
colmax = ((self.inarr)).max()
elif event.button is 3 :
if value > colmin and value < colmax:
colmax = value
P.clim(colmin, colmax)
P.draw()
def draw_img(self):
global colmax
global colmin
fig = P.figure(num=None, figsize=(13.5, 5), dpi=100, facecolor='w', edgecolor='k')
cid1 = fig.canvas.mpl_connect('key_press_event', self.on_keypress)
cid2 = fig.canvas.mpl_connect('button_press_event', self.on_click)
canvas = fig.add_subplot(121)
canvas.set_title(self.filename)
self.axes = P.imshow(self.inarr, origin='lower', vmax = colmax, vmin = colmin)
self.colbar = P.colorbar(self.axes, pad=0.01)
self.orglims = self.axes.get_clim()
canvas = fig.add_subplot(122)
canvas.set_title("angular average")
P.plot(self.inangavg)
maxAngAvg = (self.inangavg).max()
numQLabels = len(self.HIceQ.keys())+1
labelPosition = maxAngAvg/numQLabels
for i,j in self.HIceQ.iteritems():
P.axvline(j,0,colmax,color='r')
P.text(j,labelPosition,str(i), rotation="45")
labelPosition += maxAngAvg/numQLabels
P.show()
instructions="Right-click on colorbar to set maximum scale.\nLeft-click on colorbar to set minimum scale.\nCenter-click on colorbar (or press 'r') to reset color scale.\nInteractive controls for zooming at the bottom of figure screen (zooming..etc).\nPress 'p' to save PNG of image (with the current colorscales) in the appropriately named folder.\nHit Ctl-\ or close all windows (Alt-F4) to terminate viewing program."
#######################################################
#Records strong and weak file names into a textfile in output dir
######################################################
cutoff = int(input("weak/strong hits cutoff? "))
weakFiles = orderedH5Files[:cutoff]
recordTag = write_dir + runtag + "_weakAvgFiles.txt"
N.array(weakFiles).tofile(recordTag, sep='\n')
strongFiles = orderedH5Files[cutoff:]
recordTag = write_dir + runtag + "_strongAvgFiles.txt"
N.array(strongFiles).tofile(recordTag, sep='\n')
#Copies only new files that were
if(options.store_files):
sStrong = set(strongFiles)
sWeak = set(weakFiles)
sFound = set(foundFiles)
diffFiles = sStrong - sFound # != sFound-sStrong
#Check we found angavg files in outputDir that weren't in searchDir
if(len(sFound & set(h5files)) != len(sFound)):
print "There are angavg files in the %s that are not in %s. Confused. Aborting." % (write_dir, searchDir)
sys.exit(1)
#Check if we found more(or fewer) angavg files than already in outputDir
elif(len(diffFiles)==0):
diffFiles = sFound - sStrong
if(len(diffFiles) > 0):
recordTag = write_dir + runtag + "_excessFiles.txt"
N.array(list(diffFiles)).tofile(recordTag, sep='\n')
print "Excess angavg files in %s that did not make the cutoff stored in %s" % (write_dir, recordTag)
else:
print "All strong hits already in %s. Nothing copied." % (write_dir)
else:
print "%d strong hits copied earlier. Will copy only %d of the remainder" % (len(foundFiles), len(diffFiles))
for fname in diffFiles:
diffractionName = source_dir+runtag+"/"+re.sub("-angavg",'',fname)
angAvgName = ang_avg_dir + runtag + '/' + fname
if(os.path.exists(diffractionName)):
shutil.copyfile(angAvgName, write_dir+fname)
########################################################
# Weakly scattering files
#########################################################
waveLengths=[]
if (options.weakHitsTreatment == 1):
print "averaging weak hits..."
arr = []
avg = N.zeros(1233)
fcounter = 0
for fname in weakFiles:
diffractionName = source_dir+runtag+"/"+re.sub("-angavg",'',fname)
if(options.verbose and (fcounter%10==0)):
print str(fcounter) + " of " + str(cutoff) + " weak files"
fcounter += 1
if(os.path.exists(diffractionName)):
f = H.File(diffractionName, 'r')
d = N.array(f['/data/data'])
waveLengths.append(f['LCLS']['photon_wavelength_A'][0])
if arr == []:
arr = d.astype('float64')
arr += d
f.close()
angAvgName = ang_avg_dir + runtag + '/' + fname
f = H.File(angAvgName, 'r')
davg = N.array(f['data']['data'][0])
avg += davg
f.close()
arr /= len(weakFiles)
[colmax, colmin] = [arr.max(), 0.]
currImg = img_class(arr, avg/len(weakFiles), runtag+"_AvgPattWeakHits", meanWaveLengthInAngs=N.mean(waveLengths))
print instructions
currImg.draw_img()
avg.tofile(write_dir + runtag + "_weak_avg.txt", sep = "\n", format="%lf")
########################################################
# Strongly scattering files
########################################################
waveLengths=[]
if(options.strongHitsTreatment == 1):
print "averaging strong hits"
arr = []
avg = N.zeros(1233)
fcounter = 0
numStrongFiles = len(strongFiles)
numPresentStrongFiles = 0
for fname in strongFiles:
diffractionName = source_dir+runtag+"/"+re.sub("-angavg",'',fname)
if(options.verbose and (fcounter%10==0)):
print str(fcounter) + " of " + str(numStrongFiles) + " strong files"
fcounter += 1
if(os.path.exists(diffractionName)):
f = H.File(diffractionName, 'r')
d = N.array(f['/data/data'])
waveLengths.append(f['LCLS']['photon_wavelength_A'][0])
if arr == []:
arr = d.astype('float64')
arr += d
f.close()
angAvgName = ang_avg_dir + runtag + '/' + fname
f = H.File(angAvgName, 'r')
davg = N.array(f['data']['data'][0])
avg += davg
f.close()
numPresentStrongFiles += 1.
else:
print diffractionName + " not found!"
arr /= numPresentStrongFiles
[colmax, colmin] = [arr.max(), 0.]
currImg = img_class(arr, avg/len(strongFiles), runtag+"_AvgPattStrongHits", meanWaveLengthInAngs=N.mean(waveLengths))
print instructions
currImg.draw_img()
avg.tofile(write_dir + runtag + "_strong_avg.txt", sep = "\n", format="%lf")