| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
| 45 |
self._data = input[:] |
| 46 |
|
| 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 |