-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyRauLCF.py
More file actions
89 lines (80 loc) · 2.96 KB
/
PyRauLCF.py
File metadata and controls
89 lines (80 loc) · 2.96 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
#pythonnet setup
from pythonnet import load
load("coreclr")
import clr
import os
pathDLL = os.getcwd() + "\\RauLowCountsFilter.dll"
clr.AddReference(pathDLL)
from RauLowCountsFilter import RauLCF
import numpy as np
import numpy.typing as npt
from typing import Any
import ctypes
import System
from System import Array, Int32
from System.Runtime.InteropServices import GCHandle, GCHandleType
_MAP_NP_NET = {
np.dtype('float32'): System.Single,
np.dtype('float64'): System.Double,
np.dtype('int8') : System.SByte,
np.dtype('int16') : System.Int16,
np.dtype('int32') : System.Int32,
np.dtype('int64') : System.Int64,
np.dtype('uint8') : System.Byte,
np.dtype('uint16') : System.UInt16,
np.dtype('uint32') : System.UInt32,
np.dtype('uint64') : System.UInt64,
np.dtype('bool') : System.Boolean,
}
_MAP_NET_NP = {
'Single' : np.dtype('float32'),
'Double' : np.dtype('float64'),
'SByte' : np.dtype('int8'),
'Int16' : np.dtype('int16'),
'Int32' : np.dtype('int32'),
'Int64' : np.dtype('int64'),
'Byte' : np.dtype('uint8'),
'UInt16' : np.dtype('uint16'),
'UInt32' : np.dtype('uint32'),
'UInt64' : np.dtype('uint64'),
'Boolean': np.dtype('bool'),
}
def asNetArray(npArray):
'''
Given a `numpy.ndarray` returns a CLR `System.Array`. See _MAP_NP_NET for
the mapping of Numpy dtypes to CLR types.
Note: `complex64` and `complex128` arrays are converted to `float32`
and `float64` arrays respectively with shape [m,n,...] -> [m,n,...,2]
'''
dims = npArray.shape
dtype = npArray.dtype
# For complex arrays, we must make a view of the array as its corresponding
# float type.
if dtype == np.complex64:
dtype = np.dtype('float32')
dims.append(2)
npArray = npArray.view(np.float32).reshape(dims)
elif dtype == np.complex128:
dtype = np.dtype('float64')
dims.append(2)
npArray = npArray.view(np.float64).reshape(dims)
netDims = Array.CreateInstance(Int32, npArray.ndim)
for I in range(npArray.ndim):
netDims[I] = Int32(dims[I])
if not npArray.flags.c_contiguous:
npArray = npArray.copy(order='C')
assert npArray.flags.c_contiguous
try:
netArray = Array.CreateInstance(_MAP_NP_NET[dtype], netDims)
except KeyError:
raise NotImplementedError("asNetArray does not yet support dtype {}".format(dtype))
try: # Memmove
destHandle = GCHandle.Alloc(netArray, GCHandleType.Pinned)
sourcePtr = npArray.__array_interface__['data'][0]
destPtr = destHandle.AddrOfPinnedObject().ToInt64()
ctypes.memmove(destPtr, sourcePtr, npArray.nbytes)
finally:
if destHandle.IsAllocated: destHandle.Free()
return netArray
def FindOptimalThreshold(npMatrix: np.ndarray[np.float32, np.shape([Any, Any])], vector: list[str], min: float, max: float, count: int):
return RauLCF.FindOptimalThreshold(asNetArray(npMatrix), vector, min, max, count)[0];