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 pathviewRun.py
executable file
·358 lines (320 loc) · 13.5 KB
/
viewRun.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python
import numpy as N
from numpy import linalg as LA
import h5py as H
import glob as G
import os
import re
import pylab as P
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="rxxxx")
parser.add_option("-i", "--inspectOnly", action="store_true", dest="inspectOnly", help="inspect output directory", default=False)
parser.add_option("-s", "--step", action="store_true", dest="stepThroughImages", help="step through individual images in types (except type 0)", default=False)
parser.add_option("-o", "--outputDir", action="store", type="string", dest="outputDir", help="output directory (also inspection directory) will be appended by run number (default: output_rxxxx); separate types will be stored in output_rxxxx/anomaly/type[1-3]", default="output")
parser.add_option("-M", "--maxIntens", action="store", type="int", dest="maxIntens", help="doesn't plot intensities above this value", default=10000)
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()
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 + '/'
write_anomaly_dir = write_dir
originaldir=os.getcwd()
foundTypes=G.glob(write_anomaly_dir+"type[1-9]")
numTypes=len(foundTypes)
print "Found %d types in addition to type0" % numTypes
viewTypes = N.zeros(numTypes+1, dtype='int64')
files = [""]*(numTypes+1)
write_anomaly_dir_types = [""]*(numTypes+1)
storeFlag = 0
write_anomaly_dir_types[0] = write_dir
os.chdir(write_dir)
files[0] = G.glob("LCLS*angavg.h5")
curr_dir = os.getcwd()
write_anomaly_dir_types[0] = curr_dir+'/'
os.chdir(originaldir)
viewTypes[0] = int(input("view "+write_dir+" (1 for yes, 0 for no)? "))
#write_anomaly_dir_types_jumbled = [write_dir]
for cDir in foundTypes:
subString = re.sub(r""+write_dir+"type(\d+)",r"\1", cDir)
storeFlag = int(subString)
viewTypes[storeFlag] = int(input("view "+cDir+" (1 for yes, 0 for no)? "))
os.chdir(cDir)
files[storeFlag] = G.glob("LCLS*angavg.h5")
curr_dir = os.getcwd()
write_anomaly_dir_types[storeFlag] = curr_dir+'/'
os.chdir(originaldir)
# write_anomaly_dir_types_jumbled.append(i+"/")
# write_anomaly_dir_types.append(i+"/")
#for cDir in write_anomaly_dir_types_jumbled:
# if (cDir == write_dir):
# storeFlag = 0
# else:
# subString = re.sub(r""+write_dir+"type(\d+)/",r"\1", cDir)
# storeFlag = int(subString)
# viewTypes[storeFlag] = int(input("view "+cDir+" (1 for yes, 0 for no)? "))
# os.chdir(cDir)
# files[storeFlag] = G.glob("LCLS*angavg.h5")
# curr_dir = os.getcwd()
# write_anomaly_dir_types[storeFlag] = curr_dir+'/'
# os.chdir(originaldir)
colmax = options.maxIntens
colmin = 0
storeFlag = 0
########################################################
# Imaging class copied from Ingrid Ofte's pyana_misc code
########################################################
class img_class (object):
def __init__(self, inarr, inangavg , filename, currTag, meanWaveLengthInAngs=eDD.nominalWavelengthInAngs, detectorDistance=eDD.get_detector_dist_in_meters(runtag)):
self.origarr = inarr.copy()
self.inarr = inarr*(inarr>0)
self.filename = filename
self.inangavg = inangavg
self.wavelength = meanWaveLengthInAngs
self.detectorDistance = detectorDistance
self.HIceQ ={}
global colmax
global colmin
global storeFlag
self.tag = currTag
def on_keypress_for_retagging(self,event):
global colmax
global colmin
global storeFlag
if event.key in [str(i) for i in range(numTypes+1)]:
storeFlag = int(event.key)
if(options.inspectOnly):
print "Inspection only mode."
else:
if(not os.path.exists(write_anomaly_dir_types[storeFlag])):
os.mkdir(write_anomaly_dir_types[storeFlag])
pngtag = write_anomaly_dir_types[storeFlag] + "%s.png" % (self.filename)
if(self.tag != 0):
#delete previous assignment
pngtag = write_anomaly_dir_types[self.tag] + "%s.png" % (self.filename)
if os.path.isfile(pngtag):
os.remove(pngtag)
print "%s removed!" % (pngtag)
else:
print "No action taken."
#Save new assignment if it's store flag not type 0
if (storeFlag !=0):
pngtag = write_anomaly_dir_types[storeFlag] + "%s.png" % (self.filename)
P.savefig(pngtag)
print "%s saved." % (pngtag)
self.tag = storeFlag
else:
P.savefig(pngtag)
print "%s saved." % (pngtag)
self.tag = storeFlag
if event.key == 'r':
colmin = self.inarr.min()
colmax = ((self.inarr<options.maxIntens)*self.inarr).max()
P.clim(colmin, colmax)
P.draw()
def on_keypress_for_viewing(self,event):
global colmax
global colmin
global storeFlag
if event.key == 'p':
pngtag = write_anomaly_dir_types[storeFlag] + "%s.png" % (self.filename)
if(options.inspectOnly):
print "Inspection only mode."
else:
P.savefig(pngtag)
print "%s saved." % (pngtag)
if event.key == 'r':
colmin = self.inarr.min()
colmax = ((self.inarr<options.maxIntens)*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_for_viewing(self):
if(not options.inspectOnly):
print "Press 'p' to save PNG."
global colmax
global colmin
for i,j in eDD.iceHInvAngQ.iteritems():
self.HIceQ[i] = eDD.get_pix_from_invAngsQ_and_detectorDist(runtag,j,self.detectorDistance, wavelengthInAngs=self.wavelength)
fig = P.figure(num=None, figsize=(13.5, 6), dpi=100, facecolor='w', edgecolor='k')
cid1 = fig.canvas.mpl_connect('key_press_event', self.on_keypress_for_viewing)
cid2 = fig.canvas.mpl_connect('button_press_event', self.on_click)
canvas = fig.add_axes([0.05,0.05,0.5,0.9])
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()
(rTemp,cTemp) = self.inarr.shape
#Approximate, center
for i,j in self.HIceQ.iteritems():
circ = P.Circle((rTemp/2, cTemp/2), radius=j)
circ.set_fill(False)
circ.set_edgecolor('k')
canvas.add_patch(circ)
canvas = fig.add_axes([0.6,0.05,0.35,0.4])
canvas.set_title("angular average")
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.plot(self.inangavg)
canvas = fig.add_axes([0.6,0.55,0.35,0.4], xlabel="pixel value", ylabel="count")
canvas.set_title("histogram (omiting values<1) ")
nonzeroarr = N.compress(self.origarr.flat>1, self.origarr.flat)
canvas.hist(nonzeroarr, bins=N.arange(0,2000,10),log=True)
P.show()
def draw_img_for_retagging(self):
if(not options.inspectOnly):
print "Press 1-"+ str(numTypes)+ " to save png (overwrites old PNGs); Press 0 to undo (deletes png if wrongly saved)."
global colmax
global colmin
for i,j in eDD.iceHInvAngQ.iteritems():
self.HIceQ[i] = eDD.get_pix_from_invAngsQ_and_detectorDist(runtag,j,self.detectorDistance, wavelengthInAngs=self.wavelength)
fig = P.figure(num=None, figsize=(13.5, 6), dpi=100, facecolor='w', edgecolor='k')
cid1 = fig.canvas.mpl_connect('key_press_event', self.on_keypress_for_retagging)
cid2 = fig.canvas.mpl_connect('button_press_event', self.on_click)
canvas = fig.add_axes([0.05,0.05,0.5,0.9])
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()
(rTemp,cTemp) = self.inarr.shape
#Approximate, center
for i,j in self.HIceQ.iteritems():
circ = P.Circle((rTemp/2, cTemp/2), radius=j)
circ.set_fill(False)
circ.set_edgecolor('k')
canvas.add_patch(circ)
canvas = fig.add_axes([0.6,0.05,0.35,0.4])
canvas.set_title("angular average")
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.plot(self.inangavg)
canvas = fig.add_axes([0.6,0.55,0.35,0.4], xlabel="pixel value", ylabel="count")
canvas.set_title("histogram (omiting values<1) ")
nonzeroarr = N.compress(self.origarr.flat>1, self.origarr.flat)
canvas.hist(nonzeroarr, bins=N.arange(0,2000,10),log=True)
P.show()
def draw_spectrum(self):
print "Press 'p' to save PNG to correct type folder."
global colmax
global colmin
localColMax=self.inarr.max()
localColMin=self.inarr.min()
aspectratio = 1.5*(self.inarr.shape[1])/(float(self.inarr.shape[0]))
fig = P.figure(num=None, figsize=(13, 10), dpi=100, facecolor='w', edgecolor='k')
cid1 = fig.canvas.mpl_connect('key_press_event', self.on_keypress_for_viewing)
cid2 = fig.canvas.mpl_connect('button_press_event', self.on_click)
canvas = fig.add_axes([0.05,0.05,0.6,0.9], xlabel="q", ylabel="normalized angular average (will prompt to examine data larger than cutoff)")
canvas.set_title(self.filename)
self.axes = P.imshow(self.inarr, origin='lower', aspect=aspectratio, vmax = localColMax, vmin = localColMin)
self.colbar = P.colorbar(self.axes, pad=0.01)
self.orglims = self.axes.get_clim()
canvas2 = fig.add_axes([0.7,0.05,0.25,0.9], xlabel="log(sorting score)", ylabel="data")
canvas2.set_ylim([0,numData])
canvas2.plot(N.log(N.array(scoreKeeper)[ordering]),range(numData))
P.show()
avgArr = N.zeros((numTypes+1,1760,1760))
avgRadAvg = N.zeros((numTypes+1,1233))
typeOccurences = N.zeros(numTypes+1)
waveLengths={}
for i in range(numTypes+1):
waveLengths[i] = []
#global storeFlag gets modified from retagging
#but currentlyExamining flag steps sequentially through types
for currentlyExamining in range(numTypes+1):
dirName = write_anomaly_dir_types[currentlyExamining]
fcounter = 0
numFilesInDir = len(files[currentlyExamining])
if (viewTypes[currentlyExamining]==1):
print "now examining "+ dirName
os.chdir(dirName)
for fname in files[currentlyExamining]:
storeFlag = currentlyExamining
if (options.verbose and (fcounter%10)==0):
print "%d of %d files" % (fcounter, numFilesInDir)
diffractionName = source_dir+runtag+"/"+re.sub("-angavg",'',fname)
f = H.File(diffractionName, 'r')
d = N.array(f['/data/data'])
currWavelengthInAngs=f['LCLS']['photon_wavelength_A'][0]
f.close()
angAvgName = fname
f = H.File(angAvgName, 'r')
davg = N.array(f['data']['data'][0])
f.close()
if(options.stepThroughImages and (currentlyExamining !=0) and (options.inspectOnly == False)):
currImg = img_class(d, davg, fname, currentlyExamining, meanWaveLengthInAngs=currWavelengthInAngs)
currImg.draw_img_for_retagging()
if(storeFlag != currentlyExamining):
diffractionName = write_anomaly_dir_types[currentlyExamining]+"/"+re.sub("-angavg",'',fname)
if(storeFlag != 0):
print "moving angavg and pattern from " + str(currentlyExamining) + " to " + str(storeFlag)
os.system("mv " + angAvgName + " " + write_anomaly_dir_types[storeFlag])
os.system("mv " + diffractionName + " " + write_anomaly_dir_types[storeFlag])
elif(storeFlag == 0):
print "moving angavg from " + str(currentlyExamining) + " to " + str(storeFlag) + " , deleting pattern"
os.system("mv " + angAvgName + " " + write_anomaly_dir_types[storeFlag])
os.system("rm " + diffractionName)
waveLengths[storeFlag].append(currWavelengthInAngs)
avgArr[storeFlag] += d
avgRadAvg[storeFlag] += davg
typeOccurences[storeFlag] += 1
fcounter += 1
os.chdir(originaldir)
########################################################
# Loop to display all H5 files with ice anomalies.
########################################################
print "Right-click on colorbar to set maximum scale."
print "Left-click on colorbar to set minimum scale."
print "Center-click on colorbar (or press 'r') to reset color scale."
print "Interactive controls for zooming at the bottom of figure screen (zooming..etc)."
print "Hit Ctl-\ or close all windows (Alt-F4) to terminate viewing program."
storeFlag=0
for dirName in write_anomaly_dir_types:
if (typeOccurences[storeFlag] > 0.):
avgArr[storeFlag] /= typeOccurences[storeFlag]
avgRadAvg[storeFlag] /= typeOccurences[storeFlag]
if(storeFlag>0):
typeTag = runtag+'_'+(dirName.split('/')[-2])
else:
typeTag = runtag+'_type0'
currImg = img_class(avgArr[storeFlag], avgRadAvg[storeFlag], typeTag, storeFlag, meanWaveLengthInAngs=N.mean(waveLengths[storeFlag]))
currImg.draw_img_for_viewing()
if(options.inspectOnly):
print "Inspection only mode."
else:
f = H.File(dirName +'/'+ typeTag + ".h5", "w")
entry_1 = f.create_group("/data")
entry_1.create_dataset("diffraction", data=avgArr[storeFlag])
entry_1.create_dataset("angavg", data=avgRadAvg[storeFlag])
f.close()
storeFlag += 1