Coverage for larch/wxxrd/ImageControlsFrame.py: 10%
176 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
1#!/usr/bin/env python
2'''
3popup frame for 2D XRD image control
5'''
6import numpy as np
7import matplotlib.cm as colormap
8import wx
10###################################
12class ImageToolboxFrame(wx.Frame):
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))
21 #self.SetMinSize((700,500))
23 ## Set inputs
24 self.plot2Dframe = imageframe
25 #self.raw_img = image
26 self.plt_img = image
28 ## Set defaults
29 self.bkgd_scale = 0
31 self.color = 'bone'
32 self.flip = 'vertical'
34 self.Init()
35 self.Centre()
36 self.Show(True)
38 def Init(self):
40 self.panel = wx.Panel(self)
41 vbox = wx.BoxSizer(wx.VERTICAL)
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)
53 self.ch_clr.Bind(wx.EVT_CHOICE,self.onColor)
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)
59 ###########################
60 ## Contrast
61 vbox_ct = wx.BoxSizer(wx.VERTICAL)
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='')
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)
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)
76 self.sldr_min.Bind(wx.EVT_SLIDER,self.onSlider)
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)
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)
88 self.sldr_max.Bind(wx.EVT_SLIDER,self.onSlider)
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)
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')
99 self.btn_ct1.Bind(wx.EVT_BUTTON,self.autoContrast)
100 self.btn_ct2.Bind(wx.EVT_BUTTON,self.onContrastRange)
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)
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)
116 self.ch_flp.Bind(wx.EVT_CHOICE,self.onFlip)
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)
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)
129 self.ch_scl.Bind(wx.EVT_CHOICE,self.onScale)
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)
136 ###########################
137 ## Set defaults
138 self.ch_clr.SetStringSelection(self.color)
139 self.ch_flp.SetStringSelection(self.flip)
140 self.setSlider()
142 self.panel.SetSizer(vbox)
144 def autoContrast(self,event=None):
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()
162 def onContrastRange(self,event=None):
164 newMIN = int(self.entr_min.GetValue())
165 newMAX = int(self.entr_max.GetValue())
167 self.minCURRENT = newMIN
168 self.maxCURRENT = newMAX
170 self.sldr_min.SetRange(newMIN,newMAX)
171 self.sldr_max.SetRange(newMIN,newMAX)
173 self.setContrast()
175 def setSlider(self):
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']
182 self.entr_min.SetLabel(str(self.minCURRENT))
183 self.entr_max.SetLabel(str(self.maxCURRENT))
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
196 try:
197 self.sldr_min.SetValue(self.minCURRENT)
198 self.sldr_max.SetValue(self.maxCURRENT)
199 except:
200 pass
202 def onSlider(self,event=None):
204 self.minCURRENT = self.sldr_min.GetValue()
205 self.maxCURRENT = self.sldr_max.GetValue()
207 ## Create safety to keep min. below max.
208 ## mkak 2016.10.20
210 self.setContrast()
212 def setContrast(self):
214 self.sldr_min.SetValue(self.minCURRENT)
215 self.sldr_max.SetValue(self.maxCURRENT)
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
223 self.plot2Dframe.redraw()
225 self.entr_min.SetLabel(str(self.minCURRENT))
226 self.entr_max.SetLabel(str(self.maxCURRENT))
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 '''
234 if self.ch_flp.GetString(self.ch_flp.GetSelection()) != self.flip:
235 self.flip = self.ch_flp.GetString(self.ch_flp.GetSelection())
237 self.checkFLIPS()
239 self.plot2Dframe.redraw()
241 def checkFLIPS(self):
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
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()
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()
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()
276 def SetContrast(self):
278 self.txt_ct2.SetLabel('[ full range: %i, %i ]' %
279 (np.min(self.plt_img),np.max(self.plt_img)))
281 self.autoContrast()
282 self.checkFLIPS()
284 self.plot2Dframe.redraw()
286#############################################################
287################ IN PROGRESS - START ###################
288#############################################################
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# #############################################################