| Conditions | 16 |
| Total Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like pyclustering.utils.examples.display_two_dimensional_cluster_distances() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """! |
||
| 76 | def display_two_dimensional_cluster_distances(path_sample, amount_clusters): |
||
| 77 | distances = ['euclidian', 'manhattan', 'avr-inter', 'avr-intra', 'variance']; |
||
| 78 | |||
| 79 | ajacency = [ [0] * amount_clusters for i in range(amount_clusters) ]; |
||
| 80 | |||
| 81 | sample = utils.read_sample(path_sample); |
||
| 82 | |||
| 83 | agglomerative_instance = agglomerative(sample, amount_clusters); |
||
| 84 | agglomerative_instance.process(); |
||
| 85 | |||
| 86 | obtained_clusters = agglomerative_instance.get_clusters(); |
||
| 87 | stage = utils.draw_clusters(sample, obtained_clusters, display_result = False); |
||
| 88 | |||
| 89 | for index_cluster in range(len(ajacency)): |
||
| 90 | for index_neighbor_cluster in range(index_cluster + 1, len(ajacency)): |
||
| 91 | if ( (index_cluster == index_neighbor_cluster) or (ajacency[index_cluster][index_neighbor_cluster] is True) ): |
||
| 92 | continue; |
||
| 93 | |||
| 94 | ajacency[index_cluster][index_neighbor_cluster] = True; |
||
| 95 | ajacency[index_neighbor_cluster][index_cluster] = True; |
||
| 96 | |||
| 97 | cluster1 = obtained_clusters[index_cluster]; |
||
| 98 | cluster2 = obtained_clusters[index_neighbor_cluster]; |
||
| 99 | |||
| 100 | center_cluster1 = utils.centroid(sample, cluster1); |
||
| 101 | center_cluster2 = utils.centroid(sample, cluster2); |
||
| 102 | |||
| 103 | x_maximum, x_minimum, y_maximum, y_minimum = None, None, None, None; |
||
| 104 | x_index_maximum, y_index_maximum = 1, 1; |
||
| 105 | |||
| 106 | if (center_cluster2[0] > center_cluster1[0]): |
||
| 107 | x_maximum = center_cluster2[0]; |
||
| 108 | x_minimum = center_cluster1[0]; |
||
| 109 | x_index_maximum = 1; |
||
| 110 | else: |
||
| 111 | x_maximum = center_cluster1[0]; |
||
| 112 | x_minimum = center_cluster2[0]; |
||
| 113 | x_index_maximum = -1; |
||
| 114 | |||
| 115 | if (center_cluster2[1] > center_cluster1[1]): |
||
| 116 | y_maximum = center_cluster2[1]; |
||
| 117 | y_minimum = center_cluster1[1]; |
||
| 118 | y_index_maximum = 1; |
||
| 119 | else: |
||
| 120 | y_maximum = center_cluster1[1]; |
||
| 121 | y_minimum = center_cluster2[1]; |
||
| 122 | y_index_maximum = -1; |
||
| 123 | |||
| 124 | print("Cluster 1:", cluster1, ", center:", center_cluster1);
|
||
| 125 | print("Cluster 2:", cluster2, ", center:", center_cluster2);
|
||
| 126 | |||
| 127 | stage.annotate(s = '', xy = (center_cluster1[0], center_cluster1[1]), xytext = (center_cluster2[0], center_cluster2[1]), arrowprops = dict(arrowstyle = '<->')); |
||
| 128 | |||
| 129 | for index_distance_type in range(len(distances)): |
||
| 130 | distance = None; |
||
| 131 | distance_type = distances[index_distance_type]; |
||
| 132 | |||
| 133 | if (distance_type == 'euclidian'): |
||
| 134 | distance = utils.euclidean_distance(center_cluster1, center_cluster2); |
||
| 135 | |||
| 136 | elif (distance_type == 'manhattan'): |
||
| 137 | distance = utils.manhattan_distance(center_cluster1, center_cluster2); |
||
| 138 | |||
| 139 | elif (distance_type == 'avr-inter'): |
||
| 140 | distance = utils.average_inter_cluster_distance(cluster1, cluster2, sample); |
||
| 141 | |||
| 142 | elif (distance_type == 'avr-intra'): |
||
| 143 | distance = utils.average_intra_cluster_distance(cluster1, cluster2, sample); |
||
| 144 | |||
| 145 | elif (distance_type == 'variance'): |
||
| 146 | distance = utils.variance_increase_distance(cluster1, cluster2, sample); |
||
| 147 | |||
| 148 | print("\tCluster distance -", distance_type, ":", distance);
|
||
| 149 | |||
| 150 | x_multiplier = index_distance_type + 3; |
||
| 151 | if (x_index_maximum < 0): |
||
| 152 | x_multiplier = len(distances) - index_distance_type + 3; |
||
| 153 | |||
| 154 | y_multiplier = index_distance_type + 3; |
||
| 155 | if (y_index_maximum < 0): |
||
| 156 | y_multiplier = len(distances) - index_distance_type + 3; |
||
| 157 | |||
| 158 | x_text = x_multiplier * (x_maximum - x_minimum) / (len(distances) + 6) + x_minimum; |
||
| 159 | y_text = y_multiplier * (y_maximum - y_minimum) / (len(distances) + 6) + y_minimum; |
||
| 160 | |||
| 161 | #print(x_text, y_text, "\n"); |
||
| 162 | stage.text(x_text, y_text, distance_type + " {:.3f}".format(distance), fontsize = 9, color='blue');
|
||
| 163 | |||
| 164 | plt.show(); |
||
| 165 | |||
| 191 |