Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python
0002 """
0003 ::
0004  
0005     PICK=AB ~/o/qudarap/tests/QRngTest.sh pdb 
0006 
0007 """
0008 import logging 
0009 log = logging.getLogger(__name__)
0010 import os,sys, numpy as np
0011 
0012 try:
0013     import matplotlib.pyplot as plt 
0014 except ImportError:
0015     plt = None
0016 pass
0017 
0018 np.set_printoptions(suppress=True, edgeitems=4, precision=5, linewidth=200 )
0019 
0020 
0021 class QRngTest(object):
0022     FOLD = os.path.expandvars("$FOLD")
0023     def __init__(self, reldir):
0024         base = os.path.join(self.FOLD, reldir)
0025 
0026         upath = os.path.join(base, "u_0.npy")
0027         uupath = os.path.join(base, "uu.npy")
0028 
0029         if os.path.exists(upath):
0030             os.system("ls -l %s" % upath)
0031             u = np.load(upath)
0032         pass
0033         if os.path.exists(uupath):
0034             os.system("ls -l %s" % uupath)
0035             uu = np.load(uupath)
0036         pass
0037 
0038         xtype = np.float32 if reldir.startswith("float") else np.float64
0039 
0040         if not u is None:assert(u.dtype == xtype)
0041         if not uu is None:assert(uu.dtype == xtype)
0042 
0043         self.reldir = reldir
0044         self.upath = upath
0045         self.uupath = uupath
0046 
0047         self.u = u
0048         self.uu = uu
0049         self.uuh = np.histogram( self.uu ) if not uu is None else None
0050 
0051         self.title = "qudarap/tests/QRngTest.py %s " % base
0052 
0053 
0054     def check_skipahead_shifts(self, offset):
0055         """
0056         For example when using skipaheadstep of 1::
0057 
0058            In [21]: np.all( uu[1,:,:-1] == uu[0,:,1:] )
0059            Out[21]: True
0060 
0061         The first dimension is the event index, so with skipahead per event
0062         of one expect to see the same randoms for each event but with a shift
0063         of one from the prior event. 
0064 
0065         """
0066         uu = self.uu
0067         assert len(uu.shape) == 3
0068         ni, nj, nk = uu.shape 
0069         for i in range(ni-1):
0070             i0 = i
0071             i1 = i+1
0072             assert np.all( uu[i1,:,:-offset] == uu[i0,:,offset:] )
0073             log.info("i0 %d i1 %d " % (i0,i1)) 
0074         pass
0075 
0076     def uu_plot(self):
0077         """
0078         Plot the uuh histogram : should be flat from 0 to 1 
0079         """
0080         if plt is None: return 
0081         t = self
0082         h = self.uuh 
0083 
0084         fig, ax = plt.subplots(figsize=[12.8,7.2])
0085         fig.suptitle(self.title)
0086 
0087         ax.plot( h[1][:-1], h[0], label="h", drawstyle="steps-post" )
0088         ax.set_ylim( 0, h[0].max()*1.1 )
0089 
0090         pass
0091         ax.legend()
0092         fig.show()
0093         path = os.path.join(t.FOLD, self.reldir, "fig.png")
0094         log.info("save to %s " % path)
0095         fig.savefig(path)
0096 
0097 
0098 if __name__ == '__main__':
0099     logging.basicConfig(level=logging.INFO)
0100     TEST = os.environ.get("TEST", "generate") 
0101     PICK = os.environ.get("PICK", "A") 
0102 
0103     TYPE = "float"
0104     a_IMPL = "CHUNKED_CURANDSTATE"
0105     b_IMPL = "OLD_MONOLITHIC_CURANDSTATE"
0106 
0107     a_reldir = "%s/%s" % (TYPE, a_IMPL)
0108     b_reldir = "%s/%s" % (TYPE, b_IMPL)
0109 
0110     reldir = None
0111     if PICK == "A":reldir = a_reldir
0112     if PICK == "B":reldir = b_reldir
0113 
0114     print("%s:TEST:%s PICK:%s FOLD:%s reldir:%s" % ( sys.argv[0], TEST, PICK, QRngTest.FOLD, reldir))
0115 
0116     if PICK in tuple("AB"):       
0117         t = QRngTest(reldir)  
0118 
0119         if TEST == "generate": 
0120             uu = t.uu
0121             uuh = t.uuh
0122             print("uu.shape\n",uu.shape)
0123             print("uu[:10]\n",uu[:10])
0124             t.check_skipahead_shifts(1)
0125             #t.uu_plot()
0126         else:
0127             print("%s:TEST:%s unhandled : run ana/pdb individually for each TEST" % (sys.argv[0],TEST) )
0128         pass
0129 
0130     elif PICK == "AB":
0131         a = QRngTest(a_reldir)  
0132         b = QRngTest(b_reldir)  
0133 
0134         if TEST == "generate":
0135             auu = a.uu
0136             buu = b.uu
0137             auu_buu_match = np.all( auu == buu )  
0138             print("auu.shape\n",auu.shape)
0139             print("buu.shape\n",buu.shape)
0140             print("auu_buu_match:%d\n" % auu_buu_match)
0141             assert auu_buu_match
0142         else:
0143             print("%s:TEST:%s unhandled : run ana/pdb individually for each TEST" % (sys.argv[0],TEST) )
0144         pass
0145     pass
0146 pass
0147 
0148