MCPcopy Create free account
hub / github.com/brainflow-dev/brainflow / __init__

Method __init__

python_package/brainflow/ml_model.py:71–161  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

69 return cls.__instance
70
71 def __init__(self):
72 if platform.system() == 'Windows':
73 if struct.calcsize("P") * 8 == 64:
74 dll_path = 'lib\\MLModule.dll'
75 else:
76 dll_path = 'lib\\MLModule32.dll'
77 elif platform.system() == 'Darwin':
78 dll_path = 'lib/libMLModule.dylib'
79 else:
80 dll_path = 'lib/libMLModule.so'
81 try:
82 resource = files(__name__).joinpath(dll_path)
83 full_path = str(resource)
84 except (TypeError, AttributeError):
85 # Fallback for:
86 # 1. Python < 3.9 (importlib.resources.files not available)
87 # 2. NixOS/packaging edge cases where importlib.resources may not work
88 import pkg_resources
89 full_path = pkg_resources.resource_filename(__name__, dll_path)
90 if os.path.isfile(full_path):
91 # for python we load dll by direct path but this dll may depend on other dlls and they will not be found!
92 # to solve it we can load all of them before loading the main one or change PATH\LD_LIBRARY_PATH env var.
93 # env variable looks better, since it can be done only once for all dependencies
94 dir_path = os.path.abspath(os.path.dirname(full_path))
95 try:
96 os.add_dll_directory(dir_path)
97 except (AttributeError, OSError):
98 # Ignore when API is unavailable or directory registration fails;
99 # PATH/LD_LIBRARY_PATH fallback is applied below.
100 pass
101 if platform.system() == 'Windows':
102 os.environ['PATH'] = dir_path + os.pathsep + os.environ.get('PATH', '')
103 else:
104 os.environ['LD_LIBRARY_PATH'] = dir_path + os.pathsep + os.environ.get('LD_LIBRARY_PATH', '')
105 self.lib = ctypes.cdll.LoadLibrary(full_path)
106 else:
107 raise FileNotFoundError(
108 'Dynamic library %s is missed, did you forget to compile brainflow before installation of python package?' % full_path)
109
110 self.set_log_level_ml_module = self.lib.set_log_level_ml_module
111 self.set_log_level_ml_module.restype = ctypes.c_int
112 self.set_log_level_ml_module.argtypes = [
113 ctypes.c_int
114 ]
115
116 self.set_log_file_ml_module = self.lib.set_log_file_ml_module
117 self.set_log_file_ml_module.restype = ctypes.c_int
118 self.set_log_file_ml_module.argtypes = [
119 ctypes.c_char_p
120 ]
121
122 self.log_message_ml_module = self.lib.log_message_ml_module
123 self.log_message_ml_module.restype = ctypes.c_int
124 self.log_message_ml_module.argtypes = [
125 ctypes.c_int,
126 ctypes.c_char_p
127 ]
128

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected