source: sasview/src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py @ fbfc488

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since fbfc488 was fbfc488, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More Qt5 related fixes.

  • Property mode set to 100644
File size: 30.9 KB
Line 
1"""
2This object is a small tool to allow user to quickly
3determine the variance in q  from the
4instrumental parameters.
5"""
6from PyQt5 import QtCore
7from PyQt5 import QtGui
8from PyQt5 import QtWidgets
9
10from twisted.internet import threads
11import sas.qtgui.Utilities.GuiUtils as GuiUtils
12from sas.qtgui.Plotting.PlotterData import Data2D
13from sas.qtgui.Plotting.Plotter2D import Plotter2DWidget
14from sas.sascalc.calculator.resolution_calculator import ResolutionCalculator
15import matplotlib.patches as patches
16
17import numpy
18import sys
19import logging
20import os
21import re
22
23from .UI.ResolutionCalculatorPanelUI import Ui_ResolutionCalculatorPanel
24
25_SOURCE_MASS = {'Alpha': 6.64465620E-24,
26                'Deuteron': 3.34358320E-24,
27                'Neutron': 1.67492729E-24,
28                'Photon': 0.0,
29                'Proton': 1.67262137E-24,
30                'Triton': 5.00826667E-24}
31
32BG_WHITE = "background-color: rgb(255, 255, 255);"
33BG_RED = "background-color: rgb(244, 170, 164);"
34
35
36class ResolutionCalculatorPanel(QtWidgets.QDialog, Ui_ResolutionCalculatorPanel):
37    """
38    compute resolution in 2D
39    """
40    def __init__(self, parent=None):
41        super(ResolutionCalculatorPanel, self).__init__()
42        self.setupUi(self)
43        self.manager = parent
44
45        # New font to display angstrom symbol
46        new_font = 'font-family: -apple-system, "Helvetica Neue", "Ubuntu";'
47        self.lblUnitWavelength.setStyleSheet(new_font)
48        self.lblUnitQx.setStyleSheet(new_font)
49        self.lblUnitQy.setStyleSheet(new_font)
50        self.lblUnitSigmax.setStyleSheet(new_font)
51        self.lblUnitSigmay.setStyleSheet(new_font)
52        self.lblUnitSigmalamd.setStyleSheet(new_font)
53        self.lblUnit1DSigma.setStyleSheet(new_font)
54
55        # by default Spectrum label and cbCustomSpectrum are not visible
56        self.cbCustomSpectrum.setVisible(False)
57        self.lblSpectrum.setVisible(False)
58        # self.onReset()
59
60        # change index of comboboxes
61        self.cbWaveColor.currentIndexChanged.connect(self.onSelectWaveColor)
62        self.cbCustomSpectrum.currentIndexChanged.connect(self.onSelectCustomSpectrum)
63
64        # push buttons
65        self.cmdClose.clicked.connect(self.accept)
66        self.cmdHelp.clicked.connect(self.onHelp)
67        self.cmdCompute.clicked.connect(self.onCompute)
68        self.cmdReset.clicked.connect(self.onReset)
69
70        # input defaults
71        self.qx = []
72        self.qy = []
73        # dQ defaults
74        self.sigma_r = None
75        self.sigma_phi = None
76        self.sigma_1d = None
77
78        # number of bins for wavelength and wavelength spread
79        self.num_wave = 10
80        self.spectrum_dic = {}
81
82        # dQ 2d image
83        self.image = None
84        # Source selection dic
85        self.source_mass = _SOURCE_MASS
86        # detector coordinate of estimation of sigmas
87        self.det_coordinate = 'cartesian'
88
89        self.resolution = ResolutionCalculator()
90        self.spectrum_dic['Add new'] = ''
91        self.spectrum_dic['Flat'] = self.resolution.get_default_spectrum()
92        self.resolution.set_spectrum(self.spectrum_dic['Flat'])
93
94        # validators
95        self.txtWavelength.editingFinished.connect(self.checkWavelength)
96        self.txtWavelengthSpread.editingFinished.connect(self.checkWavelengthSpread)
97
98        self.txtDetectorPixSize.editingFinished.connect(self.checkPixels)
99        self.txtDetectorSize.editingFinished.connect(self.checkPixels)
100
101        self.txtSourceApertureSize.editingFinished.connect(self.checkAperture)
102        self.txtSampleApertureSize.editingFinished.connect(self.checkAperture)
103
104        self.txtQx.editingFinished.connect(self.checkQx_y)
105        self.txtQy.editingFinished.connect(self.checkQx_y)
106
107        # double validator
108        self.txtSource2SampleDistance.setValidator(QtGui.QDoubleValidator())
109        self.txtSample2DetectorDistance.setValidator(QtGui.QDoubleValidator())
110        self.txtSampleOffset.setValidator(QtGui.QDoubleValidator())
111
112        # call compute to calculate with default values
113        self.createTemplate2DPlot()
114        #self.onCompute()
115
116    # #################################
117    # Validators: red background in line edits when wrong input
118    # and display of info logging message
119    # #################################
120
121    def checkWavelength(self):
122        """ Validator for Wavelength
123         if TOF, wavelength = min - max else only one number """
124        text_edit = self.txtWavelength  # self.sender()
125        if text_edit.isModified():
126            text_edit.setStyleSheet(BG_WHITE)
127            input_string = str(text_edit.text())
128            if self.cbWaveColor.currentText() != 'TOF':
129                input_wavelength = re.match('\d+\.?\d*', input_string)
130                if input_wavelength is None:
131                    text_edit.setStyleSheet(BG_RED)
132                    self.cmdCompute.setEnabled(False)
133                    logging.info('Wavelength has to be a number.')
134                else:
135                    text_edit.setStyleSheet(BG_WHITE)
136                    self.cmdCompute.setEnabled(True)
137            else:
138                interval_wavelength = re.match('^\d+\.?\d*\s*-\s*\d+\.?\d*$',
139                                               input_string)
140
141                if interval_wavelength is None:
142                    text_edit.setStyleSheet(BG_RED)
143                    self.cmdCompute.setEnabled(False)
144                    logging.info("Wavelength's input has to be an interval: "
145                                 "min - max.")
146                else:
147                    # check on min < max
148                    [wavelength_min, wavelength_max] = \
149                        re.findall('\d+\.?\d*', interval_wavelength.group())
150
151                    if float(wavelength_min) >= float(wavelength_max):
152                        text_edit.setStyleSheet(BG_RED)
153                        self.cmdCompute.setEnabled(False)
154                        logging.info("Wavelength: min must be smaller than max.")
155
156                    else:
157                        text_edit.setStyleSheet(BG_WHITE)
158                        self.cmdCompute.setEnabled(True)
159
160    def checkWavelengthSpread(self):
161        """ Validator for WavelengthSpread
162         Input can be a 'number or min - max (; Number of bins)' """
163        text_edit = self.sender()
164
165        if text_edit.isModified():
166            text_edit.setStyleSheet(BG_WHITE)
167            if self.cbWaveColor.currentText() != 'TOF':
168                pattern = '^\d+\.?\d*(|;\s*\d+)$'
169                input_string = str(text_edit.text())
170                wavelength_spread_input = re.match(pattern, input_string)
171
172                if wavelength_spread_input is None:
173                    text_edit.setStyleSheet(BG_RED)
174                    self.cmdCompute.setEnabled(False)
175                    logging.info('Wavelength spread has to be specified: '
176                                 'single value or value; integer number of bins.')
177
178                else:
179                    split_input = wavelength_spread_input.group().split(';')
180                    self.num_wave = split_input[1] if len(split_input) > 1 else 10
181                    text_edit.setStyleSheet(BG_WHITE)
182                    self.cmdCompute.setEnabled(True)
183            else:
184                pattern = '^\d+\.?\d*\s*-\s*\d+\.?\d*(|;\s*\d+)$'
185                input_string = str(text_edit.text())
186                wavelength_spread_input = re.match(pattern, input_string)
187
188                if wavelength_spread_input is None:
189                    text_edit.setStyleSheet(BG_RED)
190                    self.cmdCompute.setEnabled(False)
191                    logging.info("Wavelength spread has to be specified: "
192                                 "doublet separated by '-' with optional "
193                                 "number of bins (given after ';'). "
194                                 "For example, 0.1 - 0.1 (; 20).")
195
196                else:
197                    split_input = wavelength_spread_input.group().split(';')
198                    self.num_wave = split_input[1] if len(
199                        split_input) > 1 else 10
200                    text_edit.setStyleSheet(BG_WHITE)
201                    self.cmdCompute.setEnabled(True)
202
203    def checkPixels(self):
204        """ Validator for detector pixel size and number """
205        text_edit = self.sender()
206
207        if text_edit.isModified():
208            text_edit.setStyleSheet(BG_WHITE)
209            pattern = '^\d+\.?\d*,\s*\d+\.?\d*$'
210            input_string = str(text_edit.text())
211            pixels_input = re.match(pattern, input_string)
212
213            if pixels_input is None:
214                text_edit.setStyleSheet(BG_RED)
215                self.cmdCompute.setEnabled(False)
216                logging.info('The input for the detector should contain 2 '
217                             'values separated by a comma.')
218
219            else:
220                text_edit.setStyleSheet(BG_WHITE)
221                self.cmdCompute.setEnabled(True)
222
223    def checkQx_y(self):
224        """ Validator for qx and qy inputs """
225        Q_modified = [self.txtQx.isModified(), self.txtQy.isModified()]
226        if any(Q_modified):
227            pattern = '^-?\d+\.?\d*(,\s*-?\d+\.?\d*)*$'
228            text_edit = self.txtQx if Q_modified[0] else self.txtQy
229            input_string = str(text_edit.text())
230            q_input = re.match(pattern, input_string)
231            if q_input is None:
232                text_edit.setStyleSheet(BG_RED)
233                self.cmdCompute.setEnabled(False)
234                logging.info('Qx and Qy should contain one or more comma-separated numbers.')
235            else:
236                text_edit.setStyleSheet(BG_WHITE)
237                self.cmdCompute.setEnabled(True)
238                qx = str(self.txtQx.text()).split(',')
239                qy = str(self.txtQy.text()).split(',')
240
241                if len(qx) == 1 and len(qy) > 1:
242                    fill_qx = ', '.join([qx[0]] * len(qy))
243                    self.txtQx.setText(fill_qx)
244
245                elif len(qy) == 1 and len(qx) > 1:
246                    fill_qy = ', '.join([qy[0]] * len(qx))
247                    self.txtQy.setText(fill_qy)
248
249                elif len(qx) != len(qy):
250                    text_edit.setStyleSheet(BG_RED)
251                    self.cmdCompute.setEnabled(False)
252                    logging.info(
253                        'Qx and Qy should have the same number of elements.')
254
255                else:
256                    text_edit.setStyleSheet(BG_WHITE)
257                    self.cmdCompute.setEnabled(True)
258
259    def checkAperture(self):
260        """ Validator for Sample and Source apertures"""
261        text_edit = self.sender()
262
263        if text_edit.isModified():
264            text_edit.setStyleSheet(BG_WHITE)
265            input_string = str(text_edit.text())
266            pattern = '^\d+\.?\d*(|,\s*\d+)$'
267            aperture_input = re.match(pattern, input_string)
268
269            if aperture_input is None:
270                text_edit.setStyleSheet(BG_RED)
271                self.cmdCompute.setEnabled(False)
272                logging.info('A circular aperture is defined by a single '
273                             'value (diameter). A rectangular aperture is '
274                             'defined by 2 values separated by a comma.')
275
276            else:
277                text_edit.setStyleSheet(BG_WHITE)
278                self.cmdCompute.setEnabled(True)
279
280    # #################################
281    # Slots associated with signals from comboboxes
282    # #################################
283
284    def onSelectWaveColor(self):
285        """ Modify layout of GUI when TOF selected: add elements
286        and modify default entry of Wavelength """
287        list_wdata = self.resolution.get_wave_list()
288        min_lambda = min(list_wdata[0])
289
290        min_wspread = min(list_wdata[1])
291        max_wspread = max(list_wdata[1])
292
293        if self.cbWaveColor.currentText() == 'TOF':
294            self.cbCustomSpectrum.setVisible(True)
295            self.lblSpectrum.setVisible(True)
296            # Get information about wavelength and spread
297
298            if len(list_wdata[0]) < 2:
299                max_lambda = 2 * min_lambda
300            else:
301                max_lambda = max(list_wdata[0])
302            self.txtWavelength.setText('{} - {}'.format(min_lambda, max_lambda))
303            self.txtWavelengthSpread.setText('{} - {}'.format(min_wspread,
304                                                    max_wspread))
305
306        else:
307            self.cbCustomSpectrum.setVisible(False)
308            self.lblSpectrum.setVisible(False)
309            # modify Wavelength line edit only if set for TOF (2 elements)
310
311            if len(self.txtWavelength.text().split('-')) >= 2:
312                self.txtWavelength.setText(str(min_lambda))
313                self.txtWavelengthSpread.setText(str(min_wspread))
314
315    def onSelectCustomSpectrum(self):
316        """ On Spectrum Combobox event"""
317        if self.cbCustomSpectrum.currentText() == 'Add New':
318            datafile = QtWidgets.QFileDialog.getOpenFileName(
319                self, "Choose a spectral distribution file","",
320                "All files (*.*)", None,
321                QtWidgets.QFileDialog.DontUseNativeDialog)[0]
322
323            if datafile is None or str(datafile) == '':
324                logging.info("No spectral distribution data chosen.")
325                self.cbCustomSpectrum.setCurrentIndex(0)
326                self.resolution.set_spectrum(self.spectrum_dic['Flat'])
327                return
328
329            try:
330                basename = os.path.basename(datafile)
331                if basename not in list(self.spectrum_dic.keys()):
332                    self.cbCustomSpectrum.addItem(basename)
333
334                input_f = open(datafile, 'r')
335                buff = input_f.read()
336                lines = buff.split('\n')
337
338                wavelength = []
339                intensity = []
340
341                for line in lines:
342                    toks = line.split()
343                    try:
344                        wave = float(toks[0])
345                        intens = float(toks[1])
346                        wavelength.append(wave)
347                        intensity.append(intens)
348                    except:
349                        logging.info('Could not extract values from file')
350            except:
351                raise
352
353            self.spectrum_dic[basename] = [wavelength, intensity]
354            self.resolution.set_spectrum(self.spectrum_dic[basename])
355        return
356
357    # #################################
358    # Slots associated with signals from push buttons
359    # #################################
360
361    def onHelp(self):
362        """
363        Bring up the Resolution Calculator Documentation whenever
364        the HELP button is clicked.
365        Calls Documentation Window with the path of the location within the
366        documentation tree (after /doc/ ....".
367        """
368        try:
369            location = GuiUtils.HELP_DIRECTORY_LOCATION + \
370                       "/user/sasgui/perspectives/calculator/resolution_calculator_help.html"
371            self.manager._helpView.load(QtCore.QUrl(location))
372            self.manager._helpView.show()
373
374        except AttributeError:
375            # No manager defined - testing and standalone runs
376            pass
377
378    def onReset(self):
379        # by default Spectrum label and cbCustomSpectrum are not visible
380        self.cbCustomSpectrum.setVisible(False)
381        self.lblSpectrum.setVisible(False)
382        # Comboboxes
383        self.cbCustomSpectrum.setCurrentIndex([self.cbCustomSpectrum.itemText(i)
384                                               for i in range(self.cbCustomSpectrum.count())].index('Flat'))
385        self.cbSource.setCurrentIndex([self.cbSource.itemText(i) for i in
386                                       range(self.cbSource.count())].index('Neutron'))
387        self.cbWaveColor.setCurrentIndex([self.cbWaveColor.itemText(i) for i
388                                          in range(self.cbWaveColor.count())].index('Monochromatic'))
389        # LineEdits
390        self.txtDetectorPixSize.setText('0.5, 0.5')
391        self.txtDetectorSize.setText('128, 128')
392        self.txtSample2DetectorDistance.setText('1000')
393        self.txtSampleApertureSize.setText('1.27')
394        self.txtSampleOffset.setText('0')
395        self.txtSource2SampleDistance.setText('1627')
396        self.txtSourceApertureSize.setText('3.81')
397        self.txtWavelength.setText('6.0')
398        self.txtWavelengthSpread.setText('0.125')
399        self.txtQx.setText('0.0')
400        self.txtQy.setText('0.0')
401        self.txt1DSigma.setText('0.0008289')
402        self.txtSigma_x.setText('0.0008288')
403        self.txtSigma_y.setText('0.0008288')
404        self.txtSigma_lamd.setText('3.168e-05')
405
406        self.image = None
407        self.source_mass = _SOURCE_MASS
408        self.det_coordinate = 'cartesian'
409        self.num_wave = 10
410        self.spectrum_dic = {}
411        self.spectrum_dic['Add new'] = ''
412        self.spectrum_dic['Flat'] = self.resolution.get_default_spectrum()
413        self.resolution.set_spectrum(self.spectrum_dic['Flat'])
414        # Reset plot
415        self.onCompute()
416
417    # TODO Keep legacy validators??
418    def onCompute(self):
419        """
420        Execute the computation of resolution
421        """
422        # Q min max list default
423        qx_min = []
424        qx_max = []
425        qy_min = []
426        qy_max = []
427        # possible max qrange
428        self.resolution.qxmin_limit = 0
429        self.resolution.qxmax_limit = 0
430        self.resolution.qymin_limit = 0
431        self.resolution.qymax_limit = 0
432
433        try:
434            # Get all the values to compute
435            wavelength = self._str2longlist(self.txtWavelength.text())
436
437            source = self.cbSource.currentText()
438            mass = self.source_mass[str(source)]
439            self.resolution.set_neutron_mass(float(mass))
440
441            wavelength_spread = self._str2longlist(\
442                        self.txtWavelengthSpread.text().split(';')[0])
443            # Validate the wave inputs
444            wave_input = self._validate_q_input(wavelength, wavelength_spread)
445            if wave_input is not None:
446                wavelength, wavelength_spread = wave_input
447
448            self.resolution.set_wave(wavelength)
449            self.resolution.set_wave_spread(wavelength_spread)
450
451            # use legacy validator for correct input assignment
452
453            source_aperture_size = self.txtSourceApertureSize.text()
454            source_aperture_size = self._str2longlist(source_aperture_size)
455            self.resolution.set_source_aperture_size(source_aperture_size)
456
457            sample_aperture_size = self.txtSampleApertureSize.text()
458            sample_aperture_size = self._string2list(sample_aperture_size)
459            self.resolution.set_sample_aperture_size(sample_aperture_size)
460
461            source2sample_distance = self.txtSource2SampleDistance.text()
462            source2sample_distance = self._string2list(source2sample_distance)
463            self.resolution.set_source2sample_distance(source2sample_distance)
464
465            sample2sample_distance = self.txtSampleOffset.text()
466            sample2sample_distance = self._string2list(sample2sample_distance)
467            self.resolution.set_sample2sample_distance(sample2sample_distance)
468
469            sample2detector_distance = self.txtSample2DetectorDistance.text()
470            sample2detector_distance = self._string2list(
471                sample2detector_distance)
472            self.resolution.set_sample2detector_distance(
473                sample2detector_distance)
474
475            detector_size = self.txtDetectorSize.text()
476            detector_size = self._string2list(detector_size)
477            self.resolution.set_detector_size(detector_size)
478
479            detector_pix_size = self.txtDetectorPixSize.text()
480            detector_pix_size = self._string2list(detector_pix_size)
481            self.resolution.set_detector_pix_size(detector_pix_size)
482
483            self.qx = self._string2inputlist(self.txtQx.text())
484            self.qy = self._string2inputlist(self.txtQy.text())
485
486            # Find min max of qs
487            xmin = min(self.qx)
488            xmax = max(self.qx)
489            ymin = min(self.qy)
490            ymax = max(self.qy)
491            if not self._validate_q_input(self.qx, self.qy):
492                raise ValueError("Invalid Q input")
493        except:
494            msg = "An error occurred during the resolution computation."
495            msg += "Please check your inputs..."
496            logging.warning(msg)
497            return
498
499        # Validate the q inputs
500        q_input = self._validate_q_input(self.qx, self.qy)
501        if q_input is not None:
502            self.qx, self.qy = q_input
503
504        # Make list of q min max for mapping
505        for i in range(len(self.qx)):
506            qx_min.append(xmin)
507            qx_max.append(xmax)
508        for i in range(len(self.qy)):
509            qy_min.append(ymin)
510            qy_max.append(ymax)
511
512        # Compute the resolution
513        if self.image is not None:
514            self.resolution.reset_image()
515
516        # Compute and get the image plot
517        try:
518            cal_res = threads.deferToThread(self.map_wrapper,
519                                            self.calc_func,
520                                            self.qx,
521                                            self.qy,
522                                            qx_min,
523                                            qx_max,
524                                            qy_min, qy_max)
525
526            cal_res.addCallback(self.complete)
527            cal_res.addErrback(self.calculateFailed)
528
529            # logging.info("Computation is in progress...")
530            self.cmdCompute.setText('Wait...')
531            self.cmdCompute.setEnabled(False)
532        except:
533            raise
534
535    def calculateFailed(self, reason):
536        print("calculateFailed Failed with:\n", reason)
537        pass
538
539    def complete(self, image):
540        """
541        Complete computation
542        """
543        self.image = image
544
545        # Get and format the sigmas
546        sigma_r = self.formatNumber(self.resolution.sigma_1)
547        sigma_phi = self.formatNumber(self.resolution.sigma_2)
548        sigma_lamd = self.formatNumber(self.resolution.sigma_lamd)
549        sigma_1d = self.formatNumber(self.resolution.sigma_1d)
550
551        # Set output values
552        self.txtSigma_x.setText(str(sigma_r))
553        self.txtSigma_y.setText(str(sigma_phi))
554        self.txtSigma_lamd.setText(str(sigma_lamd))
555        self.txt1DSigma.setText(str(sigma_1d))
556
557        self.cmdCompute.setText('Compute')
558        self.cmdCompute.setEnabled(True)
559
560        self.new2DPlot()
561
562        return
563
564    def map_wrapper(self, func, qx, qy, qx_min, qx_max, qy_min, qy_max):
565        """
566        Prepare the Mapping for the computation
567        : params qx, qy, qx_min, qx_max, qy_min, qy_max:
568        : return: image (numpy array)
569        """
570        image = list(map(func, qx, qy,
571                    qx_min, qx_max,
572                    qy_min, qy_max))[0]
573
574        return image
575
576    def calc_func(self, qx, qy, qx_min, qx_max, qy_min, qy_max):
577        """
578        Perform the calculation for a given set of Q values.
579        : return: image (numpy array)
580        """
581        try:
582            qx_value = float(qx)
583            qy_value = float(qy)
584        except :
585            raise ValueError
586
587        # calculate 2D resolution distribution image
588        image = self.resolution.compute_and_plot(qx_value, qy_value,
589                                                 qx_min, qx_max, qy_min,
590                                                 qy_max,
591                                                 self.det_coordinate)
592        return image
593
594    # #################################
595    # Legacy validators
596    # #################################
597    def _string2list(self, input_string):
598        """
599        Change NNN, NNN to list,ie. [NNN, NNN] where NNN is a number
600        """
601        new_numbers_list = []
602        # check the number of floats
603        try:
604            strg = float(input_string)
605            new_numbers_list.append(strg)
606        except:
607            string_split = input_string.split(',')
608            if len(string_split) == 1 or len(string_split) == 2:
609                new_numbers_list = [float(item) for item in string_split]
610            else:
611                msg = "The numbers must be one or two (separated by ',')"
612                logging.info(msg)
613                raise RuntimeError(msg)
614
615        return new_numbers_list
616
617    def _string2inputlist(self, input_string):
618        """
619        Change NNN, NNN,... to list,ie. [NNN, NNN,...] where NNN is a number
620        : return new_list: string like list
621        """
622        new_list = []
623        string_split = input_string.split(',')
624        try:
625            new_list = [float(t) for t in string_split]
626        except:
627            logging.error(sys.exc_info()[1])
628        return new_list
629
630    def _str2longlist(self, input_string):
631        """
632          Change NNN, NNN,... to list, NNN - NNN ; NNN to list, or float to list
633          : return new_string: string like list
634          """
635        try:
636            # is float
637            out = [float(input_string)]
638            return out
639        except:
640            if self.cbWaveColor.currentText() == 'Monochromatic':
641                logging.warning("Wrong format of inputs.")
642            else:
643                try:
644                    # has a '-'
645                    if input_string.count('-') > 0:
646                        value = input_string.split('-')
647                        if value[1].count(';') > 0:
648                            # has a ';'
649                            last_list = value[1].split(';')
650                            num = numpy.ceil(float(last_list[1]))
651                            max_value = float(last_list[0])
652                            self.num_wave = num
653                        else:
654                            # default num
655                            num = self.num_wave
656                            max_value = float(value[1])
657                        min_value = float(value[0])
658                        # make a list
659                        bin_size = numpy.fabs(max_value - min_value) / (num - 1)
660                        out = [min_value + bin_size * bnum for bnum in
661                               range(num)]
662                        return out
663                    if input_string.count(',') > 0:
664                        out = self._string2inputlist(input_string)
665                        return out
666                except:
667                    logging.error(sys.exc_info()[1])
668
669    def _validate_q_input(self, qx, qy):
670        """
671        Check if q inputs are valid
672        : params qx:  qx as a list
673        : params qy:  qy as a list
674        : return: True/False
675        """
676        # check qualifications
677        if qx.__class__.__name__ != 'list':
678            return None
679        if qy.__class__.__name__ != 'list':
680            return None
681        if len(qx) < 1:
682            return None
683        if len(qy) < 1:
684            return None
685        # allow one input
686        if len(qx) == 1 and len(qy) > 1:
687            qx = [qx[0] for ind in range(len(qy))]
688
689        if len(qy) == 1 and len(qx) > 1:
690            qy = [qy[0] for ind in range(len(qx))]
691        # check length
692        if len(qx) != len(qy):
693            return None
694        if qx is None or qy is None:
695            return None
696        return qx, qy
697
698    def formatNumber(self, value=None):
699        """
700        Return a float in a standardized, human-readable formatted string
701        """
702        try:
703            value = float(value)
704        except:
705            output = None
706            return output
707
708        output = "%-7.4g" % value
709        return output.lstrip().rstrip()
710
711    # #################################
712    # Plot
713    # #################################
714
715    def createTemplate2DPlot(self):
716        """
717        Create a template for 2D data
718        """
719        self.plotter = Plotter2DWidget(self, quickplot=True)
720        self.plotter.scale = 'linear'
721        self.plotter.cmap = None
722        layout = QtWidgets.QHBoxLayout()
723        layout.setContentsMargins(0, 0, 0, 0)
724        self.graphicsView.setLayout(layout)
725        layout.addWidget(self.plotter)
726
727    def new2DPlot(self):
728        """
729        Create a new 2D data instance based on computing results
730        """
731        qx_min, qx_max, qy_min, qy_max = self.resolution.get_detector_qrange()
732
733        dx_size = (qx_max - qx_min) / (1000 - 1)
734        dy_size = (qy_max - qy_min) / (1000 - 1)
735        x_val = numpy.arange(qx_min, qx_max, dx_size)
736        y_val = numpy.arange(qy_max, qy_min, -dy_size)
737
738        if len(self.plotter.ax.patches):
739            self.plotter.ax.patches[0].remove()
740
741        self.drawLines()
742
743        self.plotter.data = Data2D(image=self.image,
744                      qx_data=x_val,
745                      qy_data=y_val,
746                      xmin=qx_min, xmax=qx_max,
747                      ymin=qy_min, ymax=qy_max)
748
749        self.plotter.plot()
750        self.plotter.show()
751        self.plotter.update()
752
753    def drawLines(self):
754        """
755        Draw lines in image if applicable
756        """
757        wave_list, _ = self.resolution.get_wave_list()
758        if len(wave_list) > 1 and wave_list[-1] == max(wave_list):
759            color = 'g'
760            # draw a green rectangle(limit for the longest wavelength
761            # to be involved) for tof inputs
762            # Get the params from resolution
763            # plotting range for largest wavelength
764            qx_min = self.resolution.qx_min
765            qx_max = self.resolution.qx_max
766            qy_min = self.resolution.qy_min
767            qy_max = self.resolution.qy_max
768            # detector range
769            detector_qx_min = self.resolution.detector_qx_min
770            detector_qx_max = self.resolution.detector_qx_max
771            detector_qy_min = self.resolution.detector_qy_min
772            detector_qy_max = self.resolution.detector_qy_max
773
774            rect = patches.Rectangle((detector_qx_min + 0.0002,
775                                      detector_qy_min + 0.0002),
776                                     detector_qx_max - detector_qx_min,
777                                     detector_qy_max - detector_qy_min,
778                                     linewidth=2,
779                                     edgecolor=color, facecolor='none')
780            self.plotter.ax.add_patch(rect)
781        else:
782            qx_min, qx_max, qy_min, qy_max = self.resolution.get_detector_qrange()
783            # detector range
784            detector_qx_min = self.resolution.qxmin_limit
785            detector_qx_max = self.resolution.qxmax_limit
786            detector_qy_min = self.resolution.qymin_limit
787            detector_qy_max = self.resolution.qymax_limit
788
789            xmin = min(self.qx)
790            xmax = max(self.qx)
791            ymin = min(self.qy)
792            ymax = max(self.qy)
793
794            if xmin < detector_qx_min or xmax > detector_qx_max or \
795                            ymin < detector_qy_min or ymax > detector_qy_max:
796                # message
797                msg = 'At least one q value located out side of\n'
798                msg += " the detector range (%s < qx < %s, %s < qy < %s),\n" % \
799                       (self.formatNumber(detector_qx_min),
800                        self.formatNumber(detector_qx_max),
801                        self.formatNumber(detector_qy_min),
802                        self.formatNumber(detector_qy_max))
803                msg += " is ignored in computation.\n"
804
805                logging.warning(msg)
806
807        # Draw zero axis lines.
808        if qy_min < 0 <= qy_max:
809            self.plotter.ax.axhline(linewidth=1)
810
811        if qx_min < 0 <= qx_max:
812            self.plotter.ax.axvline(linewidth=1)
Note: See TracBrowser for help on using the repository browser.