h2lib 0.0.4__cp310-cp310-win_amd64.whl → 13.0.705__cp310-cp310-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- h2lib/HAWC2Lib.dll +0 -0
- h2lib/__init__.py +15 -0
- h2lib/_h2lib.py +346 -0
- h2lib/_version.py +4 -0
- {h2lib-0.0.4.data/purelib/h2lib → h2lib}/dll_wrapper.py +326 -236
- h2lib/h2lib_signatures.py +318 -0
- h2lib-13.0.705.dist-info/METADATA +18 -0
- h2lib-13.0.705.dist-info/RECORD +14 -0
- {h2lib-0.0.4.dist-info → h2lib-13.0.705.dist-info}/WHEEL +1 -1
- h2lib-13.0.705.dist-info/top_level.txt +2 -0
- multiclass_interface/mpi_interface.py +184 -0
- multiclass_interface/multi_object_list.py +57 -0
- multiclass_interface/multiprocess_interface.py +184 -0
- multiclass_interface/my_test_cls.py +33 -0
- h2lib-0.0.4.data/purelib/h2lib/TestLib.dll +0 -0
- h2lib-0.0.4.data/purelib/h2lib/__init__.py +0 -1
- h2lib-0.0.4.data/purelib/h2lib/_h2lib.py +0 -13
- h2lib-0.0.4.data/purelib/h2lib/calc.py +0 -21
- h2lib-0.0.4.dist-info/METADATA +0 -14
- h2lib-0.0.4.dist-info/RECORD +0 -9
- h2lib-0.0.4.dist-info/top_level.txt +0 -1
@@ -1,236 +1,326 @@
|
|
1
|
-
import numpy as np
|
2
|
-
from numpy.ctypeslib import ndpointer
|
3
|
-
import ctypes as ct
|
4
|
-
import _ctypes
|
5
|
-
|
6
|
-
import os
|
7
|
-
import ctypes
|
8
|
-
from _ctypes import POINTER
|
9
|
-
from ctypes import c_int, c_double, c_char, c_char_p, c_long
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
import
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def
|
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
|
-
def
|
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
|
-
def
|
145
|
-
|
146
|
-
|
147
|
-
f.
|
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
|
-
for
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
self.
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
return
|
233
|
-
|
234
|
-
def
|
235
|
-
|
236
|
-
|
1
|
+
import numpy as np
|
2
|
+
from numpy.ctypeslib import ndpointer
|
3
|
+
import ctypes as ct
|
4
|
+
import _ctypes
|
5
|
+
import platform
|
6
|
+
import os
|
7
|
+
import ctypes
|
8
|
+
from _ctypes import POINTER
|
9
|
+
from ctypes import c_int, c_double, c_char, c_char_p, c_long, c_longlong
|
10
|
+
from contextlib import contextmanager
|
11
|
+
try:
|
12
|
+
from ctypes import windll
|
13
|
+
except ImportError:
|
14
|
+
pass
|
15
|
+
import sys
|
16
|
+
from pathlib import Path
|
17
|
+
import atexit
|
18
|
+
c_int_p = POINTER(ctypes.c_long)
|
19
|
+
c_long_p = POINTER(ctypes.c_longlong)
|
20
|
+
|
21
|
+
c_double_p = POINTER(ctypes.c_double)
|
22
|
+
c_float_p = POINTER(ctypes.c_float)
|
23
|
+
|
24
|
+
in_use = []
|
25
|
+
|
26
|
+
|
27
|
+
class SuppressStream(object):
|
28
|
+
|
29
|
+
def __init__(self, stream=sys.stderr):
|
30
|
+
self.orig_stream_fileno = stream.fileno()
|
31
|
+
|
32
|
+
def __enter__(self):
|
33
|
+
self.orig_stream_dup = os.dup(self.orig_stream_fileno)
|
34
|
+
self.devnull = open(os.devnull, 'w')
|
35
|
+
os.dup2(self.devnull.fileno(), self.orig_stream_fileno)
|
36
|
+
|
37
|
+
def __exit__(self, type, value, traceback):
|
38
|
+
self.devnull.flush()
|
39
|
+
os.close(self.orig_stream_fileno)
|
40
|
+
os.dup2(self.orig_stream_dup, self.orig_stream_fileno)
|
41
|
+
os.close(self.orig_stream_dup)
|
42
|
+
self.devnull.close()
|
43
|
+
|
44
|
+
|
45
|
+
def suppress_output(f):
|
46
|
+
def wrap(*args, **kwargs):
|
47
|
+
if 'verbose' not in kwargs or not kwargs.pop('verbose'):
|
48
|
+
with SuppressStream(sys.stdout), SuppressStream(sys.stderr):
|
49
|
+
f(*args, **kwargs)
|
50
|
+
else:
|
51
|
+
f(*args, **kwargs)
|
52
|
+
return wrap
|
53
|
+
|
54
|
+
|
55
|
+
@contextmanager
|
56
|
+
def chdir(path):
|
57
|
+
oldpwd = os.getcwd()
|
58
|
+
os.chdir(path)
|
59
|
+
try:
|
60
|
+
yield
|
61
|
+
finally:
|
62
|
+
os.chdir(oldpwd)
|
63
|
+
|
64
|
+
|
65
|
+
def cwd(f):
|
66
|
+
def wrap(self, *args, **kwargs):
|
67
|
+
with chdir(self.model_path):
|
68
|
+
f(self, *args, **kwargs)
|
69
|
+
return wrap
|
70
|
+
|
71
|
+
|
72
|
+
def wrap(self, f, *args, **kwargs):
|
73
|
+
c_args = []
|
74
|
+
args = list(args)
|
75
|
+
for i, arg in enumerate(args):
|
76
|
+
if isinstance(arg, (list, tuple)):
|
77
|
+
if all([isinstance(e, int) for e in arg]):
|
78
|
+
# default to int64 which is default on linux but not windows
|
79
|
+
args[i] = np.array(arg, dtype=np.int64)
|
80
|
+
else:
|
81
|
+
args[i] = np.array(arg)
|
82
|
+
if isinstance(args[i], np.ndarray):
|
83
|
+
|
84
|
+
if self.fortran:
|
85
|
+
if not args[i].flags.f_contiguous:
|
86
|
+
sys.stderr.write(f'argument {i} for {f.__name__} not f_contiguous\n')
|
87
|
+
else:
|
88
|
+
if not args[i].flags.c_contiguous:
|
89
|
+
sys.stderr.write(f'argument {i} for {f.__name__} not c_contiguous\n')
|
90
|
+
|
91
|
+
args[i] = np.require(args[i], requirements=['C', 'F'][self.fortran])
|
92
|
+
|
93
|
+
for arg in args:
|
94
|
+
if isinstance(arg, int):
|
95
|
+
c_args.append(c_long_p(c_longlong(arg)))
|
96
|
+
elif isinstance(arg, float):
|
97
|
+
c_args.append(c_double_p(c_double(arg)))
|
98
|
+
elif isinstance(arg, str):
|
99
|
+
c_args.append(c_char_p(arg.encode('cp1252')))
|
100
|
+
# c_args.append(c_int_p(c_int(len(arg))))
|
101
|
+
|
102
|
+
elif isinstance(arg, np.ndarray):
|
103
|
+
if arg.dtype in [np.int32]:
|
104
|
+
c_args.append(arg.ctypes.data_as(c_int_p))
|
105
|
+
elif arg.dtype in [np.int64]:
|
106
|
+
c_args.append(arg.ctypes.data_as(c_long_p))
|
107
|
+
elif arg.dtype == np.float64:
|
108
|
+
c_args.append(arg.ctypes.data_as(c_double_p))
|
109
|
+
elif arg.dtype == np.float32:
|
110
|
+
c_args.append(arg.ctypes.data_as(c_float_p))
|
111
|
+
else:
|
112
|
+
raise NotImplementedError(arg.dtype)
|
113
|
+
|
114
|
+
else:
|
115
|
+
# raise NotImplementedError(arg.__class__.__name__)
|
116
|
+
c_args.append(arg)
|
117
|
+
if 'restype' in kwargs:
|
118
|
+
restype = kwargs['restype']
|
119
|
+
if hasattr(restype, 'dtype'):
|
120
|
+
restype = np.ctypeslib.as_ctypes_type(restype)
|
121
|
+
f.restype = restype
|
122
|
+
with chdir(self.cwd):
|
123
|
+
if self.suppress_output:
|
124
|
+
with SuppressStream(sys.stdout), SuppressStream(sys.stderr):
|
125
|
+
res = f(*c_args)
|
126
|
+
else:
|
127
|
+
res = f(*c_args)
|
128
|
+
ret_args = []
|
129
|
+
for arg in args:
|
130
|
+
c_arg = c_args.pop(0)
|
131
|
+
if isinstance(arg, (int, float)):
|
132
|
+
ret_args.append(c_arg.contents.value)
|
133
|
+
elif isinstance(arg, (str)):
|
134
|
+
ret_args.append(c_arg.value.decode('cp1252'))
|
135
|
+
# c_args.pop(0)
|
136
|
+
elif isinstance(arg, np.ndarray):
|
137
|
+
ret_args.append(arg)
|
138
|
+
else:
|
139
|
+
raise NotImplementedError(arg.__class__.__name__)
|
140
|
+
return ret_args, res
|
141
|
+
|
142
|
+
|
143
|
+
class DLLWrapper(object):
|
144
|
+
def __init__(self, filename, cwd='.', cdecl=True, fortran=True):
|
145
|
+
self.filename = str(filename)
|
146
|
+
if os.path.abspath(self.filename) in in_use:
|
147
|
+
raise Exception(f'{os.path.abspath(self.filename)} already in use in current process.')
|
148
|
+
self.cwd = cwd
|
149
|
+
self.cdecl = cdecl
|
150
|
+
self.fortran = fortran
|
151
|
+
self.suppress_output = False
|
152
|
+
self.open()
|
153
|
+
in_use.append(os.path.abspath(self.filename))
|
154
|
+
atexit.register(self.close)
|
155
|
+
|
156
|
+
@staticmethod
|
157
|
+
def find_dll(path, name):
|
158
|
+
p = Path(path)
|
159
|
+
|
160
|
+
# if sys.platform == "win32":
|
161
|
+
# prefixes = ['']
|
162
|
+
# if sys.maxsize > 2**32:
|
163
|
+
# suffixes = ['.dll', '_64.dll']
|
164
|
+
# else:
|
165
|
+
# suffixes = ['.dll']
|
166
|
+
# elif sys.platform == 'linux':
|
167
|
+
# prefixes = ['lib','']
|
168
|
+
# suffixes = ['.so']
|
169
|
+
# else:
|
170
|
+
# raise NotImplementedError()
|
171
|
+
|
172
|
+
dll_lst = []
|
173
|
+
file_patterns = ['*%s*.dll' % name, '*%s*.so' % name]
|
174
|
+
for fp in file_patterns:
|
175
|
+
dll_lst.extend(list(p.glob("**/" + fp)))
|
176
|
+
|
177
|
+
def use_first(dll_lst):
|
178
|
+
f = str(dll_lst[0])
|
179
|
+
print("Using ", os.path.abspath(f))
|
180
|
+
return DLLWrapper(f)
|
181
|
+
|
182
|
+
if len(dll_lst) == 1:
|
183
|
+
return use_first(dll_lst)
|
184
|
+
elif len(dll_lst) > 1:
|
185
|
+
# check if excluding dlls in hawc2-binary, i.e. "hawc2-<platform>" results in one dll
|
186
|
+
dll_lst2 = [d for d in dll_lst if not str(d).startswith('hawc2-')]
|
187
|
+
if len(dll_lst2) == 1:
|
188
|
+
return use_first(dll_lst2)
|
189
|
+
raise FileExistsError("Multiple dlls found:\n" + "\n".join([str(p) for p in dll_lst]))
|
190
|
+
else:
|
191
|
+
raise FileNotFoundError("No " + " or ".join(file_patterns) +
|
192
|
+
" files found in " + os.path.abspath(p.absolute()))
|
193
|
+
|
194
|
+
def open(self):
|
195
|
+
assert os.path.isfile(self.filename), os.path.abspath(self.filename)
|
196
|
+
if self.cdecl:
|
197
|
+
try:
|
198
|
+
# python < (3, 8) and > 3.10?:
|
199
|
+
self.lib = ct.CDLL(self.filename)
|
200
|
+
except BaseException:
|
201
|
+
self.lib = ct.CDLL(self.filename, winmode=ctypes.DEFAULT_MODE)
|
202
|
+
else:
|
203
|
+
self.lib = windll.LoadLibrary(self.filename)
|
204
|
+
|
205
|
+
def close(self):
|
206
|
+
if "FreeLibrary" in dir(_ctypes):
|
207
|
+
_ctypes.FreeLibrary(self.lib._handle)
|
208
|
+
else:
|
209
|
+
_ctypes.dlclose(self.lib._handle)
|
210
|
+
del self.lib
|
211
|
+
atexit.unregister(self.close)
|
212
|
+
in_use.remove(os.path.abspath(self.filename))
|
213
|
+
|
214
|
+
# def __enter__(self):
|
215
|
+
# self.open()
|
216
|
+
# return self
|
217
|
+
#
|
218
|
+
# def __exit__(self, type, value, traceback):
|
219
|
+
# self.close()
|
220
|
+
# return False
|
221
|
+
|
222
|
+
def __getattr__(self, name):
|
223
|
+
if name == 'lib':
|
224
|
+
raise Exception("DLL not loaded. Run using: 'with dll: ...'")
|
225
|
+
return self.get_lib_function(name)
|
226
|
+
|
227
|
+
def get_lib_function(self, name):
|
228
|
+
try:
|
229
|
+
f = getattr(self.lib, name)
|
230
|
+
except AttributeError as e:
|
231
|
+
raise AttributeError("Attribute '%s' not found in dll ('%s')" % (name, self.filename))
|
232
|
+
return lambda *args, **kwargs: wrap(self, f, *args, **kwargs)
|
233
|
+
|
234
|
+
def version(self, function_name='get_version'):
|
235
|
+
try:
|
236
|
+
f = getattr(self.lib, function_name)
|
237
|
+
f.argtypes = [c_char_p, c_long]
|
238
|
+
s = "".ljust(255)
|
239
|
+
arg = c_char_p(s.encode('utf-8'))
|
240
|
+
f(arg, len(s))
|
241
|
+
return arg.value.decode().strip()
|
242
|
+
except AttributeError:
|
243
|
+
if function_name == 'get_version':
|
244
|
+
return self.version('version')
|
245
|
+
|
246
|
+
def getFileProperties(self):
|
247
|
+
if sys.platform != "win32":
|
248
|
+
raise OSError("Only supported for Windows")
|
249
|
+
import win32api
|
250
|
+
fname = self.filename
|
251
|
+
|
252
|
+
# ==============================================================================
|
253
|
+
"""
|
254
|
+
Read all properties of the given file return them as a dictionary.
|
255
|
+
"""
|
256
|
+
propNames = ('Comments', 'InternalName', 'ProductName',
|
257
|
+
'CompanyName', 'LegalCopyright', 'ProductVersion',
|
258
|
+
'FileDescription', 'LegalTrademarks', 'PrivateBuild',
|
259
|
+
'FileVersion', 'OriginalFilename', 'SpecialBuild')
|
260
|
+
|
261
|
+
props = {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
|
262
|
+
|
263
|
+
try:
|
264
|
+
# backslash as parm returns dictionary of numeric info corresponding to VS_FIXEDFILEINFO struc
|
265
|
+
fixedInfo = win32api.GetFileVersionInfo(fname, '\\')
|
266
|
+
props['FixedFileInfo'] = fixedInfo
|
267
|
+
props['FileVersion'] = "%d.%d.%d.%d" % (fixedInfo['FileVersionMS'] / 65536,
|
268
|
+
fixedInfo['FileVersionMS'] % 65536, fixedInfo['FileVersionLS'] / 65536,
|
269
|
+
fixedInfo['FileVersionLS'] % 65536)
|
270
|
+
|
271
|
+
# \VarFileInfo\Translation returns list of available (language, codepage)
|
272
|
+
# pairs that can be used to retreive string info. We are using only the first pair.
|
273
|
+
lang, codepage = win32api.GetFileVersionInfo(fname, '\\VarFileInfo\\Translation')[0]
|
274
|
+
|
275
|
+
# any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle
|
276
|
+
# two are language/codepage pair returned from above
|
277
|
+
|
278
|
+
strInfo = {}
|
279
|
+
for propName in propNames:
|
280
|
+
strInfoPath = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, propName)
|
281
|
+
# print str_info
|
282
|
+
strInfo[propName] = win32api.GetFileVersionInfo(fname, strInfoPath)
|
283
|
+
|
284
|
+
props['StringFileInfo'] = strInfo
|
285
|
+
except BaseException:
|
286
|
+
pass
|
287
|
+
|
288
|
+
return props
|
289
|
+
|
290
|
+
|
291
|
+
class Type2DllWrapper(DLLWrapper):
|
292
|
+
def __init__(self, filename, dll_subroutine_init, dll_subroutine_update,
|
293
|
+
arraysizes_init, arraysizes_update,
|
294
|
+
init_array):
|
295
|
+
super().__init__(filename)
|
296
|
+
self.dll_subroutine_init = dll_subroutine_init
|
297
|
+
self.dll_subroutine_update = dll_subroutine_update
|
298
|
+
self.arraysizes_init = arraysizes_init
|
299
|
+
self.arraysizes_update = arraysizes_update
|
300
|
+
self.init_array = init_array
|
301
|
+
|
302
|
+
def open(self):
|
303
|
+
DLLWrapper.open(self)
|
304
|
+
self.init()
|
305
|
+
|
306
|
+
def call(self, name, array, n1, n2):
|
307
|
+
f = getattr(self.lib, name)
|
308
|
+
f.argtypes = [ndpointer(shape=n1, dtype=ct.c_double, flags='FORTRAN'),
|
309
|
+
ndpointer(shape=n2, dtype=ct.c_double, flags='FORTRAN')]
|
310
|
+
f.restype = None
|
311
|
+
|
312
|
+
pad_array = np.zeros(n1)
|
313
|
+
pad_array[:len(array)] = array
|
314
|
+
arg1 = np.array(pad_array, dtype=ct.c_double, order='F')
|
315
|
+
arg2 = np.zeros(n2, dtype=ct.c_double, order='F')
|
316
|
+
|
317
|
+
f(arg1, arg2)
|
318
|
+
return arg2
|
319
|
+
|
320
|
+
def init(self):
|
321
|
+
n1, n2 = self.arraysizes_init
|
322
|
+
return self.call(self.dll_subroutine_init, self.init_array, n1, n2)
|
323
|
+
|
324
|
+
def update(self, array):
|
325
|
+
n1, n2 = self.arraysizes_update
|
326
|
+
return self.call(self.dll_subroutine_update, array, n1, n2)
|