Author: scooter
Date: 2012-02-06 14:35:55 -0800 (Mon, 06 Feb 2012)
New Revision: 28199
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/resources/plugin.props
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterAlgorithm.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterer.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KCluster.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KMeansCluster.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMCluster.java
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMedoidCluster.java
Log:
Added David's assignment changes
Modified: csplugins/trunk/ucsf/scooter/clusterMaker/resources/plugin.props
===================================================================
--- csplugins/trunk/ucsf/scooter/clusterMaker/resources/plugin.props
2012-02-06 22:29:38 UTC (rev 28198)
+++ csplugins/trunk/ucsf/scooter/clusterMaker/resources/plugin.props
2012-02-06 22:35:55 UTC (rev 28199)
@@ -13,7 +13,7 @@
pluginDescription=Cluster Nodes and/or Edges
# Plugin version number, this must be two numbers separated by a decimlal.
Ex. 0.2, 14.03
-pluginVersion=1.10
+pluginVersion=1.11
# Compatible Cytoscape version
cytoscapeVersion=2.8
@@ -31,4 +31,4 @@
pluginAuthorsIntsitutions=John "Scooter" Morris, Leonard Apeltsin:UCSF;Aaron
Newman:UCSB;Jan Baumbach:Max Planck Institut fur Informatik;Tobias Wittkop:Buck
Institute;Gang Su:University of Michigan;Gary Bader:University of Toronto
# Date this plugin/plugin version was released
-releaseDate=April 10, 2011
+releaseDate=October 23, 2011
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterAlgorithm.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterAlgorithm.java
2012-02-06 22:29:38 UTC (rev 28198)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterAlgorithm.java
2012-02-06 22:35:55 UTC (rev 28199)
@@ -71,6 +71,7 @@
protected Integer[] rowOrder;
protected String weightAttributes[] = null;
protected int kMax = -1;
+ protected boolean initializeNearCenter = false;
private SilhouetteResult[] silhouetteResults = null;
protected boolean adjustDiagonals = false;
@@ -95,6 +96,7 @@
public void setUseSilhouette(boolean val) { useSilhouette = val; }
public void setKMax(int val) { kMax = val; }
public void setClusterInterface(AbstractClusterAlgorithm alg) {
clusterAlgorithm = alg; }
+ public void setInitializeNearCenter(boolean val) { initializeNearCenter
= val; }
/**
* This method is called by all of the attribute cluster algorithms to
update the
@@ -383,7 +385,58 @@
logger.info(resultString);
return resultString;
}
+
+ protected int[] chooseRandomElementsAsCenters(int nElements, int
nClusters) {
+ int[] centers = new int[nClusters];
+ for (int i = 0; i < nClusters; i++) {
+ centers[i] = (int) Math.floor(Math.random() *
nElements);
+ }
+ return centers;
+ }
+
+ protected int[] chooseCentralElementsAsCenters(int nElements, int
nClusters, double[][] distances) {
+ int[] centers = new int[nClusters];
+
+ // calculate normalized distances
+ double[][] normalized = new double[nElements][nElements];
+ for (int i = 0; i < nElements; i++) {
+ double sum = 0;
+ for (int j = 0; j < nElements; j++) {
+ double x = distances[i][j];
+ normalized[i][j] = x;
+ sum += x;
+ }
+ for (int j = 0; j < nElements; j++) {
+ normalized[i][j] /= sum;
+ }
+ }
+
+ // sum the normalized distances across all rows
+ // setup key-value pairs with summed normalized distances as
keys
+ // and element indices as values
+ KeyValuePair[] pairs = new KeyValuePair[nElements];
+ for (int i = 0; i < nElements; i++) {
+ pairs[i] = new KeyValuePair(0.0, i);
+ for (int j = 0; j < nElements; j++) {
+ pairs[i].key += normalized[i][j];
+ }
+ }
+
+ // sort the summed normalized distances
+ // for choosing the elements that are closest overall to all
other elements
+ Comparator<KeyValuePair> comparator = new
KeyValuePairComparator();
+ Arrays.sort(pairs, comparator);
+
+ // initialize the centers
+ for (int i = 0; i < nClusters; i++) {
+ centers[i] = pairs[i].value;
+ //System.out.println("i = " + i + ", center = " +
centers[i]);
+ }
+
+ return centers;
+ }
+
private void renumberClusters(int nClusters, int [] clusters) {
int[] clusterSizes = new int[nClusters];
Arrays.fill(clusterSizes, 0);
@@ -410,17 +463,6 @@
}
- class SizeComparator implements Comparator <Integer> {
- int[] sizeArray = null;
- public SizeComparator(int[] a) { this.sizeArray = a; }
-
- public int compare(Integer o1, Integer o2) {
- if (sizeArray[o1] > sizeArray[o2]) return 1;
- if (sizeArray[o1] < sizeArray[o2]) return -1;
- return 0;
- }
- }
-
private void runThreadedSilhouette(int kMax, int nIterations, int
nThreads, TaskMonitor saveMonitor) {
// Set up the thread pools
ExecutorService[] threadPools = new ExecutorService[nThreads];
@@ -460,6 +502,39 @@
return false;
}
+ // private class pairing key and and value
+ // abandon generic here and hard-code types, since arrays and generics
do not work well in Java!
+ private class KeyValuePair {
+ public double key;
+ public int value;
+
+ public KeyValuePair(double key, int value) {
+ this.key = key;
+ this.value = value;
+ }
+ }
+
+ // private class comparator for sorting key-value pairs
+ private class KeyValuePairComparator implements
Comparator<KeyValuePair> {
+ public int compare(KeyValuePair a, KeyValuePair b) {
+ if ((Double)a.key < (Double)b.key) {
+ return -1;
+ }
+ return 1;
+ }
+ }
+
+ private class SizeComparator implements Comparator <Integer> {
+ int[] sizeArray = null;
+ public SizeComparator(int[] a) { this.sizeArray = a; }
+
+ public int compare(Integer o1, Integer o2) {
+ if (sizeArray[o1] > sizeArray[o2]) return 1;
+ if (sizeArray[o1] < sizeArray[o2]) return -1;
+ return 0;
+ }
+ }
+
private class RunKMeans implements Runnable {
Matrix matrix;
int[] clusters;
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterer.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterer.java
2012-02-06 22:29:38 UTC (rev 28198)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/AbstractAttributeClusterer.java
2012-02-06 22:35:55 UTC (rev 28199)
@@ -64,6 +64,7 @@
protected boolean adjustDiagonals = false;
protected boolean zeroMissing = false;
protected boolean useSilhouette = false;
+ protected boolean initializeNearCenter = false;
protected int kMax = 0;
protected int kNumber = 0;
protected TaskMonitor monitor = null;
@@ -132,6 +133,11 @@
(Object)null, (Object)null, 0);
if (useSilhouette) t.setImmutable(true);
clusterProperties.add(t);
+
+ // Whether to initialize cluster centers by choosing the most
central elements
+ clusterProperties.add(new Tunable("initializeNearCenter",
+ "Initialize cluster
centers from most central elements",
+ Tunable.BOOLEAN, new
Boolean(initializeNearCenter)));
}
protected void updateKTunables(boolean force) {
@@ -146,6 +152,11 @@
t = clusterProperties.get("kNumber");
if ((t != null) && (t.valueChanged() || force))
kNumber = ((Integer) t.getValue()).intValue();
+
+ t = clusterProperties.get("initializeNearCenter");
+ if ((t != null) && (t.valueChanged() || force)) {
+ initializeNearCenter = ((Boolean)
t.getValue()).booleanValue();
+ }
}
protected void updateKEstimates() {
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KCluster.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KCluster.java
2012-02-06 22:29:38 UTC (rev 28198)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KCluster.java
2012-02-06 22:35:55 UTC (rev 28199)
@@ -76,106 +76,6 @@
resetAttributes();
}
-/*
- public String cluster(int nClusters, int nIterations, boolean
transpose) {
- String keyword = "GENE";
- if (transpose) keyword = "ARRY";
-
- for (int att = 0; att < weightAttributes.length; att++)
- if (debug)
- logger.debug("Attribute:
'"+weightAttributes[att]+"'");
-
- if (monitor != null)
- monitor.setStatus("Creating distance matrix");
-
- // Create the matrix
- matrix = new Matrix(weightAttributes, transpose, ignoreMissing,
selectedOnly);
- logger.info("cluster matrix has "+matrix.nRows()+" rows");
-
- // Create a weight vector of all ones (we don't use individual
weighting, yet)
- matrix.setUniformWeights();
-
- if (monitor != null)
- monitor.setStatus("Clustering...");
-
- if (useSilhouette) {
- TaskMonitor saveMonitor = monitor;
- monitor = null;
-
- silhouetteResults = new SilhouetteResult[kMax];
-
- int nThreads =
Runtime.getRuntime().availableProcessors()-1;
- if (nThreads > 1)
- runThreadedSilhouette(kMax, nIterations,
nThreads);
- else
- runLinearSilhouette(kMax, nIterations);
-
- // Now get the results and find our best k
- double maxSil = Double.MIN_VALUE;
- for (int kEstimate = 2; kEstimate < kMax; kEstimate++) {
- double sil =
silhouetteResults[kEstimate].getAverageSilhouette();
- // System.out.println("Average silhouette for
"+kEstimate+" clusters is "+sil);
- if (sil > maxSil) {
- maxSil = sil;
- nClusters = kEstimate;
- }
- }
- monitor = saveMonitor;
- // System.out.println("maxSil = "+maxSil+" nClusters =
"+nClusters);
- }
-
- int[] clusters = new int[matrix.nRows()];
- // Cluster
- int ifound = kmeans(nClusters, nIterations, matrix, metric,
clusters);
-
- // OK, now run our silhouette on our final result
- SilhouetteResult sResult =
SilhouetteUtil.SilhouetteCalculator(matrix, metric, clusters);
- // System.out.println("Average silhouette =
"+sResult.getAverageSilhouette());
- // SilhouetteUtil.printSilhouette(sResult, clusters);
-
- if (!interimRun) {
- if (!matrix.isTransposed())
- createGroups(nClusters, clusters);
- rowOrder = matrix.indexSort(clusters, clusters.length);
- // Update the network attributes
- updateAttributes("kmeans");
- }
-
- return "Created "+nClusters+" clusters with average silhouette
= "+sResult.getAverageSilhouette();
- }
-
- private void runThreadedSilhouette(int kMax, int nIterations, int
nThreads) {
- // Set up the thread pools
- ExecutorService[] threadPools = new ExecutorService[nThreads];
- for (int pool = 0; pool < threadPools.length; pool++)
- threadPools[pool] = Executors.newFixedThreadPool(1);
-
- // Dispatch a kmeans calculation to each pool
- for (int kEstimate = 2; kEstimate < kMax; kEstimate++) {
- int[] clusters = new int[matrix.nRows()];
- Runnable r = new RunKMeans(matrix, clusters, kEstimate,
nIterations);
- threadPools[(kEstimate-2)%nThreads].submit(r);
- // threadPools[0].submit(r);
- }
-
- // OK, now wait for each thread to complete
- for (int pool = 0; pool < threadPools.length; pool++) {
- threadPools[pool].shutdown();
- try {
- boolean result =
threadPools[pool].awaitTermination(7, TimeUnit.DAYS);
- } catch (Exception e) {}
- }
- }
-
- private void runLinearSilhouette(int kMax, int nIterations) {
- for (int kEstimate = 2; kEstimate < kMax; kEstimate++) {
- int[] clusters = new int[matrix.nRows()];
- int ifound = kmeans(kEstimate, nIterations, matrix,
metric, clusters);
- silhouetteResults[kEstimate] =
SilhouetteUtil.SilhouetteCalculator(matrix, metric, clusters);
- }
- }
-*/
-
// The kmeans implementation of a k-clusterer
public int kcluster(int nClusters, int nIterations, Matrix matrix,
DistanceMetric metric, int[] clusterID) {
if (monitor != null)
@@ -221,8 +121,15 @@
int period = 10;
// Randomly assign elements to clusters
- if (nIterations != 0) randomAssign(nClusters,
nelements, tclusterid);
- // if (nIterations != 0) debugAssign(nClusters,
nelements, tclusterid);
+ if (nIterations != 0) {
+ if (!initializeNearCenter) {
+ // Use the cluster 3.0 version to be
consistent
+ randomAssign(nClusters, nelements,
tclusterid);
+ // if (nIterations != 0)
debugAssign(nClusters, nelements, tclusterid);
+ } else {
+ tclusterid =
chooseCentralElementsAsCenters(nelements, nClusters,
matrix.getDistanceMatrix(metric));
+ }
+ }
// Initialize
for (int i = 0; i < nClusters; i++) counts[i] = 0;
@@ -512,57 +419,7 @@
}
}
- /**
- * This routine returns a uniform random number between 0.0 and 1.0.
Both 0.0
- * and 1.0 are excluded. This random number generator is described in:
- *
- * Pierre l'Ecuyer
- * Efficient and Portable Combined Random Number Generators
- * Communications of the ACM, Volume 31, Number 6, June 1988, pages
742-749,774.
- *
- * The first time this routine is called, it initializes the random
number
- * generator using the current time. First, the current epoch time in
seconds is
- * used as a seed for the random number generator in the C library. The
first two
- * random numbers generated by this generator are used to initialize
the random
- * number generator implemented in this routine.
- *
- * NOTE: how different is this from Java's Math.random() or
Random.nextDouble()?
- *
- * @return A double-precison number between 0.0 and 1.0.
- */
-/*
private double uniform() {
- int z;
- int m1 = 2147483563;
- int m2 = 2147483399;
- double scale = 1.0/m1;
-
- if (seed1==0 || seed2==0) // initialize
- {
- Date date = new Date();
- int initseed = (int) date.getTime();
- Random r = new Random(initseed);
- seed1 = r.nextInt();
- seed2 = r.nextInt();
- }
-
- do
- {
- int k;
- k = seed1/53668;
- seed1 = 40014*(seed1-k*53668)-k*12211;
- if (seed1 < 0) seed1+=m1;
- k = seed2/52774;
- seed2 = 40692*(seed2-k*52774)-k*3791;
- if(seed2 < 0) seed2+=m2;
- z = seed1-seed2;
- if(z < 1) z+=(m1-1);
- } while (z==m1); // To avoid returning 1.0
-
- return z*scale;
- }
-*/
- private double uniform() {
if (random == null) {
// Date date = new Date();
// random = new Random(date.getTime());
@@ -572,27 +429,4 @@
return random.nextDouble();
}
-/*
- class RunKMeans implements Runnable {
- Matrix matrix;
- int[] clusters;
- int kEstimate;
- int nIterations;
-
- public RunKMeans (Matrix matrix, int[] clusters, int k, int
nIterations) {
- this.matrix = matrix;
- this.clusters = clusters;
- this.kEstimate = k;
- this.nIterations = nIterations;
- }
-
- public void run() {
- int[] clusters = new int[matrix.nRows()];
- int ifound = kmeans(kEstimate, nIterations, matrix,
metric, clusters);
- try {
- silhouetteResults[kEstimate] =
SilhouetteUtil.SilhouetteCalculator(matrix, metric, clusters);
- } catch (Exception e) { e.printStackTrace(); }
- }
- }
-*/
}
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KMeansCluster.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KMeansCluster.java
2012-02-06 22:29:38 UTC (rev 28198)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmeans/KMeansCluster.java
2012-02-06 22:35:55 UTC (rev 28199)
@@ -141,6 +141,13 @@
clusterProperties.add(new Tunable("createGroups",
"Create groups from
clusters",
Tunable.BOOLEAN, new
Boolean(createGroups)));
+
+ /*
+ // Whether to initialize cluster centers by choosing the most
central elements
+ clusterProperties.add(new Tunable("initializeNearCenter",
+ "Initialize cluster
centers from most central elements",
+ Tunable.BOOLEAN, new
Boolean(initializeNearCenter)));
+ */
clusterProperties.initializeProperties();
updateSettings(true);
@@ -187,6 +194,13 @@
if ((t != null) && (t.valueChanged() || force)) {
dataAttributes = (String) t.getValue();
}
+
+ /*
+ t = clusterProperties.get("initializeNearCenter");
+ if ((t != null) && (t.valueChanged() || force)) {
+ initializeNearCenter = ((Boolean)
t.getValue()).booleanValue();
+ }
+ */
}
public void doCluster(TaskMonitor monitor) {
@@ -217,6 +231,7 @@
algorithm.setDebug(debug);
algorithm.setUseSilhouette(useSilhouette);
algorithm.setKMax(kMax);
+ //algorithm.setInitializeNearCenter(initializeNearCenter);
algorithm.setClusterInterface(this);
String resultsString = "K-Means results:";
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMCluster.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMCluster.java
2012-02-06 22:29:38 UTC (rev 28198)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMCluster.java
2012-02-06 22:35:55 UTC (rev 28199)
@@ -36,6 +36,7 @@
import java.lang.Math;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -75,44 +76,6 @@
resetAttributes();
}
-/*
- public String cluster(int nClusters, int nIterations, boolean
transpose) {
- String keyword = "GENE";
- if (transpose) keyword = "ARRY";
- debug = true;
-
- for (int att = 0; att < weightAttributes.length; att++)
- if (debug)
- logger.debug("Attribute:
'"+weightAttributes[att]+"'");
-
- if (monitor != null)
- monitor.setStatus("Creating distance matrix");
-
- // Create the matrix
- matrix = new Matrix(weightAttributes, transpose, ignoreMissing,
selectedOnly);
- logger.info("cluster matrix has "+matrix.nRows()+" rows");
-
- // Create a weight vector of all ones (we don't use individual
weighting, yet)
- matrix.setUniformWeights();
-
- int[] clusters = new int[matrix.nRows()];
-
- if (monitor != null)
- monitor.setStatus("Clustering...");
-
- // Cluster
- int ifound = kmedoid(nClusters, nIterations, matrix, metric,
clusters);
- if (!interimRun) {
- if (!matrix.isTransposed())
- createGroups(nClusters, clusters);
- rowOrder = matrix.indexSort(clusters, clusters.length);
- updateAttributes("kmedoid");
- }
-
- return "Complete";
- }
-*/
-
public int kcluster(int nClusters, int nIterations, Matrix matrix,
DistanceMetric metric, int[] clusterId) {
if (monitor != null)
@@ -128,7 +91,12 @@
}
}
- int[] centers = chooseRandomElementsAsCenters(matrix,
nClusters);
+ int[] centers;
+ if (initializeNearCenter) {
+ centers =
chooseCentralElementsAsCenters(matrix.nRows(), nClusters, distances);
+ } else {
+ centers = chooseRandomElementsAsCenters(matrix.nRows(),
nClusters);
+ }
int[] oldCenters = null;
// outputCenters(centers);
@@ -146,15 +114,6 @@
return 1;
}
- private int[] chooseRandomElementsAsCenters(Matrix matrix, int
nClusters) {
- int[] centers = new int[nClusters];
-
- for (int i = 0; i < nClusters; i++) {
- centers[i] = (int) Math.floor(Math.random() *
matrix.nRows());
- }
- return centers;
- }
-
private void assignPointsToClosestCenter(int[] centers, double[][]
distances, int[] clusterId) {
for (int row = 0; row < distances.length; row++) {
double minDistance = Double.MAX_VALUE;
Modified:
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMedoidCluster.java
===================================================================
---
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMedoidCluster.java
2012-02-06 22:29:38 UTC (rev 28198)
+++
csplugins/trunk/ucsf/scooter/clusterMaker/src/clusterMaker/algorithms/attributeClusterers/kmedoid/KMedoidCluster.java
2012-02-06 22:35:55 UTC (rev 28199)
@@ -131,18 +131,11 @@
"Cluster attributes as well
as nodes",
Tunable.BOOLEAN, new
Boolean(clusterAttributes)));
-/*
- // For expression data, we might want to exclude missing data
- clusterProperties.add(new Tunable("ignoreMissing",
- "Ignore nodes with no data",
- Tunable.BOOLEAN, new
Boolean(false)));
-*/
-
// Whether or not to create groups
clusterProperties.add(new Tunable("createGroups",
"Create groups from
clusters",
Tunable.BOOLEAN, new
Boolean(createGroups)));
-
+
clusterProperties.initializeProperties();
updateSettings(true);
}
@@ -173,12 +166,6 @@
if ((t != null) && (t.valueChanged() || force))
selectedOnly = ((Boolean) t.getValue()).booleanValue();
-/*
- t = clusterProperties.get("ignoreMissing");
- if ((t != null) && (t.valueChanged() || force))
- ignoreMissing = ((Boolean) t.getValue()).booleanValue();
-*/
-
t = clusterProperties.get("createGroups");
if ((t != null) && (t.valueChanged() || force))
createGroups = ((Boolean) t.getValue()).booleanValue();
@@ -216,6 +203,7 @@
algorithm.setDebug(debug);
algorithm.setUseSilhouette(useSilhouette);
algorithm.setKMax(kMax);
+ algorithm.setInitializeNearCenter(initializeNearCenter);
algorithm.setClusterInterface(this);
String resultsString = "K-Medoid results:";
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.