summaryrefslogtreecommitdiff
path: root/silx/gui/fit/FitWidgets.py
blob: 408666b8c0d71623156fa2000b381b42d8ce1341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# coding: utf-8
# /*##########################################################################
# Copyright (C) 2004-2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ######################################################################### */
"""Collection of widgets used to build
:class:`silx.gui.fit.FitWidget.FitWidget`"""

from collections import OrderedDict

from silx.gui import qt
from silx.gui.fit.Parameters import Parameters

QTVERSION = qt.qVersion()

__authors__ = ["V.A. Sole", "P. Knobel"]
__license__ = "MIT"
__date__ = "13/10/2016"


class FitActionsButtons(qt.QWidget):
    """Widget with 3 ``QPushButton``:

    The buttons can be accessed as public attributes::

        - ``EstimateButton``
        - ``StartFitButton``
        - ``DismissButton``

    You will typically need to access these attributes to connect the buttons
    to actions. For instance, if you have 3 functions ``estimate``,
    ``runfit`` and  ``dismiss``, you can connect them like this::

        >>> fit_actions_buttons = FitActionsButtons()
        >>> fit_actions_buttons.EstimateButton.clicked.connect(estimate)
        >>> fit_actions_buttons.StartFitButton.clicked.connect(runfit)
        >>> fit_actions_buttons.DismissButton.clicked.connect(dismiss)

    """

    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)

        self.resize(234, 53)

        grid_layout = qt.QGridLayout(self)
        grid_layout.setContentsMargins(11, 11, 11, 11)
        grid_layout.setSpacing(6)
        layout = qt.QHBoxLayout(None)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(6)

        self.EstimateButton = qt.QPushButton(self)
        self.EstimateButton.setText("Estimate")
        layout.addWidget(self.EstimateButton)
        spacer = qt.QSpacerItem(20, 20,
                                qt.QSizePolicy.Expanding,
                                qt.QSizePolicy.Minimum)
        layout.addItem(spacer)

        self.StartFitButton = qt.QPushButton(self)
        self.StartFitButton.setText("Start Fit")
        layout.addWidget(self.StartFitButton)
        spacer_2 = qt.QSpacerItem(20, 20,
                                  qt.QSizePolicy.Expanding,
                                  qt.QSizePolicy.Minimum)
        layout.addItem(spacer_2)

        self.DismissButton = qt.QPushButton(self)
        self.DismissButton.setText("Dismiss")
        layout.addWidget(self.DismissButton)

        grid_layout.addLayout(layout, 0, 0)


class FitStatusLines(qt.QWidget):
    """Widget with 2 greyed out write-only ``QLineEdit``.

    These text widgets can be accessed as public attributes::

        - ``StatusLine``
        - ``ChisqLine``

    You will typically need to access these widgets to update the displayed
    text::

        >>> fit_status_lines = FitStatusLines()
        >>> fit_status_lines.StatusLine.setText("Ready")
        >>> fit_status_lines.ChisqLine.setText("%6.2f" % 0.01)

    """

    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)

        self.resize(535, 47)

        layout = qt.QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(6)

        self.StatusLabel = qt.QLabel(self)
        self.StatusLabel.setText("Status:")
        layout.addWidget(self.StatusLabel)

        self.StatusLine = qt.QLineEdit(self)
        self.StatusLine.setText("Ready")
        self.StatusLine.setReadOnly(1)
        layout.addWidget(self.StatusLine)

        self.ChisqLabel = qt.QLabel(self)
        self.ChisqLabel.setText("Reduced chisq:")
        layout.addWidget(self.ChisqLabel)

        self.ChisqLine = qt.QLineEdit(self)
        self.ChisqLine.setMaximumSize(qt.QSize(16000, 32767))
        self.ChisqLine.setText("")
        self.ChisqLine.setReadOnly(1)
        layout.addWidget(self.ChisqLine)


class FitConfigWidget(qt.QWidget):
    """Widget whose purpose is to select a fit theory and a background
    theory, load a new fit theory definition file and provide
    a "Configure" button to open an advanced configuration dialog.

    This is used in :class:`silx.gui.fit.FitWidget.FitWidget`, to offer
    an interface to quickly modify the main parameters prior to running a fit:

      - select a fitting function through :attr:`FunComBox`
      - select a background function through :attr:`BkgComBox`
      - open a dialog for modifying advanced parameters through
        :attr:`FunConfigureButton`
    """
    def __init__(self, parent=None):
        qt.QWidget.__init__(self, parent)

        self.setWindowTitle("FitConfigGUI")

        layout = qt.QGridLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(6)

        self.FunLabel = qt.QLabel(self)
        self.FunLabel.setText("Function")
        layout.addWidget(self.FunLabel, 0, 0)

        self.FunComBox = qt.QComboBox(self)
        self.FunComBox.addItem("Add Function(s)")
        self.FunComBox.setItemData(self.FunComBox.findText("Add Function(s)"),
                                   "Load fit theories from a file",
                                   qt.Qt.ToolTipRole)
        layout.addWidget(self.FunComBox, 0, 1)

        self.BkgLabel = qt.QLabel(self)
        self.BkgLabel.setText("Background")
        layout.addWidget(self.BkgLabel, 1, 0)

        self.BkgComBox = qt.QComboBox(self)
        self.BkgComBox.addItem("Add Background(s)")
        self.BkgComBox.setItemData(self.BkgComBox.findText("Add Background(s)"),
                                   "Load background theories from a file",
                                   qt.Qt.ToolTipRole)
        layout.addWidget(self.BkgComBox, 1, 1)

        self.FunConfigureButton = qt.QPushButton(self)
        self.FunConfigureButton.setText("Configure")
        self.FunConfigureButton.setToolTip(
                "Open a configuration dialog for the selected function")
        layout.addWidget(self.FunConfigureButton, 0, 2)

        self.BgConfigureButton = qt.QPushButton(self)
        self.BgConfigureButton.setText("Configure")
        self.BgConfigureButton.setToolTip(
                "Open a configuration dialog for the selected background")
        layout.addWidget(self.BgConfigureButton, 1, 2)

        self.WeightCheckBox = qt.QCheckBox(self)
        self.WeightCheckBox.setText("Weighted fit")
        self.WeightCheckBox.setToolTip(
                "Enable usage of weights in the least-square problem.\n Use" +
                " the uncertainties (sigma) if provided, else use sqrt(y).")

        layout.addWidget(self.WeightCheckBox, 0, 3, 2, 1)

        layout.setColumnStretch(4, 1)


class ParametersTab(qt.QTabWidget):
    """This widget provides tabs to display and modify fit parameters. Each
    tab contains a table with fit data such as parameter names, estimated
    values, fit constraints, and final fit results.

    The usual way to initialize the table is to fill it with the fit
    parameters from a :class:`silx.math.fit.fitmanager.FitManager` object, after
    the estimation process or after the final fit.

    In the following example we use a :class:`ParametersTab` to display the
    results of two separate fits::

        from silx.math.fit import fittheories
        from silx.math.fit import fitmanager
        from silx.math.fit import functions
        from silx.gui import qt
        import numpy

        a = qt.QApplication([])

        # Create synthetic data
        x = numpy.arange(1000)
        y1 = functions.sum_gauss(x, 100, 400, 100)

        fit = fitmanager.FitManager(x=x, y=y1)

        fitfuns = fittheories.FitTheories()
        fit.addtheory(theory="Gaussian",
                      function=functions.sum_gauss,
                      parameters=("height", "peak center", "fwhm"),
                      estimate=fitfuns.estimate_height_position_fwhm)
        fit.settheory('Gaussian')
        fit.configure(PositiveFwhmFlag=True,
                      PositiveHeightAreaFlag=True,
                      AutoFwhm=True,)

        # Fit
        fit.estimate()
        fit.runfit()

        # Show first fit result in a tab in our widget
        w = ParametersTab()
        w.show()
        w.fillFromFit(fit.fit_results, view='Gaussians')

        # new synthetic data
        y2 = functions.sum_splitgauss(x,
                                  100, 400, 100, 40,
                                  10, 600, 50, 500,
                                  80, 850, 10, 50)
        fit.setData(x=x, y=y2)

        # Define new theory
        fit.addtheory(theory="Asymetric gaussian",
                      function=functions.sum_splitgauss,
                      parameters=("height", "peak center", "left fwhm", "right fwhm"),
                      estimate=fitfuns.estimate_splitgauss)
        fit.settheory('Asymetric gaussian')

        # Fit
        fit.estimate()
        fit.runfit()

        # Show first fit result in another tab in our widget
        w.fillFromFit(fit.fit_results, view='Asymetric gaussians')
        a.exec_()

    """

    def __init__(self, parent=None, name="FitParameters"):
        """

        :param parent: Parent widget
        :param name: Widget title
        """
        qt.QTabWidget.__init__(self, parent)
        self.setWindowTitle(name)
        self.setContentsMargins(0, 0, 0, 0)

        self.views = OrderedDict()
        """Dictionary of views. Keys are view names,
         items are :class:`Parameters` widgets"""

        self.latest_view = None
        """Name of latest view"""

        # the widgets/tables themselves
        self.tables = {}
        """Dictionary of :class:`silx.gui.fit.parameters.Parameters` objects.
        These objects store fit results
        """

        self.setContentsMargins(10, 10, 10, 10)

    def setView(self, view=None, fitresults=None):
        """Add or update a table. Fill it with data from a fit

        :param view: Tab name to be added or updated. If ``None``, use the
            latest view.
        :param fitresults: Fit data to be added to the table
        :raise: KeyError if no view name specified and no latest view
            available.
        """
        if view is None:
            if self.latest_view is not None:
                view = self.latest_view
            else:
                raise KeyError(
                    "No view available. You must specify a view" +
                    " name the first time you call this method."
                )

        if view in self.tables.keys():
            table = self.tables[view]
        else:
            # create the parameters instance
            self.tables[view] = Parameters(self)
            table = self.tables[view]
            self.views[view] = table
            self.addTab(table, str(view))

        if fitresults is not None:
            table.fillFromFit(fitresults)

        self.setCurrentWidget(self.views[view])
        self.latest_view = view

    def renameView(self, oldname=None, newname=None):
        """Rename a view (tab)

        :param oldname: Name of the view to be renamed
        :param newname: New name of the view"""
        error = 1
        if newname is not None:
            if newname not in self.views.keys():
                if oldname in self.views.keys():
                    parameterlist = self.tables[oldname].getFitResults()
                    self.setView(view=newname, fitresults=parameterlist)
                    self.removeView(oldname)
                    error = 0
        return error

    def fillFromFit(self, fitparameterslist, view=None):
        """Update a view with data from a fit (alias for :meth:`setView`)

        :param view: Tab name to be added or updated (default: latest view)
        :param fitparameterslist: Fit data to be added to the table
        """
        self.setView(view=view, fitresults=fitparameterslist)

    def getFitResults(self, name=None):
        """Call :meth:`getFitResults` for the
        :class:`silx.gui.fit.parameters.Parameters` corresponding to the
        latest table or to the named table (if ``name`` is not
        ``None``). This return a list of dictionaries in the format used by
        :class:`silx.math.fit.fitmanager.FitManager` to store fit parameter
        results.

        :param name: View name.
        """
        if name is None:
            name = self.latest_view
        return self.tables[name].getFitResults()

    def removeView(self, name):
        """Remove a view by name.

        :param name: View name.
        """
        if name in self.views:
            index = self.indexOf(self.tables[name])
            self.removeTab(index)
            index = self.indexOf(self.views[name])
            self.removeTab(index)
            del self.tables[name]
            del self.views[name]

    def removeAllViews(self, keep=None):
        """Remove all views, except the one specified (argument
        ``keep``)

        :param keep: Name of the view to be kept."""
        for view in self.tables:
            if view != keep:
                self.removeView(view)

    def getHtmlText(self, name=None):
        """Return the table data as HTML

        :param name: View name."""
        if name is None:
            name = self.latest_view
        table = self.tables[name]
        lemon = ("#%x%x%x" % (255, 250, 205)).upper()
        hcolor = ("#%x%x%x" % (230, 240, 249)).upper()
        text = ""
        text += "<nobr>"
        text += "<table>"
        text += "<tr>"
        ncols = table.columnCount()
        for l in range(ncols):
            text += ('<td align="left" bgcolor="%s"><b>' % hcolor)
            if QTVERSION < '4.0.0':
                text += (str(table.horizontalHeader().label(l)))
            else:
                text += (str(table.horizontalHeaderItem(l).text()))
            text += "</b></td>"
        text += "</tr>"
        nrows = table.rowCount()
        for r in range(nrows):
            text += "<tr>"
            item = table.item(r, 0)
            newtext = ""
            if item is not None:
                newtext = str(item.text())
            if len(newtext):
                color = "white"
                b = "<b>"
            else:
                b = ""
                color = lemon
            try:
                # MyQTable item has color defined
                cc = table.item(r, 0).color
                cc = ("#%x%x%x" % (cc.red(), cc.green(), cc.blue())).upper()
                color = cc
            except:
                pass
            for c in range(ncols):
                item = table.item(r, c)
                newtext = ""
                if item is not None:
                    newtext = str(item.text())
                if len(newtext):
                    finalcolor = color
                else:
                    finalcolor = "white"
                if c < 2:
                    text += ('<td align="left" bgcolor="%s">%s' %
                             (finalcolor, b))
                else:
                    text += ('<td align="right" bgcolor="%s">%s' %
                             (finalcolor, b))
                text += newtext
                if len(b):
                    text += "</td>"
                else:
                    text += "</b></td>"
            item = table.item(r, 0)
            newtext = ""
            if item is not None:
                newtext = str(item.text())
            if len(newtext):
                text += "</b>"
            text += "</tr>"
            text += "\n"
        text += "</table>"
        text += "</nobr>"
        return text

    def getText(self, name=None):
        """Return the table data as CSV formatted text, using tabulation
        characters as separators.

        :param name: View name."""
        if name is None:
            name = self.latest_view
        table = self.tables[name]
        text = ""
        ncols = table.columnCount()
        for l in range(ncols):
            text += (str(table.horizontalHeaderItem(l).text())) + "\t"
        text += "\n"
        nrows = table.rowCount()
        for r in range(nrows):
            for c in range(ncols):
                newtext = ""
                if c != 4:
                    item = table.item(r, c)
                    if item is not None:
                        newtext = str(item.text())
                else:
                    item = table.cellWidget(r, c)
                    if item is not None:
                        newtext = str(item.currentText())
                text += newtext + "\t"
            text += "\n"
        text += "\n"
        return text


def test():
    from silx.math.fit import fittheories
    from silx.math.fit import fitmanager
    from silx.math.fit import functions
    from silx.gui.plot.PlotWindow import PlotWindow
    import numpy

    a = qt.QApplication([])

    x = numpy.arange(1000)
    y1 = functions.sum_gauss(x, 100, 400, 100)

    fit = fitmanager.FitManager(x=x, y=y1)

    fitfuns = fittheories.FitTheories()
    fit.addtheory(name="Gaussian",
                  function=functions.sum_gauss,
                  parameters=("height", "peak center", "fwhm"),
                  estimate=fitfuns.estimate_height_position_fwhm)
    fit.settheory('Gaussian')
    fit.configure(PositiveFwhmFlag=True,
                  PositiveHeightAreaFlag=True,
                  AutoFwhm=True,)

    # Fit
    fit.estimate()
    fit.runfit()

    w = ParametersTab()
    w.show()
    w.fillFromFit(fit.fit_results, view='Gaussians')

    y2 = functions.sum_splitgauss(x,
                                  100, 400, 100, 40,
                                  10, 600, 50, 500,
                                  80, 850, 10, 50)
    fit.setdata(x=x, y=y2)

    # Define new theory
    fit.addtheory(name="Asymetric gaussian",
                  function=functions.sum_splitgauss,
                  parameters=("height", "peak center", "left fwhm", "right fwhm"),
                  estimate=fitfuns.estimate_splitgauss)
    fit.settheory('Asymetric gaussian')

    # Fit
    fit.estimate()
    fit.runfit()

    w.fillFromFit(fit.fit_results, view='Asymetric gaussians')

    # Plot
    pw = PlotWindow(control=True)
    pw.addCurve(x, y1, "Gaussians")
    pw.addCurve(x, y2, "Asymetric gaussians")
    pw.show()

    a.exec_()


if __name__ == "__main__":
    test()