Skip to content

Commit 9854777

Browse files
committed
sorting can be enabled and disabled
1 parent b447487 commit 9854777

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

clustering_api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def loadAttribute(jsonData, attr):
100100

101101
########################################################################################################################
102102

103-
@app.route('/distances/<metric>/<datasetID>', methods=['POST'])
104-
def getDistances(metric, datasetID):
103+
@app.route('/distances/<metric>/<datasetID>/<sorted>', methods=['POST'])
104+
def getDistances(metric, datasetID, sorted):
105105
"""
106106
Compute the distances of the current stratification values to its centroid.
107107
:param metric:
@@ -118,7 +118,7 @@ def getDistances(metric, datasetID):
118118
else:
119119
return ''
120120

121-
response = getClusterDistances(data, labels, metric, externLabels)
121+
response = getClusterDistances(data, labels, metric, externLabels, sorted)
122122
return flask.jsonify(response)
123123

124124
########################################################################################################################

clustering_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def runFuzzy(data, numClusters, m, threshold):
118118

119119
########################################################################################################################
120120

121-
def getClusterDistances(data, labels, metric, externLabels = None):
121+
def getClusterDistances(data, labels, metric, externLabels = None, sorted = True):
122122
"""
123123
Compute the cluster distances in a given data among certain rows (labels)
124124
:param data: genomic data
@@ -128,7 +128,7 @@ def getClusterDistances(data, labels, metric, externLabels = None):
128128
:return: labels and distances values sorted in ascending order
129129
"""
130130
from clustering_util import computeClusterInternDistances, computeClusterExternDistances
131-
distLabels, distValues = computeClusterInternDistances(data, labels, True, metric)
131+
distLabels, distValues = computeClusterInternDistances(data, labels, sorted, metric)
132132

133133
if externLabels is not None:
134134
externDists = computeClusterExternDistances(data, distLabels, externLabels, metric)

clustering_util.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def similarityMeasurement(matrix, vector, method='euclidean'):
244244
if method == 'sqeuclidean':
245245
return euclideanDistance(matrix, vector, True)
246246

247-
spatialMethods = ['cityblock', 'chebyshev', 'canberra', 'correlation', 'hamming', 'mahalanobis', 'correlation']
247+
spatialMethods = ['cityblock', 'chebyshev', 'canberra', 'correlation', 'hamming', 'mahalanobis',]
248248

249249
if method in spatialMethods:
250250
return cdist(matrix, np.atleast_2d(vector), method).flatten()
@@ -376,7 +376,7 @@ def similarityMeasurementMatrix(matrix, method):
376376
return euclideanDistanceMatrix(matrix, True)
377377
# return squareform(pdist(matrix, method))
378378

379-
spatialMethods = ['cityblock', 'chebyshev', 'canberra', 'correlation', 'hamming', 'mahalanobis', 'correlation']
379+
spatialMethods = ['cityblock', 'chebyshev', 'canberra', 'correlation', 'hamming', 'mahalanobis']
380380

381381
if method in spatialMethods:
382382
return squareform(pdist(matrix, method))
@@ -411,12 +411,19 @@ def computeClusterInternDistances(matrix, labels, sorted=True, metric='euclidean
411411
# compute distances to centroid
412412
dists = similarityMeasurement(subMatrix, centroid, metric)
413413

414-
if sorted:
414+
if sorted == 'true':
415415
# sort values
416416
indices = range(len(dists))
417417
indices.sort(key=dists.__getitem__)
418418
dists.sort()
419419

420+
# reverse order if correlation coefficient is used
421+
# (1 means perfect correlation while -1 denotes opposite correlation)
422+
corrMetrics = ['pearson', 'spearman', 'kendall']
423+
if metric in corrMetrics:
424+
indices.reverse()
425+
dists = dists[::-1]
426+
420427
# write back to our arrays
421428
distLabels = clusterLabels[indices].tolist()
422429
distValues = dists.tolist()

0 commit comments

Comments
 (0)