__init__.py
1 """!
2 
3 @brief pyclustering module for samples.
4 
5 @authors Andrei Novikov (pyclustering@yandex.ru)
6 @date 2014-2020
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 
28  """!
29  @brief Answer reader for samples that are used by pyclustering library.
30 
31  """
32 
33  def __init__(self, answer_path):
34  """!
35  @brief Creates instance of answer reader to read proper clustering results of samples.
36 
37  @param[in] answer_path (string): Path to clustering results (answers).
38 
39  """
40  self.__answer_path = answer_path
41  self.__clusters = None
42  self.__noise = None
43 
44 
45  def get_clusters(self):
46  """!
47  @brief Read proper clustering results.
48 
49  @return (list) Clusters where each cluster is represented by list of index point from dataset.
50 
51  """
52  self.__read_answer()
53  return self.__clusters
54 
55 
56  def get_noise(self):
57  """!
58  @brief Read proper clustering results
59 
60  @return (list) Noise where each outlier is represented by index point from dataset.
61 
62  """
63  self.__read_answer()
64  return self.__noise
65 
66 
68  """!
69  @brief Read proper cluster lengths.
70  @details Cluster length means amount of point in a cluster.
71 
72  @return (list) Cluster lengths where each length means amount of points in a cluster.
73 
74  """
75  clusters = self.get_clusters()
76  return [len(cluster) for cluster in clusters]
77 
78 
79  def __read_answer_from_line(self, index_point, line):
80  """!
81  @brief Read information about point from the specific line and place it to cluster or noise in line with that
82  information.
83 
84  @param[in] index_point (uint): Index point that should be placed to cluster or noise.
85  @param[in] line (string): Line where information about point should be read.
86 
87  """
88 
89  if line[0] == 'n':
90  self.__noise.append(index_point)
91  else:
92  index_cluster = int(line)
93  if index_cluster >= len(self.__clusters):
94  self.__clusters.append([index_point])
95  else:
96  self.__clusters[index_cluster].append(index_point)
97 
98 
99  def __read_answer(self):
100  """!
101  @brief Read information about proper clusters and noises from the file.
102 
103  """
104 
105  if self.__clusters is not None:
106  return
107 
108  file = open(self.__answer_path, 'r')
109 
110  self.__clusters, self.__noise = [], []
111 
112  index_point = 0
113  for line in file:
114  self.__read_answer_from_line(index_point, line)
115  index_point += 1
116 
117  file.close()
Answer reader for samples that are used by pyclustering library.
Definition: __init__.py:27
def get_noise(self)
Read proper clustering results.
Definition: __init__.py:56
def get_cluster_lengths(self)
Read proper cluster lengths.
Definition: __init__.py:67
def __read_answer(self)
Read information about proper clusters and noises from the file.
Definition: __init__.py:99
def get_clusters(self)
Read proper clustering results.
Definition: __init__.py:45
def __init__(self, answer_path)
Creates instance of answer reader to read proper clustering results of samples.
Definition: __init__.py:33
def __read_answer_from_line(self, index_point, line)
Read information about point from the specific line and place it to cluster or noise in line with tha...
Definition: __init__.py:79