Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 """
0022 ::
0023 
0024     In [36]: run textgrid.py
0025      E0  C1  C2  C3  C4  C5  C6  C7  C8  F0
0026 
0027      E1  A1   .   .   .   .   .   .  B8  F1
0028 
0029      E2   .  A2   .   .   .   .  B7   .  F2
0030 
0031      E3   .   .  A3   .   .  B6   .   .  F3
0032 
0033      E4   .   .   .  A4  B5   .   .   .  F4
0034 
0035      E5   .   .   .  B4  A5   .   .   .  F5
0036 
0037      E6   .   .  B3   .   .  A6   .   .  F6
0038 
0039      E7   .  B2   .   .   .   .  A7   .  F7
0040 
0041      E8  B1   .   .   .   .   .   .  A8  F8
0042 
0043      E9  D1  D2  D3  D4  D5  D6  D7  D8  F9
0044 
0045 
0046 """
0047 import logging
0048 log = logging.getLogger(__name__)
0049 import numpy as np
0050 
0051 class T(np.ndarray):
0052     @classmethod
0053     def init(cls, a, itemfmt="%3s", rowjoin="\n", empty=""):  
0054         assert len(a.shape) == 2, a
0055         t = a.view(cls)
0056         t.itemfmt = itemfmt
0057         t.rowjoin = rowjoin
0058         t.empty = empty 
0059         return t
0060 
0061     def __repr__(self):
0062         row_ = lambda r:" ".join(map(lambda _:self.itemfmt % (_ if _ is not None else self.empty) ,r))
0063         tab_ = lambda a:self.rowjoin.join(map(row_, a))
0064         return tab_(self)
0065 
0066 
0067 class TextGrid(object):
0068     def __init__(self, ni, nj, **kwa):
0069         """
0070         grid of None (might produce gibberish in some imps?)
0071         dont want to use zeros as then get zeros at every spot on the grid
0072         """
0073         a = np.empty((ni,nj),dtype=np.object)  
0074         t = T.init(a, **kwa)
0075         self.a = a
0076         self.t = t 
0077 
0078     def __str__(self):
0079         return repr(self.t)
0080 
0081 
0082 
0083 if __name__ == '__main__':
0084     pass
0085 
0086     n = 10 
0087     tg = TextGrid(n,n, itemfmt="%3s", rowjoin="\n\n", empty=".") 
0088 
0089     for k in range(n): 
0090         tg.a[k,k] = "A%d"%k       # diagonal from top-left to bottom-right
0091         tg.a[n-1-k,k] = "B%d"%k   # diagonal from bottom-left to top right
0092         tg.a[0,k] = "C%d"%k       # top row
0093         tg.a[-1,k] = "D%d"%k      # bottom row
0094         tg.a[k,0] = "E%d"%k       # left column 
0095         tg.a[k,-1] = "F%d"%k      # right column
0096     pass
0097 
0098     print(tg) 
0099 
0100 
0101 
0102 
0103 
0104 
0105 
0106 
0107 
0108 
0109 
0110 
0111 
0112 
0113      
0114