File: QtMcaAdvancedFitReport.py

package info (click to toggle)
pymca 5.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,392 kB
  • sloc: python: 155,456; ansic: 15,843; makefile: 116; sh: 73; xml: 55
file content (779 lines) | stat: -rw-r--r-- 36,714 bytes parent folder | download | duplicates (2)
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2019 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# 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.
#
#############################################################################*/
__author__ = "V. Armando Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import os
import sys
import time
MATPLOTLIB = True
from PyMca5.PyMcaGui import PyMcaQt as qt
QTVERSION = qt.qVersion()

#this is installation dependent I guess
from matplotlib import rcParams
from matplotlib import __version__ as matplotlib_version
#rcParams['numerix'] = "numeric"
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
MATPLOTLIB = True

from PyMca5.PyMcaIO import ConfigDict
from PyMca5.PyMcaCore import PyMcaLogo
from PyMca5.PyMcaPhysics.xrf import ConcentrationsTool
ConcentrationsConversion = ConcentrationsTool.ConcentrationsConversion

class QtMcaAdvancedFitReport:
    def __init__(self, fitfile = None, outfile = None, outdir = None,
                    sourcename = None,
                    selection = None,
                    fitresult = None,htmltext=None,
                    concentrations=None, table = None,
                    plotdict=None):

        self.concentrations = concentrations
        self.concentrationsConversion = ConcentrationsConversion()
        if table is None: table = 2
        self.tableFlag = table
        if fitfile is not None:
            #generate output from fit result file
            self.fitfile = fitfile
            self.outfile = outfile
            self.outdir  = outdir
            self.generateReportFromFitFile()
        else:
            #generate output from fitresult INCLUDING fit file
            self.fitfile = fitfile
            self.outfile = outfile
            self.outdir  = outdir
            self.sourcename=sourcename
            self.selection =selection
            self.fitresult =fitresult
            if self.outfile is None:
                if selection is not None:
                    self.outfile = selection
            if (self.outfile is None) or (self.outfile == 'Unknown Origin'):
                if sourcename is not None:
                    self.outfile = os.path.basename(sourcename)
        self.outfile = self.outfile.replace(" ","_")
        self.outfile = self.outfile.replace("/","_over_")
        self.graph = None
        if htmltext is None:
            htmltext={}
        self.otherhtmltext=htmltext
        if plotdict is None:
            self.plotDict = {'logy':None,
                             'xmin':None,
                             'xmax':None,
                             'ymin':None,
                             'ymax':None}
        else:
            self.plotDict = plotdict

    def writeReport(self,text=None):
        if len(self.outfile) > 5:
            if self.outfile[-5:] != ".html":
                outfile = os.path.join(self.outdir, self.outfile+".html")
            else:
                outfile = os.path.join(self.outdir, self.outfile)
        else:
            outfile = os.path.join(self.outdir, self.outfile+".html")
        try:
            os.remove(outfile)
        except:
            pass
        concentrationsfile = outfile[:-5]+"_concentrations.txt"
        try:
            os.remove(concentrationsfile)
        except:
            pass
        if text is None:
            text = self.getText()
        f=open(outfile,"w")
        f.write(text)
        f.close()
        if len(self._concentrationsTextASCII) > 1:
             f=open(concentrationsfile, "w")
             f.write(self._concentrationsTextASCII)
             f.close()
        return outfile

    def generateReportFromFitFile(self):
        d=ConfigDict.ConfigDict()
        d.read(self.fitfile)
        sourcename = "Unknown Source"
        selection  = "Unknown Selection"
        if 'info' in d:
            if 'key' in d['info']:
                selection=d['info']['key']
            elif 'Key' in d['info']:
                selection=d['info']['Key']
            for key in d['info'].keys():
                if key.upper() == 'SOURCENAME':
                    sourcename = d['info'][key]
                elif (key.upper() == 'SELECTION') or\
                     (key.upper() == 'LEGEND'):
                    selection = d['info'][key]
        self.sourcename = sourcename
        self.selection  = selection
        if self.outfile is None:
            if  self.outdir is None:
                self.outdir = os.getcwd()
            self.outfile= os.path.basename(self.fitfile)
        else:
            if self.outdir is None:
                self.outdir = os.path.dirname(self.outfile)
            self.outfile= os.path.basename(self.outfile)
        if self.outdir == '':self.outdir = "."
        self.fitresult=d
        if 'concentrations' in d:
            self.concentrations = d['concentrations']

    def getText(self):
        newlinks = []
        for key in self.otherhtmltext.keys():
            newlinks.append(["#%s" % (key),"%s" % key])
        text =self.getHeader(newlinks)
        text+=self.getInfo()
        text+=self.getImage()
        text+=self.getParam()
        text+=self.getConcentrations()
        self._concentrationsTextASCII = self.getConcentrationsASCII()
        text+=self.getResult()
        for key in self.otherhtmltext.keys():
             text+="\n"
             text+= "<H2><a NAME=""%s""></a><FONT color=#009999>" % key
             text+= "%s:" % key
             text+= "</FONT></H2>"
             text+= self.otherhtmltext[key]
             text+="<br>"
        text+=self.getFooter()
        return text

    def getHeader(self,addlink=None):
        link = [ ['http://pymca.sourceforge.net', 'PyMCA home'],
                 ['http://www.esrf.fr', 'ESRF home'],
                 ['http://www.esrf.fr/UsersAndScience/Experiments/TBS/BLISS', 'BLISS home']]
        if self.concentrations is not None:
            link.append(['#Concentrations', 'Concentrations'])
        if self.tableFlag:link.append(['#Fit_Peak_Results', 'Fit Peak Results'])
        if addlink is not None:
            for item in addlink:
                link.append(item)
        text =""
        text+= "<HTML>"
        text+= "<HEAD>"
        text+= "<TITLE>PyMCA : Advanced Fit Results</TITLE>"
        text+= "</HEAD>"
        text+= "<BODY TEXT=#000000 BGCOLOR=#FFFFFF ALINK=#ff6600 LINK=#0000cc VLINK=#0000cc marginwidth=10 marginheight=10  topmargin=10 leftmargin=10>"
        text+= "<CENTER>"
        text+= "<TABLE WIDTH=100%% border=0 Height=70>"
        text+= "  <TR>"
        text+= "    <TD><Font Size=5 Color=#0000cc>"
        text+= "        <b>PyMCA : Advanced Fit Results</b></Font>"
        text+= "    </td>"
        text+= "    <td rowspan=2 ALIGN=RIGHT VALIGN=bottom>"
        text+= "        <a HREF=""http://www.esrf.fr/"">"
        logofile = self.outdir + "/" + "PyMcaLogo.png"
        if not os.path.exists(logofile):
            pixmap = qt.QPixmap(PyMcaLogo.PyMcaLogo)
            pixmap.save(logofile,"PNG")
        text+= "        <img SRC=%s ALT=""ESRF home"" WIDTH=55 HEIGHT=68 BORDER=0></a>" % "PyMcaLogo.png"
        text+= "    </td>"
        text+= "  </tr>"
        text+= "  <tr>"
        text+= "     <td width=100%%  VALIGN=bottom>"
        text+= "        <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%%>"
        text+= "          <TR>"
        text+= "            <TD WIDTH=100%% BGCOLOR=#ee22aa HEIGHT=17  ALIGN=LEFT VALIGN=middle>"
        text+= "            <FONT color=#000000>&nbsp;"
        for name in link:
            text+= "|&nbsp;&nbsp;<A STYLE=""color: #FFFFFF"" HREF=""%s"">%s</a>&nbsp;&nbsp;"%(tuple(name))
        text+= "            </FONT>"
        text+= "            </TD>"
        text+= "          </TR>"
        text+= "        </TABLE>"
        text+= "     </td>"
        text+= "  </tr>"
        text+= "  <tr>"
        text+= "     <td colspan=2 height=5><spacer type=block height=10 width=0>"
        text+= "     </td>"
        text+= "  </tr>"
        text+= "</table>"
        text+= "</center>"
        return text

    def getInfo(self):
        text =""
        text+= "<nobr><H2><FONT color=#0000cc>"
        text+= "Computed File :&nbsp;"
        text+= "</FONT>"
        text+= "<FONT color=#000000>"
        if self.fitfile is not None:
            if os.path.basename(self.fitfile) == self.fitfile:
                text+= "<b><I>%s</I></b>" % (os.getcwd()+"/"+self.fitfile)
            else:
                text+= "<b><I>%s</I></b>" % (self.fitfile)
        else:
            text+= "<b><I>%s</I></b>" % (self.outdir+"/"+self.outfile+".fit")
            #and I have to generate it!!!!!!!!!!!!"
            d=ConfigDict.ConfigDict(self.fitresult)
            try:
                os.remove(self.outdir+"/"+self.outfile+".fit")
            except:
                pass
            if self.concentrations is not None:
                d['concentrations'] = self.concentrations
            d.write(self.outdir+"/"+self.outfile+".fit")
        text+= "</FONT>"
        text+= "</H2>"
        text+= "</nobr>"
        text+= "<LEFT>"
        text+= "<TABLE border=0>"
        text+= "<TR><TD><SPACER TYPE=BLOCK WIDTH=50></TD><TD>"
        text+= "<TABLE border=0 cellpadding=1 cellspacing=2>"
        text+= "  <TR><TH ALIGN=LEFT>Source : &nbsp;</TH><TD ALIGN=LEFT>%s</TD></TR>"    % (self.sourcename)
        text+= "  <TR><TH ALIGN=LEFT>Selection : &nbsp;</TH><TD ALIGN=LEFT>%s</TD></TR>" % (self.selection)
        text+= "  <TR><TH ALIGN=LEFT>Parameters : &nbsp;</TH><TD ALIGN=LEFT>"
        d=ConfigDict.ConfigDict(self.fitresult['result']['config'])
        try:
            os.remove(self.outdir+"/"+self.outfile+".txt")
        except:
            pass
        d.write(self.outdir+"/"+self.outfile+".txt")
        text+= "<a HREF=""%s"">%s</a>"% (self.outfile+".txt",self.outfile+".txt")
        text+="</TD></TR>"

        """
        text+= "  <TR><TH ALIGN=RIGHT>Source : </TH><TD ALIGN=LEFT>%s</TD>"%(self.sourcename)
        text+= "  <TH ALIGN=RIGHT>Selection : </TH><TD ALIGN=LEFT>%s</TD></TR>"%(self.selection)
        keys= [ key for key in info.keys() if key not in ['paramfile', 'peakfile'] ]
        for idx in range(0, len(keys), 2):
            text+= "  <TR><TH ALIGN=RIGHT>%s : </TH><TD ALIGN=LEFT>%s</TD>"%(keys[idx], info[keys[idx]])
            if idx+1<len(keys):
                text+= "  <TH ALIGN=RIGHT>%s : </TH><TD ALIGN=LEFT>%s</TD></TR>"%(keys[idx+1], info[keys[idx+1]])
            else:
                text+= "  <TD COLSPAN=2></TD></TR>"
        """
        text+= "</TABLE>"
        text+= "</TD></TR></TABLE>"
        text+= "</LEFT>"
        return text


    def getParam(self):
        text=""
        zero = self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Zero')]
        gain = self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Gain')]
        noise= self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Noise')]
        fano = self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Fano')]
        sum  = self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Sum')]
        stdzero = self.fitresult['result']['sigmapar'][self.fitresult['result']['parameters'].index('Zero')]
        stdgain = self.fitresult['result']['sigmapar'][self.fitresult['result']['parameters'].index('Gain')]
        stdnoise= self.fitresult['result']['sigmapar'][self.fitresult['result']['parameters'].index('Noise')]
        stdfano = self.fitresult['result']['sigmapar'][self.fitresult['result']['parameters'].index('Fano')]
        stdsum  = self.fitresult['result']['sigmapar'][self.fitresult['result']['parameters'].index('Sum')]

        hypermetflag = self.fitresult['result']['config']['fit']['hypermetflag']
        if not ('fitfunction' in self.fitresult['result']['config']['fit']):
            if hypermetflag:
                self.fitresult['result']['config']['fit']['fitfunction'] = 0
            else:
                self.fitresult['result']['config']['fit']['fitfunction'] = 1
        if self.fitresult['result']['config']['fit']['fitfunction'] or\
           (hypermetflag != 1):
            #the peaks are not pure gaussians
            if self.fitresult['result']['config']['fit']['fitfunction']:
                #peaks are pseudo-voigt functions
                hypermetnames = ['Eta Factor']
            else:
                hypermetnames = ['ST AreaR', 'ST SlopeR',
                                 'LT AreaR', 'LT SlopeR',
                                 'STEP HeightR']
            hypermetvalues=[]
            hypermetstd   =[]
            hypermetfinalnames = []
            for name in hypermetnames:
                if name in self.fitresult['result']['parameters']:
                    hypermetvalues.append(self.fitresult['result']['fittedpar'] \
                            [self.fitresult['result']['parameters'].index(name)])
                    hypermetstd.append(self.fitresult['result']['sigmapar'] \
                            [self.fitresult['result']['parameters'].index(name)])
                    hypermetfinalnames.append(name)

        # --- html table
        text+="<H2><FONT color=#009999>"
        text+="Fit Parameters :"
        text+="</FONT></H2>"
        text+="<CENTER>"
        text+="<TABLE border=0 cellpadding=0 cellspacing=2 width=80%>"
        text+="<TR>"
        text+="    <TD><TABLE border=1 cellpadding=1 cellspacing=0 width=100%>"
        text+="        <TR align=center>"
        text+="            <TH colspan=2>FIT parameters</TH>"
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Region of Fit</I></TD>"
        text+="            <TD>&nbsp;%d - %d</TD>" % (self.fitresult['result']['config']['fit']['xmin'],self.fitresult['result']['config']['fit']['xmax'])
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Number of iterations</I></TD>"
        #text+="            <TD>&nbsp;%d</TD>" % (fitpar['fit_numiter'])
        text+="            <TD>&nbsp;%d</TD>" % (self.fitresult['result']['niter'])
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Chi square</I></TD>"
        #text+="            <TD>&nbsp;%.4f</TD>" % (fitpar['fit_chi'])
        text+="            <TD>&nbsp;%.4f</TD>" % (self.fitresult['result']['chisq'])
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Last Chi square difference</I></TD>"
        #text+="            <TD>&nbsp;%.4f %%</TD>" % (fitpar['fit_lastchi'])
        text+="            <TD>&nbsp;%.4f %%</TD>" % (self.fitresult['result']['lastdeltachi']*100)
        text+="        </TR>"
        text+="        </TABLE>"
        text+="    </TD>"
        text+="</TR>"
        text+="<TR>"
        text+="    <TD><TABLE border=1 cellpadding=1 cellspacing=0 width=100%>"
        text+="        <TR align=center>"
        text+="            <TH colspan=2>Calibration parameters</TH>"
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Zero</I></TD>"
        text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (zero, stdzero)
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Gain</I></TD>"
        text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (gain, stdgain)
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Noise</I></TD>"
        text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (noise, stdnoise)
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Fano</I></TD>"
        text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (fano, stdfano)
        text+="        </TR>"
        text+="        <TR align=left>"
        text+="            <TD><I>&nbsp;Sum</I></TD>"
        text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (sum, stdsum)
        text+="        </TR>"
        text+="        </TABLE>"
        text+="    </TD>"
        text+="</TR>"

        # --- Peak shape parameters ---
        if hypermetflag != 1:
            text+="<TR>"
            text+="    <TD><TABLE border=1 cellpadding=1 cellspacing=0 width=100%>"
            text+="        <TR align=center>"
            text+="            <TH colspan=2>Peak shape parameters</TH>"
            text+="        </TR>"
            for i in range(len(hypermetfinalnames)):
                text+="        <TR align=left>"
                text+="            <TD><I>&nbsp;%s</I></TD>" % hypermetnames[i]
                text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (hypermetvalues[i],
                                                                      hypermetstd[i])
                text+="        </TR>"
            text+="        </TABLE>"
            text+="    </TD>"
            text+="</TR>"



        # --- Continuum parameters ---
        text+="<TR>"
        text+="    <TD><TABLE border=1 cellpadding=1 cellspacing=0 width=100%>"
        text+="        <TR align=center>"
        text+="            <TH colspan=2>Continuum parameters</TH>"
        text+="        </TR>"
        # Stripping
        if self.fitresult['result']['config']['fit']['stripflag']:
             constant    = 1.0
             iterations = 20000
             stripwidth = 1
             stripfilterwidth = 1
             stripalgorithm = 0
             snipwidth = 30
             if 'stripalgorithm' in self.fitresult['result']['config']['fit']:
                stripalgorithm=self.fitresult['result']['config']['fit']['stripalgorithm']
             if 'snipwidth' in self.fitresult['result']['config']['fit']:
                snipwidth=self.fitresult['result']['config']['fit']['snipwidth']
             if 'stripconstant' in self.fitresult['result']['config']['fit']:
                constant=self.fitresult['result']['config']['fit']['stripconstant']
             if 'stripiterations' in self.fitresult['result']['config']['fit']:
                iterations=self.fitresult['result']['config']['fit']['stripiterations']
             if 'stripwidth' in self.fitresult['result']['config']['fit']:
                stripwidth=self.fitresult['result']['config']['fit']['stripwidth']
             if 'stripfilterwidth' in self.fitresult['result']['config']['fit']:
                stripfilterwidth=self.fitresult['result']['config']['fit']['stripfilterwidth']
             if stripalgorithm == 1:
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;Type</I></TD>"
                 text+="            <TD>&nbsp;%s</TD>" % "SNIP Background"
                 text+="        </TR>"
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;%s<I></TD>" % "SNIP width"
                 text+="            <TD>&nbsp;%.5f</TD>" % snipwidth
                 text+="        </TR>"
             else:
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;Type</I></TD>"
                 text+="            <TD>&nbsp;%s</TD>" % "Strip Background"
                 text+="        </TR>"
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;%s<I></TD>" % "Strip Constant"
                 text+="            <TD>&nbsp;%.5f</TD>" % constant
                 text+="        </TR>"
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;%s<I></TD>" % "Strip Iterations"
                 text+="            <TD>&nbsp;%d</TD>" % iterations
                 text+="        </TR>"
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;%s<I></TD>" % "Strip Width"
                 text+="            <TD>&nbsp;%d</TD>" % stripwidth
                 text+="        </TR>"
             text+="        <TR align=left>"
             text+="            <TD><I>&nbsp;%s<I></TD>" % "Smoothing Filter Width"
             text+="            <TD>&nbsp;%d</TD>" % stripfilterwidth
             text+="        </TR>"
             stripanchorslist = []
             stripanchorsflag = self.fitresult['result']['config']['fit'].get('stripanchorsflag', 0)
             if stripanchorsflag:
                 stripanchorslist = self.fitresult['result']['config']['fit'].get('stripanchorslist', [])
             i = 0
             for anchor in stripanchorslist:
                 if anchor != 0:
                     text+="        <TR align=left>"
                     text+="            <TD><I>&nbsp;%s%d<I></TD>" % ("Anchor",i)
                     text+="            <TD>&nbsp;%d</TD>" % anchor
                     text+="        </TR>"
                     i += 1

        # --- Background Function
        if self.fitresult['result']['config']['fit']['continuum']:
             text+="        <TR align=left>"
             text+="            <TD><I>&nbsp;Type</I></TD>"
             if 'continuum_name' in self.fitresult['result']['config']['fit']:
                name = self.fitresult['result']['config']['fit']['continuum_name']
                text+="            <TD>&nbsp;%s</TD>" % name
             elif self.fitresult['result']['config']['fit']['continuum'] == 1:
                text+="            <TD>&nbsp;%s</TD>" % "Constant Polymomial"
             elif self.fitresult['result']['config']['fit']['continuum'] == 2:
                text+="            <TD>&nbsp;%s</TD>" % "1st Order Polymomial"
             elif self.fitresult['result']['config']['fit']['continuum'] == 3:
                text+="            <TD>&nbsp;%s</TD>" % "2nd Order Polymomial"
             else:
                #compatibility with previous versions
                text+="            <TD>&nbsp;%s</TD>" % "1st Order Polymomial"
             text+="        </TR>"
             isum = self.fitresult['result']['parameters'].index('Sum')
             a=0
             if hypermetflag:a=5
             nglobal = len(self.fitresult['result']['parameters']) - len(self.fitresult['result']['groups'])
             for i in range(isum+1,nglobal-a):
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;%s<I></TD>" % self.fitresult['result']['parameters'][i]
                 value    = self.fitresult['result']['fittedpar'][i]
                 stdvalue = self.fitresult['result']['sigmapar'] [i]
                 text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (value, stdvalue)
                 text+="        </TR>"
             if 0:
                 text+="        <TR align=left>"
                 text+="            <TD><I>&nbsp;%s<I></TD>" % 'Constant'
                 value    = self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Constant')]
                 stdvalue = self.fitresult['result']['sigmapar'] [self.fitresult['result']['parameters'].index('Constant')]
                 text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (value, stdvalue)
                 text+="        </TR>"
                 if self.fitresult['result']['config']['fit']['continuum'] > 1:
                      text+="        <TR align=left>"
                      text+="            <TD><I>&nbsp;%s<I></TD>" % 'Slope'
                      value    = self.fitresult['result']['fittedpar'][self.fitresult['result']['parameters'].index('Constant')+1]
                      stdvalue = self.fitresult['result']['sigmapar'] [self.fitresult['result']['parameters'].index('Constant')+1]
                      text+="            <TD>&nbsp;% .5E +/- % .5E</TD>" % (value, stdvalue)
                      text+="        </TR>"
             text+="</TR>"
        text+="        </TABLE>"
        text+="    </TD>"
        text+="</TR>"
        if 0:
            #not yet implemented
            text+="<TR>"
            text+="    <TD align=center>"
            text+="         <I>FIT END STATUS : </I>%s<BR>"% "STATUS"
            text+="         <B>%s</B>" % "MESSAGE"
            text+="    </TD>"
            text+="</TR>"
        text+="</TABLE>"
        text+="</CENTER>"
        return text

    def getFooter(self):
        now = time.time()
        text =""
        text+= "<center>"
        text+= "<table width=100%% border=0 cellspacing=0 cellpadding=0>"
        text+= "    <tr><td colspan=2 height=10><spacer type=block height=10 width=0></td></tr>"
        text+= "    <tr><td colspan=2 bgcolor=#cc0066 height=5><spacer type=block height=5 width=0></td></tr>"
        text+= "    <tr><td colspan=2 height=5><spacer type=block height=5 width=0></td></tr>"
        text+= "    <TR>"
        text+= "        <TD><FONT size=1 >created:  %s</font></TD>" % time.ctime(now)
        #text+= "        <TD ALIGN=RIGHT><FONT size=1 >last modified: %s" % time.ctime(now)
        text+= "        <TD ALIGN=RIGHT><FONT size=1 >last modified: %s by" % time.ctime(now)
        #text+= "        <A STYLE=""color: #0000cc"" HREF=""mailto:papillon@esrf.fr"">papillon@esrf.fr</A></FONT></TD>"
        if sys.platform == 'win32':
            try:
                user = os.getenv('USERNAME')
                text+= "        <A STYLE=""color: #0000cc"">%s</A></FONT></TD>" % user
            except:
                text +="</FONT></TD>"
        else:
            try:
                user = os.getenv("USER")
                text+= "        <A STYLE=""color: #0000cc"">%s</A></FONT></TD>" % user
            except:
                text +="</FONT></TD>"
        text+= "    </TR>"
        text+= "</TABLE>"
        text+= "</center>"
        text+= "</BODY>"
        text+= "</HTML>"
        return text

    def __getFitImage(self,imagefile=None):
        if imagefile is None:imagefile=self.outdir+"/"+self.outfile+".png"
        filelink = "%s" % imagefile
        text = ""
        text+= "<H2><FONT color=#009999>"
        text+= "Spectrum, Continuum and Fitted values :"
        text+= "</FONT></H2>"
        text+= "<CENTER>"
        text+= "<IMG SRC=%s ALT=""fit graph"" ALIGN=center>"%filelink
        text+= "</CENTER>"
        return text

    def getImage(self):
        ddict=self.fitresult
        try:
            fig = Figure(figsize=(6,3)) # in inches
            canvas = FigureCanvas(fig)
            ax = fig.add_axes([.15, .15, .8, .8])
            ax.set_axisbelow(True)
            logplot = self.plotDict.get('logy', True)
            if logplot is None:
                if (ddict['result']['ydata'].max() - ddict['result']['ydata'].min()) < 200:
                    logplot = False
                elif ddict['result']['yfit'].min() < 0.01:
                    logplot = False
                else:
                    logplot = True
            if logplot:
                axplot = ax.semilogy
            else:
                axplot = ax.plot
            axplot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5)
            axplot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5)
            legendlist = ['spectrum', 'continuum', 'fit']
            axplot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5)
            fontproperties = FontProperties(size=8)
            if ddict['result']['config']['fit']['sumflag']:
                axplot(ddict['result']['energy'],
                       ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5)
                legendlist.append('pileup')
            if matplotlib_version < '0.99.0':
                legend = ax.legend(legendlist,0,
                                   prop = fontproperties, labelsep=0.02)
            elif matplotlib_version < '1.5':
                legend = ax.legend(legendlist,0,
                                   prop = fontproperties, labelspacing=0.02)
            else:
                legend = ax.legend(legendlist, loc=0,
                                   prop = fontproperties, labelspacing=0.02)
        except ValueError:
            # It seems this error is not caught with matplotlib 2.2.4 and the
            # report crashes instead of switching to a linear plot
            fig = Figure(figsize=(6,3)) # in inches
            canvas = FigureCanvas(fig)
            ax = fig.add_axes([.15, .15, .8, .8])
            ax.set_axisbelow(True)
            ax.plot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5)
            ax.plot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5)
            legendlist = ['spectrum', 'continuum', 'fit']
            ax.plot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5)
            fontproperties = FontProperties(size=8)
            if ddict['result']['config']['fit']['sumflag']:
                ax.plot(ddict['result']['energy'],
                            ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5)
                legendlist.append('pileup')
            if matplotlib_version < '0.99.0':
                legend = ax.legend(legendlist,0,
                               prop = fontproperties, labelsep=0.02)
            elif matplotlib_version < '1.5':
                legend = ax.legend(legendlist,0,
                               prop = fontproperties, labelspacing=0.02)
            else:
                legend = ax.legend(legendlist, loc=0,
                               prop = fontproperties, labelspacing=0.02)

        ax.set_xlabel('Energy')
        ax.set_ylabel('Counts')
        legend.draw_frame(False)

        outfile = self.outdir+"/"+self.outfile+".png"
        try:
            os.remove(outfile)
        except:
            pass

        canvas.print_figure(outfile)
        return self.__getFitImage(self.outfile+".png")

    def getConcentrations(self):
        return self.concentrationsConversion.getConcentrationsAsHtml(\
                                                self.concentrations)

    def getConcentrationsASCII(self):
        return self.concentrationsConversion.getConcentrationsAsAscii(\
                                                self.concentrations)

    def getResult(self):
        text = ""
        if self.tableFlag == 0:
            return text
        text+="\n"
        text+= "<H2><a NAME=""%s""></a><FONT color=#009999>" % 'Fit_Peak_Results'
        text+= "%s:" % 'Fit Peak Results'
        text+= "</FONT></H2>"
        text+="<br>"
        result = self.fitresult['result']
        if self.tableFlag == 1:
            labels=['Element','Group','Fit&nbsp; Area','Sigma']
        else:
            labels=['Element','Group','Fit&nbsp; Area','Sigma','Energy','Ratio','FWHM','Chi&nbsp; square']
        lemmon = ("#%x%x%x" % (255,250,205)).upper()
        hcolor = ("#%x%x%x" % (230,240,249)).upper()
        text += "<CENTER>"
        text += ("<nobr>")
        text += '<table width="80%" border="0" cellspacing="1" cellpadding="1" >'
        text += ( "<tr><b>")
        for l in range(len(labels)):
            if l < 2:
                text += '<td align="left" bgcolor=%s><b>%s</b></td>' % (hcolor,labels[l])
            elif (l > 3) or (self.tableFlag == 1):
                text += '<td align="right" bgcolor=%s><b>%s</b></td>' % (hcolor,labels[l])
            else:
                text += '<td align="center" bgcolor=%s><b>%s</b></td>' % (hcolor,labels[l])
        text+="</b></tr>\n"

        for group in result['groups']:
            text+=("<tr>")
            ele,group0 = group.split()
            text += '<td align="left"><b>%s</b></td>' % ele
            text += '<td align="left"><b>%s</b></td>' % group0
            fitarea    = "%.6e" % result[group]['fitarea']
            sigmaarea  = "%.2e" % result[group]['sigmaarea']
            text += '<td align="right"><b>%s</b></td>' % fitarea
            text += '<td align="right"><b>%s</b></td>' % sigmaarea
            text += '<td align="right"><b>&nbsp;</b></td>'
            text += '<td align="right"><b>&nbsp;</b></td>'
            text += '<td align="right"><b>&nbsp;</b></td>'
            text += '<td align="right"><b>&nbsp;</b></td>'
            text += '</tr>\n'
            if type(result[group]['peaks']) != type([]):
                iterator = [result[group]['peaks']]
            else:
                iterator = 1 * result[group]['peaks']
            if self.tableFlag == 1:
                iterator = []
            for peak in iterator:
                text += '<tr><td>&nbsp;</td>'
                name  = peak
                energy = ("%.3f" % (result[group][peak]['energy']))
                ratio  = ("%.5f" % (result[group][peak]['ratio']))
                area   = ("%.6e" % (result[group][peak]['fitarea']))
                sigma  = ("%.2e" % (result[group][peak]['sigmaarea']))
                fwhm   = ("%.3f" % (result[group][peak]['fwhm']))
                chisq  = ("%.2f" % (result[group][peak]['chisq']))
                fields = [name,area,sigma,energy,ratio,fwhm,chisq]
                for field in fields:
                    if field == name:
                        text+=('<td align="left"  bgcolor=%s>%s</td>' % (lemmon,field))
                    else:
                        text+=('<td align="right" bgcolor=%s>%s</td>' % (lemmon,field))
                text+="</tr>\n"
            if type(result[group]['escapepeaks']) != type([]):
                iterator = [result[group]['escapepeaks']]
            else:
                iterator = 1 * result[group]['escapepeaks']
            if self.tableFlag == 1:
                iterator = []
            for peak0 in iterator:
                name  = peak0+"esc"
                peak  = peak0+"esc"
                if result[group][name]['ratio'] > 0.0:
                    text += '<tr><td></td>'
                    energy = ("%.3f" % (result[group][peak]['energy']))
                    ratio  = ("%.5f" % (result[group][peak]['ratio']))
                    area   = ("%.6e" % (result[group][peak]['fitarea']))
                    sigma  = ("%.2e" % (result[group][peak]['sigmaarea']))
                    fwhm   = ("%.3f" % (result[group][peak]['fwhm']))
                    chisq  = ("%.2f" % (result[group][peak]['chisq']))
                    fields = [name,area,sigma,energy,ratio,fwhm,chisq]
                    for field in fields:
                        if field == name:
                            text+=('<td align="left"  bgcolor=%s>%s</td>' % (lemmon,field))
                        else:
                            text+=('<td align="right" bgcolor=%s>%s</td>' % (lemmon,field))
                    text+="</tr>\n"
        text+=("</table>")
        text+=("</nobr>")
        text+="</CENTER>"
        return text

def generateoutput(fitfile,outfile=None):
    report = QtMcaAdvancedFitReport(fitfile, outfile)
    report.writeReport()

if __name__ == "__main__":
    if len(sys.argv) <2 :
        print("Usage: %s Input_Fit_Result_File [optional_output_file]" %\
              sys.argv[0])
        sys.exit(1)
    app = qt.QApplication(sys.argv)
    fitfile=sys.argv[1]
    if len(sys.argv) > 2:
        outfile = sys.argv[2]
    else:
        outfile = None
    generateoutput(fitfile,outfile)
    app.quit()