File: base.py

package info (click to toggle)
python-cluster 1.4.1.post3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: python: 812; makefile: 146; sh: 4
file content (69 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download | duplicates (4)
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
#
# This is part of "python-cluster". A library to group similar items together.
# Copyright (C) 2006    Michel Albert
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#


class BaseClusterMethod(object):
    """
    The base class of all clustering methods.

    :param input: a list of objects
    :distance_function: a function returning the distance - or opposite of
        similarity ``(distance = -similarity)`` - of two items from the input.
        In other words, the closer the two items are related, the smaller this
        value needs to be.  With 0 meaning they are exactly the same.

    .. note::
        The distance function should always return the absolute distance between
        two given items of the list. Say::

            distance(input[1], input[4]) = distance(input[4], input[1])

        This is very important for the clustering algorithm to work!  Naturally,
        the data returned by the distance function MUST be a comparable
        datatype, so you can perform arithmetic comparisons on them (``<`` or
        ``>``)! The simplest examples would be floats or ints. But as long as
        they are comparable, it's ok.
    """

    def __init__(self, input, distance_function, progress_callback=None):
        self.distance = distance_function
        self._input = input    # the original input
        self._data = input[:]  # clone the input so we can work with it
                               # without distroying the original data.
        self.progress_callback = progress_callback

    def topo(self):
        """
        Returns the structure (topology) of the cluster.

        See :py:meth:`~cluster.cluster.Cluster.topology` for more information.
        """
        return self.data[0].topology()

    @property
    def data(self):
        """
        Returns the data that is currently in process.
        """
        return self._data

    @property
    def raw_data(self):
        """
        Returns the raw data (data without being clustered).
        """
        return self._input