-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnpmeta.py
More file actions
executable file
·283 lines (216 loc) · 6.92 KB
/
Copy pathnpmeta.py
File metadata and controls
executable file
·283 lines (216 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
"""
NPMeta.py
===========
Parsing metadata lines from NP.hh
Currently have two copies of this source at::
~/np/npmeta.py
~/opticks/ana/npmeta.py
The np repository version is regarded as the
primary and is occasionally copied together with
NP.hh NPFold.h headers via::
cd ~/np
./cp.sh
"""
import os, logging
from collections import OrderedDict as odict
import numpy as np
log = logging.getLogger(__name__)
class NPMetaCompare(object):
def __init__(self, am, bm):
ak = list(filter(None,am.d.keys()))
bk = list(filter(None,bm.d.keys())) # avoid blank key
kk = ak if ak == bk else list(set(list(ak)+list(bk))) ## common keys
skk = np.array( list(map(lambda _:"%30s"%_, kk )), dtype="|S30" )
tab = np.zeros( [len(kk),2], dtype="|U25" )
lines = []
hfmt = "%-30s : %7s : %7s : %7s : %7s : %s "
vfmt = "%-30s : %7d : %7d : %7.3f : %7.3f : %7.3f"
lines.append(hfmt % ("key", "a", "b", "a/b", "b/a", "(a-b)^2/(a+b)" ))
for i, k in enumerate(kk):
if k == "": continue
al = am.d.get(k,[])
bl = bm.d.get(k,[])
av = al[0] if len(al) == 1 else 0
bv = bl[0] if len(bl) == 1 else 0
#av_bv = 0 if bv == 0. else av/bv
#bv_av = 0 if av == 0. else bv/av
#c2 = 0 if av+bv == 0 else (av-bv)*(av-bv)/(av+bv)
#lines.append(vfmt % ( k, av, bv, av_bv, bv_av, c2 ))
tab[i,0] = av
tab[i,1] = bv
pass
stab = np.c_[skk, tab]
self.ak = ak
self.bk = bk
self.am = am
self.bm = bm
self.kk = kk
self.skk = skk
self.tab = tab
self.lines = lines
self.stab = stab
def __str__(self):
return "\n".join(self.lines)
def __repr__(self):
lines = []
lines.append("skk")
return "\n".join(lines)
class NPMeta(object):
ENCODING = "utf-8"
@classmethod
def Compare(cls, am, bm):
cfm = NPMetaCompare(am,bm)
return cfm
@classmethod
def AsDict_OLD(cls, lines):
d = odict()
key = ""
d[key] = []
for line in lines:
dpos = line.find(":")
if dpos > -1:
key = line[:dpos]
d[key] = []
val = line[dpos+1:]
else:
val = line
pass
d[key].append(val)
pass
return d
@classmethod
def AsDict(cls, lines):
d = odict()
for line in lines:
dpos = line.find(":")
if dpos == -1: continue
key = line[:dpos]
val = line[dpos+1:]
d[key] = val
pass
return d
@classmethod
def Load(cls, path):
name = os.path.basename(path)
with open(path, "r") as f:
lines = f.read().splitlines()
pass
return cls(lines)
@classmethod
def LoadAsArray(cls, path):
with open(path, "r") as f:
lines = f.read().splitlines()
pass
return np.array(lines)
def __init__(self, lines):
self.lines = lines
self.d = self.AsDict(lines)
def __len__(self):
return len(self.lines)
def find(self, k, fallback=None):
return self.d.get(k, fallback)
def __getattr__(self, k):
if not k in self.d:
raise AttributeError("No attribute %s " % k)
return self.find(k)
def get_value(self,k,fallback=""):
if not k in self.d:return fallback
f = self.find(k, fallback)
return f[0] if type(f) is list else f
def __getitem__(self, idx):
"""
item access useful for simple lists of names, not metadata dicts
"""
return self.lines[idx]
def oldfind(self, k_start, fallback=None, encoding=ENCODING):
meta = self.meta
ii = np.flatnonzero(np.char.startswith(meta, k_start.encode(encoding)))
log.debug( " ii %s len(ii) %d " % (str(ii), len(ii)) )
ret = fallback
if len(ii) == 1:
i = ii[0]
line = meta[i].decode(encoding)
ret = line[len(k_start):]
log.debug(" line [%s] ret [%s] " % (line,ret) )
else:
log.debug("did not find line starting with %s or found more than 1" % k_start)
pass
return ret
def __repr__(self):
return "\n".join(self.lines)
def __str__(self):
return repr(self.d)
def has_key(self, k):
return k in self.d
def keys(self):
return self.d.keys()
def values(self):
return self.d.values()
def smry(self, keys="red,green,blue"):
kv = []
for k in keys.split(","):
if self.has_key(k):
v = self.d[k]
kv.append("%s:%s" % (k,v) )
pass
return " ".join(kv)
@classmethod
def Summarize(cls, label):
"""
Shorten stamp labels via heuristics of distinctive chars
"""
smry = ""
p = None
for c in label:
if p is None: # always take first char
smry += c
elif c.isalnum() and p == "_": # first alnum char after _
smry += c
elif c.isupper() and p.islower(): # upper char following lower
smry += c
elif p == "P" and c in "ro": # accept r or o after P to distinguish Pre and Post
smry += c
pass
p = c
pass
return smry
def test_load():
path = "/tmp/t_meta.txt"
multiline = "hello:world\nmoi:red\nmidx:green\nmord:blue\niidx:grey\nTOPLINE:yellow\nBOTLINE:red\n"
oneline = "hello:world\n"
test = oneline
open(path, "w").write(test)
pm = NPMeta.Load(path)
moi = pm.find("moi:")
midx = pm.find("midx:")
mord = pm.find("mord:")
iidx = pm.find("iidx:")
print(" moi:[%s] midx:[%s] mord:[%s] iidx:[%s] " % (moi, midx, mord, iidx) )
TOPLINE = pm.find("TOPLINE:")
BOTLINE = pm.find("BOTLINE:")
print(" TOPLINE:[%s] " % TOPLINE )
print(" BOTLINE:[%s] " % BOTLINE )
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
lines = ['PV:nnvt_body_phys',
'nnvt_inner1_phys',
'nnvt_inner2_phys',
'nnvt_tube_phy',
'nnvt_edge_phy',
'hama_body_phys',
'nnvt_plate_phy',
'hama_inner1_phys',
'hama_inner2_phys',
'hama_outer_edge_phy',
'hama_plate_phy',
'hama_dynode_tube_phy',
'hama_inner_ring_phy',
'MLV:nnvt_log',
'nnvt_body_log',
'nnvt_inner2_log',
'hama_log',
'hama_body_log',
'hama_inner2_log']
m = NPMeta(lines)
print(m.d)