def set_slicer(self, type, params): """ Rebuild the panel """ self.bck.Clear(True) self.type = type if type == None: label = "Right-click on 2D plot for slicer options" title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) self.bck.Add(title, (0, 0), (1, 2), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) else: title_text = str(type) + "Parameters" title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) self.bck.Add(title, (0, 0), (1, 2), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) n = 1 self.parameters = [] keys = params.keys() keys.sort() for item in keys: if not item.lower() in ["num_points", "avg", "avg_error", "sum", "sum_error"]: n += 1 text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) self.bck.Add(text, (n - 1, 0), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) ctl = wx.TextCtrl(self, -1, size=(80, 20), style=wx.TE_PROCESS_ENTER) hint_msg = "Modify the value of %s to change " % item hint_msg += "the 2D slicer" ctl.SetToolTipString(hint_msg) ctl.SetValue(str(format_number(params[item]))) self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) self.parameters.append([item, ctl]) self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, border=0) for item in keys: if item.lower() in ["num_points", "avg", "avg_error", "sum", "sum_error"]: n += 1 text = wx.StaticText(self, -1, item + ": ", style=wx.ALIGN_LEFT) self.bck.Add(text, (n - 1, 0), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) ctl = wx.StaticText(self, -1, str(format_number(params[item])), style=wx.ALIGN_LEFT) ctl.SetToolTipString("Result %s" % item) self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, border=0) self.bck.Layout() self.Layout() psizer = self.parent.GetSizer() if psizer != None: psizer.Layout()
def calculateSld(self, event): """ Calculate the neutron scattering density length of a molecule """ self.clear_outputs() try: #Check validity user inputs flag, msg = self.check_inputs() if self.base is not None and msg.lstrip().rstrip() != "": msg = "SLD Calculator: %s" % str(msg) wx.PostEvent(self.base, StatusEvent(status=msg)) if not flag: return #get ready to compute self.sld_formula = formula(self.compound, density=self.density) (sld_real, sld_im, _), (_, absorp, incoh), \ length = neutron_scattering(compound=self.compound, density=self.density, wavelength=self.wavelength) cu_real, cu_im = self.calculate_sld_helper( element="Cu", density=self.density, molecule_formula=self.sld_formula) mo_real, mo_im = self.calculate_sld_helper( element="Mo", density=self.density, molecule_formula=self.sld_formula) # set neutron sld values val = format_number(sld_real * _SCALE) self.neutron_sld_real_ctl.SetValue(val) val = format_number(math.fabs(sld_im) * _SCALE) self.neutron_sld_im_ctl.SetValue(val) # Compute the Cu SLD self.cu_ka_sld_real_ctl.SetValue(format_number(cu_real * _SCALE)) val = format_number(math.fabs(cu_im) * _SCALE) self.cu_ka_sld_im_ctl.SetValue(val) # Compute the Mo SLD self.mo_ka_sld_real_ctl.SetValue(format_number(mo_real * _SCALE)) val = format_number(math.fabs(mo_im) * _SCALE) self.mo_ka_sld_im_ctl.SetValue(val) # set incoherence and absorption self.neutron_inc_ctl.SetValue(format_number(incoh)) self.neutron_abs_ctl.SetValue(format_number(absorp)) # Neutron length self.neutron_length_ctl.SetValue(format_number(length)) # display wavelength self.wavelength_ctl.SetValue(str(self.wavelength)) except: if self.base is not None: msg = "SLD Calculator: %s" % (sys.exc_value) wx.PostEvent(self.base, StatusEvent(status=msg)) if event is not None: event.Skip()
def set_slicer(self, type, params): """ Rebuild the panel """ self.bck.Clear(True) self.type = type if type == None: label = "Right-click on 2D plot for slicer options" title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) self.bck.Add(title, (0, 0), (1, 2), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) else: title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) self.bck.Add(title, (0, 0), (1, 2), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) ix = 0 iy = 0 self.parameters = [] keys = params.keys() keys.sort() for item in keys: iy += 1 ix = 0 if not item in ["count", "errors"]: text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) self.bck.Add(text, (iy, ix), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) ctl = wx.TextCtrl(self, -1, size=(80, 20), style=wx.TE_PROCESS_ENTER) hint_msg = "Modify the value of %s to change" % item hint_msg += " the 2D slicer" ctl.SetToolTipString(hint_msg) ix = 1 ctl.SetValue(format_number(str(params[item]))) self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) self.parameters.append([item, ctl]) self.bck.Add(ctl, (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) ix = 3 self.bck.Add((20, 20), (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) else: text = wx.StaticText(self, -1, item + " : ", style=wx.ALIGN_LEFT) self.bck.Add(text, (iy, ix), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) ctl = wx.StaticText(self, -1, format_number(str(params[item])), style=wx.ALIGN_LEFT) ix = 1 self.bck.Add(ctl, (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) iy += 1 ix = 1 self.bck.Add((20, 20), (iy, ix), (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) self.bck.Layout() self.bck.Fit(self) self.parent.GetSizer().Layout()
def calculateSld(self, event): """ Calculate the neutron scattering density length of a molecule """ self.clear_outputs() try: #Check validity user inputs flag, msg = self.check_inputs() if self.base is not None and msg.lstrip().rstrip() != "": msg = "SLD Calculator: %s" % str(msg) wx.PostEvent(self.base, StatusEvent(status=msg)) if not flag: return #get ready to compute self.sld_formula = formula(self.compound, density=self.density) (sld_real, sld_im, _), (_, absorp, incoh), \ length = neutron_scattering(compound=self.compound, density=self.density, wavelength=self.wavelength) cu_real, cu_im = self.calculate_sld_helper(element="Cu", density=self.density, molecule_formula=self.sld_formula) mo_real, mo_im = self.calculate_sld_helper(element="Mo", density=self.density, molecule_formula=self.sld_formula) # set neutron sld values val = format_number(sld_real * _SCALE) self.neutron_sld_real_ctl.SetValue(val) val = format_number(math.fabs(sld_im) * _SCALE) self.neutron_sld_im_ctl.SetValue(val) # Compute the Cu SLD self.cu_ka_sld_real_ctl.SetValue(format_number(cu_real * _SCALE)) val = format_number(math.fabs(cu_im) * _SCALE) self.cu_ka_sld_im_ctl.SetValue(val) # Compute the Mo SLD self.mo_ka_sld_real_ctl.SetValue(format_number(mo_real * _SCALE)) val = format_number(math.fabs(mo_im) * _SCALE) self.mo_ka_sld_im_ctl.SetValue(val) # set incoherence and absorption self.neutron_inc_ctl.SetValue(format_number(incoh)) self.neutron_abs_ctl.SetValue(format_number(absorp)) # Neutron length self.neutron_length_ctl.SetValue(format_number(length)) # display wavelength self.wavelength_ctl.SetValue(str(self.wavelength)) except: if self.base is not None: msg = "SLD Calculator: %s" % (sys.exc_value) wx.PostEvent(self.base, StatusEvent(status=msg)) if event is not None: event.Skip()
def onParamChange(self, evt): """ Receive and event and reset the text field contained in self.parameters """ evt.Skip() for item in self.parameters: if item[0] in evt.params: item[1].SetValue(format_number(evt.params[item[0]])) item[1].Refresh()
def setContent(self, xnpts, ynpts, qmax, beam, zmin=None, zmax=None, sym=False): """ received value and displayed them :param xnpts: the number of point of the x_bins of data :param ynpts: the number of point of the y_bins of data :param qmax: the maxmimum value of data pixel :param beam: the radius of the beam :param zmin: the value to get the minimum color :param zmax: the value to get the maximum color :param sym: """ self.xnpts_ctl.SetLabel(str(format_number(xnpts))) self.ynpts_ctl.SetLabel(str(format_number(ynpts))) self.qmax_ctl.SetLabel(str(format_number(qmax))) self.beam_ctl.SetLabel(str(format_number(beam))) if zmin != None: self.zmin_ctl.SetValue(str(format_number(zmin))) if zmax != None: self.zmax_ctl.SetValue(str(format_number(zmax)))
def set_values(self): """ Set value of txtcrtl """ value = format_number(self.qstar_container.qstar) self.invariant_tcl.SetValue(value) value = format_number(self.qstar_container.qstar_err) self.invariant_err_tcl.SetValue(value) value = format_number(self.qstar_container.qstar_low) self.invariant_low_tcl.SetValue(value) value = format_number(self.qstar_container.qstar_low_err) self.invariant_low_err_tcl.SetValue(value) value = format_number(self.qstar_container.qstar_high) self.invariant_high_tcl.SetValue(value) value = format_number(self.qstar_container.qstar_high_err) self.invariant_high_err_tcl.SetValue(value)
def set_slicer(self, type, params): """ Rebuild the panel """ self.bck.Clear(True) self.type = type if type == None: label = "Right-click on 2D plot for slicer options" title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) self.bck.Add(title, (0, 0), (1, 2), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) else: title_text = str(type) + "Parameters" title = wx.StaticText(self, -1, title_text, style=wx.ALIGN_LEFT) self.bck.Add(title, (0, 0), (1, 2), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) n = 1 self.parameters = [] keys = params.keys() keys.sort() for item in keys: if not item.lower() in [ "num_points", "avg", "avg_error", "sum", "sum_error" ]: n += 1 text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) self.bck.Add(text, (n - 1, 0), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) ctl = wx.TextCtrl(self, -1, size=(80, 20), style=wx.TE_PROCESS_ENTER) hint_msg = "Modify the value of %s to change " % item hint_msg += "the 2D slicer" ctl.SetToolTipString(hint_msg) ctl.SetValue(str(format_number(params[item]))) self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) self.parameters.append([item, ctl]) self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, border=0) for item in keys: if item.lower() in [ "num_points", "avg", "avg_error", "sum", "sum_error" ]: n += 1 text = wx.StaticText(self, -1, item + ": ", style=wx.ALIGN_LEFT) self.bck.Add(text, (n - 1, 0), flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) ctl = wx.StaticText(self, -1, str(format_number(params[item])), style=wx.ALIGN_LEFT) ctl.SetToolTipString("Result %s" % item) self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, border=0) self.bck.Layout() self.Layout() psizer = self.parent.GetSizer() if psizer != None: psizer.Layout()
def on_paint(self, event): """ Draw the chart """ dc = wx.PaintDC(self.panel_chart) try: gc = wx.GraphicsContext.Create(dc) except NotImplementedError: msg = "This build of wxPython does not support " msg += "the wx.GraphicsContext family of classes." dc.DrawText(msg, 25, 25) return # Start the drawing font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) font.SetWeight(wx.BOLD) gc.SetFont(font) # Draw a rectangle path = gc.CreatePath() path.AddRectangle(-RECTANGLE_WIDTH / 2, -RECTANGLE_HEIGHT / 2, RECTANGLE_WIDTH / 2, RECTANGLE_HEIGHT / 2) x_origine = 20 y_origine = 15 # Draw low rectangle gc.PushState() label = "Q* from Low-Q " PathFunc = gc.DrawPath w, h = gc.GetTextExtent(label) gc.DrawText(label, x_origine, y_origine) # Translate the rectangle x_center = x_origine + RECTANGLE_WIDTH * self.low_scale / 2 + w + 10 y_center = y_origine + h gc.Translate(x_center, y_center) gc.SetPen(wx.Pen("black", 1)) gc.SetBrush(wx.Brush(self.extrapolation_color_low)) if self.low_inv_percent is None: low_percent = 'Not Computed' elif self.low_inv_percent == 'error': low_percent = 'Error' else: low_percent = format_number(self.low_inv_percent * 100) + '%' x_center = 20 y_center = -h gc.DrawText(low_percent, x_center, y_center) # Increase width by self.low_scale gc.Scale(self.low_scale, 1.0) PathFunc(path) gc.PopState() # Draw rectangle for invariant gc.PushState() # save it again y_origine += 20 gc.DrawText("Q* from Data ", x_origine, y_origine) # offset to the lower part of the window x_center = x_origine + RECTANGLE_WIDTH * self.inv_scale / 2 + w + 10 y_center = y_origine + h gc.Translate(x_center, y_center) # 128 == half transparent gc.SetBrush(wx.Brush(self.invariant_color)) # Increase width by self.inv_scale if self.inv_percent is None: inv_percent = 'Not Computed' elif self.inv_percent == 'error': inv_percent = 'Error' else: inv_percent = format_number(self.inv_percent * 100) + '%' x_center = 20 y_center = -h gc.DrawText(inv_percent, x_center, y_center) gc.Scale(self.inv_scale, 1.0) gc.DrawPath(path) gc.PopState() # restore saved state # Draw rectangle for high invariant gc.PushState() y_origine += 20 gc.DrawText("Q* from High-Q ", x_origine, y_origine) # define the position of the new rectangle x_center = x_origine + RECTANGLE_WIDTH * self.high_scale / 2 + w + 10 y_center = y_origine + h gc.Translate(x_center, y_center) gc.SetBrush(wx.Brush(self.extrapolation_color_high)) # increase scale by self.high_scale if self.high_inv_percent is None: high_percent = 'Not Computed' elif self.high_inv_percent == 'error': high_percent = 'Error' else: high_percent = format_number(self.high_inv_percent * 100) + '%' x_center = 20 y_center = -h gc.DrawText(high_percent, x_center, y_center) gc.Scale(self.high_scale, 1.0) gc.DrawPath(path) gc.PopState()
def __str__(self): """ Pretty print : return: string representing the state """ # Input string compute_num = self.saved_state['compute_num'] compute_state = self.state_list[str(compute_num)] my_time, date = self.timestamp file_name = self.file state_num = int(self.saved_state['state_num']) state = "\n[Invariant computation for %s: " % file_name state += "performed at %s on %s] \n" % (my_time, date) state += "State No.: %d \n" % state_num state += "\n=== Inputs ===\n" # text ctl general inputs ( excluding extrapolation text ctl) for key, value in self.input_list.iteritems(): if value == '': continue key_split = key.split('_') max_ind = len(key_split) - 1 if key_split[max_ind] == 'tcl': name = "" if key_split[1] == 'low' or key_split[1] == 'high': continue for ind in range(0, max_ind): name += " %s" % key_split[ind] state += "%s: %s\n" % (name.lstrip(" "), value) # other input parameters extra_lo = compute_state['enable_low_cbox'] if compute_state['enable_low_cbox']: if compute_state['guinier']: extra_lo = 'Guinier' else: extra_lo = 'Power law' extra_hi = compute_state['enable_high_cbox'] if compute_state['enable_high_cbox']: extra_hi = 'Power law' state += "\nExtrapolation: High=%s; Low=%s\n" % (extra_hi, extra_lo) low_off = False high_off = False for key, value in self.input_list.iteritems(): key_split = key.split('_') max_ind = len(key_split) - 1 if key_split[max_ind] == 'tcl': name = "" # check each buttons whether or not ON or OFF if key_split[1] == 'low' or key_split[1] == 'high': if not compute_state['enable_low_cbox'] and \ key_split[max_ind - 1] == 'low': low_off = True continue elif not compute_state['enable_high_cbox'] and \ key_split[max_ind - 1] == 'high': high_off = True continue elif extra_lo == 'Guinier' and key_split[0] == 'power' and \ key_split[max_ind - 1] == 'low': continue for ind in range(0, max_ind): name += " %s" % key_split[ind] name = name.lstrip(" ") if name == "power low": if compute_state['fix_enable_low']: name += ' (Fixed)' else: name += ' (Fitted)' if name == "power high": if compute_state['fix_enable_high']: name += ' (Fixed)' else: name += ' (Fitted)' state += "%s: %s\n" % (name, value) # Outputs state += "\n=== Outputs ===" for item in output_list: item_split = item[0].split('_') # Exclude the extrapolation that turned off if len(item_split) > 1: if low_off and item_split[1] == 'low': continue if high_off and item_split[1] == 'high': continue max_ind = len(item_split) - 1 value = None if hasattr(self.container, item[0]): # Q* outputs value = getattr(self.container, item[0]) else: # other outputs than Q* name = item[0] + "_tcl" if name in self.saved_state.keys(): value = self.saved_state[name] # Exclude the outputs w/'' if value == '': continue # Error outputs if item_split[max_ind] == 'err': state += "+- %s " % format_number(value) # Percentage outputs elif item_split[max_ind] == 'percent': value = float(value) * 100 state += "(%s %s)" % (format_number(value), '%') # Outputs else: state += "\n%s: %s " % (item[1], format_number(value, high=True)) # Include warning msg if self.container is not None: state += "\n\nNote:\n" + self.container.warning_msg return state