1
|
|
|
"""! |
2
|
|
|
|
3
|
|
|
@brief CCORE Wrapper for metrics. |
4
|
|
|
|
5
|
|
|
@authors Andrei Novikov ([email protected]) |
6
|
|
|
@date 2014-2018 |
7
|
|
|
@copyright GNU Public License |
8
|
|
|
|
9
|
|
|
@cond GNU_PUBLIC_LICENSE |
10
|
|
|
PyClustering is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
PyClustering is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
@endcond |
23
|
|
|
|
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
from pyclustering.core.wrapper import ccore_library; |
28
|
|
|
|
29
|
|
|
from pyclustering.core.pyclustering_package import package_builder, package_extractor, pyclustering_package; |
30
|
|
|
|
31
|
|
|
from ctypes import c_double, c_size_t, POINTER, c_void_p, CFUNCTYPE; |
32
|
|
|
|
33
|
|
|
from pyclustering.utils.metric import type_metric; |
34
|
|
|
|
35
|
|
|
metric_callback = CFUNCTYPE(c_double, POINTER(pyclustering_package), POINTER(pyclustering_package)); |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class metric_wrapper: |
39
|
|
|
def __init__(self, type_metric, arguments, func): |
|
|
|
|
40
|
|
|
self.__func = lambda p1, p2: func(package_extractor(p1).extract(), package_extractor(p2).extract()); |
41
|
|
|
|
42
|
|
|
package_arguments = package_builder(arguments, c_double).create(); |
43
|
|
|
|
44
|
|
|
ccore = ccore_library.get(); |
45
|
|
|
|
46
|
|
|
ccore.metric_create.restype = POINTER(c_void_p); |
47
|
|
|
|
48
|
|
|
self.__pointer = ccore.metric_create(c_size_t(type_metric), package_arguments, metric_callback(self.__func)); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def __del__(self): |
52
|
|
|
if self.__pointer: |
53
|
|
|
ccore = ccore_library.get(); |
54
|
|
|
ccore.metric_destroy(self.__pointer); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def __call__(self, point1, point2): |
58
|
|
|
point_package1 = package_builder(point1, c_double).create(); |
59
|
|
|
point_package2 = package_builder(point2, c_double).create(); |
60
|
|
|
|
61
|
|
|
ccore = ccore_library.get(); |
62
|
|
|
|
63
|
|
|
ccore.metric_calculate.restype = c_double; |
64
|
|
|
return ccore.metric_calculate(self.__pointer, point_package1, point_package2); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def get_pointer(self): |
68
|
|
|
return self.__pointer; |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
@staticmethod |
72
|
|
|
def create_instance(metric): |
73
|
|
|
mtype = metric.get_type(); |
74
|
|
|
arguments = []; |
75
|
|
|
|
76
|
|
|
if mtype == type_metric.MINKOWSKI: |
77
|
|
|
arguments = [ metric.get_arguments().get('degree') ]; |
78
|
|
|
|
79
|
|
|
return metric_wrapper(mtype, arguments, metric.get_function()); |
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: