Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:20

0001 #!/usr/bin/env python
0002 """
0003 NPFold_profile_test.py
0004 =======================
0005 
0006 TODO : connect this with sreport.cc subprofile output 
0007 
0008 
0009 ::
0010 
0011     In [8]: a.profile[:,:,0] - a.profile[:,0,0, np.newaxis]
0012     Out[8]:
0013     array([[    0,   116, 33834],
0014            [    0,   100, 14633],
0015            [    0,   105, 14500],
0016            [    0,   100, 14586],
0017            [    0,   107, 14930],
0018            [    0,   103, 14525],
0019            [    0,   106, 14541],
0020            [    0,   101, 14728],
0021            [    0,   105, 14497],
0022            [    0,   107, 14511]])
0023               BOE0  BOE1  EOE
0024 
0025 
0026 
0027 """
0028 import os, numpy as np
0029 from np.fold import Fold
0030 from np.npmeta import NPMeta
0031 
0032 MODE =  int(os.environ.get("MODE", "2"))
0033 PICK =  os.environ.get("PICK", "CF")
0034 PLOT =  os.environ.get("PLOT", "PWE")
0035 TLIM =  np.array(list(map(int,os.environ.get("TLIM", "0,0").split(","))),dtype=np.int32)
0036 QWN = os.environ.get("QWN", "vm")
0037 
0038 US, VM, RS = 0, 1, 2 
0039 
0040 # https://matplotlib.org/stable/gallery/color/named_colors.html
0041 palette = ["red","green", "blue", 
0042            "cyan", "magenta", "yellow", 
0043            "tab:orange", "tab:pink", "tab:olive",
0044            "tab:purple", "tab:grey", "tab:cyan"
0045            ]
0046 
0047 
0048 COLORS = {
0049    'A':"red", 
0050    'A0':"pink",
0051    'A1':"tab:orange",
0052    'A2':"purple",
0053    'B':"blue",
0054    'B0':"blue",
0055    'B1':"lightblue",
0056    'B2':"tab:cyan" 
0057    }
0058 
0059 if MODE != 0:
0060     from opticks.ana.pvplt import * 
0061 pass
0062 
0063 
0064 class ProfileWithinEvent(object):
0065     """
0066     Timeline is folded to present info for multiple 
0067     events together using times relative to start of each event. 
0068     """
0069     def __init__(self, f, symbol="A"):
0070 
0071         lab = f.labels_names   # of the timestamps
0072         prof = f.subprofile
0073         meta = f.subprofile_meta
0074 
0075         slab = list(map(NPMeta.Summarize, lab))
0076         base = meta.base.replace("/data/blyth/opticks/GEOM/", "")
0077         smry = meta.smry("GPUMeta,prefix,creator")
0078         sfmt = meta.smry("stampFmt") 
0079         titl = "%s:ProfileWithinEvent %s " % (symbol, sfmt) 
0080         title = " ".join([titl,base,smry]) 
0081 
0082         t = prof[:,:,0] - prof[:,0,0, np.newaxis] 
0083 
0084         self.prof = prof
0085         self.lab = lab
0086         self.slab = slab
0087         self.title = title
0088         self.t = t 
0089         self.f = f 
0090         self.symbol = symbol
0091         print(repr(self))
0092 
0093     def __repr__(self):
0094         return "\n".join([self.title, "%s.t" % self.symbol, repr(self.t)])
0095 
0096     def plot(self):
0097         t = self.t
0098         if MODE == 2:
0099             fig, axs = mpplt_plotter(nrows=1, ncols=1, label=self.title, equal=False)
0100             ax = axs[0]
0101 
0102             if TLIM[1] > TLIM[0]:
0103                 ax.set_xlim(*TLIM)
0104             pass
0105             for i in range(len(t)):
0106                 ax.vlines( t[i,:], i-0.5, i+0.5  ) 
0107             pass
0108             ax.legend()
0109             fig.show()
0110         pass  
0111         return ax
0112 
0113     @classmethod
0114     def ABPlot(cls, a, b):
0115         """
0116         PICK=CF PLOT=PWE ~/np/tests/NPFold_profile_test.sh ana
0117         """
0118         A = cls(a, symbol="A")
0119         B = cls(b, symbol="B") 
0120 
0121  
0122         avB = np.average(B.t[1:,-1])
0123         avA = np.average(A.t[1:,-1])
0124 
0125         BOA = B.t[:,-1]/A.t[:,-1]  
0126         _BOA = ( "%4.1f " * len(BOA) ) % tuple(BOA) 
0127         avBOA = np.average(BOA[1:]) 
0128         _avBOA = " avg(BOA[1:]) %4.1f " % avBOA
0129         sBOA = "BOA : %s %s " % (_BOA, _avBOA)
0130         QQ = [A,B]
0131 
0132         title = "\n".join(["Profile.ABPlot", A.title, B.title, sBOA])
0133         print(title)
0134 
0135         if MODE == 2:
0136             fig, axs = mpplt_plotter(nrows=1, ncols=2, label=title, equal=False)
0137             for p in range(len(axs)):
0138                 ax = axs[p]
0139                 Q = QQ[p]
0140 
0141                 if TLIM[1] > TLIM[0]:
0142                     ax.set_xlim(*TLIM)
0143                 pass
0144                 t = Q.t
0145                 for i in range(len(t)): 
0146                     ax.vlines( t[i,:], i-0.5, i+0.5 )
0147                 pass
0148             pass
0149             ax.legend()
0150             fig.show()
0151         pass  
0152         return A, B
0153 
0154 
0155 class Profile(object):
0156     def __init__(self, f, symbol="A", offset=0 ):
0157         meta = f.profile_meta
0158         base = f.profile_meta.base.replace("/data/blyth/opticks/GEOM/", "")
0159         smry = f.profile_meta.smry("GPUMeta,prefix,creator")
0160         titl = "%s:Profile " % symbol 
0161         title = " ".join([titl,base,smry]) 
0162 
0163         pr = f.profile[1:]   ## skip the initialization jump 
0164         color = COLORS[symbol] 
0165 
0166         j = int(symbol[-1]) if symbol[-1].isnumeric() else None
0167 
0168         if j is None:
0169             # dont distinguish between profile stamps, treat them all the same
0170             us = pr[:,:,US].ravel() 
0171             vm = pr[:,:,VM].ravel()
0172             rs = pr[:,:,RS].ravel()
0173         else:
0174             # pick j-th profile stamp eg BeginOfEvent 
0175             us = pr[:,j,US] 
0176             vm = pr[:,j,VM]
0177             rs = pr[:,j,RS]
0178         pass
0179 
0180         us = (us - us.min())     # us from first stamp 
0181         vm = vm/1e6 + offset     # GB 
0182         rs = rs/1e6 + offset     # GB
0183 
0184         self.f = f    
0185         self.title = title
0186         self.pr = pr 
0187         self.us = us
0188         self.vm = vm              
0189         self.rs = rs              
0190         self.symbol = symbol 
0191         self.color = color 
0192 
0193     def plot(self):
0194         p = self
0195         if MODE == 2:
0196             fig, axs = mpplt_plotter(nrows=1, ncols=1, label=p.title, equal=False)
0197             ax = axs[0]
0198 
0199             if TLIM[1] > TLIM[0]:
0200                 ax.set_xlim(*TLIM)
0201             pass
0202             assert QWN in ["vm", "rs"]
0203             qwn = getattr(p, QWN)      
0204             #ax.scatter(p.us, qwn, label=QWN) 
0205             ax.vlines( p.us, qwn-0.01, qwn+0.01, label=QWN ) 
0206 
0207             pass
0208             ax.legend()
0209             fig.show()
0210         pass  
0211         return ax
0212 
0213     @classmethod
0214     def ABPlot(cls, a, b):
0215 
0216         A = cls(a, symbol="A")
0217         B = cls(b, symbol="B") 
0218 
0219         A0 = cls(a, symbol="A0", offset=0.01)
0220         A1 = cls(a, symbol="A1", offset=0.01)
0221         A2 = cls(a, symbol="A2", offset=0.01)
0222 
0223         B0 = cls(b, symbol="B0")
0224         B1 = cls(b, symbol="B1")
0225         B2 = cls(b, symbol="B2")
0226     
0227         #QQ = [A0, B0]
0228         #QQ = [A1, B1]
0229         QQ = [A2, B2]
0230         #QQ = [A0,A1,A2,B0,B1,B2]
0231         #QQ = [A,B]
0232 
0233         title = "\n".join(["Profile.ABPlot", A.title, B.title])
0234         print(title)
0235 
0236         if MODE == 2:
0237             fig, axs = mpplt_plotter(nrows=1, ncols=1, label=title, equal=False)
0238             ax = axs[0]
0239 
0240             if TLIM[1] > TLIM[0]:
0241                 ax.set_xlim(*TLIM)
0242             pass
0243             assert QWN in ["vm", "rs"]
0244       
0245             for Q in QQ:
0246                 qwn = getattr(Q, QWN)
0247                 label = "%s:%s" % (Q.symbol, QWN)
0248 
0249                 j = int(Q.symbol[-1]) if Q.symbol[-1].isnumeric() else -1 
0250                 qd = 0.005 if j == -1 else 0.002 + 0.001*j 
0251                 ax.vlines( Q.us, qwn-qd, qwn+qd, label=label, color=Q.color ) 
0252                 ax.scatter(Q.us, qwn, label=label, color=Q.color ) 
0253             pass
0254             ax.legend()
0255             fig.show()
0256         pass  
0257         return A,B
0258 
0259 if __name__ == '__main__':
0260     ab = Fold.Load(symbol="ab")
0261     print(repr(ab))
0262     a = ab.a
0263     b = ab.b 
0264     have_both = not a is None and not b is None
0265     ap = a.profile if not a is None else None 
0266     bp = b.profile if not b is None else None
0267 
0268     yn_ = lambda _:"NO " if _ is None else "YES"
0269     print("PICK:%s a:%s b:%s  have_both:%s " % (PICK, yn_(a), yn_(b), have_both ))
0270 
0271     if PICK == "CF" and not have_both:
0272         print("PICK=CF requires both a and b to exist, use PICK=A or PICK=B if otherwise" )
0273     elif PICK == "CF":
0274         if PLOT == "PRO":
0275             A,B = Profile.ABPlot(a, b)
0276         elif PLOT == "PWE":
0277             A,B = ProfileWithinEvent.ABPlot(a,b)
0278         else:
0279             A,B = None,None
0280         pass
0281     elif PICK in ["AB", "BA", "A", "B"]:
0282         for symbol in PICK:
0283             sym = symbol.lower()
0284             e = getattr(ab, sym, None)
0285             if e is None:
0286                 print("%s:SKIP as MISSING" % sym )
0287                 continue
0288             pass
0289             if PLOT == "PRO":
0290                 p = Profile(e, symbol=symbol)
0291             elif PLOT == "PWE":
0292                 p = ProfileWithinEvent(e, symbol=symbol)
0293             else:
0294                 p = None
0295             pass 
0296             ax = p.plot() if not p is None else None
0297         pass
0298     pass
0299 
0300 
0301