[da8f4c8] | 1 | """ |
---|
| 2 | Widget to display a 2D map of the detector |
---|
| 3 | """ |
---|
[ea290ee] | 4 | import wx |
---|
[8dfdd20] | 5 | import sys |
---|
[d85c194] | 6 | from sas.sasgui.guiframe.utils import format_number |
---|
| 7 | from sas.sasgui.guiframe.events import StatusEvent |
---|
[8dfdd20] | 8 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas |
---|
[0783a2e] | 9 | import matplotlib as mpl |
---|
[32c0841] | 10 | from matplotlib import pylab |
---|
[b5de88e] | 11 | # FONT size |
---|
[32c0841] | 12 | if sys.platform.count("win32") > 0: |
---|
[1f1cacf] | 13 | FONT_VARIANT = 0 |
---|
| 14 | else: |
---|
| 15 | FONT_VARIANT = 1 |
---|
[b5de88e] | 16 | |
---|
[c05234f] | 17 | DEFAULT_CMAP = pylab.cm.get_cmap('jet') |
---|
[d955bf19] | 18 | |
---|
[ea290ee] | 19 | class DetectorDialog(wx.Dialog): |
---|
| 20 | """ |
---|
[d955bf19] | 21 | Dialog box to let the user edit detector settings |
---|
[ea290ee] | 22 | """ |
---|
[b5de88e] | 23 | |
---|
[32c0841] | 24 | def __init__(self, parent, id=1, base=None, dpi=None, |
---|
[b5de88e] | 25 | cmap=DEFAULT_CMAP, reset_zmin_ctl=None, |
---|
| 26 | reset_zmax_ctl=None, *args, **kwds): |
---|
[d955bf19] | 27 | """ |
---|
| 28 | """ |
---|
[ea290ee] | 29 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
[32c0841] | 30 | wx.Dialog.__init__(self, parent, id=1, *args, **kwds) |
---|
[1f1cacf] | 31 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[32c0841] | 32 | self.parent = base |
---|
[8dfdd20] | 33 | self.dpi = dpi |
---|
[9c0fe9a5] | 34 | self.cmap = cmap |
---|
[32c0841] | 35 | self.reset_zmin_ctl = reset_zmin_ctl |
---|
| 36 | self.reset_zmax_ctl = reset_zmax_ctl |
---|
[ea290ee] | 37 | self.label_xnpts = wx.StaticText(self, -1, "Detector width in pixels") |
---|
| 38 | self.label_ynpts = wx.StaticText(self, -1, "Detector Height in pixels") |
---|
| 39 | self.label_qmax = wx.StaticText(self, -1, "Q max") |
---|
[32c0841] | 40 | self.label_zmin = wx.StaticText(self, -1, |
---|
[b5de88e] | 41 | "Min amplitude for color map (optional)") |
---|
[32c0841] | 42 | self.label_zmax = wx.StaticText(self, -1, |
---|
[b5de88e] | 43 | "Max amplitude for color map (optional)") |
---|
[32c0841] | 44 | self.label_beam = wx.StaticText(self, -1, |
---|
| 45 | "Beam stop radius in units of q") |
---|
[ea290ee] | 46 | self.xnpts_ctl = wx.StaticText(self, -1, "") |
---|
| 47 | self.ynpts_ctl = wx.StaticText(self, -1, "") |
---|
| 48 | self.qmax_ctl = wx.StaticText(self, -1, "") |
---|
| 49 | self.beam_ctl = wx.StaticText(self, -1, "") |
---|
[32c0841] | 50 | self.zmin_ctl = wx.TextCtrl(self, -1, size=(60, 20)) |
---|
[8dfdd20] | 51 | self.zmin_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[32c0841] | 52 | self.zmax_ctl = wx.TextCtrl(self, -1, size=(60, 20)) |
---|
[8dfdd20] | 53 | self.zmax_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[ea290ee] | 54 | self.static_line_3 = wx.StaticLine(self, -1) |
---|
[b5de88e] | 55 | self.button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
[32c0841] | 56 | self.button_reset = wx.Button(self, wx.NewId(), "Reset") |
---|
[8dfdd20] | 57 | self.Bind(wx.EVT_BUTTON, self.resetValues, self.button_reset) |
---|
[b5de88e] | 58 | self.button_ok = wx.Button(self, wx.ID_OK, "OK") |
---|
| 59 | self.Bind(wx.EVT_BUTTON, self.checkValues, self.button_ok) |
---|
[ea290ee] | 60 | self.__set_properties() |
---|
| 61 | self.__do_layout() |
---|
| 62 | self.Fit() |
---|
[b5de88e] | 63 | |
---|
| 64 | class Event(object): |
---|
[d955bf19] | 65 | """ |
---|
| 66 | """ |
---|
[ea290ee] | 67 | xnpts = 0 |
---|
| 68 | ynpts = 0 |
---|
| 69 | qpax = 0 |
---|
| 70 | beam = 0 |
---|
| 71 | zmin = 0 |
---|
| 72 | zmax = 0 |
---|
[32c0841] | 73 | cmap = None |
---|
[ea290ee] | 74 | sym4 = False |
---|
[b5de88e] | 75 | |
---|
[8dfdd20] | 76 | def onSetFocus(self, event): |
---|
| 77 | """ |
---|
[d955bf19] | 78 | Highlight the txtcrtl |
---|
[8dfdd20] | 79 | """ |
---|
| 80 | # Get a handle to the TextCtrl |
---|
| 81 | widget = event.GetEventObject() |
---|
| 82 | # Select the whole control, after this event resolves |
---|
[32c0841] | 83 | wx.CallAfter(widget.SetSelection, -1, -1) |
---|
[b5de88e] | 84 | |
---|
[8dfdd20] | 85 | def resetValues(self, event): |
---|
| 86 | """ |
---|
[d955bf19] | 87 | reset detector info |
---|
[8dfdd20] | 88 | """ |
---|
| 89 | try: |
---|
[32c0841] | 90 | zmin = self.reset_zmin_ctl |
---|
| 91 | zmax = self.reset_zmax_ctl |
---|
| 92 | if zmin == None: |
---|
| 93 | zmin = "" |
---|
| 94 | if zmax == None: |
---|
[b5de88e] | 95 | zmax = "" |
---|
[7526ad4] | 96 | self.zmin_ctl.SetValue(str(zmin)) |
---|
| 97 | self.zmax_ctl.SetValue(str(zmax)) |
---|
[8dfdd20] | 98 | self.cmap = DEFAULT_CMAP |
---|
[7526ad4] | 99 | self.cmap_selector.SetStringSelection("jet") |
---|
[8dfdd20] | 100 | self._on_select_cmap(event=None) |
---|
| 101 | except: |
---|
[32c0841] | 102 | msg = "error occurs while resetting Detector: %s" % sys.exc_value |
---|
| 103 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[b5de88e] | 104 | |
---|
[ea290ee] | 105 | def checkValues(self, event): |
---|
[eba08f1a] | 106 | """ |
---|
[d955bf19] | 107 | Check the valitidity of zmin and zmax value |
---|
| 108 | zmax should be a float and zmin less than zmax |
---|
[eba08f1a] | 109 | """ |
---|
[ea290ee] | 110 | flag = True |
---|
| 111 | try: |
---|
[32c0841] | 112 | value = self.zmin_ctl.GetValue() |
---|
[9d64310] | 113 | self.zmin_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 114 | self.zmin_ctl.Refresh() |
---|
[ea290ee] | 115 | except: |
---|
| 116 | flag = False |
---|
[9a585d0] | 117 | wx.PostEvent(self.parent, StatusEvent(status="Enter float value")) |
---|
[ea290ee] | 118 | self.zmin_ctl.SetBackgroundColour("pink") |
---|
| 119 | self.zmin_ctl.Refresh() |
---|
| 120 | try: |
---|
[32c0841] | 121 | value = self.zmax_ctl.GetValue() |
---|
| 122 | if value and float(value) == 0.0: |
---|
[ea290ee] | 123 | flag = False |
---|
[32c0841] | 124 | wx.PostEvent(self.parent, |
---|
[b5de88e] | 125 | StatusEvent(status="Enter number greater than zero")) |
---|
[ea290ee] | 126 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
| 127 | self.zmax_ctl.Refresh() |
---|
| 128 | else: |
---|
| 129 | self.zmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 130 | self.zmax_ctl.Refresh() |
---|
| 131 | except: |
---|
| 132 | flag = False |
---|
[9a585d0] | 133 | wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value")) |
---|
[ea290ee] | 134 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
| 135 | self.zmax_ctl.Refresh() |
---|
| 136 | if flag: |
---|
| 137 | event.Skip(True) |
---|
[b5de88e] | 138 | |
---|
[32c0841] | 139 | def setContent(self, xnpts, ynpts, qmax, beam, |
---|
| 140 | zmin=None, zmax=None, sym=False): |
---|
[eba08f1a] | 141 | """ |
---|
[d955bf19] | 142 | received value and displayed them |
---|
[b5de88e] | 143 | |
---|
[d955bf19] | 144 | :param xnpts: the number of point of the x_bins of data |
---|
| 145 | :param ynpts: the number of point of the y_bins of data |
---|
| 146 | :param qmax: the maxmimum value of data pixel |
---|
| 147 | :param beam: the radius of the beam |
---|
| 148 | :param zmin: the value to get the minimum color |
---|
| 149 | :param zmax: the value to get the maximum color |
---|
| 150 | :param sym: |
---|
[b5de88e] | 151 | |
---|
[eba08f1a] | 152 | """ |
---|
[80a7f77] | 153 | self.xnpts_ctl.SetLabel(str(format_number(xnpts))) |
---|
| 154 | self.ynpts_ctl.SetLabel(str(format_number(ynpts))) |
---|
| 155 | self.qmax_ctl.SetLabel(str(format_number(qmax))) |
---|
| 156 | self.beam_ctl.SetLabel(str(format_number(beam))) |
---|
[32c0841] | 157 | if zmin != None: |
---|
[80a7f77] | 158 | self.zmin_ctl.SetValue(str(format_number(zmin))) |
---|
[32c0841] | 159 | if zmax != None: |
---|
[80a7f77] | 160 | self.zmax_ctl.SetValue(str(format_number(zmax))) |
---|
[ea290ee] | 161 | |
---|
| 162 | def getContent(self): |
---|
[eba08f1a] | 163 | """ |
---|
[d955bf19] | 164 | return event containing value to reset the detector of a given data |
---|
[eba08f1a] | 165 | """ |
---|
[ea290ee] | 166 | event = self.Event() |
---|
| 167 | t_min = self.zmin_ctl.GetValue() |
---|
| 168 | t_max = self.zmax_ctl.GetValue() |
---|
| 169 | v_min = None |
---|
| 170 | v_max = None |
---|
[32c0841] | 171 | if len(t_min.lstrip()) > 0: |
---|
[ea290ee] | 172 | try: |
---|
| 173 | v_min = float(t_min) |
---|
| 174 | except: |
---|
| 175 | v_min = None |
---|
[32c0841] | 176 | if len(t_max.lstrip()) > 0: |
---|
[ea290ee] | 177 | try: |
---|
| 178 | v_max = float(t_max) |
---|
| 179 | except: |
---|
| 180 | v_max = None |
---|
| 181 | event.zmin = v_min |
---|
| 182 | event.zmax = v_max |
---|
[32c0841] | 183 | event.cmap = self.cmap |
---|
[ea290ee] | 184 | return event |
---|
| 185 | |
---|
| 186 | def __set_properties(self): |
---|
[eba08f1a] | 187 | """ |
---|
[d955bf19] | 188 | set proprieties of the dialog window |
---|
[eba08f1a] | 189 | """ |
---|
[fdc690f] | 190 | self.SetTitle("2D Color Map") |
---|
[ea290ee] | 191 | self.SetSize((600, 595)) |
---|
| 192 | |
---|
| 193 | def __do_layout(self): |
---|
[eba08f1a] | 194 | """ |
---|
[d955bf19] | 195 | fill the dialog window . |
---|
[eba08f1a] | 196 | """ |
---|
[ea290ee] | 197 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
| 198 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
[32c0841] | 199 | sizer_params = wx.GridBagSizer(5, 5) |
---|
[8dfdd20] | 200 | sizer_colormap = wx.BoxSizer(wx.VERTICAL) |
---|
[32c0841] | 201 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
[b5de88e] | 202 | |
---|
[ea290ee] | 203 | iy = 0 |
---|
[32c0841] | 204 | sizer_params.Add(self.label_xnpts, (iy, 0), (1, 1), |
---|
[b5de88e] | 205 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 206 | sizer_params.Add(self.xnpts_ctl, (iy, 1), (1, 1), |
---|
| 207 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[ea290ee] | 208 | iy += 1 |
---|
[32c0841] | 209 | sizer_params.Add(self.label_ynpts, (iy, 0), (1, 1), |
---|
[b5de88e] | 210 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 211 | sizer_params.Add(self.ynpts_ctl, (iy, 1), (1, 1), |
---|
| 212 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[ea290ee] | 213 | iy += 1 |
---|
[32c0841] | 214 | sizer_params.Add(self.label_qmax, (iy, 0), (1, 1), |
---|
[b5de88e] | 215 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 216 | sizer_params.Add(self.qmax_ctl, (iy, 1), (1, 1), |
---|
| 217 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[ea290ee] | 218 | iy += 1 |
---|
[32c0841] | 219 | sizer_params.Add(self.label_beam, (iy, 0), (1, 1), |
---|
[b5de88e] | 220 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 221 | sizer_params.Add(self.beam_ctl, (iy, 1), (1, 1), |
---|
| 222 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[ea290ee] | 223 | iy += 1 |
---|
[32c0841] | 224 | sizer_params.Add(self.label_zmin, (iy, 0), (1, 1), |
---|
[b5de88e] | 225 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 226 | sizer_params.Add(self.zmin_ctl, (iy, 1), (1, 1), |
---|
| 227 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[ea290ee] | 228 | iy += 1 |
---|
[32c0841] | 229 | sizer_params.Add(self.label_zmax, (iy, 0), (1, 1), |
---|
[b5de88e] | 230 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
| 231 | sizer_params.Add(self.zmax_ctl, (iy, 1), (1, 1), |
---|
| 232 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[ea290ee] | 233 | iy += 1 |
---|
[32c0841] | 234 | self.fig = mpl.figure.Figure(dpi=self.dpi, figsize=(4, 1)) |
---|
[8dfdd20] | 235 | self.ax1 = self.fig.add_axes([0.05, 0.65, 0.9, 0.15]) |
---|
| 236 | self.norm = mpl.colors.Normalize(vmin=0, vmax=100) |
---|
| 237 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
[b5de88e] | 238 | norm=self.norm, |
---|
| 239 | orientation='horizontal') |
---|
[8dfdd20] | 240 | self.cb1.set_label('Detector Colors') |
---|
| 241 | self.canvas = Canvas(self, -1, self.fig) |
---|
[b5de88e] | 242 | sizer_colormap.Add(self.canvas, 0, wx.LEFT | wx.EXPAND, 5) |
---|
[8dfdd20] | 243 | self.cmap_selector = wx.ComboBox(self, -1) |
---|
| 244 | self.cmap_selector.SetValue(str(self.cmap.name)) |
---|
| 245 | maps = sorted(m for m in pylab.cm.datad if not m.endswith("_r")) |
---|
[b5de88e] | 246 | |
---|
[32c0841] | 247 | for i, m in enumerate(maps): |
---|
[8dfdd20] | 248 | self.cmap_selector.Append(str(m), pylab.get_cmap(m)) |
---|
[b5de88e] | 249 | |
---|
[32c0841] | 250 | wx.EVT_COMBOBOX(self.cmap_selector, -1, self._on_select_cmap) |
---|
[da8f4c8] | 251 | sizer_selection.Add(wx.StaticText(self, -1, "Select Cmap: "), 0, |
---|
[b5de88e] | 252 | wx.LEFT | wx.ADJUST_MINSIZE, 5) |
---|
| 253 | sizer_selection.Add(self.cmap_selector, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 254 | sizer_main.Add(sizer_params, 0, wx.EXPAND | wx.ALL, 5) |
---|
| 255 | sizer_main.Add(sizer_selection, 0, wx.EXPAND | wx.ALL, 5) |
---|
| 256 | note = " Note: This is one time option. " + \ |
---|
[079072e] | 257 | "It will be reset on updating the image." |
---|
| 258 | note_txt = wx.StaticText(self, -1, note) |
---|
[b5de88e] | 259 | sizer_main.Add(note_txt, 0, wx.EXPAND | wx.ALL, 5) |
---|
| 260 | sizer_main.Add(sizer_colormap, 1, wx.EXPAND | wx.ALL, 5) |
---|
[ea290ee] | 261 | sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0) |
---|
[b5de88e] | 262 | sizer_button.Add(self.button_reset, 0, wx.LEFT | wx.ADJUST_MINSIZE, 100) |
---|
| 263 | sizer_button.Add(self.button_ok, 0, wx.LEFT | wx.ADJUST_MINSIZE, 10) |
---|
| 264 | sizer_button.Add(self.button_cancel, 0, |
---|
| 265 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
| 266 | sizer_main.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) |
---|
[ea290ee] | 267 | self.SetAutoLayout(True) |
---|
| 268 | self.SetSizer(sizer_main) |
---|
| 269 | self.Layout() |
---|
| 270 | self.Centre() |
---|
| 271 | # end wxGlade |
---|
[b5de88e] | 272 | |
---|
[8dfdd20] | 273 | def _on_select_cmap(self, event): |
---|
| 274 | """ |
---|
[b5de88e] | 275 | display a new cmap |
---|
[8dfdd20] | 276 | """ |
---|
[32c0841] | 277 | cmap_name = self.cmap_selector.GetCurrentSelection() |
---|
| 278 | current_cmap = self.cmap_selector.GetClientData(cmap_name) |
---|
| 279 | self.cmap = current_cmap |
---|
[8dfdd20] | 280 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
[b5de88e] | 281 | norm=self.norm, orientation='horizontal') |
---|
| 282 | self.canvas.draw() |
---|