comparison pp_dexela.py @ 0:4d2a2268a17a draft

"planemo upload"
author mzhuang
date Thu, 23 Sep 2021 21:00:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4d2a2268a17a
1 from __future__ import print_function
2
3 import time
4 import os
5 import numpy as np
6
7 from hexrd import imageseries
8 from hexrd.imageseries.process import ProcessedImageSeries
9
10
11
12
13 class ProcessedDexelaIMS(ProcessedImageSeries):
14
15 ADDROW = 'add-row'
16 ADDCOL = 'add-column'
17
18 def __init__(self, imser, oplist, **kwargs):
19 super(ProcessedDexelaIMS, self).__init__(imser, oplist, **kwargs)
20 self.addop(self.ADDROW, self._addrow)
21 self.addop(self.ADDCOL, self._addcol)
22
23 def _addrow(self, img, k):
24 """insert row into position k"""
25 shp = img.shape
26 pimg = np.insert(img, k, 0, axis=0)
27 if k==0:
28 pimg[0] = pimg[1]
29 elif k==shp[0]:
30 pimg[k] = pimg[k-1]
31 else: # in middle
32 pimg[k] = (pimg[k-1] + pimg[k+1])/2
33
34 return pimg
35
36 def _addcol(self, img, k):
37 """insert row into position k"""
38 shp = img.shape
39 pimg = np.insert(img, k, 0, axis=1)
40 if k==0:
41 pimg[:,0] = pimg[:,1]
42 elif k==shp[0]:
43 pimg[:,k] = pimg[:,k-1]
44 else: # in middle
45 pimg[:,k] = (pimg[:,k-1] + pimg[:,k+1])/2
46
47 return pimg
48
49
50 DexelaIMS=ProcessedDexelaIMS
51
52 class PP_Dexela(object):
53 """PP_Dexela"""
54 PROCFMT = 'frame-cache'
55 RAWFMT = 'hdf5'
56 RAWPATH = '/imageseries'
57 DARKPCTILE = 50
58
59 def __init__(self,
60 fname, omw, panel_opts, panel_id,
61 frame_start=0, raw_format='hdf5',dark=None):
62 """Constructor for PP_Dexela"""
63 self._panel_id = panel_id
64 self.fname = fname
65 self.omwedges = omw
66 self.panel_opts = panel_opts
67 self.frame_start = frame_start
68 self.use_frame_list = (self.frame_start > 0)
69 if raw_format.lower() == 'hdf5':
70 self.raw = imageseries.open(
71 self.fname, self.RAWFMT, path=self.RAWPATH
72 )
73 else:
74 self.raw = imageseries.open(self.fname, raw_format.lower())
75 self._dark = dark
76
77 #print(
78 # 'On Init:\n\t%s, %d frames, %d omw, %d total'
79 # % (self.fname, self.nframes, self.omwedges.nframes, len(self.raw))
80 #)
81
82 @property
83 def panel_id(self):
84 return self._panel_id
85
86 @property
87 def oplist(self):
88 return [('dark', self.dark)]+self.panel_opts
89
90 @property
91 def framelist(self):
92 return range(self.frame_start, self.nframes + self.frame_start)
93
94 #
95 # ============================== API
96 #
97 @property
98 def nframes(self):
99 return self.omwedges.nframes
100
101 @property
102 def omegas(self):
103 return self.omwedges.omegas
104
105 def processed(self):
106 kw = {}
107 if self.use_frame_list:
108 kw = dict(frame_list=self.framelist)
109 return DexelaIMS(self.raw, self.oplist, **kw)
110
111 @property
112 def dark(self, nframes=100):
113 """build and return dark image"""
114 if self._dark is None:
115 usenframes = min(nframes, self.nframes)
116 print(
117 "building dark images using %s frames (may take a while)..."
118 % usenframes
119 )
120 start = time.time()
121 # self._dark = imageseries.stats.percentile(
122 # self.raw, self.DARKPCTILE, nframes=usenframes
123 # )
124 self._dark = imageseries.stats.median(
125 self.raw, nframes=usenframes
126 )#changed to median by DCP 11/18/17
127 elapsed = (time.time() - start)
128 print(
129 "done building background (dark) image: " +
130 "elapsed time is %f seconds" % elapsed
131 )
132
133 return self._dark
134
135 def save_processed(self, name, threshold, output_dir=None):
136 if output_dir is None:
137 output_dir = os.getcwd()
138 else:
139 os.mkdir(output_dir)
140
141 # add omegas
142 pims = self.processed()
143 metad = pims.metadata
144 metad['omega'] = self.omegas
145 metad['panel_id'] = self.panel_id
146 cache = '%s-cachefile.npz' % name
147 imageseries.write(pims, "dummy", self.PROCFMT,
148 style="npz",
149 threshold=threshold,
150 cache_file=cache)
151 pass # end class
152
153