File: compara.py

package info (click to toggle)
pyhst2 2020c-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 12,532 kB
  • sloc: ansic: 11,807; python: 9,663; cpp: 6,786; makefile: 147; sh: 31
file content (294 lines) | stat: -rw-r--r-- 8,294 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
import matplotlib
matplotlib.use('Qt4Agg')
from pylab import *
from matplotlib.colors import LogNorm
from matplotlib.colors import LinearSegmentedColormap, LogNorm, Normalize
import numpy as np
from  PyQt4 import Qt, QtCore
from PyQt4.QtCore import SIGNAL, SLOT

referenceprefix  ="/data/scisofttmp/mirone/TEST_PYHST/RESULTS/2018bb_corrected/2gpu/tests/"

newversionprefix ="/data/scisofttmp/mirone/TEST_PYHST/RESULTS/2018bb_corrected/1gpu_fix/tests/"

# referenceprefix="/data/scisofttmp/mirone/TEST_PYHST/RESULTS/2018bb/2gpu_bis/tests/"




# /data/scisofttmp/mirone/TEST_PYHST/RESULTS/2015d/gpu2-0102/tests"

# newversionprefix ="/data/scisofttmp/mirone/TEST_PYHST/RESULTS/2017c_final/2gpu_bis/tests/MULTIPAGANIN/"
# referenceprefix="/data/scisofttmp/mirone/TEST_PYHST/RESULTS/2015d/gpu2-0102/tests/SIRT_LENA/"
# referenceprefix ="/data/scisofttmp/mirone/TEST_PYHST/RESULTS/2017c_final/1gpu_bis/tests/MULTIPAGANIN/"

import numpy
from mpi4py import MPI
myrank = MPI.COMM_WORLD.Get_rank()
nprocs = MPI.COMM_WORLD.Get_size()
procnm = MPI.Get_processor_name()
comm = MPI.COMM_WORLD
print "MPI LOADED , nprocs = ", nprocs

def compara_vols(ref, new  ):
    class parInfo:
        LOWBYTEFIRST=1
        s = open(ref+".info").read()
        s=s.replace("!","#")
        exec( s )
    print ref
    print new
    f_ref = open(ref,"r")
    f_new = open(new,"r")
    maxerr=-1
    overerr = 0
    miz=-1

    nblockfl = parInfo.NUM_Z/nprocs+1


    for iz in range(parInfo.NUM_Z):

        if (iz/nblockfl)%nprocs != myrank:
            continue
        sr = f_ref.read( parInfo.NUM_X*parInfo.NUM_Y * 4 )
        sn = f_new.read( parInfo.NUM_X*parInfo.NUM_Y * 4 )

        r  = numpy.reshape(numpy.fromstring(sr,"f"), [   parInfo.NUM_Y, parInfo.NUM_X ] )
        n  = numpy.reshape(numpy.fromstring(sn,"f"), [   parInfo.NUM_Y, parInfo.NUM_X ] )
        err = (r-n)
        err=(err*err).sum()
        rsum = (r*r).sum()
        nsum = (n*n).sum()

        if rsum==0 and nsum==0 :
            err = -1
            
        print iz, " err " , err
        if err> maxerr:
            maxerr=err
            dr = r
            dn = n
            overerr = (r*r).sum()
            miz = iz



    a    = numpy.array( [maxerr],"f" )
    a_r  = numpy.array( [0]    ,   "f"        )
    comm.Allreduce([a, MPI.FLOAT  ], [a_r, MPI.FLOAT  ], op=MPI.MAX)
    if a[0]==a_r[0]:
        a[0]=myrank
    else:
        a[0]=-1
    comm.Allreduce([a, MPI.FLOAT  ], [a_r, MPI.FLOAT  ], op=MPI.MAX)
    
    if myrank == a_r[0]:
        print "COMPARISON :   %s %s"%( ref, new  )
        print       " Relative Error : %e "% numpy.sqrt(maxerr/overerr)
        print " for slice ", miz
        mostra_slices( dr,dn, ref, new)
    comm.Barrier()

def compara_edfs(ref, new  ):
    import fabio
    
    maxerr=-1
    overerr = 0
    miz=-1
    done =0

    NN = len( zip (ref, new ))

    nblockfl = NN/nprocs+1



    for iz,(rname, nname) in enumerate(zip (ref, new )) :
        done=1

        if (iz/nblockfl)%nprocs != myrank:
            continue
     

        r  = fabio.open(rname).data
        n  = fabio.open(nname).data
        err = (r-n)
        err=(err*err).sum()
        if err> maxerr:
            maxerr=err
            dr = r
            dn = n
            overerr = (r*r).sum()
            mrname = rname
            mnname = nname 
    if done:



        a    = numpy.array( [maxerr],"f" )
        a_r  = numpy.array( [0]    ,   "f"        )
        comm.Allreduce([a, MPI.FLOAT  ], [a_r, MPI.FLOAT  ], op=MPI.MAX)
        if a[0]==a_r[0]:
            a[0]=myrank
        else:
            a[0]=-1
        comm.Allreduce([a, MPI.FLOAT  ], [a_r, MPI.FLOAT  ], op=MPI.MAX)
    
        if myrank == a_r[0]:
            print "COMPARISON :   %s %s"%(mrname , mnname  )
            print       " Relative Error : %e "% numpy.sqrt(maxerr/overerr)
            mostra_slices( dr,dn, mrname, mnname)
        comm.Barrier()


import PyMca5.PyMcaGui
from  PyMca5.PyMcaGui import MaskImageWidget as  pymcaMask



class MaskImageWidget(pymcaMask.MaskImageWidget):
    closealso=[]
    
    def __init__(self, *args, **argw):
        self.closealso.append(self)
        super(MaskImageWidget,self).__init__(*args, **argw)

        
    def closeEvent(self, event):
        print " in close ",  self.closealso
        
        if self in self.closealso:
            self.closealso.remove(self)
            
        for t in self.closealso:
            self.closealso.remove(t)
            t.close()
        event.accept() #

def     mostra_slices( dr,dn, namer, namen):
    app=Qt.QApplication([])

  
   
    maskW1 = MaskImageWidget(None , aspect=True,profileselection=True)
    maskW1.setImageData(dr  , xScale=(0.0, 1.0), yScale=(0., 1.))
    maskW2 = MaskImageWidget(None , aspect=True,profileselection=True)
    maskW2.setImageData(dn  , xScale=(0.0, 1.0), yScale=(0., 1.))
    maskW3 = MaskImageWidget(None , aspect=True,profileselection=True)
    maskW3.setImageData(abs(dn-dr)  , xScale=(0.0, 1.0), yScale=(0., 1.))
    maskW1.setDefaultColormap(0,0)
    maskW2.setDefaultColormap(0,0)
    maskW3.setDefaultColormap(2,1)
    maskW1.plotImage(True)
    maskW2.plotImage(True)
    maskW3.plotImage(True)

    maskW1.setWindowTitle("new")
    maskW2.setWindowTitle("old")
    maskW3.setWindowTitle("new - old")
    
    maskW1.show()
    maskW2.show()
    maskW3.show()
    app.connect(maskW1, SIGNAL("destroyed()"),
                app, SLOT("quit()"))

    
    app.exec_()
  
    # cdict = {'red': ((0.0, 0.0, 0.0),
    #                  (0.5, 0.0, 0.0),
    #                  (0.75, 1.0, 1.0),
    #                  (1.0, 1.0, 1.0)),
    #          'green': ((0.0, 0.0, 0.0),
    #                    (0.25, 1.0, 1.0),
    #                    (0.75, 1.0, 1.0),
    #                    (1.0, 0.0, 0.0)),
    #          'blue': ((0.0, 1.0, 1.0),
    #                   (0.25, 1.0, 1.0),
    #                   (0.5, 0.0, 0.0),
    #                   (1.0, 0.0, 0.0))}
    # temperature = LinearSegmentedColormap('temperature',cdict, 65536)
    # fig=plt.figure()
    # plt.subplot(1, 3, 1)
    # title(namer)
    # im = plt.imshow(dr,  cmap=temperature)
    # plt.subplot(1, 3, 2)
    # title(namen)
    # im = plt.imshow(dn,  cmap=temperature)
    # plt.subplot(1, 3, 3)
    # title('diff (abs)')
    # diff = abs( dr-dn  )
    # im = plt.imshow(diff,  cmap=temperature,
    #             norm=LogNorm(vmin=max(diff.min(), diff.max()*1.0e-8), vmax=diff.max()))
    # show()
    
def get_results(radice):
    N=len(radice)
    import os
    import glob
    result={}
    ws=os.walk(radice)
    leafs = [ t[0] for t in ws if len(t[1])==0 ]
    for l in leafs:
        result[l[N:]] = {}
        print l+"/abs.vol"
        result[l[N:]]["vol"] = glob.glob(l+"/vol*.vol")+glob.glob(l+"/reconstruction*.vol")
        lf = glob.glob(l+"/*.edf")
        result[l[N:]]["edf"] = [ t for t in lf if ("histo" not in t and "phase1" not in t and "phase2" not in t )]
    return result



ress_ref = get_results(referenceprefix)

print ress_ref
ress_new = get_results(newversionprefix)
print ress_new


leafs = list(ress_ref.keys())




for l in leafs:
    print "======================="
    print l
    print " continuo?"

    res=raw_input()
    if res=="1":

        print ress_ref[l]["vol"]
        print ress_new[l]["vol"]
        print ress_ref[l]["edf"]
        print ress_new[l]["edf"]
        
        if len(ress_ref[l]["vol"]):
            if(len(ress_new[l]["vol"])==0):
                print " ERRORE vol : new n'a pas produit de output pour ", l
            else:
                assert( len(ress_ref[l]["vol"])==1)
                ress_vol = ress_ref[l]["vol"][0]
                new_vol  = ress_new[l]["vol"][0]
                compara_vols(ress_vol,  new_vol   )
        else:

            if(len(ress_new[l]["edf"])==0):
                print " ERRORE edf : new n'a pas produit de output pour ", l
            else:
                # assert( len(ress_ref[l]["edf"] = len(ress_new[l]["edf"])==1) )

                
                ress_edf = ress_ref[l]["edf"]
                new_edf  = ress_new[l]["edf"]
                ress_edf.sort()
                new_edf .sort()
                compara_edfs(ress_edf,  new_edf   )
        
    
print ress_ref
print ress_new
print " END \n"*100