Completed
Push — 0.8.dev ( e84698...d968c0 )
by Andrei
01:00
created

metric_wrapper.__call__()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
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
34
metric_callback = CFUNCTYPE(c_double, POINTER(pyclustering_package), POINTER(pyclustering_package));
35
36
37
class metric_wrapper:
38
    def __init__(self, type_metric, arguments, func):
39
        self.__func = lambda p1, p2: func(package_extractor(p1).extract(), package_extractor(p2).extract());
40
41
        package_arguments = package_builder(arguments, c_double).create();
42
43
        ccore = ccore_library.get();
44
45
        ccore.metric_create.restype = POINTER(c_void_p);
46
47
        self.__pointer = ccore.metric_create(c_size_t(type_metric), package_arguments, metric_callback(self.__func));
48
49
50
    def __del__(self):
51
        ccore = ccore_library.get();
52
        ccore.metric_destroy(self.__pointer);
53
54
55
    def __call__(self, point1, point2):
56
        point_package1 = package_builder(point1, c_double).create();
57
        point_package2 = package_builder(point2, c_double).create();
58
59
        ccore = ccore_library.get();
60
61
        ccore.metric_calculate.restype = c_double;
62
        return ccore.metric_calculate(self.__pointer, point_package1, point_package2);