Debian Codegraph @

cluster/method/base.py

raw symbols SCIP
1 #
2 # This is part of "python-cluster". A library to group similar items together.
3 # Copyright (C) 2006 Michel Albert
4 #
5 # This library is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation; either version 2.1 of the License, or (at your
8 # option) any later version.
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation,
15 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #
17
18
19 class BaseClusterMethod(object):
20 """
21 The base class of all clustering methods.
22
23 :param input: a list of objects
24 :distance_function: a function returning the distance - or opposite of
25 similarity ``(distance = -similarity)`` - of two items from the input.
26 In other words, the closer the two items are related, the smaller this
27 value needs to be. With 0 meaning they are exactly the same.
28
29 .. note::
30 The distance function should always return the absolute distance between
31 two given items of the list. Say::
32
33 distance(input[1], input[4]) = distance(input[4], input[1])
34
35 This is very important for the clustering algorithm to work! Naturally,
36 the data returned by the distance function MUST be a comparable
37 datatype, so you can perform arithmetic comparisons on them (``<`` or
38 ``>``)! The simplest examples would be floats or ints. But as long as
39 they are comparable, it's ok.
40 """
41
42 def __init__(self, input, distance_function, progress_callback=None):
43 self.distance = distance_function
44 self._input = input # the original input
45 self._data = input[:] # clone the input so we can work with it
46 # without distroying the original data.
47 self.progress_callback = progress_callback
48
49 def topo(self):
50 """
51 Returns the structure (topology) of the cluster.
52
53 See :py:meth:`~cluster.cluster.Cluster.topology` for more information.
54 """
55 return self.data[0].topology()
56
57 @property
58 def data(self):
59 """
60 Returns the data that is currently in process.
61 """
62 return self._data
63
64 @property
65 def raw_data(self):
66 """
67 Returns the raw data (data without being clustered).
68 """
69 return self._input