Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:46

0001 #!/usr/bin/env python
0002 
0003 import numpy as np
0004 
0005 class ArrayReprMixin(object):
0006     """
0007     ArrayReprMixin
0008     ================
0009 
0010     This mixin base class is added with::
0011 
0012         class PhotonDV(ArrayReprMixin, object):
0013          
0014     And used from the repr with::
0015     
0016         def __repr__(self):
0017             return self.MakeRepr(self.dv, symbol="pdv.dv")
0018 
0019     It simply presents the array provided with rows and columns annotated, providing eg::
0020 
0021               pdv.dv :         1e-06 1e-05  0.0001 0.001  0.01   0.1    1      10     100    1000    
0022 
0023                  pos : array([[   47,   117,  1732,  4412,  2710,   965,    16,     1,     0,     0],
0024                 time :        [ 2746,  5430,  1724,    96,     4,     0,     0,     0,     0,     0],
0025                  mom :        [ 6404,  2937,   647,    11,     1,     0,     0,     0,     0,     0],
0026                  pol :        [ 9995,     1,     1,     3,     0,     0,     0,     0,     0,     0],
0027                   wl :        [10000,     0,     0,     0,     0,     0,     0,     0,     0,     0]], dtype=uint32)
0028 
0029     The adopting class is required to provide two class level lists containing the annotation::
0030 
0031         COLUMN_LABEL
0032         ROW_LABEL  
0033 
0034     """
0035     @classmethod
0036     def FindColumnPos(cls, s):
0037         """
0038         :param s: first line of np.array repr string
0039         :return pos: array of string position indices of first spaces in each column
0040 
0041         HMM: for smaller arrays often first column has no space
0042         'array([[1., 0., 0., 0.],'  
0043 
0044         """
0045         if s is None: s = 'array([[   47,   117,  1732,  4412,  2710,   965,    16,     1,     0,     0],'
0046         pos = []
0047         for i in range(len(s)):
0048             if len(pos) == 0:
0049                 if i > 0 and s[i-1] == "[" and s[i] != "[": 
0050                     pos.append(i) 
0051                 pass
0052             else:
0053                 if i > 0 and s[i-1] != " " and s[i] == " ": 
0054                     pos.append(i) 
0055                 pass
0056             pass
0057         pass
0058         return pos 
0059 
0060     @classmethod
0061     def ShowColumnPos(cls, s):
0062         """
0063         :param s: first line of np.array repr string
0064         :return s2: string with the first spaces in each column replaced with a "+"
0065 
0066         """
0067         if s is None: s = 'array([[   47,   117,  1732,  4412,  2710,   965,    16,     1,     0,     0],'
0068         pos = cls.FindColumnPos(s)
0069         c = np.zeros( len(s), dtype=np.int8 )     
0070         for i in range(len(s)):c[i] = ord(s[i])  
0071         for p in pos: c[p] = ord("+")
0072         s2 = "".join(list(map(chr,c)))   
0073         return s2
0074 
0075     @classmethod
0076     def MakeHdr(cls, s):
0077         """
0078         :param s: first line of np.array repr string
0079         :return hdr: 
0080         """
0081         pos = cls.FindColumnPos(s)
0082         assert len(cls.COLUMN_LABEL) == len(pos) 
0083 
0084         h = np.zeros(len(s), dtype=np.int8 )  
0085         h.fill(ord(" ")) 
0086         for i,p in enumerate(pos):
0087             label = cls.COLUMN_LABEL[i]
0088             for j in range(len(label)):
0089                 h[p+j] = ord(label[j])
0090             pass
0091         pass
0092         hdr = "".join(list(map(chr,h)))   
0093         return hdr 
0094 
0095     @classmethod
0096     def MakeRepr(cls, arr, symbol="arr"):  
0097         srep = repr(arr).split("\n")
0098         hdr = cls.MakeHdr(srep[0])
0099         fmt = "%20s : %s" 
0100         lines = []
0101         lines.append("")
0102         lines.append(fmt % (symbol, hdr)) 
0103         lines.append("")
0104         for i, line in enumerate(srep):
0105             item = cls.ROW_LABEL[i] 
0106             lines.append(fmt % (item, line)) 
0107         pass
0108         return "\n".join(lines)
0109 
0110 
0111 
0112 class Example(ArrayReprMixin, object):
0113     """
0114     In [1]: run array_repr_mixin.py
0115 
0116                     eg.a :         x  y   z   w    
0117 
0118                        a : array([[1., 0., 0., 0.],
0119                        b :        [0., 1., 0., 0.],
0120                        c :        [0., 0., 1., 0.],
0121                        d :        [0., 0., 0., 1.]])
0122 
0123     """
0124     ROW_LABEL = ["a","b","c","d" ]
0125     COLUMN_LABEL = ["x","y","z","w" ]
0126  
0127     def __init__(self):
0128         self.a = np.eye(4)
0129     def __repr__(self):
0130         return self.MakeRepr(self.a, "eg.a")
0131 
0132 if __name__ == '__main__':
0133     eg = Example()
0134     print(eg)
0135 
0136 
0137 
0138