File indexing completed on 2026-04-09 07:49:00
0001
0002 import os, numpy as np, logging
0003 log = logging.getLogger(__name__)
0004
0005
0006 class Values(object):
0007 @classmethod
0008 def FindDirUpTree(cls, origpath, name="Values"):
0009 """
0010 :param origpath: to traverse upwards looking for sibling dirs with *name*
0011 :param name: sibling directory name to look for
0012 :return full path to *name* directory
0013 """
0014 elem = origpath.split("/")
0015 found = None
0016 for i in range(len(elem),0,-1):
0017 path = "/".join(elem[:i])
0018 cand = os.path.join(path, name)
0019 log.debug(cand)
0020 if os.path.isdir(cand):
0021 found = cand
0022 break
0023 pass
0024 pass
0025 return found
0026
0027 @classmethod
0028 def Find(cls, origfold=None, symbol="v"):
0029 if origfold is None:
0030 origfold = os.environ.get("FOLD", "/tmp" )
0031 else:
0032 origfold = os.path.expandvars(origfold)
0033 pass
0034 fold = cls.FindDirUpTree(origfold)
0035 return None if fold is None else cls(fold, symbol=symbol)
0036
0037 def __init__(self, fold_=None, symbol="v"):
0038 fold = os.path.expandvars(fold_)
0039 a_path = os.path.join(fold, "values.npy")
0040 n_path = os.path.join(fold, "values_names.txt")
0041 a = np.load(a_path)
0042 n = np.loadtxt(n_path, dtype=np.object, delimiter="\n")
0043 assert a.shape == n.shape
0044
0045 self.fold = fold
0046 self.symbol = symbol
0047 self.a_path = a_path
0048 self.n_path = n_path
0049 self.a = a
0050 self.n = n
0051 self.contains = None
0052
0053 def hdr(self):
0054 return "Values.%s : %s : contains:%s " % (self.symbol,self.fold, self.contains )
0055
0056 def __str__(self):
0057 return "\n".join([
0058 "Values",
0059 "%s.fold: %s " % (self.symbol, self.fold),
0060 "%s.a_path: %s " % (self.symbol, self.a_path),
0061 "%s.n_path: %s " % (self.symbol, self.n_path),
0062 "%s.a: %s " % (self.symbol, str(self.a.shape)),
0063 "%s.n: %s " % (self.symbol, str(self.n.shape))
0064 ])
0065
0066
0067 def get_idx(self, q):
0068 n_idx = np.where( self.n == q )[0]
0069 assert len(n_idx) in (0,1)
0070 return -1 if len(n_idx) == 0 else n_idx[0]
0071
0072 def get(self, name):
0073 """
0074 :param name: eg SolidMask.SolidMaskVirtual.htop_out
0075 :return val: scalar
0076
0077 sv.get("SolidMask.SolidMaskVirtual.htop_out") 194.0
0078 """
0079 idx = self.get_idx(name)
0080 return self.a[idx] if idx > -1 else None
0081
0082 def __repr__(self):
0083 a = self.a
0084 n = self.n
0085 contains = self.contains
0086 assert a.shape == n.shape
0087 lines = []
0088 lines.append(self.hdr())
0089 for i in range(len(a)):
0090 k = n[i]
0091 v = a[i]
0092 match = contains is None or (not contains is None and contains in k)
0093 if match:
0094 line = " %2d : %10.4f : %s " % (i, v, k )
0095 lines.append(line)
0096 pass
0097 pass
0098 return "\n".join(lines)
0099
0100 def __getitem__(self, contains):
0101 self.contains = contains
0102 return self
0103
0104
0105
0106 if __name__ == '__main__':
0107 pass
0108 logging.basicConfig(level=logging.INFO)
0109 v = Values.Find()
0110 print(repr(v))
0111
0112