non_reg_testing_XRS_raman_extraction.py 11.4 KB
Newer Older
mirone's avatar
mirone committed
1
2
3
4
5
6
7
from __future__ import print_function
#####
# this is important with old version of matplotlib  to avoid
# that matplotlib import pyqt4 while silx imports pyqt5
#
try:
    import PyQt4.QtCore
myron's avatar
myron committed
8
    print("   QT4 ")
mirone's avatar
mirone committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
except:
    pass

import os
import numpy as np
# from  pylab import  *
# ion()

import  XRStools
import  XRStools.ramanWidget
import  XRStools.ramanWidget.MainWindow

import XRStools.roiSelectionWidget

XRStools.roiSelectionWidget.SKIP_WARNING = True

myron's avatar
myron committed
25
26
save_path="results_non_reg_testing_XRS_raman_extraction"

27
28
29
30
def tosavepath( filename) :
    return os.path.join("results_non_reg_testing_XRS_raman_extraction",filename )


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

def check_results(fn_a, fn_b):
    a = np.loadtxt(fn_a)
    b = np.loadtxt(fn_b)
    amin = a[0, 0]
    amax = a[-1,0]
    bmin = b[0, 0]
    bmax = b[-1,0]
    c0 = max(amin,bmin)
    c1 = min(amax,bmax)
    try:
        assert(  abs( (  c1-c0    )    /(amax-amin)      )   >0.9 )
        assert(  abs( (  c1-c0    )    /(bmax-bmin)      )   >0.9 )
    except:
        print (  "  amin,amx, bmib, bmax, c0, c1 " ,amin,amx, bmib, bmax, c0, c1  ) 
        raise


    ref = np.interp( a[:,0],  b[:,0],  b[:,1]      )

myron's avatar
myron committed
51

52
    try:
myron's avatar
myron committed
53
        assert(    (abs( a[:,1] -  ref)[np.less( c0, a[:,0])* np.less(  a[:,0], c1)]   ).sum()  <1.0e-4    )
54
55
56
57
58
59
60
    except:
        raise





mirone's avatar
mirone committed
61
62
63
64
65
#####################################################################
# for programmatic testing
from silx.gui.utils.testutils import QTest, TestCaseQt
from silx.gui import qt

66
config_file = None
67
# config_file = "conf_raman.yaml"
mirone's avatar
mirone committed
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


def qWait( ms=None):
    if ms is None:
        ms = cls.DEFAULT_TIMEOUT_WAIT

    if qt.BINDING in ('PySide', 'PySide2'):
        # PySide has no qWait, provide a replacement
        timeout = int(ms)
        endTimeMS = int(time.time() * 1000) + timeout
        while timeout > 0:
            _qapp.processEvents(qt.QEventLoop.AllEvents,
                                maxtime=timeout)
            timeout = endTimeMS - int(time.time() * 1000)
    else:
        QTest.qWait(ms )

##
########################################################################

class Pippo(TestCaseQt):
    def runTest(self):
        pass

_qapp = qt.QApplication.instance() or qt.QApplication([])

wtest = XRStools.ramanWidget.MainWindow.main( manageQApp = False)
qWait( ms=10)

97
98
99
100
101
102
103
104
if config_file is  None:
    ntab = 0
    QTest.mouseClick(  wtest.tabWidget.tabBar(),
                       qt.Qt.LeftButton ,
                       pos=qt.QPoint(
                           wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                           wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
    )
mirone's avatar
mirone committed
105

106
    qWait( ms=40)
mirone's avatar
mirone committed
107

108
    wtest.rsw.LoadLocal( sf="/data/id20/inhouse/data/run5_17/run7_ihr/hydra", fn="/data/id20/inhouse/data/run5_17/run7_ihr/edf/hydra_12828.edf", ns=611)
mirone's avatar
mirone committed
109

110
    print( " PIPPO " )
mirone's avatar
mirone committed
111

112
    qWait( ms=40)
mirone's avatar
mirone committed
113

114
115
116
117
    for act in wtest.rsw.menuActions .actions():
        if  str(act.objectName()) == "actionGlobalSpotDetection":
            act.trigger()
    QTest.mouseClick(  wtest.rsw.globalSpotDectionWidget.detectionButton ,qt.Qt.LeftButton, pos= qt.QPoint(3,3))
mirone's avatar
mirone committed
118

119
    wtest.rsw.write_maskDict_to_hdf5(tosavepath("spatialroi.h5"))
mirone's avatar
mirone committed
120

121
    qWait( ms=40)
mirone's avatar
mirone committed
122
123


124
125
126
127
128
129
130
    ntab = 1
    QTest.mouseClick(  wtest.tabWidget.tabBar(),
                       qt.Qt.LeftButton ,
                       pos=qt.QPoint(
                           wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                           wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
    )
mirone's avatar
mirone committed
131

132
    qWait( ms=40)
mirone's avatar
mirone committed
133

134
135
    uinp =  wtest.sprsw.load_user_input
    print (uinp)
mirone's avatar
mirone committed
136
137


138
139
140
    # uinp["sf"] = "/data/id20/inhouse/data/run5_17/run7_ihr/" c'e' gia
    # uinp["roi"] = "spatialroi.h5:/datas/ROI" c'e' gia
    uinp["ns_s"] = [613,617,621,625]
mirone's avatar
mirone committed
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    wtest.sprsw.LoadLocalOption(uinp)

    qWait( ms=40)
    wtest.showMaximized()
    qWait( ms=40)

    for ntab in range( 1,7):
        QTest.mouseClick(  wtest.sprsw.viewsTab.tabBar(),
                           qt.Qt.LeftButton ,
                           pos=qt.QPoint(
                               wtest.sprsw.viewsTab.tabBar().tabRect(ntab).x()+wtest.sprsw.viewsTab.tabBar().tabRect(ntab).width()/2 ,
                               wtest.sprsw.viewsTab.tabBar().tabRect(ntab).y()+   wtest.sprsw.viewsTab.tabBar().tabRect(ntab).height()/2 )
        )

        qWait( ms=40)
        mw = wtest.sprsw.mws[ntab]
        mw.gc.nofcomps.setText("3")
        mw.gc.choosedcomp.setText("0")
        mw.gc.threshold.setText("0.2")
        qWait( ms=40)
myron's avatar
myron committed
162
        mw.gc.pushButton_calccomps.clicked.emit(True)
163
        qWait( ms=40)
myron's avatar
myron committed
164
165
        mw.gc.pushButton_calccomps.clicked.emit(True)
        mw.gc.pushButton_threshold.clicked.emit(True)
166
167
        qWait( ms=40)

168
    wtest.sprsw.write_maskDict_to_hdf5_option(tosavepath("sp_roi.h5"))
169
170

    QTest.qWait(40)
mirone's avatar
mirone committed
171

172
173
    ntab = 2
    QTest.mouseClick(  wtest.tabWidget.tabBar(),
174
175
                       qt.Qt.LeftButton ,
                       pos=qt.QPoint(
176
177
                           wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                           wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
178
179
180
    )

    qWait( ms=40)
181
182
else:
    wtest.loadConfigurationOption(config_file)
183

184
185
186
187
188
189
190
191
192
ntab = 3
QTest.mouseClick(  wtest.tabWidget.tabBar(),
                   qt.Qt.LeftButton ,
                   pos=qt.QPoint(
                       wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                       wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
)
qWait( ms=40)
myw = wtest.tabWidget.currentWidget().widget()
myron's avatar
myron committed
193
myw.plusLine.clicked.emit(True)
194
195
196
197
198
qWait( ms=40)
myw.abstract[0][1].setText("lowq")
l = myw.abstract[0][3:3+24]+ myw.abstract[0][36+3:36+3+24]
for t in l:
    t.cb.setChecked(True)
myron's avatar
myron committed
199
myw.plusLine.clicked.emit(True)
200
201
qWait( ms=40)
myw.abstract[1][1].setText("mediumq")
myron's avatar
myron committed
202
l = myw.abstract[1][3+24:3+24+11]+myw.abstract[1][3+24+12:3+24+24]
203
204
for t in l:
    t.cb.setChecked(True)
mirone's avatar
mirone committed
205

myron's avatar
myron committed
206
myw.plusLine.clicked.emit(True)
207
208
qWait( ms=40)
myw.abstract[2][1].setText("highq")
mirone's avatar
mirone committed
209

210
211
212
l = myw.abstract[2][3+60:3+60+12]
for t in l:
    t.cb.setChecked(True)
mirone's avatar
mirone committed
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
qWait( ms=40)
ntab = 4

QTest.mouseClick(  wtest.tabWidget.tabBar(),
                   qt.Qt.LeftButton ,
                   pos=qt.QPoint(
                       wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                       wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
)

qWait( ms=40)
myw = wtest.tabWidget.currentWidget().widget()
print(myw)

myron's avatar
myron committed
228
myw.plusLine.clicked.emit(True)
229
230
231
qWait( ms=40)
myw.abstract[0][1].setText("elastic")
qWait( ms=40)
myron's avatar
myron committed
232
myw.plusLine.clicked.emit(True)
233
234
235
qWait( ms=40)
myw.abstract[1][1].setText("ok0")
qWait( ms=40)
myron's avatar
myron committed
236
myw.plusLine.clicked.emit(True)
237
238
239
240
qWait( ms=40)
myw.abstract[2][1].setText("ok1")

qWait( ms=40)
myron's avatar
myron committed
241
myw.plusLine.clicked.emit(True)
242
243
244
245
246
qWait( ms=40)
myw.abstract[3][1].setText("ok2")


qWait( ms=40)
myron's avatar
myron committed
247
myw.plusLine.clicked.emit(True)
248
qWait( ms=40)
249
myw.abstract[4][1].setText("ok3")
250
251
252
253
254
255
256
257
qWait( ms=40)


fois = [4,1,4,4,4]
for row in range(5):
    for i in range(fois[row]):
        for k in myw.bottoni_info.keys():
            if myw.bottoni_info[k] == (+1,row):
myron's avatar
myron committed
258
                k.clicked.emit(True)
259
260
261
262
263
264
                qWait( ms=40)
                break
    for f in range(fois[row]):
        if row ==1 :
            myw.abstract[row][3+f].setValue(628)
        else:
265
            myw.abstract[row][3+f].setValue(611 +f*4 +row-(row>1))      
266
267


268
269
270
271
272
273
274
275
276
277
278
279
280
qWait( ms=40)


ntab = 5
QTest.mouseClick(  wtest.tabWidget.tabBar(),
                   qt.Qt.LeftButton ,
                   pos=qt.QPoint(
                       wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                       wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
)

qWait( ms=40)
myw = wtest.tabWidget.currentWidget()
myron's avatar
myron committed
281
myw.plusLine.clicked.emit(True)
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
qWait( ms=40)

print(myw.abstract)
myw.abstract[0][0].setText("H2O")
myw.abstract[0][0].editingFinished.emit()
qWait( ms=40)


abstr_stoichio, edges = myw.get_abstractN()

print("   abstr_stoichio      " , abstr_stoichio)
print("   edges      " , edges)

edges["O"]["K"]=1

myw.set_abstractN(abstr_stoichio, edges)

abstr_stoichio, edges
# myw.abstract[0][1].setText("lowq")
# l = myw.abstract[0][3:3+24]+ myw.abstract[0][36+3:36+3+24]
# for t in l:
#     t.cb.setChecked(True)
myron's avatar
myron committed
304
# myw.plusLine.clicked.emit(True)
305
306
307
308
309
310
# qWait( ms=40)
# myw.abstract[1][1].setText("mediumq")
# l = myw.abstract[1][3+24:3+24+24]
# for t in l:
#     t.cb.setChecked(True)

myron's avatar
myron committed
311
# myw.plusLine.clicked.emit(True)
312
313
314
# qWait( ms=40)
# myw.abstract[2][1].setText("highq")

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
qWait( ms=40)




ntab = 6
QTest.mouseClick(  wtest.tabWidget.tabBar(),
                   qt.Qt.LeftButton ,
                   pos=qt.QPoint(
                       wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                       wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
)

myw = wtest.tabWidget.currentWidget()

myw.spinBox_scan.setValue(611)
qWait( ms=40)


myw.pushButton.clicked.emit(True)

myron's avatar
myron committed
336
337
qWait( ms=40)

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


for ntab in [7,8,9] :
    QTest.mouseClick(  wtest.tabWidget.tabBar(),
                       qt.Qt.LeftButton ,
                       pos=qt.QPoint(
                           wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                           wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
    )

    myw = wtest.tabWidget.currentWidget()
    qWait( ms=40)
    rois = myw.plot.getCurvesRoiDockWidget().getRois()
    rois["range1"].setFrom(100.0)
    rois["range1"].setTo  (500.0)
    myw.lineEdit_hfshift .setText("-5.0")
    if ntab==9 :
        qWait( ms=40)   
        myw.pushButton_guess.clicked.emit(True)
        myw.lineEdit_A0.setText("300.0")
        myw.lineEdit_A0.editingFinished.emit()

    qWait( ms=40)    
    myw.pushButton_guess.clicked.emit(True)
    qWait( ms=40)
    rois["range2"].setFrom(550.0)
    rois["range2"].setTo  (594.0)
    myw.pushButton_fit.clicked.emit(True)
    rois["Output"].setFrom(457.0)
    rois["Output"].setTo  (577.0)
    rois["Norm"].setFrom(516.0)
    rois["Norm"].setTo  (553.0)
    qWait( ms=40)

ntab = 6
myron's avatar
myron committed
373
374
375
376
377
378
379
QTest.mouseClick(  wtest.tabWidget.tabBar(),
                   qt.Qt.LeftButton ,
                   pos=qt.QPoint(
                       wtest.tabWidget.tabBar().tabRect(ntab).x()+wtest.tabWidget.tabBar().tabRect(ntab).width()/2 ,
                       wtest.tabWidget.tabBar().tabRect(ntab).y()+   wtest.tabWidget.tabBar().tabRect(ntab).height()/2 )
)
myw = wtest.tabWidget.currentWidget()
380
myw.lineEdit_outputPrefix.setText(tosavepath("non_reg_output_gui_raman"))
381
myw.pushButton_saveAnalysis.clicked.emit(True)
myron's avatar
myron committed
382
383
qWait( ms=40)

mirone's avatar
mirone committed
384

385
386
387
check_results(tosavepath("non_reg_output_gui_raman_lowq.txt"),tosavepath("non_reg_output_gui_raman_reference_lowq.txt"))
check_results(tosavepath("non_reg_output_gui_raman_mediumq.txt"),tosavepath("non_reg_output_gui_raman_reference_mediumq.txt"))
check_results(tosavepath("non_reg_output_gui_raman_highq.txt"),tosavepath("non_reg_output_gui_raman_reference_highq.txt"))
388
print(" OK " )
389

myron's avatar
myron committed
390
if "CLEAN_RESULTS" in os.environ and  os.environ["CLEAN_RESULTS"] == "true" :
myron's avatar
myron committed
391
    os.system("rm %s/*" % save_path)