Completed
Push — master ( 7a46cc...a1c9a1 )
by Andrei
01:04
created

metric_wrapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 42
rs 10
c 3
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 10 2
A get_pointer() 0 2 1
A __del__() 0 4 2
A create_instance() 0 9 2
A __call__() 0 8 1
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):
0 ignored issues
show
Comprehensibility Bug introduced by
type_metric is re-defining a name which is already available in the outer-scope (previously defined on line 33).

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:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
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());