forked from microsoft/Multiverso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
77 lines (65 loc) · 2.58 KB
/
utils.py
File metadata and controls
77 lines (65 loc) · 2.58 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
#!/usr/bin/env python
# coding:utf8
import ctypes
import os
import platform
from ctypes.util import find_library
import numpy as np
PACKAGE_PATH = os.path.abspath(os.path.dirname(__file__))
class Loader(object):
'''
This loader is responsible for loading multiverso dynamic library in both
*nux and windows
'''
LIB = None
@classmethod
def _find_mv_path(cls):
if platform.system() == "Windows":
mv_lib_path = find_library("Multiverso")
if mv_lib_path is None:
print "* Fail to load Multiverso.dll from the windows $PATH."\
"Because Multiverso.dll can not be found in the $PATH "\
"directories. Go on loading Multiverso from the package."
else:
return mv_lib_path
mv_lib_path = os.path.join(PACKAGE_PATH, "Multiverso.dll")
if not os.path.exists(mv_lib_path):
print "* Fail to load Multiverso.dll from the package. Because"\
" the file " + mv_lib_path + " can not be found."
else:
return mv_lib_path
else:
mv_lib_path = find_library("multiverso")
if mv_lib_path is None:
print "* Fail to load libmultiverso.so from the system"\
"libraries. Because libmultiverso.so can't be found in"\
"library paths. Go on loading Multiverso from the package."
else:
return mv_lib_path
mv_lib_path = os.path.join(PACKAGE_PATH, "libmultiverso.so")
if not os.path.exists(mv_lib_path):
print "* Fail to load libmultiverso.so from the package. Because"\
" the file " + mv_lib_path + " can not be found."
else:
return mv_lib_path
return None
@classmethod
def load_lib(cls):
mv_lib_path = cls._find_mv_path()
if mv_lib_path is None:
print "Fail to load the multiverso library. Please make sure you"\
" have installed multiverso successfully"
else:
print "Find the multiverso library successfully(%s)" % mv_lib_path
return ctypes.cdll.LoadLibrary(mv_lib_path)
@classmethod
def get_lib(cls):
if not cls.LIB:
cls.LIB = cls.load_lib()
cls.LIB.MV_NumWorkers.restype = ctypes.c_int
return cls.LIB
def convert_data(data):
'''convert the data to float32 ndarray'''
if not isinstance(data, np.ndarray):
data = np.array(data)
return data.astype(np.float32)