-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboost_util.py
More file actions
54 lines (48 loc) · 1.56 KB
/
boost_util.py
File metadata and controls
54 lines (48 loc) · 1.56 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
import numpy, warnings, pdb
with warnings.catch_warnings():
warnings.simplefilter('ignore')
import libboost_common
def v2l(v):
l = []
if hasattr(v, 'at'):
atf = 'at'
elif hasattr(v, '__getitem__'):
atf = '__getitem__'
else:
raise RuntimeError("unrecognized vector type, has no 'at' or '__getitem__' methods")
for i in xrange(len(v)):
l.append(getattr(v, atf)(i))
return l
def l2v(l, t):
if t == numpy.float64:
v = libboost_common.pyarr_double_vec()
for i in l:
if not isinstance(i, numpy.ndarray):
raise RuntimeError("list entry not ndarray")
if i.dtype != t:
raise RuntimeError("list entry type not {}: {}".format(t, i.dtype))
v.append(i.copy())
elif t == numpy.uint8:
v = libboost_common.pyarr_unsigned_char_vec()
for i in l:
if not isinstance(i, numpy.ndarray):
raise RuntimeError("list entry not ndarray")
if i.dtype != t:
raise RuntimeError("list entry type not {}: {}".format(t, i.dtype))
v.append(i.copy())
elif t == 'unsigned int':
zz = libboost_common.uint_vec()
for i in l:
if hasattr(i, 'item'):
zz.append(numpy.asscalar(i))
else:
zz.append(i)
return zz
else:
raise RuntimeError("boost_util.l2v unrecognized type: {}".format(t))
return v
def vecpair2ltp(vp):
l = []
for p in vp:
l.append((p.first, p.second))
return l