Coverage for /Users/Newville/Codes/xraylarch/larch/wxxrd/ImageControlsFrame.py: 10%

177 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-09 10:08 -0600

1#!/usr/bin/env python 

2''' 

3popup frame for 2D XRD image control 

4 

5''' 

6import numpy as np 

7import matplotlib.cm as colormap 

8import wx 

9 

10################################### 

11 

12class ImageToolboxFrame(wx.Frame): 

13 

14 def __init__(self,imageframe,image): #,mask=False,bkgd=False): 

15 ''' 

16 Frame for visual toolbox 

17 ''' 

18 label = 'Image Toolbox' 

19 wx.Frame.__init__(self, None, -1,title=label, size=(330, 300)) 

20 

21 #self.SetMinSize((700,500)) 

22 

23 ## Set inputs 

24 self.plot2Dframe = imageframe 

25 #self.raw_img = image 

26 self.plt_img = image 

27 

28 ## Set defaults 

29 self.bkgd_scale = 0 

30 

31 self.color = 'bone' 

32 self.flip = 'vertical' 

33 

34 self.Init() 

35 self.Centre() 

36 self.Show(True) 

37 

38 def Init(self): 

39 

40 self.panel = wx.Panel(self) 

41 vbox = wx.BoxSizer(wx.VERTICAL) 

42 

43 ########################### 

44 ## Color 

45 hbox_clr = wx.BoxSizer(wx.HORIZONTAL) 

46 self.txt_clr = wx.StaticText(self.panel, label='COLOR') 

47 colors = [] 

48 for key in colormap.datad: 

49 if not key.endswith('_r'): 

50 colors.append(key) 

51 self.ch_clr = wx.Choice(self.panel,choices=colors) 

52 

53 self.ch_clr.Bind(wx.EVT_CHOICE,self.onColor) 

54 

55 hbox_clr.Add(self.txt_clr, flag=wx.RIGHT, border=8) 

56 hbox_clr.Add(self.ch_clr, flag=wx.EXPAND, border=8) 

57 vbox.Add(hbox_clr, flag=wx.ALL, border=10) 

58 

59 ########################### 

60 ## Contrast 

61 vbox_ct = wx.BoxSizer(wx.VERTICAL) 

62 

63 hbox_ct1 = wx.BoxSizer(wx.HORIZONTAL) 

64 self.txt_ct1 = wx.StaticText(self.panel, label='CONTRAST') 

65 self.txt_ct2 = wx.StaticText(self.panel, label='') 

66 

67 hbox_ct1.Add(self.txt_ct1, flag=wx.EXPAND|wx.RIGHT, border=8) 

68 hbox_ct1.Add(self.txt_ct2, flag=wx.ALIGN_RIGHT, border=8) 

69 vbox_ct.Add(hbox_ct1, flag=wx.BOTTOM, border=8) 

70 

71 hbox_ct2 = wx.BoxSizer(wx.HORIZONTAL) 

72 self.ttl_min = wx.StaticText(self.panel, label='min') 

73 self.sldr_min = wx.Slider(self.panel, style=wx.SL_LABELS, maxValue=5e6) 

74 self.entr_min = wx.TextCtrl(self.panel,wx.TE_PROCESS_ENTER) 

75 

76 self.sldr_min.Bind(wx.EVT_SLIDER,self.onSlider) 

77 

78 hbox_ct2.Add(self.ttl_min, flag=wx.RIGHT, border=8) 

79 hbox_ct2.Add(self.sldr_min, flag=wx.EXPAND, border=8) 

80 hbox_ct2.Add(self.entr_min, flag=wx.RIGHT, border=8) 

81 vbox_ct.Add(hbox_ct2, flag=wx.BOTTOM, border=8) 

82 

83 hbox_ct3 = wx.BoxSizer(wx.HORIZONTAL) 

84 self.ttl_max = wx.StaticText(self.panel, label='max') 

85 self.sldr_max = wx.Slider(self.panel, style=wx.SL_LABELS, maxValue=5e6) 

86 self.entr_max = wx.TextCtrl(self.panel,wx.TE_PROCESS_ENTER) 

87 

88 self.sldr_max.Bind(wx.EVT_SLIDER,self.onSlider) 

89 

90 hbox_ct3.Add(self.ttl_max, flag=wx.RIGHT, border=8) 

91 hbox_ct3.Add(self.sldr_max, flag=wx.EXPAND, border=8) 

92 hbox_ct3.Add(self.entr_max, flag=wx.RIGHT, border=8) 

93 vbox_ct.Add(hbox_ct3, flag=wx.BOTTOM, border=8) 

94 

95 hbox_ct4 = wx.BoxSizer(wx.HORIZONTAL) 

96 self.btn_ct1 = wx.Button(self.panel,label='reset range') 

97 self.btn_ct2 = wx.Button(self.panel,label='set range') 

98 

99 self.btn_ct1.Bind(wx.EVT_BUTTON,self.autoContrast) 

100 self.btn_ct2.Bind(wx.EVT_BUTTON,self.onContrastRange) 

101 

102 hbox_ct4.Add(self.btn_ct1, flag=wx.RIGHT, border=8) 

103 hbox_ct4.Add(self.btn_ct2, flag=wx.RIGHT, border=8) 

104 vbox_ct.Add(hbox_ct4, flag=wx.ALIGN_RIGHT|wx.BOTTOM,border=8) 

105 vbox.Add(vbox_ct, flag=wx.ALL, border=10) 

106 

107 

108 

109 ########################### 

110 ## Flip 

111 hbox_flp = wx.BoxSizer(wx.HORIZONTAL) 

112 self.txt_flp = wx.StaticText(self.panel, label='IMAGE FLIP') 

113 flips = ['none','vertical','horizontal','both'] 

114 self.ch_flp = wx.Choice(self.panel,choices=flips) 

115 

116 self.ch_flp.Bind(wx.EVT_CHOICE,self.onFlip) 

117 

118 hbox_flp.Add(self.txt_flp, flag=wx.RIGHT, border=8) 

119 hbox_flp.Add(self.ch_flp, flag=wx.EXPAND, border=8) 

120 vbox.Add(hbox_flp, flag=wx.ALL, border=10) 

121 

122 ########################### 

123 ## Scale 

124 hbox_scl = wx.BoxSizer(wx.HORIZONTAL) 

125 self.txt_scl = wx.StaticText(self.panel, label='SCALE') 

126 scales = ['linear','log'] 

127 self.ch_scl = wx.Choice(self.panel,choices=scales) 

128 

129 self.ch_scl.Bind(wx.EVT_CHOICE,self.onScale) 

130 

131 hbox_scl.Add(self.txt_scl, flag=wx.RIGHT, border=8) 

132 hbox_scl.Add(self.ch_scl, flag=wx.EXPAND, border=8) 

133 vbox.Add(hbox_scl, flag=wx.ALL, border=10) 

134 

135 

136 ########################### 

137 ## Set defaults 

138 self.ch_clr.SetStringSelection(self.color) 

139 self.ch_flp.SetStringSelection(self.flip) 

140 self.setSlider() 

141 

142 self.panel.SetSizer(vbox) 

143 

144 def autoContrast(self,event=None): 

145 

146 self.minINT = int(np.min(self.plt_img)) 

147 self.maxINT = int(np.max(self.plt_img)/15) # /15 scales image to viewable 

148 if self.maxINT == self.minINT: 

149 self.minINT = self.minINT 

150 self.maxINT = self.minINT+100 

151 try: 

152 self.sldr_min.SetRange(self.minINT,self.maxINT) 

153 self.sldr_max.SetRange(self.minINT,self.maxINT) 

154 except: 

155 pass 

156 self.minCURRENT = self.minINT 

157 self.maxCURRENT = self.maxINT 

158 if self.maxCURRENT > self.maxINT: 

159 self.maxCURRENT = self.maxINT 

160 self.setContrast() 

161 

162 def onContrastRange(self,event=None): 

163 

164 newMIN = int(self.entr_min.GetValue()) 

165 newMAX = int(self.entr_max.GetValue()) 

166 

167 self.minCURRENT = newMIN 

168 self.maxCURRENT = newMAX 

169 

170 self.sldr_min.SetRange(newMIN,newMAX) 

171 self.sldr_max.SetRange(newMIN,newMAX) 

172 

173 self.setContrast() 

174 

175 def setSlider(self): 

176 

177 self.minCURRENT = self.plot2Dframe.conf.int_lo[0] 

178 self.maxCURRENT = self.plot2Dframe.conf.int_hi[0] 

179# self.minCURRENT = self.plot2Dframe.conf.int_lo['int'] 

180# self.maxCURRENT = self.plot2Dframe.conf.int_hi['int'] 

181 

182 self.entr_min.SetLabel(str(self.minCURRENT)) 

183 self.entr_max.SetLabel(str(self.maxCURRENT)) 

184 

185 self.minINT = int(np.min(self.plt_img)) 

186 self.maxINT = int(np.max(self.plt_img)/15) # /15 scales image to viewable 

187 if self.maxINT == self.minINT: 

188 self.minINT = self.minINT 

189 self.maxINT = self.minINT+100 

190 try: 

191 self.sldr_min.SetRange(self.minINT,self.maxINT) 

192 self.sldr_max.SetRange(self.minINT,self.maxINT) 

193 except: 

194 pass 

195 

196 try: 

197 self.sldr_min.SetValue(self.minCURRENT) 

198 self.sldr_max.SetValue(self.maxCURRENT) 

199 except: 

200 pass 

201 

202 def onSlider(self,event=None): 

203 

204 self.minCURRENT = self.sldr_min.GetValue() 

205 self.maxCURRENT = self.sldr_max.GetValue() 

206 

207 ## Create safety to keep min. below max. 

208 ## mkak 2016.10.20 

209 

210 self.setContrast() 

211 

212 def setContrast(self): 

213 

214 self.sldr_min.SetValue(self.minCURRENT) 

215 self.sldr_max.SetValue(self.maxCURRENT) 

216 

217 self.plot2Dframe.conf.auto_intensity = False 

218 self.plot2Dframe.conf.int_lo[0] = self.minCURRENT 

219 self.plot2Dframe.conf.int_hi[0] = self.maxCURRENT 

220# self.plot2Dframe.conf.int_lo['int'] = self.minCURRENT 

221# self.plot2Dframe.conf.int_hi['int'] = self.maxCURRENT 

222 

223 self.plot2Dframe.redraw() 

224 

225 self.entr_min.SetLabel(str(self.minCURRENT)) 

226 self.entr_max.SetLabel(str(self.maxCURRENT)) 

227 

228 def onFlip(self,event=None): 

229 ''' 

230 Eventually, should just set self.raw_img or self.fli_img - better than this 

231 mkak 2016.10.20 

232 ''' 

233 

234 if self.ch_flp.GetString(self.ch_flp.GetSelection()) != self.flip: 

235 self.flip = self.ch_flp.GetString(self.ch_flp.GetSelection()) 

236 

237 self.checkFLIPS() 

238 

239 self.plot2Dframe.redraw() 

240 

241 def checkFLIPS(self): 

242 

243 if self.flip == 'vertical': # Vertical 

244 self.plot2Dframe.conf.flip_ud = True 

245 self.plot2Dframe.conf.flip_lr = False 

246 elif self.flip == 'horizontal': # Horizontal 

247 self.plot2Dframe.conf.flip_ud = False 

248 self.plot2Dframe.conf.flip_lr = True 

249 elif self.flip == 'both': # both 

250 self.plot2Dframe.conf.flip_ud = True 

251 self.plot2Dframe.conf.flip_lr = True 

252 else: # None 

253 self.plot2Dframe.conf.flip_ud = False 

254 self.plot2Dframe.conf.flip_lr = False 

255 

256 def onScale(self,event=None): 

257 if self.ch_scl.GetSelection() == 1: ## log 

258 self.plot2Dframe.conf.log_scale = True 

259 else: ## linear 

260 self.plot2Dframe.conf.log_scale = False 

261 self.plot2Dframe.redraw() 

262 

263 def onColor(self,event=None): 

264 if self.color != self.ch_clr.GetString(self.ch_clr.GetSelection()): 

265 self.color = self.ch_clr.GetString(self.ch_clr.GetSelection()) 

266 self.setColor() 

267 

268 def setColor(self): 

269 self.plot2Dframe.conf.cmap[0] = getattr(colormap, self.color) 

270# self.plot2Dframe.conf.cmap['int'] = getattr(colormap, self.color) 

271 self.plot2Dframe.display(self.plt_img) 

272 self.checkFLIPS() 

273 self.plot2Dframe.redraw() 

274 

275 

276 def SetContrast(self): 

277 

278 self.txt_ct2.SetLabel('[ full range: %i, %i ]' % 

279 (np.min(self.plt_img),np.max(self.plt_img))) 

280 

281 self.autoContrast() 

282 self.checkFLIPS() 

283 

284 self.plot2Dframe.redraw() 

285 

286############################################################# 

287################ IN PROGRESS - START ################### 

288############################################################# 

289 

290# class ImageToolboxPanel(wx.Panel): 

291# 

292# def __init__(self,parent):#,imageframe,image,mask=False,bkgd=False): 

293# ''' 

294# Panel for visual toolbox 

295# ''' 

296# label = 'Image Toolbox' 

297# wx.Panel.__init__(self, parent, -1) 

298# #wx.Panel.__init__(self, None, -1) 

299# #wx.Panel.__init__(self, parent, -1, **kws) 

300# 

301# #self.SetMinSize((700,500)) 

302# 

303# self.plot2Dframe = None 

304# self.raw_img = None 

305# self.mask = False 

306# self.bkgd = False 

307# 

308# self.Init() 

309# 

310# def Init(self): 

311# 

312# self.panel = wx.Panel(self) 

313# vbox = wx.BoxSizer(wx.VERTICAL) 

314# 

315# 

316# ############################################################# 

317# ################ IN PROGRESS - END ################### 

318# #############################################################