2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: SVMGUI.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.9 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/03/24 15:51:53 $ |
2 |
26 Feb 07 |
jari |
* $Author: eleanorahowe $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
|
2 |
26 Feb 07 |
jari |
13 |
package org.tigr.microarray.mev.cluster.gui.impl.svm; |
2 |
26 Feb 07 |
jari |
14 |
|
2 |
26 Feb 07 |
jari |
15 |
import java.awt.Frame; |
2 |
26 Feb 07 |
jari |
16 |
import java.awt.event.ActionEvent; |
2 |
26 Feb 07 |
jari |
17 |
import java.awt.event.WindowEvent; |
2 |
26 Feb 07 |
jari |
18 |
import java.io.BufferedReader; |
2 |
26 Feb 07 |
jari |
19 |
import java.io.File; |
2 |
26 Feb 07 |
jari |
20 |
import java.io.FileInputStream; |
2 |
26 Feb 07 |
jari |
21 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
22 |
import java.io.InputStreamReader; |
2 |
26 Feb 07 |
jari |
23 |
import java.util.ArrayList; |
2 |
26 Feb 07 |
jari |
24 |
import java.util.Arrays; |
2 |
26 Feb 07 |
jari |
25 |
import java.util.Vector; |
2 |
26 Feb 07 |
jari |
26 |
|
2 |
26 Feb 07 |
jari |
27 |
import javax.swing.JFileChooser; |
2 |
26 Feb 07 |
jari |
28 |
import javax.swing.JOptionPane; |
2 |
26 Feb 07 |
jari |
29 |
import javax.swing.tree.DefaultMutableTreeNode; |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
import org.tigr.microarray.mev.TMEV; |
2 |
26 Feb 07 |
jari |
32 |
import org.tigr.microarray.mev.cluster.Cluster; |
2 |
26 Feb 07 |
jari |
33 |
import org.tigr.microarray.mev.cluster.Node; |
2 |
26 Feb 07 |
jari |
34 |
import org.tigr.microarray.mev.cluster.NodeList; |
2 |
26 Feb 07 |
jari |
35 |
import org.tigr.microarray.mev.cluster.NodeValue; |
2 |
26 Feb 07 |
jari |
36 |
import org.tigr.microarray.mev.cluster.NodeValueList; |
2 |
26 Feb 07 |
jari |
37 |
import org.tigr.microarray.mev.cluster.algorithm.AbortException; |
2 |
26 Feb 07 |
jari |
38 |
import org.tigr.microarray.mev.cluster.algorithm.AbstractAlgorithm; |
2 |
26 Feb 07 |
jari |
39 |
import org.tigr.microarray.mev.cluster.algorithm.Algorithm; |
2 |
26 Feb 07 |
jari |
40 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmData; |
2 |
26 Feb 07 |
jari |
41 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmEvent; |
2 |
26 Feb 07 |
jari |
42 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmException; |
2 |
26 Feb 07 |
jari |
43 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmFactory; |
2 |
26 Feb 07 |
jari |
44 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmListener; |
2 |
26 Feb 07 |
jari |
45 |
import org.tigr.microarray.mev.cluster.algorithm.AlgorithmParameters; |
2 |
26 Feb 07 |
jari |
46 |
import org.tigr.microarray.mev.cluster.gui.Experiment; |
2 |
26 Feb 07 |
jari |
47 |
import org.tigr.microarray.mev.cluster.gui.IClusterGUI; |
2 |
26 Feb 07 |
jari |
48 |
import org.tigr.microarray.mev.cluster.gui.IData; |
2 |
26 Feb 07 |
jari |
49 |
import org.tigr.microarray.mev.cluster.gui.IDistanceMenu; |
2 |
26 Feb 07 |
jari |
50 |
import org.tigr.microarray.mev.cluster.gui.IFramework; |
2 |
26 Feb 07 |
jari |
51 |
import org.tigr.microarray.mev.cluster.gui.IViewer; |
2 |
26 Feb 07 |
jari |
52 |
import org.tigr.microarray.mev.cluster.gui.LeafInfo; |
2 |
26 Feb 07 |
jari |
53 |
import org.tigr.microarray.mev.cluster.gui.helpers.CentroidUserObject; |
2 |
26 Feb 07 |
jari |
54 |
import org.tigr.microarray.mev.cluster.gui.impl.dialogs.DialogListener; |
2 |
26 Feb 07 |
jari |
55 |
import org.tigr.microarray.mev.cluster.gui.impl.dialogs.Logger; |
2 |
26 Feb 07 |
jari |
56 |
import org.tigr.microarray.mev.cluster.gui.impl.dialogs.Monitor; |
2 |
26 Feb 07 |
jari |
57 |
import org.tigr.microarray.mev.cluster.gui.impl.hcl.HCLInitDialog; |
2 |
26 Feb 07 |
jari |
58 |
import org.tigr.microarray.mev.cluster.gui.impl.hcl.HCLTreeData; |
2 |
26 Feb 07 |
jari |
59 |
import org.tigr.microarray.mev.cluster.gui.impl.hcl.HCLViewer; |
2 |
26 Feb 07 |
jari |
60 |
import org.tigr.microarray.mev.script.scriptGUI.IScriptGUI; |
2 |
26 Feb 07 |
jari |
61 |
import org.tigr.util.FloatMatrix; |
2 |
26 Feb 07 |
jari |
62 |
|
2 |
26 Feb 07 |
jari |
63 |
public class SVMGUI implements IClusterGUI, IScriptGUI { |
2 |
26 Feb 07 |
jari |
64 |
|
2 |
26 Feb 07 |
jari |
// GUI |
2 |
26 Feb 07 |
jari |
66 |
protected Frame parentFrame; |
2 |
26 Feb 07 |
jari |
67 |
private IFramework framework; |
2 |
26 Feb 07 |
jari |
68 |
protected IDistanceMenu menu; |
2 |
26 Feb 07 |
jari |
69 |
protected Logger logger; |
2 |
26 Feb 07 |
jari |
70 |
protected Monitor monitor; |
2 |
26 Feb 07 |
jari |
71 |
protected Listener listener; |
2 |
26 Feb 07 |
jari |
72 |
|
2 |
26 Feb 07 |
jari |
// Calculation stuff |
2 |
26 Feb 07 |
jari |
74 |
protected IData experiment; |
2 |
26 Feb 07 |
jari |
75 |
protected Algorithm algorithm; |
2 |
26 Feb 07 |
jari |
76 |
protected SVMData data = new SVMData(); |
2 |
26 Feb 07 |
jari |
77 |
protected GeneralInfo info = new GeneralInfo(); |
2 |
26 Feb 07 |
jari |
78 |
protected Experiment experimentMap; |
2 |
26 Feb 07 |
jari |
79 |
|
2 |
26 Feb 07 |
jari |
//Training data members |
2 |
26 Feb 07 |
jari |
81 |
private File SVMFile; |
2 |
26 Feb 07 |
jari |
82 |
private float[] Weights; |
2 |
26 Feb 07 |
jari |
83 |
private FloatMatrix trainingMatrix; |
2 |
26 Feb 07 |
jari |
84 |
private FloatMatrix kernelMatrix; |
2 |
26 Feb 07 |
jari |
85 |
private int[] classes; |
2 |
26 Feb 07 |
jari |
86 |
|
2 |
26 Feb 07 |
jari |
87 |
public static int TRAIN_AND_CLASSIFY = 0; |
2 |
26 Feb 07 |
jari |
88 |
public static int TRAIN_ONLY = 1; |
2 |
26 Feb 07 |
jari |
89 |
public static int CLASSIFY_ONLY = 2; |
2 |
26 Feb 07 |
jari |
90 |
public static int ONE_OUT_VALIDATION = 3; |
2 |
26 Feb 07 |
jari |
91 |
|
2 |
26 Feb 07 |
jari |
92 |
|
2 |
26 Feb 07 |
jari |
93 |
private int SVMMode = 0; |
2 |
26 Feb 07 |
jari |
94 |
private boolean classifyGenes; |
2 |
26 Feb 07 |
jari |
95 |
private FloatMatrix discriminantMatrix; |
2 |
26 Feb 07 |
jari |
96 |
private boolean stop = false; |
2 |
26 Feb 07 |
jari |
97 |
|
2 |
26 Feb 07 |
jari |
98 |
private boolean scripting = false; |
2 |
26 Feb 07 |
jari |
99 |
|
2 |
26 Feb 07 |
jari |
// Generic protocol for both SVM Train and SVM classify algorithms |
2 |
26 Feb 07 |
jari |
/** Creates an SVM algorithm |
2 |
26 Feb 07 |
jari |
* @param factory algorithm factory |
2 |
26 Feb 07 |
jari |
* @throws AlgorithmException |
2 |
26 Feb 07 |
jari |
104 |
*/ |
2 |
26 Feb 07 |
jari |
105 |
protected void createAlgorithm( AlgorithmFactory factory ) throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
106 |
listener = new Listener(); |
2 |
26 Feb 07 |
jari |
107 |
this.algorithm = factory.getAlgorithm("SVM"); |
2 |
26 Feb 07 |
jari |
108 |
this.algorithm.addAlgorithmListener(listener); |
2 |
26 Feb 07 |
jari |
109 |
} |
2 |
26 Feb 07 |
jari |
110 |
|
2 |
26 Feb 07 |
jari |
/** Binds common parameters. |
2 |
26 Feb 07 |
jari |
* @param data AlgorithmData to mainitain pased parameters |
2 |
26 Feb 07 |
jari |
113 |
*/ |
2 |
26 Feb 07 |
jari |
114 |
protected void bindParams( AlgorithmData data ) { |
2 |
26 Feb 07 |
jari |
115 |
if(!scripting) { |
2 |
26 Feb 07 |
jari |
116 |
FloatMatrix matrix = this.experiment.getExperiment().getMatrix(); |
2 |
26 Feb 07 |
jari |
117 |
if(!this.data.classifyGenes) |
2 |
26 Feb 07 |
jari |
118 |
matrix = matrix.transpose(); |
2 |
26 Feb 07 |
jari |
119 |
data.addMatrix("experiment", matrix); |
2 |
26 Feb 07 |
jari |
120 |
} |
2 |
26 Feb 07 |
jari |
121 |
|
2 |
26 Feb 07 |
jari |
122 |
data.addParam("distance-factor", String.valueOf(1.0f) ); |
2 |
26 Feb 07 |
jari |
123 |
data.addParam("hcl-distance-absolute", String.valueOf( this.data.absoluteDistance ) ); |
2 |
26 Feb 07 |
jari |
124 |
data.addParam("hcl-distance-function", String.valueOf( this.data.distanceFunction ) ); |
2 |
26 Feb 07 |
jari |
125 |
|
2 |
26 Feb 07 |
jari |
126 |
data.addParam("constant", String.valueOf(this.data.constant)); |
2 |
26 Feb 07 |
jari |
127 |
data.addParam("coefficient", String.valueOf( this.data.coefficient)); |
2 |
26 Feb 07 |
jari |
128 |
data.addParam("power", String.valueOf( this.data.power ) ); |
2 |
26 Feb 07 |
jari |
129 |
} |
2 |
26 Feb 07 |
jari |
130 |
|
2 |
26 Feb 07 |
jari |
131 |
|
2 |
26 Feb 07 |
jari |
/** Executes SVM and returns SVM result |
2 |
26 Feb 07 |
jari |
* @param framework Main IFramework |
2 |
26 Feb 07 |
jari |
* @throws AlgorithmException |
2 |
26 Feb 07 |
jari |
* @return Returns SVM result node |
2 |
26 Feb 07 |
jari |
136 |
*/ |
2 |
26 Feb 07 |
jari |
137 |
public DefaultMutableTreeNode execute(IFramework framework) throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
//get basic objects for data and framework |
2 |
26 Feb 07 |
jari |
139 |
this.parentFrame = framework.getFrame(); |
2 |
26 Feb 07 |
jari |
140 |
this.experiment = framework.getData(); |
2 |
26 Feb 07 |
jari |
141 |
this.experimentMap = experiment.getExperiment(); |
2 |
26 Feb 07 |
jari |
142 |
this.menu = framework.getDistanceMenu(); |
2 |
26 Feb 07 |
jari |
143 |
this.framework = framework; |
2 |
26 Feb 07 |
jari |
144 |
|
2 |
26 Feb 07 |
jari |
145 |
DefaultMutableTreeNode svmNode = null; //Result Node |
2 |
26 Feb 07 |
jari |
146 |
|
2 |
26 Feb 07 |
jari |
147 |
try { |
2 |
26 Feb 07 |
jari |
//create an algorithm, assigns algorithm to this.algorithm |
2 |
26 Feb 07 |
jari |
149 |
createAlgorithm( framework.getAlgorithmFactory() ); |
2 |
26 Feb 07 |
jari |
150 |
|
2 |
26 Feb 07 |
jari |
//Make a new data structure for binding data and params |
2 |
26 Feb 07 |
jari |
152 |
AlgorithmData data = new AlgorithmData(); |
2 |
26 Feb 07 |
jari |
153 |
|
2 |
26 Feb 07 |
jari |
//Determine sample set (exp or genes) and SVM process |
2 |
26 Feb 07 |
jari |
// (train and classify, just train, or just classify), sets SVMMode |
2 |
26 Feb 07 |
jari |
156 |
if(!selectSVMProcedure()) |
2 |
26 Feb 07 |
jari |
157 |
return null; |
2 |
26 Feb 07 |
jari |
158 |
|
2 |
26 Feb 07 |
jari |
159 |
if(SVMMode == TRAIN_AND_CLASSIFY){ |
2 |
26 Feb 07 |
jari |
//Set get data into float, run init dialog, read svc file or use class. editor |
2 |
26 Feb 07 |
jari |
161 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
162 |
return null; |
2 |
26 Feb 07 |
jari |
//bind training parameters into data |
2 |
26 Feb 07 |
jari |
164 |
bindTrainingParams(data); |
2 |
26 Feb 07 |
jari |
//Time start and execution of training |
2 |
26 Feb 07 |
jari |
166 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
167 |
AlgorithmData trainingResult = this.algorithm.execute(data); |
2 |
26 Feb 07 |
jari |
168 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
//build training result, attach to node |
2 |
26 Feb 07 |
jari |
170 |
getTrainingResults( trainingResult ); |
2 |
26 Feb 07 |
jari |
171 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
172 |
svmNode = new DefaultMutableTreeNode("SVM - genes"); |
2 |
26 Feb 07 |
jari |
173 |
else |
2 |
26 Feb 07 |
jari |
174 |
svmNode = new DefaultMutableTreeNode("SVM - samples"); |
2 |
26 Feb 07 |
jari |
175 |
svmNode.add( createTrainingGUIResult()); //add traing result viewer |
2 |
26 Feb 07 |
jari |
176 |
|
2 |
26 Feb 07 |
jari |
//classify |
2 |
26 Feb 07 |
jari |
178 |
createAlgorithm( framework.getAlgorithmFactory() ); //get a fresh SVM |
2 |
26 Feb 07 |
jari |
179 |
|
2 |
26 Feb 07 |
jari |
//classification parameters already set, weights and data and primary params for kernel |
2 |
26 Feb 07 |
jari |
181 |
bindClassificationParams(data); |
2 |
26 Feb 07 |
jari |
182 |
|
2 |
26 Feb 07 |
jari |
183 |
start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
184 |
AlgorithmData classificationResult = this.algorithm.execute(data); |
2 |
26 Feb 07 |
jari |
185 |
time += System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
186 |
getClassificationResults( classificationResult ); |
2 |
26 Feb 07 |
jari |
187 |
info.time = time; |
2 |
26 Feb 07 |
jari |
188 |
info.function = menu.getFunctionName( this.data.distanceFunction ); //SVMData |
2 |
26 Feb 07 |
jari |
189 |
|
2 |
26 Feb 07 |
jari |
190 |
svmNode.add( createClassificationGUIResult() ); //add class. viewer |
2 |
26 Feb 07 |
jari |
191 |
svmNode.add( createSVMExpressionViews( classificationResult, classes)); //add expression image viewer |
2 |
26 Feb 07 |
jari |
192 |
if(this.data.calculateHCL) |
2 |
26 Feb 07 |
jari |
193 |
svmNode.add(createHierarchicalTreeViews(classificationResult.getCluster("cluster"), classificationResult)); |
2 |
26 Feb 07 |
jari |
194 |
createSVMCentroidViews(classificationResult, svmNode); //add centroid and expression graphs |
2 |
26 Feb 07 |
jari |
195 |
createInfoView(classificationResult, svmNode); |
2 |
26 Feb 07 |
jari |
196 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
197 |
} |
2 |
26 Feb 07 |
jari |
198 |
|
2 |
26 Feb 07 |
jari |
199 |
else if(SVMMode == TRAIN_ONLY){ |
2 |
26 Feb 07 |
jari |
200 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
201 |
return null; |
2 |
26 Feb 07 |
jari |
202 |
bindTrainingParams(data); |
2 |
26 Feb 07 |
jari |
203 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
204 |
AlgorithmData trainingResult = this.algorithm.execute(data); |
2 |
26 Feb 07 |
jari |
205 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
206 |
getTrainingResults( trainingResult ); |
2 |
26 Feb 07 |
jari |
207 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
208 |
svmNode = new DefaultMutableTreeNode("SVM - genes"); |
2 |
26 Feb 07 |
jari |
209 |
else |
2 |
26 Feb 07 |
jari |
210 |
svmNode = new DefaultMutableTreeNode("SVM - samples"); |
2 |
26 Feb 07 |
jari |
211 |
svmNode.add( createTrainingGUIResult() ); |
2 |
26 Feb 07 |
jari |
212 |
info.time = time; |
2 |
26 Feb 07 |
jari |
213 |
int function = this.data.distanceFunction; |
2 |
26 Feb 07 |
jari |
214 |
info.function = menu.getFunctionName( function ); |
2 |
26 Feb 07 |
jari |
215 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
216 |
} |
2 |
26 Feb 07 |
jari |
217 |
|
2 |
26 Feb 07 |
jari |
218 |
else if(SVMMode == CLASSIFY_ONLY){ |
2 |
26 Feb 07 |
jari |
219 |
if (!initClassificationParams()) |
2 |
26 Feb 07 |
jari |
220 |
return null; |
2 |
26 Feb 07 |
jari |
221 |
bindClassificationParams(data); |
2 |
26 Feb 07 |
jari |
222 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
223 |
AlgorithmData classificationResult = this.algorithm.execute(data); |
2 |
26 Feb 07 |
jari |
224 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
225 |
getClassificationResults( classificationResult ); |
2 |
26 Feb 07 |
jari |
226 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
227 |
svmNode = new DefaultMutableTreeNode("SVM - genes"); |
2 |
26 Feb 07 |
jari |
228 |
else |
2 |
26 Feb 07 |
jari |
229 |
svmNode = new DefaultMutableTreeNode("SVM - samples"); |
2 |
26 Feb 07 |
jari |
230 |
info.time = time; |
2 |
26 Feb 07 |
jari |
231 |
int function = this.data.distanceFunction; |
2 |
26 Feb 07 |
jari |
232 |
info.function = menu.getFunctionName( function ); |
2 |
26 Feb 07 |
jari |
233 |
svmNode.add( createClassificationGUIResult() ); |
2 |
26 Feb 07 |
jari |
234 |
svmNode.add( createViewers(classificationResult) ); //Experiment viewers based on pos/neg without prior knowledge of init. class. |
2 |
26 Feb 07 |
jari |
235 |
if(this.data.calculateHCL) |
2 |
26 Feb 07 |
jari |
236 |
svmNode.add(createHierarchicalTreeViews(classificationResult.getCluster("cluster"), classificationResult)); |
2 |
26 Feb 07 |
jari |
237 |
createSVMCentroidViews(classificationResult, svmNode); //append centroid and exp. viewers |
2 |
26 Feb 07 |
jari |
238 |
createInfoView(classificationResult, svmNode); |
2 |
26 Feb 07 |
jari |
239 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
240 |
} |
2 |
26 Feb 07 |
jari |
241 |
|
2 |
26 Feb 07 |
jari |
242 |
else if(SVMMode == ONE_OUT_VALIDATION){ |
2 |
26 Feb 07 |
jari |
243 |
int iter; |
2 |
26 Feb 07 |
jari |
244 |
int n; |
2 |
26 Feb 07 |
jari |
245 |
int initClass; |
2 |
26 Feb 07 |
jari |
246 |
int numberOfNonNeutrals; |
2 |
26 Feb 07 |
jari |
247 |
FloatMatrix cumDiscriminantMatrix; |
2 |
26 Feb 07 |
jari |
248 |
int [] iterationScores; |
2 |
26 Feb 07 |
jari |
249 |
int [] elementScores; |
2 |
26 Feb 07 |
jari |
250 |
|
2 |
26 Feb 07 |
jari |
251 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
252 |
return null; |
2 |
26 Feb 07 |
jari |
253 |
|
2 |
26 Feb 07 |
jari |
254 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
255 |
n = this.experimentMap.getNumberOfGenes(); |
2 |
26 Feb 07 |
jari |
256 |
else |
2 |
26 Feb 07 |
jari |
257 |
n = this.experimentMap.getNumberOfSamples(); |
2 |
26 Feb 07 |
jari |
258 |
|
2 |
26 Feb 07 |
jari |
259 |
cumDiscriminantMatrix = new FloatMatrix(n,2); |
2 |
26 Feb 07 |
jari |
260 |
numberOfNonNeutrals = getNumberOfNonNeutrals(); |
2 |
26 Feb 07 |
jari |
261 |
iterationScores = new int[n]; |
2 |
26 Feb 07 |
jari |
262 |
elementScores = new int[n]; |
2 |
26 Feb 07 |
jari |
263 |
for(int i = 0; i < n; i++) |
2 |
26 Feb 07 |
jari |
264 |
elementScores[i] = 0; |
2 |
26 Feb 07 |
jari |
265 |
|
2 |
26 Feb 07 |
jari |
266 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
267 |
svmNode = new DefaultMutableTreeNode("SVM Val. - genes"); |
2 |
26 Feb 07 |
jari |
268 |
else |
2 |
26 Feb 07 |
jari |
269 |
svmNode = new DefaultMutableTreeNode("SVM Val. - samples"); |
2 |
26 Feb 07 |
jari |
270 |
|
2 |
26 Feb 07 |
jari |
271 |
for(iter = 0; iter < n; iter++){ |
2 |
26 Feb 07 |
jari |
272 |
initClass = classes[iter]; |
2 |
26 Feb 07 |
jari |
273 |
classes[iter] = 0; |
2 |
26 Feb 07 |
jari |
274 |
|
2 |
26 Feb 07 |
jari |
//bind training parameters into data |
2 |
26 Feb 07 |
jari |
276 |
bindTrainingParams(data); |
2 |
26 Feb 07 |
jari |
277 |
|
2 |
26 Feb 07 |
jari |
//Time start and execution of training |
2 |
26 Feb 07 |
jari |
279 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
280 |
AlgorithmData trainingResult = this.algorithm.execute(data); |
2 |
26 Feb 07 |
jari |
281 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
//build training result, attach to node |
2 |
26 Feb 07 |
jari |
283 |
getTrainingResults( trainingResult ); |
2 |
26 Feb 07 |
jari |
284 |
|
2 |
26 Feb 07 |
jari |
// svmNode.add( createTrainingGUIResult()); //add traing result viewer |
2 |
26 Feb 07 |
jari |
286 |
|
2 |
26 Feb 07 |
jari |
//classify |
2 |
26 Feb 07 |
jari |
288 |
createAlgorithm( framework.getAlgorithmFactory() ); //get a fresh SVM |
2 |
26 Feb 07 |
jari |
289 |
|
2 |
26 Feb 07 |
jari |
//classification parameters already set, weights and data and primary params for kernel |
2 |
26 Feb 07 |
jari |
291 |
bindClassificationParams(data); |
2 |
26 Feb 07 |
jari |
292 |
start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
293 |
AlgorithmData classificationResult = this.algorithm.execute(data); |
2 |
26 Feb 07 |
jari |
294 |
time += System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
295 |
getClassificationResults( classificationResult ); |
2 |
26 Feb 07 |
jari |
296 |
|
2 |
26 Feb 07 |
jari |
297 |
accumulateResult(cumDiscriminantMatrix, iter); // get cumulative result |
2 |
26 Feb 07 |
jari |
298 |
|
2 |
26 Feb 07 |
jari |
299 |
classes[iter] = initClass; //restore initial classification |
2 |
26 Feb 07 |
jari |
300 |
|
2 |
26 Feb 07 |
jari |
301 |
getNumberOfCorrectPlacements(iterationScores, elementScores, iter); |
2 |
26 Feb 07 |
jari |
302 |
} |
2 |
26 Feb 07 |
jari |
303 |
|
2 |
26 Feb 07 |
jari |
304 |
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new LeafInfo("SVM One-out Validation", |
2 |
26 Feb 07 |
jari |
305 |
new SVMOneOutViewer(experiment.getExperiment(), cumDiscriminantMatrix, this.data.classifyGenes, this.classes, elementScores, iterationScores, numberOfNonNeutrals))); |
2 |
26 Feb 07 |
jari |
306 |
svmNode.add(root); |
2 |
26 Feb 07 |
jari |
307 |
|
2 |
26 Feb 07 |
jari |
308 |
AlgorithmData cumulativeData = constructCumulativeResult(cumDiscriminantMatrix); |
2 |
26 Feb 07 |
jari |
309 |
|
2 |
26 Feb 07 |
jari |
310 |
svmNode.add( createSVMExpressionViews( cumulativeData, classes)); //add expression image viewer |
2 |
26 Feb 07 |
jari |
311 |
if(this.data.calculateHCL){ |
2 |
26 Feb 07 |
jari |
312 |
try{ |
2 |
26 Feb 07 |
jari |
313 |
calculateHCL(cumulativeData, cumDiscriminantMatrix); |
2 |
26 Feb 07 |
jari |
314 |
svmNode.add(createHierarchicalTreeViews(cumulativeData.getCluster("cluster"), cumulativeData)); |
2 |
26 Feb 07 |
jari |
315 |
}catch (Exception e){ } |
2 |
26 Feb 07 |
jari |
316 |
} |
2 |
26 Feb 07 |
jari |
317 |
createSVMCentroidViews(cumulativeData, svmNode); //add centroid and expression graphs |
2 |
26 Feb 07 |
jari |
318 |
createInfoView(cumulativeData, svmNode); |
2 |
26 Feb 07 |
jari |
319 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
320 |
|
2 |
26 Feb 07 |
jari |
321 |
} |
2 |
26 Feb 07 |
jari |
322 |
|
2 |
26 Feb 07 |
jari |
323 |
return svmNode; |
2 |
26 Feb 07 |
jari |
324 |
} finally { |
2 |
26 Feb 07 |
jari |
325 |
if (algorithm != null) { |
2 |
26 Feb 07 |
jari |
326 |
algorithm.removeAlgorithmListener(listener); |
2 |
26 Feb 07 |
jari |
327 |
} |
2 |
26 Feb 07 |
jari |
328 |
if (monitor != null) monitor.dispose(); |
2 |
26 Feb 07 |
jari |
329 |
if (logger != null) logger.dispose(); |
2 |
26 Feb 07 |
jari |
330 |
} |
2 |
26 Feb 07 |
jari |
331 |
} |
2 |
26 Feb 07 |
jari |
332 |
|
2 |
26 Feb 07 |
jari |
333 |
|
2 |
26 Feb 07 |
jari |
334 |
|
2 |
26 Feb 07 |
jari |
335 |
public AlgorithmData getScriptParameters(IFramework framework) { |
2 |
26 Feb 07 |
jari |
//get basic objects for data and framework |
2 |
26 Feb 07 |
jari |
337 |
this.scripting = true; |
2 |
26 Feb 07 |
jari |
338 |
this.parentFrame = framework.getFrame(); |
2 |
26 Feb 07 |
jari |
339 |
this.experiment = framework.getData(); |
2 |
26 Feb 07 |
jari |
340 |
this.experimentMap = experiment.getExperiment(); |
2 |
26 Feb 07 |
jari |
341 |
this.menu = framework.getDistanceMenu(); |
2 |
26 Feb 07 |
jari |
342 |
this.framework = framework; |
2 |
26 Feb 07 |
jari |
343 |
|
2 |
26 Feb 07 |
jari |
//Make a new data structure for binding data and params |
2 |
26 Feb 07 |
jari |
345 |
AlgorithmData data = new AlgorithmData(); |
2 |
26 Feb 07 |
jari |
346 |
|
2 |
26 Feb 07 |
jari |
//Determine sample set (exp or genes) and SVM process |
2 |
26 Feb 07 |
jari |
// (train and classify, just train, or just classify), sets SVMMode |
2 |
26 Feb 07 |
jari |
349 |
if(!selectSVMProcedure()) |
2 |
26 Feb 07 |
jari |
350 |
return null; |
2 |
26 Feb 07 |
jari |
351 |
|
2 |
26 Feb 07 |
jari |
352 |
if(SVMMode == TRAIN_AND_CLASSIFY){ |
2 |
26 Feb 07 |
jari |
//Set get data into float, run init dialog, read svc file or use class. editor |
2 |
26 Feb 07 |
jari |
354 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
355 |
return null; |
2 |
26 Feb 07 |
jari |
//bind training parameters into data |
2 |
26 Feb 07 |
jari |
357 |
bindTrainingParams(data); |
2 |
26 Feb 07 |
jari |
//classification parameters already set, weights and data and primary params for kernel |
2 |
26 Feb 07 |
jari |
359 |
bindClassificationParams(data); |
2 |
26 Feb 07 |
jari |
360 |
|
2 |
26 Feb 07 |
jari |
361 |
data.addParam("mode", String.valueOf(TRAIN_AND_CLASSIFY)); |
2 |
26 Feb 07 |
jari |
362 |
|
2 |
26 Feb 07 |
jari |
// alg name |
2 |
26 Feb 07 |
jari |
364 |
data.addParam("name", "SVM"); |
2 |
26 Feb 07 |
jari |
365 |
|
2 |
26 Feb 07 |
jari |
// alg type |
2 |
26 Feb 07 |
jari |
367 |
if(classifyGenes) |
2 |
26 Feb 07 |
jari |
368 |
data.addParam("alg-type", "cluster-genes"); |
2 |
26 Feb 07 |
jari |
369 |
else |
2 |
26 Feb 07 |
jari |
370 |
data.addParam("alg-type", "cluster-experiments"); |
2 |
26 Feb 07 |
jari |
371 |
|
2 |
26 Feb 07 |
jari |
// output class |
2 |
26 Feb 07 |
jari |
373 |
data.addParam("output-class", "partition-output"); |
2 |
26 Feb 07 |
jari |
374 |
|
2 |
26 Feb 07 |
jari |
//output nodes |
2 |
26 Feb 07 |
jari |
376 |
String [] outputNodes = new String[2]; |
2 |
26 Feb 07 |
jari |
377 |
outputNodes[0] = "Positives"; |
2 |
26 Feb 07 |
jari |
378 |
outputNodes[1] = "Negatives"; |
2 |
26 Feb 07 |
jari |
379 |
|
2 |
26 Feb 07 |
jari |
380 |
data.addStringArray("output-nodes", outputNodes); |
2 |
26 Feb 07 |
jari |
381 |
|
2 |
26 Feb 07 |
jari |
382 |
return data; |
2 |
26 Feb 07 |
jari |
383 |
} |
2 |
26 Feb 07 |
jari |
384 |
|
2 |
26 Feb 07 |
jari |
385 |
else if(SVMMode == TRAIN_ONLY){ |
2 |
26 Feb 07 |
jari |
386 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
387 |
return null; |
2 |
26 Feb 07 |
jari |
388 |
bindTrainingParams(data); |
2 |
26 Feb 07 |
jari |
389 |
|
2 |
26 Feb 07 |
jari |
390 |
data.addParam("mode", String.valueOf(TRAIN_ONLY)); |
2 |
26 Feb 07 |
jari |
391 |
|
2 |
26 Feb 07 |
jari |
// alg name |
2 |
26 Feb 07 |
jari |
393 |
data.addParam("name", "SVM"); |
2 |
26 Feb 07 |
jari |
394 |
|
2 |
26 Feb 07 |
jari |
// alg type |
2 |
26 Feb 07 |
jari |
396 |
if(classifyGenes) |
2 |
26 Feb 07 |
jari |
397 |
data.addParam("alg-type", "cluster-genes"); |
2 |
26 Feb 07 |
jari |
398 |
else |
2 |
26 Feb 07 |
jari |
399 |
data.addParam("alg-type", "cluster-experiments"); |
2 |
26 Feb 07 |
jari |
400 |
|
2 |
26 Feb 07 |
jari |
// output class |
2 |
26 Feb 07 |
jari |
402 |
data.addParam("output-class", "partition-output"); |
2 |
26 Feb 07 |
jari |
403 |
|
2 |
26 Feb 07 |
jari |
//output nodes |
2 |
26 Feb 07 |
jari |
405 |
String [] outputNodes = new String[1]; |
2 |
26 Feb 07 |
jari |
406 |
outputNodes[0] = "Training Weights Output"; |
2 |
26 Feb 07 |
jari |
407 |
|
2 |
26 Feb 07 |
jari |
408 |
data.addStringArray("output-nodes", outputNodes); |
2 |
26 Feb 07 |
jari |
409 |
|
2 |
26 Feb 07 |
jari |
410 |
return data; |
2 |
26 Feb 07 |
jari |
411 |
} |
2 |
26 Feb 07 |
jari |
412 |
|
2 |
26 Feb 07 |
jari |
413 |
else if(SVMMode == CLASSIFY_ONLY){ |
2 |
26 Feb 07 |
jari |
414 |
if (!initClassificationParams()) |
2 |
26 Feb 07 |
jari |
415 |
return null; |
2 |
26 Feb 07 |
jari |
416 |
bindClassificationParams(data); |
2 |
26 Feb 07 |
jari |
417 |
|
2 |
26 Feb 07 |
jari |
418 |
data.addParam("mode", String.valueOf(CLASSIFY_ONLY)); |
2 |
26 Feb 07 |
jari |
419 |
|
2 |
26 Feb 07 |
jari |
// alg name |
2 |
26 Feb 07 |
jari |
421 |
data.addParam("name", "SVM"); |
2 |
26 Feb 07 |
jari |
422 |
|
2 |
26 Feb 07 |
jari |
// alg type |
2 |
26 Feb 07 |
jari |
424 |
if(classifyGenes) |
2 |
26 Feb 07 |
jari |
425 |
data.addParam("alg-type", "cluster-genes"); |
2 |
26 Feb 07 |
jari |
426 |
else |
2 |
26 Feb 07 |
jari |
427 |
data.addParam("alg-type", "cluster-experiments"); |
2 |
26 Feb 07 |
jari |
428 |
|
2 |
26 Feb 07 |
jari |
// output class |
2 |
26 Feb 07 |
jari |
430 |
data.addParam("output-class", "partition-output"); |
2 |
26 Feb 07 |
jari |
431 |
|
2 |
26 Feb 07 |
jari |
//output nodes |
2 |
26 Feb 07 |
jari |
433 |
String [] outputNodes = new String[2]; |
2 |
26 Feb 07 |
jari |
434 |
outputNodes[0] = "Positives"; |
2 |
26 Feb 07 |
jari |
435 |
outputNodes[1] = "Negatives"; |
2 |
26 Feb 07 |
jari |
436 |
|
2 |
26 Feb 07 |
jari |
437 |
data.addStringArray("output-nodes", outputNodes); |
2 |
26 Feb 07 |
jari |
438 |
|
2 |
26 Feb 07 |
jari |
439 |
return data; |
2 |
26 Feb 07 |
jari |
440 |
} |
2 |
26 Feb 07 |
jari |
441 |
|
2 |
26 Feb 07 |
jari |
442 |
else if(SVMMode == ONE_OUT_VALIDATION){ |
2 |
26 Feb 07 |
jari |
443 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
444 |
return null; |
2 |
26 Feb 07 |
jari |
445 |
|
2 |
26 Feb 07 |
jari |
//bind training parameters into data |
2 |
26 Feb 07 |
jari |
447 |
bindTrainingParams(data); |
2 |
26 Feb 07 |
jari |
448 |
bindClassificationParams(data); |
2 |
26 Feb 07 |
jari |
449 |
|
2 |
26 Feb 07 |
jari |
450 |
data.addParam("mode", String.valueOf(ONE_OUT_VALIDATION)); |
2 |
26 Feb 07 |
jari |
451 |
|
2 |
26 Feb 07 |
jari |
// alg name |
2 |
26 Feb 07 |
jari |
453 |
data.addParam("name", "SVM"); |
2 |
26 Feb 07 |
jari |
454 |
|
2 |
26 Feb 07 |
jari |
// alg type |
2 |
26 Feb 07 |
jari |
456 |
if(classifyGenes) |
2 |
26 Feb 07 |
jari |
457 |
data.addParam("alg-type", "cluster-genes"); |
2 |
26 Feb 07 |
jari |
458 |
else |
2 |
26 Feb 07 |
jari |
459 |
data.addParam("alg-type", "cluster-experiments"); |
2 |
26 Feb 07 |
jari |
460 |
|
2 |
26 Feb 07 |
jari |
// output class |
2 |
26 Feb 07 |
jari |
462 |
data.addParam("output-class", "partition-output"); |
2 |
26 Feb 07 |
jari |
463 |
|
2 |
26 Feb 07 |
jari |
//output nodes |
2 |
26 Feb 07 |
jari |
465 |
String [] outputNodes = new String[2]; |
2 |
26 Feb 07 |
jari |
466 |
outputNodes[0] = "Positives"; |
2 |
26 Feb 07 |
jari |
467 |
outputNodes[1] = "Negatives"; |
2 |
26 Feb 07 |
jari |
468 |
|
2 |
26 Feb 07 |
jari |
469 |
data.addStringArray("output-nodes", outputNodes); |
2 |
26 Feb 07 |
jari |
470 |
|
2 |
26 Feb 07 |
jari |
471 |
return data; |
2 |
26 Feb 07 |
jari |
472 |
} |
2 |
26 Feb 07 |
jari |
473 |
return data; |
2 |
26 Feb 07 |
jari |
474 |
} |
2 |
26 Feb 07 |
jari |
475 |
|
2 |
26 Feb 07 |
jari |
476 |
public DefaultMutableTreeNode executeScript(IFramework framework, AlgorithmData algData, Experiment experiment) throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
477 |
|
2 |
26 Feb 07 |
jari |
//get basic objects for data and framework |
2 |
26 Feb 07 |
jari |
479 |
this.SVMMode = algData.getParams().getInt("mode"); |
2 |
26 Feb 07 |
jari |
480 |
|
2 |
26 Feb 07 |
jari |
//set fields |
2 |
26 Feb 07 |
jari |
482 |
rebuildSVMData(algData); |
2 |
26 Feb 07 |
jari |
483 |
|
2 |
26 Feb 07 |
jari |
484 |
this.parentFrame = framework.getFrame(); |
2 |
26 Feb 07 |
jari |
485 |
this.experiment = framework.getData(); |
2 |
26 Feb 07 |
jari |
486 |
this.experimentMap = experiment; |
2 |
26 Feb 07 |
jari |
487 |
this.menu = framework.getDistanceMenu(); |
2 |
26 Feb 07 |
jari |
488 |
this.framework = framework; |
2 |
26 Feb 07 |
jari |
489 |
|
2 |
26 Feb 07 |
jari |
490 |
this.data.classifyGenes = algData.getParams().getBoolean("classify-genes"); |
2 |
26 Feb 07 |
jari |
491 |
this.classifyGenes = this.data.classifyGenes; |
2 |
26 Feb 07 |
jari |
492 |
|
2 |
26 Feb 07 |
jari |
493 |
trainingMatrix = experiment.getMatrix(); |
2 |
26 Feb 07 |
jari |
494 |
if(this.data.classifyGenes) |
2 |
26 Feb 07 |
jari |
495 |
algData.addMatrix("training", trainingMatrix); |
2 |
26 Feb 07 |
jari |
496 |
else |
2 |
26 Feb 07 |
jari |
497 |
algData.addMatrix("training", trainingMatrix.transpose()); |
2 |
26 Feb 07 |
jari |
498 |
|
2 |
26 Feb 07 |
jari |
499 |
|
2 |
26 Feb 07 |
jari |
500 |
FloatMatrix matrix = experiment.getMatrix(); |
2 |
26 Feb 07 |
jari |
501 |
if(!this.data.classifyGenes) |
2 |
26 Feb 07 |
jari |
502 |
matrix = matrix.transpose(); |
2 |
26 Feb 07 |
jari |
503 |
algData.addMatrix("experiment", matrix); |
2 |
26 Feb 07 |
jari |
504 |
this.classes = algData.getIntArray("classes"); |
2 |
26 Feb 07 |
jari |
505 |
|
2 |
26 Feb 07 |
jari |
506 |
DefaultMutableTreeNode svmNode = null; //Result Node |
2 |
26 Feb 07 |
jari |
507 |
|
2 |
26 Feb 07 |
jari |
508 |
showLogger("SVM Log Window"); |
2 |
26 Feb 07 |
jari |
509 |
|
2 |
26 Feb 07 |
jari |
510 |
try { |
2 |
26 Feb 07 |
jari |
//create an algorithm, assigns algorithm to this.algorithm |
2 |
26 Feb 07 |
jari |
512 |
createAlgorithm( framework.getAlgorithmFactory() ); |
2 |
26 Feb 07 |
jari |
513 |
|
2 |
26 Feb 07 |
jari |
514 |
if(SVMMode == TRAIN_AND_CLASSIFY){ |
2 |
26 Feb 07 |
jari |
515 |
bindParams(algData); |
2 |
26 Feb 07 |
jari |
516 |
algData.addParam("is-classify", String.valueOf(false)); |
2 |
26 Feb 07 |
jari |
517 |
|
2 |
26 Feb 07 |
jari |
//Time start and execution of training |
2 |
26 Feb 07 |
jari |
519 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
520 |
AlgorithmData trainingResult = this.algorithm.execute(algData); |
2 |
26 Feb 07 |
jari |
521 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
//build training result, attach to node |
2 |
26 Feb 07 |
jari |
523 |
getTrainingResults( trainingResult ); |
2 |
26 Feb 07 |
jari |
524 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
525 |
svmNode = new DefaultMutableTreeNode("SVM - genes"); |
2 |
26 Feb 07 |
jari |
526 |
else |
2 |
26 Feb 07 |
jari |
527 |
svmNode = new DefaultMutableTreeNode("SVM - samples"); |
2 |
26 Feb 07 |
jari |
528 |
svmNode.add( createTrainingGUIResult()); //add traing result viewer |
2 |
26 Feb 07 |
jari |
529 |
|
2 |
26 Feb 07 |
jari |
//classify |
2 |
26 Feb 07 |
jari |
531 |
createAlgorithm( framework.getAlgorithmFactory() ); //get a fresh SVM |
2 |
26 Feb 07 |
jari |
532 |
|
2 |
26 Feb 07 |
jari |
//classification parameters already set, weights and data and primary params for kernel |
2 |
26 Feb 07 |
jari |
534 |
bindClassificationParams(algData); |
2 |
26 Feb 07 |
jari |
535 |
algData.addParam("is-classify", String.valueOf(true)); |
2 |
26 Feb 07 |
jari |
536 |
|
2 |
26 Feb 07 |
jari |
537 |
start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
538 |
AlgorithmData classificationResult = this.algorithm.execute(algData); |
2 |
26 Feb 07 |
jari |
539 |
time += System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
540 |
getClassificationResults( classificationResult ); |
2 |
26 Feb 07 |
jari |
541 |
info.time = time; |
2 |
26 Feb 07 |
jari |
542 |
|
2 |
26 Feb 07 |
jari |
543 |
info.function = framework.getDistanceMenu().getFunctionName( this.data.distanceFunction ); |
2 |
26 Feb 07 |
jari |
544 |
|
2 |
26 Feb 07 |
jari |
545 |
svmNode.add( createClassificationGUIResult() ); //add class. viewer |
2 |
26 Feb 07 |
jari |
546 |
svmNode.add( createSVMExpressionViews( classificationResult, classes)); //add expression image viewer |
2 |
26 Feb 07 |
jari |
547 |
if(this.data.calculateHCL) |
2 |
26 Feb 07 |
jari |
548 |
svmNode.add(createHierarchicalTreeViews(classificationResult.getCluster("cluster"), classificationResult)); |
2 |
26 Feb 07 |
jari |
549 |
createSVMCentroidViews(classificationResult, svmNode); //add centroid and expression graphs |
2 |
26 Feb 07 |
jari |
550 |
createInfoView(classificationResult, svmNode); |
2 |
26 Feb 07 |
jari |
551 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
552 |
} |
2 |
26 Feb 07 |
jari |
553 |
|
2 |
26 Feb 07 |
jari |
554 |
else if(SVMMode == TRAIN_ONLY){ |
2 |
26 Feb 07 |
jari |
// if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
// return null; |
2 |
26 Feb 07 |
jari |
557 |
bindParams(algData); |
2 |
26 Feb 07 |
jari |
558 |
algData.addParam("is-classify", String.valueOf(false)); |
2 |
26 Feb 07 |
jari |
559 |
|
2 |
26 Feb 07 |
jari |
560 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
561 |
AlgorithmData trainingResult = this.algorithm.execute(algData); |
2 |
26 Feb 07 |
jari |
562 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
563 |
getTrainingResults( trainingResult ); |
2 |
26 Feb 07 |
jari |
564 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
565 |
svmNode = new DefaultMutableTreeNode("SVM - genes"); |
2 |
26 Feb 07 |
jari |
566 |
else |
2 |
26 Feb 07 |
jari |
567 |
svmNode = new DefaultMutableTreeNode("SVM - samples"); |
2 |
26 Feb 07 |
jari |
568 |
svmNode.add( createTrainingGUIResult() ); |
2 |
26 Feb 07 |
jari |
569 |
info.time = time; |
2 |
26 Feb 07 |
jari |
570 |
int function = menu.getDistanceFunction(); |
2 |
26 Feb 07 |
jari |
571 |
info.function = menu.getFunctionName( function ); |
2 |
26 Feb 07 |
jari |
572 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
573 |
} |
2 |
26 Feb 07 |
jari |
574 |
|
2 |
26 Feb 07 |
jari |
575 |
else if(SVMMode == CLASSIFY_ONLY){ |
2 |
26 Feb 07 |
jari |
// if (!initClassificationParams()) |
2 |
26 Feb 07 |
jari |
// return null; |
2 |
26 Feb 07 |
jari |
578 |
this.Weights = algData.getMatrix("weights").A[0]; |
2 |
26 Feb 07 |
jari |
579 |
bindClassificationParams(algData); |
2 |
26 Feb 07 |
jari |
580 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
581 |
AlgorithmData classificationResult = this.algorithm.execute(algData); |
2 |
26 Feb 07 |
jari |
582 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
583 |
getClassificationResults( classificationResult ); |
2 |
26 Feb 07 |
jari |
584 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
585 |
svmNode = new DefaultMutableTreeNode("SVM - genes"); |
2 |
26 Feb 07 |
jari |
586 |
else |
2 |
26 Feb 07 |
jari |
587 |
svmNode = new DefaultMutableTreeNode("SVM - samples"); |
2 |
26 Feb 07 |
jari |
588 |
info.time = time; |
2 |
26 Feb 07 |
jari |
589 |
int function = menu.getDistanceFunction(); |
2 |
26 Feb 07 |
jari |
590 |
info.function = menu.getFunctionName( function ); |
2 |
26 Feb 07 |
jari |
591 |
svmNode.add( createClassificationGUIResult() ); |
2 |
26 Feb 07 |
jari |
592 |
svmNode.add( createViewers(classificationResult) ); //Experiment viewers based on pos/neg without prior knowledge of init. class. |
2 |
26 Feb 07 |
jari |
593 |
if(this.data.calculateHCL) |
2 |
26 Feb 07 |
jari |
594 |
svmNode.add(createHierarchicalTreeViews(classificationResult.getCluster("cluster"), classificationResult)); |
2 |
26 Feb 07 |
jari |
595 |
createSVMCentroidViews(classificationResult, svmNode); //append centroid and exp. viewers |
2 |
26 Feb 07 |
jari |
596 |
createInfoView(classificationResult, svmNode); |
2 |
26 Feb 07 |
jari |
597 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
598 |
} |
2 |
26 Feb 07 |
jari |
599 |
|
2 |
26 Feb 07 |
jari |
600 |
else if(SVMMode == ONE_OUT_VALIDATION){ |
2 |
26 Feb 07 |
jari |
601 |
int iter; |
2 |
26 Feb 07 |
jari |
602 |
int n; |
2 |
26 Feb 07 |
jari |
603 |
int initClass; |
2 |
26 Feb 07 |
jari |
604 |
int numberOfNonNeutrals; |
2 |
26 Feb 07 |
jari |
605 |
FloatMatrix cumDiscriminantMatrix; |
2 |
26 Feb 07 |
jari |
606 |
int [] iterationScores; |
2 |
26 Feb 07 |
jari |
607 |
int [] elementScores; |
2 |
26 Feb 07 |
jari |
608 |
|
2 |
26 Feb 07 |
jari |
609 |
if (!initTrainingParams()) |
2 |
26 Feb 07 |
jari |
610 |
return null; |
2 |
26 Feb 07 |
jari |
611 |
|
2 |
26 Feb 07 |
jari |
612 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
613 |
n = this.experimentMap.getNumberOfGenes(); |
2 |
26 Feb 07 |
jari |
614 |
else |
2 |
26 Feb 07 |
jari |
615 |
n = this.experimentMap.getNumberOfSamples(); |
2 |
26 Feb 07 |
jari |
616 |
|
2 |
26 Feb 07 |
jari |
617 |
cumDiscriminantMatrix = new FloatMatrix(n,2); |
2 |
26 Feb 07 |
jari |
618 |
numberOfNonNeutrals = getNumberOfNonNeutrals(); |
2 |
26 Feb 07 |
jari |
619 |
iterationScores = new int[n]; |
2 |
26 Feb 07 |
jari |
620 |
elementScores = new int[n]; |
2 |
26 Feb 07 |
jari |
621 |
for(int i = 0; i < n; i++) |
2 |
26 Feb 07 |
jari |
622 |
elementScores[i] = 0; |
2 |
26 Feb 07 |
jari |
623 |
|
2 |
26 Feb 07 |
jari |
624 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
625 |
svmNode = new DefaultMutableTreeNode("SVM Val. - genes"); |
2 |
26 Feb 07 |
jari |
626 |
else |
2 |
26 Feb 07 |
jari |
627 |
svmNode = new DefaultMutableTreeNode("SVM Val. - samples"); |
2 |
26 Feb 07 |
jari |
628 |
|
2 |
26 Feb 07 |
jari |
629 |
for(iter = 0; iter < n; iter++){ |
2 |
26 Feb 07 |
jari |
630 |
initClass = classes[iter]; |
2 |
26 Feb 07 |
jari |
631 |
classes[iter] = 0; |
2 |
26 Feb 07 |
jari |
632 |
|
2 |
26 Feb 07 |
jari |
//bind training parameters into data |
2 |
26 Feb 07 |
jari |
634 |
bindTrainingParams(algData); |
2 |
26 Feb 07 |
jari |
635 |
|
2 |
26 Feb 07 |
jari |
//Time start and execution of training |
2 |
26 Feb 07 |
jari |
637 |
long start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
638 |
AlgorithmData trainingResult = this.algorithm.execute(algData); |
2 |
26 Feb 07 |
jari |
639 |
long time = System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
//build training result, attach to node |
2 |
26 Feb 07 |
jari |
641 |
getTrainingResults( trainingResult ); |
2 |
26 Feb 07 |
jari |
642 |
|
2 |
26 Feb 07 |
jari |
// svmNode.add( createTrainingGUIResult()); //add traing result viewer |
2 |
26 Feb 07 |
jari |
644 |
|
2 |
26 Feb 07 |
jari |
//classify |
2 |
26 Feb 07 |
jari |
646 |
createAlgorithm( framework.getAlgorithmFactory() ); //get a fresh SVM |
2 |
26 Feb 07 |
jari |
647 |
|
2 |
26 Feb 07 |
jari |
//classification parameters already set, weights and data and primary params for kernel |
2 |
26 Feb 07 |
jari |
649 |
bindClassificationParams(algData); |
2 |
26 Feb 07 |
jari |
650 |
start = System.currentTimeMillis(); |
2 |
26 Feb 07 |
jari |
651 |
AlgorithmData classificationResult = this.algorithm.execute(algData); |
2 |
26 Feb 07 |
jari |
652 |
time += System.currentTimeMillis() - start; |
2 |
26 Feb 07 |
jari |
653 |
getClassificationResults( classificationResult ); |
2 |
26 Feb 07 |
jari |
654 |
|
2 |
26 Feb 07 |
jari |
655 |
accumulateResult(cumDiscriminantMatrix, iter); // get cumulative result |
2 |
26 Feb 07 |
jari |
656 |
|
2 |
26 Feb 07 |
jari |
657 |
classes[iter] = initClass; //restore initial classification |
2 |
26 Feb 07 |
jari |
658 |
|
2 |
26 Feb 07 |
jari |
659 |
getNumberOfCorrectPlacements(iterationScores, elementScores, iter); |
2 |
26 Feb 07 |
jari |
660 |
} |
2 |
26 Feb 07 |
jari |
661 |
|
2 |
26 Feb 07 |
jari |
662 |
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new LeafInfo("SVM One-out Validation", |
2 |
26 Feb 07 |
jari |
//new SVMOneOutViewer( framework, this.experiment, this.data, cumDiscriminantMatrix, info, this.data.classifyGenes, this.classes, elementScores, iterationScores, numberOfNonNeutrals))); |
2 |
26 Feb 07 |
jari |
664 |
new SVMOneOutViewer(this.experiment.getExperiment(), cumDiscriminantMatrix, this.data.classifyGenes, this.classes, elementScores, iterationScores, numberOfNonNeutrals))); |
2 |
26 Feb 07 |
jari |
665 |
svmNode.add(root); |
2 |
26 Feb 07 |
jari |
666 |
|
2 |
26 Feb 07 |
jari |
667 |
AlgorithmData cumulativeData = constructCumulativeResult(cumDiscriminantMatrix); |
2 |
26 Feb 07 |
jari |
668 |
|
2 |
26 Feb 07 |
jari |
669 |
svmNode.add( createSVMExpressionViews( cumulativeData, classes)); //add expression image viewer |
2 |
26 Feb 07 |
jari |
670 |
if(this.data.calculateHCL){ |
2 |
26 Feb 07 |
jari |
671 |
try{ |
2 |
26 Feb 07 |
jari |
672 |
calculateHCL(cumulativeData, cumDiscriminantMatrix); |
2 |
26 Feb 07 |
jari |
673 |
svmNode.add(createHierarchicalTreeViews(cumulativeData.getCluster("cluster"), cumulativeData)); |
2 |
26 Feb 07 |
jari |
674 |
}catch (Exception e){ } |
2 |
26 Feb 07 |
jari |
675 |
} |
2 |
26 Feb 07 |
jari |
676 |
createSVMCentroidViews(cumulativeData, svmNode); //add centroid and expression graphs |
2 |
26 Feb 07 |
jari |
677 |
createInfoView(cumulativeData, svmNode); |
2 |
26 Feb 07 |
jari |
678 |
addSVMParameterNode(svmNode); |
2 |
26 Feb 07 |
jari |
679 |
|
2 |
26 Feb 07 |
jari |
680 |
} |
2 |
26 Feb 07 |
jari |
681 |
|
2 |
26 Feb 07 |
jari |
682 |
return svmNode; |
2 |
26 Feb 07 |
jari |
683 |
} finally { |
2 |
26 Feb 07 |
jari |
684 |
if (algorithm != null) { |
2 |
26 Feb 07 |
jari |
685 |
algorithm.removeAlgorithmListener(listener); |
2 |
26 Feb 07 |
jari |
686 |
} |
2 |
26 Feb 07 |
jari |
687 |
if (monitor != null) monitor.dispose(); |
2 |
26 Feb 07 |
jari |
688 |
if (logger != null) logger.dispose(); |
2 |
26 Feb 07 |
jari |
689 |
} |
2 |
26 Feb 07 |
jari |
690 |
} |
2 |
26 Feb 07 |
jari |
691 |
|
2 |
26 Feb 07 |
jari |
692 |
private void rebuildSVMData(AlgorithmData algData) { |
2 |
26 Feb 07 |
jari |
693 |
AlgorithmParameters params = algData.getParams(); |
2 |
26 Feb 07 |
jari |
694 |
|
2 |
26 Feb 07 |
jari |
695 |
this.data.constant = params.getFloat("constant"); |
2 |
26 Feb 07 |
jari |
696 |
this.data.coefficient = params.getFloat("coefficient"); |
2 |
26 Feb 07 |
jari |
697 |
this.data.power = params.getFloat("power"); |
2 |
26 Feb 07 |
jari |
698 |
|
2 |
26 Feb 07 |
jari |
//skip training params if classification from file, params won't be in algData |
2 |
26 Feb 07 |
jari |
700 |
if(this.SVMMode != SVMGUI.CLASSIFY_ONLY) { |
2 |
26 Feb 07 |
jari |
701 |
this.data.diagonalFactor = params.getFloat("diagonal-factor"); |
2 |
26 Feb 07 |
jari |
702 |
this.data.convergenceThreshold = params.getFloat("convergence-threshold"); |
2 |
26 Feb 07 |
jari |
703 |
this.data.radial = params.getBoolean("radial"); |
2 |
26 Feb 07 |
jari |
704 |
this.data.normalize = params.getBoolean("normalize"); |
2 |
26 Feb 07 |
jari |
705 |
this.data.widthFactor = params.getFloat("width-factor"); |
2 |
26 Feb 07 |
jari |
706 |
this.data.constrainWeights = params.getBoolean("constrain-weights"); |
2 |
26 Feb 07 |
jari |
707 |
this.data.positiveConstraint = params.getFloat("positive-constraint"); |
2 |
26 Feb 07 |
jari |
708 |
this.data.negativeConstraint = params.getFloat("negative-constraint"); |
2 |
26 Feb 07 |
jari |
709 |
} |
2 |
26 Feb 07 |
jari |
710 |
this.data.useEditor = params.getBoolean("used-classification-editor"); |
2 |
26 Feb 07 |
jari |
711 |
if(!this.data.useEditor) { |
2 |
26 Feb 07 |
jari |
712 |
String fileName = params.getString("classification-file-name"); |
2 |
26 Feb 07 |
jari |
713 |
if(fileName != null) |
2 |
26 Feb 07 |
jari |
714 |
this.data.classificationFile = new File(fileName); |
2 |
26 Feb 07 |
jari |
715 |
} |
2 |
26 Feb 07 |
jari |
716 |
|
2 |
26 Feb 07 |
jari |
717 |
this.data.classifyGenes = params.getBoolean("classify-genes"); |
2 |
26 Feb 07 |
jari |
718 |
this.data.calculateHCL = params.getBoolean("calculate-hcl"); |
2 |
26 Feb 07 |
jari |
719 |
if(this.data.calculateHCL) { |
2 |
26 Feb 07 |
jari |
720 |
this.data.calcSampleHCL = params.getBoolean("calculate-samples-hcl"); |
2 |
26 Feb 07 |
jari |
721 |
this.data.calcGeneHCL = params.getBoolean("calculate-genes-hcl"); |
2 |
26 Feb 07 |
jari |
722 |
this.data.hclMethod = params.getInt("linkage-method"); |
2 |
26 Feb 07 |
jari |
723 |
} |
2 |
26 Feb 07 |
jari |
724 |
this.data.distanceFunction = params.getInt("hcl-distance-function"); |
2 |
26 Feb 07 |
jari |
725 |
this.data.absoluteDistance = params.getBoolean("hcl-absolute-distance"); |
2 |
26 Feb 07 |
jari |
726 |
} |
2 |
26 Feb 07 |
jari |
727 |
|
2 |
26 Feb 07 |
jari |
728 |
|
2 |
26 Feb 07 |
jari |
729 |
private AlgorithmData constructCumulativeResult(FloatMatrix discriminantMatrix){ |
2 |
26 Feb 07 |
jari |
730 |
AlgorithmData result = new AlgorithmData(); |
2 |
26 Feb 07 |
jari |
731 |
result.addMatrix("experiment", this.experimentMap.getMatrix()); |
2 |
26 Feb 07 |
jari |
732 |
result.addMatrix("discriminant", discriminantMatrix); |
2 |
26 Feb 07 |
jari |
733 |
result.addIntArray("positives", getPositives(discriminantMatrix)); |
2 |
26 Feb 07 |
jari |
734 |
result.addIntArray("negatives", getNegatives(discriminantMatrix)); |
2 |
26 Feb 07 |
jari |
735 |
FloatMatrix means = getMeans(discriminantMatrix); |
2 |
26 Feb 07 |
jari |
736 |
result.addMatrix("means", means); |
2 |
26 Feb 07 |
jari |
737 |
result.addMatrix("variances", getVariance(discriminantMatrix, means)); |
2 |
26 Feb 07 |
jari |
738 |
return result; |
2 |
26 Feb 07 |
jari |
739 |
} |
2 |
26 Feb 07 |
jari |
740 |
|
2 |
26 Feb 07 |
jari |
741 |
private void getNumberOfCorrectPlacements(int [] placements, int [] elementAccuracy, int index){ |
2 |
26 Feb 07 |
jari |
742 |
int clazz; |
2 |
26 Feb 07 |
jari |
743 |
int n = 0; |
2 |
26 Feb 07 |
jari |
744 |
|
2 |
26 Feb 07 |
jari |
745 |
for(int i = 0; i < discriminantMatrix.getRowDimension(); i++){ |
2 |
26 Feb 07 |
jari |
746 |
clazz = (int)discriminantMatrix.get(i,0); |
2 |
26 Feb 07 |
jari |
747 |
if(clazz == classes[i]){ |
2 |
26 Feb 07 |
jari |
748 |
elementAccuracy[i]++; |
2 |
26 Feb 07 |
jari |
749 |
n++; |
2 |
26 Feb 07 |
jari |
750 |
} |
2 |
26 Feb 07 |
jari |
751 |
} |
2 |
26 Feb 07 |
jari |
752 |
placements[index] = n; |
2 |
26 Feb 07 |
jari |
753 |
} |
2 |
26 Feb 07 |
jari |
754 |
|
2 |
26 Feb 07 |
jari |
755 |
|
2 |
26 Feb 07 |
jari |
756 |
/** |
2 |
26 Feb 07 |
jari |
* Selection of working on genes or experiments, and SVMMode, train, classify, or both. |
2 |
26 Feb 07 |
jari |
* @return Returns true if SVM is classifying genes |
2 |
26 Feb 07 |
jari |
759 |
*/ |
2 |
26 Feb 07 |
jari |
760 |
private boolean selectSVMProcedure(){ |
2 |
26 Feb 07 |
jari |
761 |
SVMProcessInitDialog dialog = new SVMProcessInitDialog( this.parentFrame, true ); |
2 |
26 Feb 07 |
jari |
762 |
if (dialog.showModal() != JOptionPane.OK_OPTION) |
2 |
26 Feb 07 |
jari |
763 |
return false; |
2 |
26 Feb 07 |
jari |
764 |
else{ |
2 |
26 Feb 07 |
jari |
765 |
classifyGenes = dialog.isEvaluateGenesSelected(); |
2 |
26 Feb 07 |
jari |
766 |
this.data.classifyGenes = classifyGenes; |
2 |
26 Feb 07 |
jari |
767 |
SVMMode = dialog.getSVMProcessSelection(); |
2 |
26 Feb 07 |
jari |
768 |
this.data.calculateHCL = dialog.getHCLSelection(); |
2 |
26 Feb 07 |
jari |
769 |
|
2 |
26 Feb 07 |
jari |
770 |
if(this.data.calculateHCL){ |
2 |
26 Feb 07 |
jari |
771 |
IDistanceMenu menu = framework.getDistanceMenu(); |
2 |
26 Feb 07 |
jari |
772 |
|
2 |
26 Feb 07 |
jari |
773 |
HCLInitDialog hcl_dialog = new HCLInitDialog(framework.getFrame(), menu.getFunctionName(menu.getDistanceFunction()), menu.isAbsoluteDistance(), true); |
2 |
26 Feb 07 |
jari |
774 |
|
2 |
26 Feb 07 |
jari |
775 |
if (hcl_dialog.showModal() == JOptionPane.OK_OPTION) { |
2 |
26 Feb 07 |
jari |
776 |
this.data.hclMethod = hcl_dialog.getMethod(); |
2 |
26 Feb 07 |
jari |
777 |
this.data.calcSampleHCL = hcl_dialog.isClusterExperiments(); |
2 |
26 Feb 07 |
jari |
778 |
this.data.calcGeneHCL = hcl_dialog.isClusterGenes(); |
2 |
26 Feb 07 |
jari |
779 |
this.data.distanceFunction = hcl_dialog.getDistanceMetric(); |
2 |
26 Feb 07 |
jari |
780 |
this.data.absoluteDistance = hcl_dialog.getAbsoluteSelection(); |
2 |
26 Feb 07 |
jari |
781 |
} |
2 |
26 Feb 07 |
jari |
782 |
else |
2 |
26 Feb 07 |
jari |
783 |
this.data.calculateHCL = false; //changed mind about HCL |
2 |
26 Feb 07 |
jari |
784 |
} |
2 |
26 Feb 07 |
jari |
785 |
return true; |
2 |
26 Feb 07 |
jari |
786 |
} |
2 |
26 Feb 07 |
jari |
787 |
} |
2 |
26 Feb 07 |
jari |
788 |
|
2 |
26 Feb 07 |
jari |
789 |
|
2 |
26 Feb 07 |
jari |
790 |
/*************************************************************************************************** |
2 |
26 Feb 07 |
jari |
791 |
* |
2 |
26 Feb 07 |
jari |
* Training Init code. |
2 |
26 Feb 07 |
jari |
793 |
*/ |
2 |
26 Feb 07 |
jari |
794 |
|
2 |
26 Feb 07 |
jari |
795 |
/** |
2 |
26 Feb 07 |
jari |
* Parameters initialization. |
2 |
26 Feb 07 |
jari |
* @throws AlgorithmException |
2 |
26 Feb 07 |
jari |
* @return Returns true if successful |
2 |
26 Feb 07 |
jari |
799 |
*/ |
2 |
26 Feb 07 |
jari |
800 |
protected boolean initTrainingParams() {//throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
801 |
trainingMatrix = experiment.getExperiment().getMatrix(); |
2 |
26 Feb 07 |
jari |
802 |
kernelMatrix = null; |
2 |
26 Feb 07 |
jari |
//data.distanceFunction = menu.getDistanceFunction(); |
2 |
26 Feb 07 |
jari |
//if (data.distanceFunction == Algorithm.DEFAULT) |
2 |
26 Feb 07 |
jari |
// data.distanceFunction = Algorithm.EUCLIDEAN; //this applies to HCL on SVM result |
2 |
26 Feb 07 |
jari |
//SVM kernal uses dot product on normalized vectors |
2 |
26 Feb 07 |
jari |
807 |
|
2 |
26 Feb 07 |
jari |
808 |
SVMInitDialog dialog = new SVMInitDialog( this.parentFrame, this.data ); |
2 |
26 Feb 07 |
jari |
809 |
|
2 |
26 Feb 07 |
jari |
810 |
if (dialog.showModal() != JOptionPane.OK_OPTION) |
2 |
26 Feb 07 |
jari |
811 |
return false; |
2 |
26 Feb 07 |
jari |
812 |
this.data = dialog.getData(); |
2 |
26 Feb 07 |
jari |
813 |
if(classifyGenes) |
2 |
26 Feb 07 |
jari |
814 |
classes = new int[trainingMatrix.getRowDimension()]; |
2 |
26 Feb 07 |
jari |
815 |
else{ |
2 |
26 Feb 07 |
jari |
816 |
classes = new int[trainingMatrix.getColumnDimension()]; |
2 |
26 Feb 07 |
jari |
817 |
} |
2 |
26 Feb 07 |
jari |
818 |
|
2 |
26 Feb 07 |
jari |
819 |
if(!scripting) |
2 |
26 Feb 07 |
jari |
820 |
showLogger("SVM Log Window"); |
2 |
26 Feb 07 |
jari |
821 |
if(this.data.useEditor){ |
2 |
26 Feb 07 |
jari |
822 |
SVMClassificationEditor editor = new SVMClassificationEditor(framework, this.data.classifyGenes); |
2 |
26 Feb 07 |
jari |
823 |
|
2 |
26 Feb 07 |
jari |
824 |
if(!scripting) |
2 |
26 Feb 07 |
jari |
825 |
logger.append("Using Classification Editor\n");// editor.show(); |
2 |
26 Feb 07 |
jari |
826 |
|
2 |
26 Feb 07 |
jari |
827 |
editor.setVisible(true); |
2 |
26 Feb 07 |
jari |
//editor.show(); |
2 |
26 Feb 07 |
jari |
// while(editor.isVisible()){ |
2 |
26 Feb 07 |
jari |
// wait until done |
2 |
26 Feb 07 |
jari |
831 |
// } |
2 |
26 Feb 07 |
jari |
832 |
if(editor.formCanceled()){ |
2 |
26 Feb 07 |
jari |
833 |
System.out.println("form canceled!!!"); |
2 |
26 Feb 07 |
jari |
834 |
return false; |
2 |
26 Feb 07 |
jari |
835 |
} |
2 |
26 Feb 07 |
jari |
836 |
classes = editor.getClassification(); |
2 |
26 Feb 07 |
jari |
837 |
} |
2 |
26 Feb 07 |
jari |
838 |
else{ |
2 |
26 Feb 07 |
jari |
839 |
logger.append("Reading classification file\n"); |
2 |
26 Feb 07 |
jari |
840 |
if (!readSVCFile()) |
2 |
26 Feb 07 |
jari |
841 |
return false; |
2 |
26 Feb 07 |
jari |
842 |
} |
2 |
26 Feb 07 |
jari |
843 |
return true; |
2 |
26 Feb 07 |
jari |
844 |
} |
2 |
26 Feb 07 |
jari |
845 |
|
2 |
26 Feb 07 |
jari |
846 |
|
2 |
26 Feb 07 |
jari |
847 |
/** |
2 |
26 Feb 07 |
jari |
* Reads an SVC classification file. Sets classes. |
2 |
26 Feb 07 |
jari |
* @return Returns true if read is successful and correct |
2 |
26 Feb 07 |
jari |
850 |
*/ |
2 |
26 Feb 07 |
jari |
851 |
private boolean readSVCFile() { |
2 |
26 Feb 07 |
jari |
852 |
String WorkString=""; |
2 |
26 Feb 07 |
jari |
853 |
String Dummy; |
2 |
26 Feb 07 |
jari |
854 |
String Value; |
2 |
26 Feb 07 |
jari |
855 |
int Position=0; |
2 |
26 Feb 07 |
jari |
856 |
int CurrentGene; |
2 |
26 Feb 07 |
jari |
857 |
boolean ReturnValue=false; |
2 |
26 Feb 07 |
jari |
858 |
BufferedReader in=null; |
2 |
26 Feb 07 |
jari |
859 |
try { |
2 |
26 Feb 07 |
jari |
860 |
in = new BufferedReader(new InputStreamReader(new FileInputStream(data.classificationFile.getPath()))); |
2 |
26 Feb 07 |
jari |
861 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
862 |
if (Dummy!=null) { |
2 |
26 Feb 07 |
jari |
863 |
WorkString=Dummy; |
2 |
26 Feb 07 |
jari |
864 |
Position=WorkString.indexOf(9); |
2 |
26 Feb 07 |
jari |
865 |
WorkString=WorkString.substring(Position+1,WorkString.length()); |
2 |
26 Feb 07 |
jari |
866 |
Value=WorkString; |
2 |
26 Feb 07 |
jari |
//int i=0; |
2 |
26 Feb 07 |
jari |
868 |
} |
2 |
26 Feb 07 |
jari |
869 |
CurrentGene=0; |
2 |
26 Feb 07 |
jari |
870 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
871 |
while (Dummy != null) { |
2 |
26 Feb 07 |
jari |
872 |
WorkString=Dummy; |
2 |
26 Feb 07 |
jari |
873 |
WorkString = WorkString.substring(WorkString.indexOf(9)+1); //chop index |
2 |
26 Feb 07 |
jari |
874 |
Position=WorkString.indexOf(9); //WorkString starts with -1 or 1 (init. class.) |
2 |
26 Feb 07 |
jari |
875 |
Value=WorkString.substring(0,Position); |
2 |
26 Feb 07 |
jari |
876 |
WorkString=WorkString.substring(Position+1,WorkString.length()); |
2 |
26 Feb 07 |
jari |
877 |
CurrentGene++; |
2 |
26 Feb 07 |
jari |
/* TODO |
2 |
26 Feb 07 |
jari |
if (Value.compareTo( experiment.getMatrix().GetUniqueID(CurrentGene-1))!=0) { |
2 |
26 Feb 07 |
jari |
JOptionPane.showMessageDialog(parentFrame, "Unique ID does not fit in line "+CurrentGene,"Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
break; |
2 |
26 Feb 07 |
jari |
882 |
} |
2 |
26 Feb 07 |
jari |
883 |
*/ |
2 |
26 Feb 07 |
jari |
884 |
if ((Value.compareTo("1")*Value.compareTo("-1"))!=0) { |
2 |
26 Feb 07 |
jari |
885 |
JOptionPane.showMessageDialog(parentFrame, "Value not Element of [1,-1] in line "+CurrentGene,"Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
886 |
break; |
2 |
26 Feb 07 |
jari |
887 |
} else { |
2 |
26 Feb 07 |
jari |
888 |
classes[CurrentGene-1]=Integer.parseInt(Value); |
2 |
26 Feb 07 |
jari |
889 |
} |
2 |
26 Feb 07 |
jari |
890 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
891 |
} |
2 |
26 Feb 07 |
jari |
892 |
if (this.classifyGenes && CurrentGene != this.experimentMap.getNumberOfGenes()) { |
2 |
26 Feb 07 |
jari |
893 |
JOptionPane.showMessageDialog(parentFrame, "Number of Genes to classify does not match current data set!","Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
894 |
} |
2 |
26 Feb 07 |
jari |
895 |
else if(!this.classifyGenes && CurrentGene != this.experimentMap.getNumberOfSamples()){ |
2 |
26 Feb 07 |
jari |
896 |
JOptionPane.showMessageDialog(parentFrame, "Number of Experiments to classify does not match current data set!","Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
897 |
} |
2 |
26 Feb 07 |
jari |
898 |
else { |
2 |
26 Feb 07 |
jari |
899 |
ReturnValue=true; |
2 |
26 Feb 07 |
jari |
900 |
} |
2 |
26 Feb 07 |
jari |
901 |
} catch (IOException e) { |
2 |
26 Feb 07 |
jari |
902 |
JOptionPane.showMessageDialog(parentFrame, "Can not read file "+data.classificationFile.getPath()+"!","Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
903 |
}; |
2 |
26 Feb 07 |
jari |
904 |
return ReturnValue; |
2 |
26 Feb 07 |
jari |
905 |
} |
2 |
26 Feb 07 |
jari |
906 |
|
2 |
26 Feb 07 |
jari |
907 |
|
2 |
26 Feb 07 |
jari |
908 |
/** |
2 |
26 Feb 07 |
jari |
* Binds parameters required for SVM Training |
2 |
26 Feb 07 |
jari |
* @param data Holds parameters for SVM training |
2 |
26 Feb 07 |
jari |
911 |
*/ |
2 |
26 Feb 07 |
jari |
912 |
protected void bindTrainingParams( AlgorithmData data ) { |
2 |
26 Feb 07 |
jari |
913 |
bindParams( data ); |
2 |
26 Feb 07 |
jari |
914 |
data.addParam("is-classify", String.valueOf(false)); |
2 |
26 Feb 07 |
jari |
915 |
data.addParam("hcl-distance-function", String.valueOf( this.data.distanceFunction ) ); |
2 |
26 Feb 07 |
jari |
916 |
data.addIntArray("classes", classes); |
2 |
26 Feb 07 |
jari |
917 |
data.addParam("seed", String.valueOf( this.data.seed)); |
2 |
26 Feb 07 |
jari |
918 |
data.addParam("normalize", String.valueOf(this.data.normalize)); |
2 |
26 Feb 07 |
jari |
919 |
data.addParam("radial", String.valueOf(this.data.radial)); |
2 |
26 Feb 07 |
jari |
920 |
data.addParam("width-factor", String.valueOf(this.data.widthFactor)); |
2 |
26 Feb 07 |
jari |
921 |
data.addParam("positive-diagonal", String.valueOf(this.data.positiveDiagonal)); |
2 |
26 Feb 07 |
jari |
922 |
data.addParam("negative-diagonal", String.valueOf(this.data.negativeDiagonal)); |
2 |
26 Feb 07 |
jari |
923 |
data.addParam("diagonal-factor", String.valueOf(this.data.diagonalFactor)); |
2 |
26 Feb 07 |
jari |
924 |
data.addParam("positive-constraint", String.valueOf(this.data.positiveConstraint)); |
2 |
26 Feb 07 |
jari |
925 |
data.addParam("negative-constraint", String.valueOf(this.data.negativeConstraint)); |
2 |
26 Feb 07 |
jari |
926 |
data.addParam("convergence-threshold", String.valueOf(this.data.convergenceThreshold)); |
2 |
26 Feb 07 |
jari |
927 |
data.addParam("constrain-weights", String.valueOf(this.data.constrainWeights)); |
2 |
26 Feb 07 |
jari |
928 |
|
2 |
26 Feb 07 |
jari |
929 |
data.addParam("used-classification-editor", String.valueOf(this.data.useEditor)); |
2 |
26 Feb 07 |
jari |
930 |
if(!this.data.useEditor && this.data.classificationFile != null) |
2 |
26 Feb 07 |
jari |
931 |
data.addParam("classification-file-name", this.data.classificationFile.getName()); |
2 |
26 Feb 07 |
jari |
932 |
|
2 |
26 Feb 07 |
jari |
933 |
data.addParam("classify-genes", String.valueOf(classifyGenes)); |
2 |
26 Feb 07 |
jari |
934 |
} |
2 |
26 Feb 07 |
jari |
935 |
|
2 |
26 Feb 07 |
jari |
936 |
/** |
2 |
26 Feb 07 |
jari |
* Extracts results from traing run. (Weights) |
2 |
26 Feb 07 |
jari |
* @param result Holds SVM training results |
2 |
26 Feb 07 |
jari |
939 |
*/ |
2 |
26 Feb 07 |
jari |
940 |
protected void getTrainingResults( AlgorithmData result ) { |
2 |
26 Feb 07 |
jari |
941 |
FloatMatrix weightsMatrix = result.getMatrix("weights"); |
2 |
26 Feb 07 |
jari |
942 |
Weights = weightsMatrix.getColumnPackedCopy(); |
2 |
26 Feb 07 |
jari |
943 |
} |
2 |
26 Feb 07 |
jari |
944 |
|
2 |
26 Feb 07 |
jari |
945 |
|
2 |
26 Feb 07 |
jari |
946 |
/** |
2 |
26 Feb 07 |
jari |
* Creates svm train result. |
2 |
26 Feb 07 |
jari |
948 |
*/ |
2 |
26 Feb 07 |
jari |
949 |
protected DefaultMutableTreeNode createTrainingGUIResult() { |
2 |
26 Feb 07 |
jari |
950 |
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new LeafInfo("SVM Training Result", |
2 |
26 Feb 07 |
jari |
//new SVMTrainViewer( framework, experiment, experimentMap, data, Weights, info, this.data.classifyGenes ))); |
2 |
26 Feb 07 |
jari |
952 |
new SVMTrainViewer( experiment.getExperiment(), Weights, this.data.classifyGenes, data))); |
2 |
26 Feb 07 |
jari |
953 |
return root; |
2 |
26 Feb 07 |
jari |
954 |
} |
2 |
26 Feb 07 |
jari |
955 |
|
2 |
26 Feb 07 |
jari |
956 |
|
2 |
26 Feb 07 |
jari |
957 |
/** |
2 |
26 Feb 07 |
jari |
958 |
* |
2 |
26 Feb 07 |
jari |
* Code for Classification |
2 |
26 Feb 07 |
jari |
* @throws AlgorithmException |
2 |
26 Feb 07 |
jari |
* @return Returns true if initialization of parameters was successful |
2 |
26 Feb 07 |
jari |
962 |
*/ |
2 |
26 Feb 07 |
jari |
963 |
|
2 |
26 Feb 07 |
jari |
964 |
protected boolean initClassificationParams() {//throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
// data.distanceFunction = menu.getDistanceFunction(); |
2 |
26 Feb 07 |
jari |
// if (data.distanceFunction == Algorithm.DEFAULT) |
2 |
26 Feb 07 |
jari |
// data.distanceFunction = Algorithm.EUCLIDEAN; //this applies to HCL on SVM result |
2 |
26 Feb 07 |
jari |
//SVM kernal uses dot product on normalized vectors |
2 |
26 Feb 07 |
jari |
969 |
|
2 |
26 Feb 07 |
jari |
970 |
final JFileChooser fc = new JFileChooser(TMEV.getFile("data/svm")); |
2 |
26 Feb 07 |
jari |
971 |
fc.addChoosableFileFilter(new SVMFileFilter()); |
2 |
26 Feb 07 |
jari |
972 |
fc.setFileView(new SVMFileView()); |
2 |
26 Feb 07 |
jari |
973 |
int returnVal = fc.showOpenDialog( parentFrame ); |
2 |
26 Feb 07 |
jari |
974 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
2 |
26 Feb 07 |
jari |
975 |
SVMFile = fc.getSelectedFile(); |
2 |
26 Feb 07 |
jari |
976 |
if(!scripting) |
2 |
26 Feb 07 |
jari |
977 |
showLogger("SVM Classify Log Window"); |
2 |
26 Feb 07 |
jari |
978 |
} else |
2 |
26 Feb 07 |
jari |
979 |
return false; |
2 |
26 Feb 07 |
jari |
980 |
|
2 |
26 Feb 07 |
jari |
981 |
if(!scripting) |
2 |
26 Feb 07 |
jari |
982 |
logger.append("Reading SVM file\n"); |
2 |
26 Feb 07 |
jari |
983 |
|
2 |
26 Feb 07 |
jari |
984 |
try { |
2 |
26 Feb 07 |
jari |
985 |
if(!ReadSVMFile()) |
2 |
26 Feb 07 |
jari |
986 |
return false; |
2 |
26 Feb 07 |
jari |
987 |
} catch (Exception e) { |
2 |
26 Feb 07 |
jari |
988 |
return false; |
2 |
26 Feb 07 |
jari |
989 |
} |
2 |
26 Feb 07 |
jari |
990 |
return true; |
2 |
26 Feb 07 |
jari |
991 |
} |
2 |
26 Feb 07 |
jari |
992 |
|
2 |
26 Feb 07 |
jari |
993 |
|
2 |
26 Feb 07 |
jari |
994 |
/** |
2 |
26 Feb 07 |
jari |
* Reads svm file. Populates Weights array |
2 |
26 Feb 07 |
jari |
* @throws Exception |
2 |
26 Feb 07 |
jari |
* @return Returns true if read is successful |
2 |
26 Feb 07 |
jari |
998 |
*/ |
2 |
26 Feb 07 |
jari |
999 |
public boolean ReadSVMFile() throws Exception { |
2 |
26 Feb 07 |
jari |
1000 |
BufferedReader in; |
2 |
26 Feb 07 |
jari |
1001 |
String Dummy=new String(); |
2 |
26 Feb 07 |
jari |
1002 |
String WorkString=new String(); |
2 |
26 Feb 07 |
jari |
1003 |
String Value=new String(); |
2 |
26 Feb 07 |
jari |
1004 |
int Position; |
2 |
26 Feb 07 |
jari |
1005 |
int NumberOfGenes; |
2 |
26 Feb 07 |
jari |
1006 |
int NumberOfSamples; |
2 |
26 Feb 07 |
jari |
1007 |
|
2 |
26 Feb 07 |
jari |
1008 |
try { |
2 |
26 Feb 07 |
jari |
1009 |
in = new BufferedReader(new InputStreamReader(new FileInputStream(SVMFile))); |
2 |
26 Feb 07 |
jari |
1010 |
|
2 |
26 Feb 07 |
jari |
1011 |
NumberOfGenes = this.experimentMap.getNumberOfGenes(); |
2 |
26 Feb 07 |
jari |
1012 |
NumberOfSamples = this.experimentMap.getNumberOfSamples(); |
2 |
26 Feb 07 |
jari |
1013 |
in.readLine(); |
2 |
26 Feb 07 |
jari |
1014 |
|
2 |
26 Feb 07 |
jari |
1015 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1016 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1017 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1018 |
data.constant=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1019 |
|
2 |
26 Feb 07 |
jari |
1020 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1021 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1022 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1023 |
data.coefficient=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1024 |
|
2 |
26 Feb 07 |
jari |
1025 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1026 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1027 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1028 |
data.power=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1029 |
|
2 |
26 Feb 07 |
jari |
1030 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1031 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1032 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1033 |
data.diagonalFactor=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1034 |
|
2 |
26 Feb 07 |
jari |
1035 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1036 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1037 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1038 |
data.convergenceThreshold=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1039 |
|
2 |
26 Feb 07 |
jari |
//in.readLine(); //normalize bool |
2 |
26 Feb 07 |
jari |
//Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
//Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
//Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
//data.normalize=Boolean.valueOf(Value).booleanValue(); |
2 |
26 Feb 07 |
jari |
1045 |
|
2 |
26 Feb 07 |
jari |
//in.readLine(); //radial bool |
2 |
26 Feb 07 |
jari |
1047 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1048 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1049 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1050 |
data.radial=Boolean.valueOf(Value).booleanValue(); |
2 |
26 Feb 07 |
jari |
1051 |
|
2 |
26 Feb 07 |
jari |
1052 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1053 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1054 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1055 |
data.widthFactor=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1056 |
|
2 |
26 Feb 07 |
jari |
1057 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1058 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1059 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1060 |
data.constrainWeights=Boolean.valueOf(Value).booleanValue(); |
2 |
26 Feb 07 |
jari |
1061 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1062 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1063 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1064 |
data.positiveConstraint=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1065 |
|
2 |
26 Feb 07 |
jari |
1066 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1067 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1068 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1069 |
data.negativeConstraint=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1070 |
|
2 |
26 Feb 07 |
jari |
1071 |
in.readLine(); |
2 |
26 Feb 07 |
jari |
//in.readLine(); //skip execution time |
2 |
26 Feb 07 |
jari |
1073 |
|
2 |
26 Feb 07 |
jari |
1074 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1075 |
Position=Dummy.indexOf(":"); |
2 |
26 Feb 07 |
jari |
1076 |
Value=Dummy.substring(Position+2,Dummy.length()); |
2 |
26 Feb 07 |
jari |
1077 |
data.objective1=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1078 |
|
2 |
26 Feb 07 |
jari |
1079 |
in.readLine(); //read blank row |
2 |
26 Feb 07 |
jari |
1080 |
in.readLine(); //read data header |
2 |
26 Feb 07 |
jari |
1081 |
|
2 |
26 Feb 07 |
jari |
1082 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
1083 |
Weights=new float[NumberOfGenes]; |
2 |
26 Feb 07 |
jari |
1084 |
else |
2 |
26 Feb 07 |
jari |
1085 |
Weights=new float[NumberOfSamples]; |
2 |
26 Feb 07 |
jari |
1086 |
|
2 |
26 Feb 07 |
jari |
//ready to read weights |
2 |
26 Feb 07 |
jari |
1088 |
|
2 |
26 Feb 07 |
jari |
1089 |
for (int i=0; i<Weights.length; i++) { |
2 |
26 Feb 07 |
jari |
1090 |
Dummy=in.readLine(); |
2 |
26 Feb 07 |
jari |
1091 |
WorkString=Dummy; |
2 |
26 Feb 07 |
jari |
1092 |
Position=WorkString.indexOf(9); |
2 |
26 Feb 07 |
jari |
1093 |
Value=WorkString.substring(0,Position); |
2 |
26 Feb 07 |
jari |
1094 |
Weights[i]=Float.valueOf(Value).floatValue(); |
2 |
26 Feb 07 |
jari |
1095 |
} |
2 |
26 Feb 07 |
jari |
1096 |
|
2 |
26 Feb 07 |
jari |
1097 |
if(in.readLine() != null){ |
2 |
26 Feb 07 |
jari |
1098 |
JOptionPane.showMessageDialog(parentFrame, SVMFile.getPath()+" has an incorrect number of weight values for current data set.", "Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
1099 |
return false; |
2 |
26 Feb 07 |
jari |
1100 |
} |
2 |
26 Feb 07 |
jari |
1101 |
|
2 |
26 Feb 07 |
jari |
1102 |
} catch (Exception e2) { |
2 |
26 Feb 07 |
jari |
1103 |
e2.printStackTrace(); |
2 |
26 Feb 07 |
jari |
1104 |
JOptionPane.showMessageDialog(parentFrame, "Can not read file "+SVMFile.getPath()+"!","Error", JOptionPane.ERROR_MESSAGE); |
2 |
26 Feb 07 |
jari |
1105 |
throw e2; |
2 |
26 Feb 07 |
jari |
1106 |
} |
2 |
26 Feb 07 |
jari |
1107 |
return true; |
2 |
26 Feb 07 |
jari |
1108 |
} |
2 |
26 Feb 07 |
jari |
1109 |
|
2 |
26 Feb 07 |
jari |
1110 |
|
2 |
26 Feb 07 |
jari |
1111 |
|
2 |
26 Feb 07 |
jari |
1112 |
/** |
2 |
26 Feb 07 |
jari |
* Binds parameters from the specified data. |
2 |
26 Feb 07 |
jari |
* @param data Maintains parameters for SVM classification |
2 |
26 Feb 07 |
jari |
1115 |
*/ |
2 |
26 Feb 07 |
jari |
1116 |
protected void bindClassificationParams( AlgorithmData data ) { |
2 |
26 Feb 07 |
jari |
1117 |
bindParams( data ); //already done |
2 |
26 Feb 07 |
jari |
1118 |
data.addParam("is-classify", String.valueOf(true)); |
2 |
26 Feb 07 |
jari |
1119 |
if(!scripting) { |
2 |
26 Feb 07 |
jari |
1120 |
if(this.SVMMode == SVMGUI.CLASSIFY_ONLY) |
2 |
26 Feb 07 |
jari |
1121 |
trainingMatrix = this.experiment.getExperiment().getMatrix(); |
2 |
26 Feb 07 |
jari |
1122 |
if(this.data.classifyGenes) |
2 |
26 Feb 07 |
jari |
1123 |
data.addMatrix("training", trainingMatrix); |
2 |
26 Feb 07 |
jari |
1124 |
else |
2 |
26 Feb 07 |
jari |
1125 |
data.addMatrix("training", trainingMatrix.transpose()); |
2 |
26 Feb 07 |
jari |
1126 |
} |
2 |
26 Feb 07 |
jari |
1127 |
if(this.Weights != null) |
2 |
26 Feb 07 |
jari |
1128 |
data.addMatrix("weights", new FloatMatrix(Weights, 1)); //weights already set by Training |
2 |
26 Feb 07 |
jari |
1129 |
data.addParam("classify-genes", String.valueOf(classifyGenes)); |
2 |
26 Feb 07 |
jari |
1130 |
if(this.data.calculateHCL){ |
2 |
26 Feb 07 |
jari |
1131 |
data.addParam("calculate-hcl", String.valueOf(this.data.calculateHCL)); |
2 |
26 Feb 07 |
jari |
1132 |
data.addParam("calculate-genes-hcl", String.valueOf(this.data.calcGeneHCL)); |
2 |
26 Feb 07 |
jari |
1133 |
data.addParam("calculate-samples-hcl", String.valueOf(this.data.calcSampleHCL)); |
2 |
26 Feb 07 |
jari |
1134 |
data.addParam("linkage-method", String.valueOf(this.data.hclMethod)); |
2 |
26 Feb 07 |
jari |
1135 |
data.addParam("hcl-distance-fucntion", String.valueOf(this.data.distanceFunction)); |
2 |
26 Feb 07 |
jari |
1136 |
data.addParam("hcl-absolute-distance", String.valueOf(this.data.absoluteDistance)); |
2 |
26 Feb 07 |
jari |
1137 |
} |
2 |
26 Feb 07 |
jari |
1138 |
} |
2 |
26 Feb 07 |
jari |
1139 |
|
2 |
26 Feb 07 |
jari |
1140 |
/** |
2 |
26 Feb 07 |
jari |
* Extracts classification result |
2 |
26 Feb 07 |
jari |
* @param result Holds SVM classification results |
2 |
26 Feb 07 |
jari |
1143 |
*/ |
2 |
26 Feb 07 |
jari |
1144 |
protected void getClassificationResults( AlgorithmData result ) { |
2 |
26 Feb 07 |
jari |
1145 |
discriminantMatrix = result.getMatrix("discriminant"); |
2 |
26 Feb 07 |
jari |
1146 |
} |
2 |
26 Feb 07 |
jari |
1147 |
|
2 |
26 Feb 07 |
jari |
1148 |
|
2 |
26 Feb 07 |
jari |
1149 |
/** |
2 |
26 Feb 07 |
jari |
* Creates analysis result to be inserted into the framework tree node. |
2 |
26 Feb 07 |
jari |
* @return Node containing <CODE>SVMClassifyViewer</CODE> |
2 |
26 Feb 07 |
jari |
1152 |
*/ |
2 |
26 Feb 07 |
jari |
1153 |
protected DefaultMutableTreeNode createClassificationGUIResult() { |
2 |
26 Feb 07 |
jari |
1154 |
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new LeafInfo("SVM Classification Result", |
2 |
26 Feb 07 |
jari |
// new SVMClassifyViewer( framework, experiment, data, discriminantMatrix, info, this.data.classifyGenes ))); |
2 |
26 Feb 07 |
jari |
1156 |
new SVMClassifyViewer(experiment.getExperiment(), discriminantMatrix, this.data.classifyGenes))); |
2 |
26 Feb 07 |
jari |
1157 |
return root; |
2 |
26 Feb 07 |
jari |
1158 |
} |
2 |
26 Feb 07 |
jari |
1159 |
|
2 |
26 Feb 07 |
jari |
1160 |
/** |
2 |
26 Feb 07 |
jari |
* Creates viewers folowing classification |
2 |
26 Feb 07 |
jari |
* @param result Holds svm classification result |
2 |
26 Feb 07 |
jari |
* @return contains result viewers |
2 |
26 Feb 07 |
jari |
1164 |
*/ |
2 |
26 Feb 07 |
jari |
1165 |
protected DefaultMutableTreeNode createViewers(AlgorithmData result){ |
2 |
26 Feb 07 |
jari |
1166 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Expression Images"); |
2 |
26 Feb 07 |
jari |
1167 |
int [][] classIndices = getClassificationIndices(result.getIntArray("positives"), result.getIntArray("negatives")); |
2 |
26 Feb 07 |
jari |
1168 |
IViewer expViewer; |
2 |
26 Feb 07 |
jari |
1169 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
1170 |
expViewer = new SVMExperimentViewer(this.experimentMap, classIndices); |
2 |
26 Feb 07 |
jari |
1171 |
else |
2 |
26 Feb 07 |
jari |
1172 |
expViewer = new SVMExperimentClusterViewer(this.experimentMap, classIndices); |
2 |
26 Feb 07 |
jari |
1173 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Positives", expViewer, new Integer(0)))); |
2 |
26 Feb 07 |
jari |
1174 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Negatives", expViewer, new Integer(1)))); |
2 |
26 Feb 07 |
jari |
1175 |
return node; |
2 |
26 Feb 07 |
jari |
1176 |
} |
2 |
26 Feb 07 |
jari |
1177 |
|
2 |
26 Feb 07 |
jari |
1178 |
|
2 |
26 Feb 07 |
jari |
1179 |
/** |
2 |
26 Feb 07 |
jari |
* Creates viewers following svm train and classify. |
2 |
26 Feb 07 |
jari |
1181 |
*/ |
2 |
26 Feb 07 |
jari |
1182 |
protected DefaultMutableTreeNode createSVMExpressionViews(AlgorithmData result, int [] classification){ |
2 |
26 Feb 07 |
jari |
1183 |
int [] pos = result.getIntArray("positives"); |
2 |
26 Feb 07 |
jari |
1184 |
int [] neg = result.getIntArray("negatives"); |
2 |
26 Feb 07 |
jari |
1185 |
|
2 |
26 Feb 07 |
jari |
1186 |
int [][] truePositives = getTruePositives(pos, classification); |
2 |
26 Feb 07 |
jari |
1187 |
int [][] falseNegatives = getFalseNegatives(pos, classification); |
2 |
26 Feb 07 |
jari |
1188 |
int [][] falsePositives = getFalsePositives(neg, classification); |
2 |
26 Feb 07 |
jari |
1189 |
int [][] trueNegatives = getTrueNegatives(neg, classification); |
2 |
26 Feb 07 |
jari |
1190 |
|
2 |
26 Feb 07 |
jari |
1191 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Expression Images"); |
2 |
26 Feb 07 |
jari |
1192 |
|
2 |
26 Feb 07 |
jari |
1193 |
if(this.classifyGenes){ |
2 |
26 Feb 07 |
jari |
1194 |
SVMDiscriminantExperimentViewer viewer = new SVMDiscriminantExperimentViewer(experimentMap, getOrderedClassIndices(truePositives[0], falseNegatives[0], falsePositives[0], trueNegatives[0]), |
2 |
26 Feb 07 |
jari |
1195 |
truePositives != null ? truePositives[0].length : 0, |
2 |
26 Feb 07 |
jari |
1196 |
falsePositives != null ? falsePositives[0].length : 0, |
2 |
26 Feb 07 |
jari |
1197 |
getDiscriminants(pos, neg), null, this.classifyGenes); |
2 |
26 Feb 07 |
jari |
1198 |
|
2 |
26 Feb 07 |
jari |
1199 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Positives", viewer, new Integer(0)))); |
2 |
26 Feb 07 |
jari |
1200 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Negatives", viewer, new Integer(1)))); |
2 |
26 Feb 07 |
jari |
1201 |
} |
2 |
26 Feb 07 |
jari |
1202 |
else{ |
2 |
26 Feb 07 |
jari |
//Classify Experiments |
2 |
26 Feb 07 |
jari |
1204 |
int [] posSamplesOrder = result.getIntArray("positve-samples-order"); |
2 |
26 Feb 07 |
jari |
1205 |
int [] negSamplesOrder = result.getIntArray("negative-samples-order"); |
2 |
26 Feb 07 |
jari |
1206 |
SVMExperimentClusterViewer viewer = new SVMExperimentClusterViewer(experimentMap, getClassificationIndices(pos, neg)); |
2 |
26 Feb 07 |
jari |
1207 |
|
2 |
26 Feb 07 |
jari |
1208 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Positive Experiments", viewer, new Integer(0)))); |
2 |
26 Feb 07 |
jari |
1209 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Negative Experiments", viewer, new Integer(1)))); |
2 |
26 Feb 07 |
jari |
1210 |
} |
2 |
26 Feb 07 |
jari |
1211 |
|
2 |
26 Feb 07 |
jari |
1212 |
return node; |
2 |
26 Feb 07 |
jari |
1213 |
} |
2 |
26 Feb 07 |
jari |
1214 |
|
2 |
26 Feb 07 |
jari |
1215 |
protected void createInfoView(AlgorithmData result, DefaultMutableTreeNode node){ |
2 |
26 Feb 07 |
jari |
1216 |
int [] pos = result.getIntArray("positives"); |
2 |
26 Feb 07 |
jari |
1217 |
int [] neg = result.getIntArray("negatives"); |
2 |
26 Feb 07 |
jari |
1218 |
SVMInfoViewer viewer; |
2 |
26 Feb 07 |
jari |
1219 |
if(SVMMode == SVMGUI.CLASSIFY_ONLY){ |
2 |
26 Feb 07 |
jari |
1220 |
viewer = new SVMInfoViewer(pos.length, neg.length, this.classifyGenes, this.SVMMode); |
2 |
26 Feb 07 |
jari |
1221 |
|
2 |
26 Feb 07 |
jari |
1222 |
} |
2 |
26 Feb 07 |
jari |
1223 |
|
2 |
26 Feb 07 |
jari |
1224 |
else{ |
2 |
26 Feb 07 |
jari |
1225 |
int [][] truePositives = getTruePositives(pos, classes); |
2 |
26 Feb 07 |
jari |
1226 |
int [][] falseNegatives = getFalseNegatives(pos, classes); |
2 |
26 Feb 07 |
jari |
1227 |
int [][] falsePositives = getFalsePositives(neg, classes); |
2 |
26 Feb 07 |
jari |
1228 |
int [][] trueNegatives = getTrueNegatives(neg, classes); |
2 |
26 Feb 07 |
jari |
1229 |
|
2 |
26 Feb 07 |
jari |
1230 |
int numPosRecruitedFromNeutrals = 0; |
2 |
26 Feb 07 |
jari |
1231 |
int numNegRecruitedFromNeutrals = 0; |
2 |
26 Feb 07 |
jari |
1232 |
int numPosExamples = getNumberOfPositiveExamples(classes); |
2 |
26 Feb 07 |
jari |
1233 |
int numNegExamples = getNumberOfNegativeExamples(classes); |
2 |
26 Feb 07 |
jari |
1234 |
|
2 |
26 Feb 07 |
jari |
//If neutrals exist |
2 |
26 Feb 07 |
jari |
1236 |
if((classes.length - numPosExamples) - numNegExamples > 0){ |
2 |
26 Feb 07 |
jari |
1237 |
numPosRecruitedFromNeutrals = getNumPosRecFromNeutrals(pos, classes); |
2 |
26 Feb 07 |
jari |
1238 |
numNegRecruitedFromNeutrals = getNumNegRecFromNeutrals(neg, classes); |
2 |
26 Feb 07 |
jari |
1239 |
} |
2 |
26 Feb 07 |
jari |
1240 |
|
2 |
26 Feb 07 |
jari |
1241 |
viewer = new SVMInfoViewer( numPosExamples, numNegExamples, (classes.length - numPosExamples) - numNegExamples, |
2 |
26 Feb 07 |
jari |
1242 |
pos.length, truePositives[0].length, falseNegatives[0].length, neg.length, trueNegatives[0].length, falsePositives[0].length, |
2 |
26 Feb 07 |
jari |
1243 |
numPosRecruitedFromNeutrals, numNegRecruitedFromNeutrals, this.classifyGenes, this.SVMMode); |
2 |
26 Feb 07 |
jari |
1244 |
} |
2 |
26 Feb 07 |
jari |
1245 |
|
2 |
26 Feb 07 |
jari |
1246 |
node.add(new DefaultMutableTreeNode(new LeafInfo("Classification Information", viewer))); |
2 |
26 Feb 07 |
jari |
1247 |
} |
2 |
26 Feb 07 |
jari |
1248 |
|
2 |
26 Feb 07 |
jari |
/** Creates SVM Centroid viewes |
2 |
26 Feb 07 |
jari |
1250 |
*/ |
2 |
26 Feb 07 |
jari |
1251 |
private DefaultMutableTreeNode createSVMCentroidViews(AlgorithmData result, DefaultMutableTreeNode root){ |
2 |
26 Feb 07 |
jari |
1252 |
DefaultMutableTreeNode centNode = new DefaultMutableTreeNode("Centroid Graphs"); |
2 |
26 Feb 07 |
jari |
1253 |
DefaultMutableTreeNode expNode = new DefaultMutableTreeNode("Expression Graphs"); |
2 |
26 Feb 07 |
jari |
1254 |
float [][] means; |
2 |
26 Feb 07 |
jari |
1255 |
float [][] vars; |
2 |
26 Feb 07 |
jari |
1256 |
|
2 |
26 Feb 07 |
jari |
1257 |
int [][] clusters = this.getClassificationIndices(result.getIntArray("positives"), result.getIntArray("negatives")); |
2 |
26 Feb 07 |
jari |
1258 |
if(this.classifyGenes){ |
2 |
26 Feb 07 |
jari |
1259 |
SVMCentroidViewer viewer = new SVMCentroidViewer(this.experimentMap, clusters); |
2 |
26 Feb 07 |
jari |
1260 |
means = result.getMatrix("means").A; |
2 |
26 Feb 07 |
jari |
1261 |
vars = result.getMatrix("variances").A; |
2 |
26 Feb 07 |
jari |
1262 |
viewer.setMeans(means); |
2 |
26 Feb 07 |
jari |
1263 |
viewer.setVariances(vars); |
2 |
26 Feb 07 |
jari |
1264 |
SVMCentroidsViewer multiviewer = new SVMCentroidsViewer(this.experimentMap, clusters); |
2 |
26 Feb 07 |
jari |
1265 |
multiviewer.setMeans(means); |
2 |
26 Feb 07 |
jari |
1266 |
multiviewer.setVariances(vars); |
2 |
26 Feb 07 |
jari |
1267 |
expNode.add(new DefaultMutableTreeNode(new LeafInfo("Positives", viewer, new CentroidUserObject(0, CentroidUserObject.VALUES_MODE)))); |
2 |
26 Feb 07 |
jari |
1268 |
expNode.add(new DefaultMutableTreeNode(new LeafInfo("Negatives", viewer, new CentroidUserObject(1, CentroidUserObject.VALUES_MODE)))); |
2 |
26 Feb 07 |
jari |
1269 |
expNode.add(new DefaultMutableTreeNode(new LeafInfo("Both Groups", multiviewer, new Integer(CentroidUserObject.VALUES_MODE)))); |
2 |
26 Feb 07 |
jari |
1270 |
centNode.add(new DefaultMutableTreeNode(new LeafInfo("Positives", viewer, new CentroidUserObject(0, CentroidUserObject.VARIANCES_MODE)))); |
2 |
26 Feb 07 |
jari |
1271 |
centNode.add(new DefaultMutableTreeNode(new LeafInfo("Negatives", viewer, new CentroidUserObject(1, CentroidUserObject.VARIANCES_MODE)))); |
2 |
26 Feb 07 |
jari |
1272 |
centNode.add(new DefaultMutableTreeNode(new LeafInfo("Both Groups", multiviewer, new Integer(CentroidUserObject.VARIANCES_MODE)))); |
2 |
26 Feb 07 |
jari |
1273 |
root.add(centNode); |
2 |
26 Feb 07 |
jari |
1274 |
root.add(expNode); |
2 |
26 Feb 07 |
jari |
1275 |
return root; |
2 |
26 Feb 07 |
jari |
1276 |
} else{ |
2 |
26 Feb 07 |
jari |
1277 |
SVMExperimentClusterCentroidViewer viewer = new SVMExperimentClusterCentroidViewer(this.experimentMap, clusters); |
2 |
26 Feb 07 |
jari |
1278 |
means = result.getMatrix("means").A; |
2 |
26 Feb 07 |
jari |
1279 |
vars = result.getMatrix("variances").A; |
2 |
26 Feb 07 |
jari |
1280 |
viewer.setMeans(means); |
2 |
26 Feb 07 |
jari |
1281 |
viewer.setVariances(vars); |
2 |
26 Feb 07 |
jari |
1282 |
SVMExperimentClusterCentroidsViewer multiviewer = new SVMExperimentClusterCentroidsViewer(this.experimentMap, clusters); |
2 |
26 Feb 07 |
jari |
1283 |
multiviewer.setMeans(means); |
2 |
26 Feb 07 |
jari |
1284 |
multiviewer.setVariances(vars); |
2 |
26 Feb 07 |
jari |
1285 |
expNode.add(new DefaultMutableTreeNode(new LeafInfo("Positives", viewer, new CentroidUserObject(0, CentroidUserObject.VALUES_MODE)))); |
2 |
26 Feb 07 |
jari |
1286 |
expNode.add(new DefaultMutableTreeNode(new LeafInfo("Negatives", viewer, new CentroidUserObject(1, CentroidUserObject.VALUES_MODE)))); |
2 |
26 Feb 07 |
jari |
1287 |
expNode.add(new DefaultMutableTreeNode(new LeafInfo("Both Groups", multiviewer, new Integer(CentroidUserObject.VALUES_MODE)))); |
2 |
26 Feb 07 |
jari |
1288 |
centNode.add(new DefaultMutableTreeNode(new LeafInfo("Positives", viewer, new CentroidUserObject(0, CentroidUserObject.VARIANCES_MODE)))); |
2 |
26 Feb 07 |
jari |
1289 |
centNode.add(new DefaultMutableTreeNode(new LeafInfo("Negatives", viewer, new CentroidUserObject(1, CentroidUserObject.VARIANCES_MODE)))); |
2 |
26 Feb 07 |
jari |
1290 |
centNode.add(new DefaultMutableTreeNode(new LeafInfo("Both Groups", multiviewer, new Integer(CentroidUserObject.VARIANCES_MODE)))); |
2 |
26 Feb 07 |
jari |
1291 |
root.add(centNode); |
2 |
26 Feb 07 |
jari |
1292 |
root.add(expNode); |
2 |
26 Feb 07 |
jari |
1293 |
return root; |
2 |
26 Feb 07 |
jari |
1294 |
} |
2 |
26 Feb 07 |
jari |
1295 |
} |
2 |
26 Feb 07 |
jari |
1296 |
|
2 |
26 Feb 07 |
jari |
1297 |
|
2 |
26 Feb 07 |
jari |
1298 |
/** |
2 |
26 Feb 07 |
jari |
* Adds nodes to display hierarchical trees. |
2 |
26 Feb 07 |
jari |
1300 |
*/ |
2 |
26 Feb 07 |
jari |
1301 |
private DefaultMutableTreeNode createHierarchicalTreeViews(Cluster result_cluster, AlgorithmData result) { |
2 |
26 Feb 07 |
jari |
1302 |
if (!this.data.calculateHCL) { |
2 |
26 Feb 07 |
jari |
1303 |
return null; |
2 |
26 Feb 07 |
jari |
1304 |
} |
2 |
26 Feb 07 |
jari |
1305 |
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Hierarchical Trees"); |
2 |
26 Feb 07 |
jari |
1306 |
NodeList nodeList = result_cluster.getNodeList(); |
2 |
26 Feb 07 |
jari |
1307 |
String nodeString = "Positives"; |
2 |
26 Feb 07 |
jari |
1308 |
int [][] classIndices = null; |
2 |
26 Feb 07 |
jari |
1309 |
|
2 |
26 Feb 07 |
jari |
1310 |
if(!this.classifyGenes){ //if classifying experiments |
2 |
26 Feb 07 |
jari |
//get classification indices |
2 |
26 Feb 07 |
jari |
1312 |
classIndices = getClassificationIndices(result.getIntArray("positives"), result.getIntArray("negatives"));; |
2 |
26 Feb 07 |
jari |
1313 |
if(this.data.calcSampleHCL){ |
2 |
26 Feb 07 |
jari |
//if have sample HCL then indices need to be ordered |
2 |
26 Feb 07 |
jari |
1315 |
classIndices = getOrderedIndices(nodeList, classIndices, this.data.calcGeneHCL); |
2 |
26 Feb 07 |
jari |
1316 |
} |
2 |
26 Feb 07 |
jari |
1317 |
} |
2 |
26 Feb 07 |
jari |
1318 |
for (int i=0; i<nodeList.getSize(); i++) { |
2 |
26 Feb 07 |
jari |
1319 |
if(i > 0) |
2 |
26 Feb 07 |
jari |
1320 |
nodeString = "Negatives"; |
2 |
26 Feb 07 |
jari |
1321 |
if(this.classifyGenes) |
2 |
26 Feb 07 |
jari |
1322 |
node.add(new DefaultMutableTreeNode(new LeafInfo(nodeString, createHCLViewer(nodeList.getNode(i), result, classIndices)))); |
2 |
26 Feb 07 |
jari |
1323 |
else |
2 |
26 Feb 07 |
jari |
1324 |
node.add(new DefaultMutableTreeNode(new LeafInfo(nodeString, createHCLViewer(nodeList.getNode(i), result, classIndices), new Integer(i)))); |
2 |
26 Feb 07 |
jari |
1325 |
} |
2 |
26 Feb 07 |
jari |
1326 |
return node; |
2 |
26 Feb 07 |
jari |
1327 |
} |
2 |
26 Feb 07 |
jari |
1328 |
|
2 |
26 Feb 07 |
jari |
1329 |
|
2 |
26 Feb 07 |
jari |
1330 |
/** |
2 |
26 Feb 07 |
jari |
* Creates an <code>HCLViewer</code>. |
2 |
26 Feb 07 |
jari |
1332 |
*/ |
2 |
26 Feb 07 |
jari |
1333 |
private IViewer createHCLViewer(Node clusterNode, AlgorithmData result, int [][] classIndices) { |
2 |
26 Feb 07 |
jari |
1334 |
HCLTreeData genes_result = this.data.calcGeneHCL ? getResult(clusterNode, 0) : null; |
2 |
26 Feb 07 |
jari |
1335 |
HCLTreeData samples_result = this.data.calcSampleHCL ? getResult(clusterNode, this.data.calcGeneHCL ? 4 : 0) : null; |
2 |
26 Feb 07 |
jari |
1336 |
if(this.classifyGenes){ |
2 |
26 Feb 07 |
jari |
1337 |
return new HCLViewer(this.experimentMap, clusterNode.getFeaturesIndexes(), genes_result, samples_result); |
2 |
26 Feb 07 |
jari |
1338 |
} |
2 |
26 Feb 07 |
jari |
1339 |
else{ |
2 |
26 Feb 07 |
jari |
1340 |
return new HCLViewer(this.experimentMap, clusterNode.getFeaturesIndexes(), genes_result, samples_result, classIndices, true); |
2 |
26 Feb 07 |
jari |
1341 |
} |
2 |
26 Feb 07 |
jari |
1342 |
} |
2 |
26 Feb 07 |
jari |
1343 |
|
2 |
26 Feb 07 |
jari |
1344 |
/** |
2 |
26 Feb 07 |
jari |
* Returns a hcl tree data from the specified cluster node. |
2 |
26 Feb 07 |
jari |
* @param clusterNode Holds result nodes |
2 |
26 Feb 07 |
jari |
* @param pos Holds indices of elements in positive classification |
2 |
26 Feb 07 |
jari |
* @return |
2 |
26 Feb 07 |
jari |
1349 |
*/ |
2 |
26 Feb 07 |
jari |
1350 |
private HCLTreeData getResult(Node clusterNode, int pos) { |
2 |
26 Feb 07 |
jari |
1351 |
HCLTreeData data = new HCLTreeData(); |
2 |
26 Feb 07 |
jari |
1352 |
NodeValueList valueList = clusterNode.getValues(); |
2 |
26 Feb 07 |
jari |
1353 |
data.child_1_array = (int[])valueList.getNodeValue(pos).value; |
2 |
26 Feb 07 |
jari |
1354 |
data.child_2_array = (int[])valueList.getNodeValue(pos+1).value; |
2 |
26 Feb 07 |
jari |
1355 |
data.node_order = (int[])valueList.getNodeValue(pos+2).value; |
2 |
26 Feb 07 |
jari |
1356 |
data.height = (float[])valueList.getNodeValue(pos+3).value; |
2 |
26 Feb 07 |
jari |
1357 |
return data; |
2 |
26 Feb 07 |
jari |
1358 |
} |
2 |
26 Feb 07 |
jari |
1359 |
|
2 |
26 Feb 07 |
jari |
1360 |
/** |
2 |
26 Feb 07 |
jari |
* Creates and adds an SVM parameter information node |
2 |
26 Feb 07 |
jari |
1362 |
*/ |
2 |
26 Feb 07 |
jari |
1363 |
private void addSVMParameterNode(DefaultMutableTreeNode root){ |
2 |
26 Feb 07 |
jari |
1364 |
String value; |
2 |
26 Feb 07 |
jari |
1365 |
DefaultMutableTreeNode genInfo = new DefaultMutableTreeNode("General Information"); |
2 |
26 Feb 07 |
jari |
1366 |
DefaultMutableTreeNode node; |
2 |
26 Feb 07 |
jari |
1367 |
DefaultMutableTreeNode childNode; |
2 |
26 Feb 07 |
jari |
1368 |
|
2 |
26 Feb 07 |
jari |
1369 |
if(this.SVMMode == SVMGUI.TRAIN_ONLY) |
2 |
26 Feb 07 |
jari |
1370 |
value = "Train SVM Only"; |
2 |
26 Feb 07 |
jari |
1371 |
else if(this.SVMMode == SVMGUI.CLASSIFY_ONLY) |
2 |
26 Feb 07 |
jari |
1372 |
value = "Classify Only"; |
2 |
26 Feb 07 |
jari |
1373 |
else if(this.SVMMode == SVMGUI.TRAIN_AND_CLASSIFY) |
2 |
26 Feb 07 |
jari |
1374 |
value = "Train SVM and Classify"; |
2 |
26 Feb 07 |
jari |
1375 |
else |
2 |
26 Feb 07 |
jari |
1376 |
value = "One-out Validation"; |
2 |
26 Feb 07 |
jari |
1377 |
|
2 |
26 Feb 07 |
jari |
1378 |
node = new DefaultMutableTreeNode("SVM Mode: "+value); |
2 |
26 Feb 07 |
jari |
1379 |
genInfo.add(node); |
2 |
26 Feb 07 |
jari |
1380 |
node = new DefaultMutableTreeNode("Kernel Parameters"); |
2 |
26 Feb 07 |
jari |
1381 |
node.add(new DefaultMutableTreeNode("Metric: Dot Product on Normalized Vectors")); |
2 |
26 Feb 07 |
jari |
1382 |
|
2 |
26 Feb 07 |
jari |
1383 |
if(!data.radial){ |
2 |
26 Feb 07 |
jari |
1384 |
childNode = new DefaultMutableTreeNode("Kernel Function: Polynomial"); |
2 |
26 Feb 07 |
jari |
1385 |
childNode.add(new DefaultMutableTreeNode("Constant: "+String.valueOf(data.constant))); |
2 |
26 Feb 07 |
jari |
1386 |
childNode.add(new DefaultMutableTreeNode("Coefficient: "+String.valueOf(data.coefficient))); |
2 |
26 Feb 07 |
jari |
1387 |
childNode.add(new DefaultMutableTreeNode("Power: "+String.valueOf(data.power))); |
2 |
26 Feb 07 |
jari |
1388 |
node.add(childNode); |
2 |
26 Feb 07 |
jari |
1389 |
} |
2 |
26 Feb 07 |
jari |
1390 |
else{ |
2 |
26 Feb 07 |
jari |
1391 |
childNode = new DefaultMutableTreeNode("Kernel Function: Radial Basis (Gausian)"); |
2 |
26 Feb 07 |
jari |
1392 |
childNode.add(new DefaultMutableTreeNode("Width Factor: "+String.valueOf(data.widthFactor))); |
2 |
26 Feb 07 |
jari |
1393 |
node.add(childNode); |
2 |
26 Feb 07 |
jari |
1394 |
} |
2 |
26 Feb 07 |
jari |
1395 |
genInfo.add(node); |
2 |
26 Feb 07 |
jari |
1396 |
|
2 |
26 Feb 07 |
jari |
1397 |
node = new DefaultMutableTreeNode("Training Parameters"); |
2 |
26 Feb 07 |
jari |
1398 |
childNode = new DefaultMutableTreeNode("Constrain Weights: "+String.valueOf(data.constrainWeights) ); |
2 |
26 Feb 07 |
jari |
1399 |
if(data.constrainWeights){ |
2 |
26 Feb 07 |
jari |
1400 |
childNode.add(new DefaultMutableTreeNode("Positive Constraint: "+String.valueOf(data.positiveConstraint))); |
2 |
26 Feb 07 |
jari |
1401 |
childNode.add(new DefaultMutableTreeNode("Negative Constraint: "+String.valueOf(data.negativeConstraint))); |
2 |
26 Feb 07 |
jari |
1402 |
} |
2 |
26 Feb 07 |
jari |
1403 |
node.add(childNode); |
2 |
26 Feb 07 |
jari |
1404 |
node.add(new DefaultMutableTreeNode("Diagonal Factor: "+String.valueOf(data.diagonalFactor))); |
2 |
26 Feb 07 |
jari |
1405 |
node.add(new DefaultMutableTreeNode("Threshold: "+String.valueOf(data.convergenceThreshold))); |
2 |
26 Feb 07 |
jari |
1406 |
genInfo.add(node); |
2 |
26 Feb 07 |
jari |
1407 |
|
2 |
26 Feb 07 |
jari |
1408 |
if(data.calculateHCL){ |
2 |
26 Feb 07 |
jari |
1409 |
node = new DefaultMutableTreeNode("HCL Parameters"); |
2 |
26 Feb 07 |
jari |
1410 |
value = AbstractAlgorithm.getDistanceName(data.distanceFunction); |
2 |
26 Feb 07 |
jari |
1411 |
node.add(new DefaultMutableTreeNode("HCL distance metric: "+value)); |
2 |
26 Feb 07 |
jari |
1412 |
String method; |
2 |
26 Feb 07 |
jari |
1413 |
if(data.hclMethod == 0) |
2 |
26 Feb 07 |
jari |
1414 |
method = "Average Linkage"; |
2 |
26 Feb 07 |
jari |
1415 |
else if(data.hclMethod == 1) |
2 |
26 Feb 07 |
jari |
1416 |
method = "Complete Linkage"; |
2 |
26 Feb 07 |
jari |
1417 |
else |
2 |
26 Feb 07 |
jari |
1418 |
method = "Single Linkage"; |
2 |
26 Feb 07 |
jari |
1419 |
node.add(new DefaultMutableTreeNode("Linkage Method: "+method)); |
2 |
26 Feb 07 |
jari |
1420 |
genInfo.add(node); |
2 |
26 Feb 07 |
jari |
1421 |
} |
2 |
26 Feb 07 |
jari |
1422 |
root.add(genInfo); |
2 |
26 Feb 07 |
jari |
1423 |
} |
2 |
26 Feb 07 |
jari |
1424 |
|
2 |
26 Feb 07 |
jari |
1425 |
/** |
2 |
26 Feb 07 |
jari |
* Returns a default ordered gene cluster array |
2 |
26 Feb 07 |
jari |
* @return cluster indices |
2 |
26 Feb 07 |
jari |
1428 |
*/ |
2 |
26 Feb 07 |
jari |
1429 |
private int [][] getDefaultGeneCluster(){ |
2 |
26 Feb 07 |
jari |
1430 |
int n = this.experimentMap.getNumberOfGenes(); |
2 |
26 Feb 07 |
jari |
1431 |
int [][] c = new int [1][n]; |
2 |
26 Feb 07 |
jari |
1432 |
for(int i = 0; i < n; i++){ |
2 |
26 Feb 07 |
jari |
1433 |
c[0][i] = i; |
2 |
26 Feb 07 |
jari |
1434 |
} |
2 |
26 Feb 07 |
jari |
1435 |
return c; |
2 |
26 Feb 07 |
jari |
1436 |
} |
2 |
26 Feb 07 |
jari |
1437 |
|
2 |
26 Feb 07 |
jari |
1438 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the discriminants for positive and negative elements |
2 |
26 Feb 07 |
jari |
* @param pos Positive element indices |
2 |
26 Feb 07 |
jari |
* @param neg Negative element indices |
2 |
26 Feb 07 |
jari |
* @return |
2 |
26 Feb 07 |
jari |
1443 |
*/ |
2 |
26 Feb 07 |
jari |
1444 |
private float [][] getDiscriminants(int [] pos, int [] neg){ |
2 |
26 Feb 07 |
jari |
//discriminat is in second column of discriminantMatrix |
2 |
26 Feb 07 |
jari |
1446 |
int n = discriminantMatrix.getRowDimension(); |
2 |
26 Feb 07 |
jari |
1447 |
float [][] A = discriminantMatrix.A; |
2 |
26 Feb 07 |
jari |
1448 |
float disc [] = new float [n]; |
2 |
26 Feb 07 |
jari |
1449 |
for(int i=0; i < n; i++){ |
2 |
26 Feb 07 |
jari |
1450 |
disc[i] = A[i][1]; |
2 |
26 Feb 07 |
jari |
1451 |
} |
2 |
26 Feb 07 |
jari |
1452 |
float [][] result = new float[2][]; |
2 |
26 Feb 07 |
jari |
1453 |
result[0] = new float[pos.length]; |
2 |
26 Feb 07 |
jari |
1454 |
result[1] = new float[neg.length]; |
2 |
26 Feb 07 |
jari |
1455 |
|
2 |
26 Feb 07 |
jari |
1456 |
for(int i = 0; i < pos.length ; i++){ |
2 |
26 Feb 07 |
jari |
1457 |
result[0][i] = disc[pos[i]]; |
2 |
26 Feb 07 |
jari |
1458 |
} |
2 |
26 Feb 07 |
jari |
1459 |
|
2 |
26 Feb 07 |
jari |
1460 |
for(int i = 0; i < neg.length ; i++){ |
2 |
26 Feb 07 |
jari |
1461 |
result[1][i] = disc[neg[i]]; |
2 |
26 Feb 07 |
jari |
1462 |
} |
2 |
26 Feb 07 |
jari |
1463 |
return result; |
2 |
26 Feb 07 |
jari |
1464 |
} |
2 |
26 Feb 07 |
jari |
1465 |
|
2 |
26 Feb 07 |
jari |
1466 |
/** |
2 |
26 Feb 07 |
jari |
* Orders the classification indicies into positive and negative element arrays |
2 |
26 Feb 07 |
jari |
1468 |
* |
2 |
26 Feb 07 |
jari |
* @param a Positive group 1 |
2 |
26 Feb 07 |
jari |
* @param b Positive group 2 |
2 |
26 Feb 07 |
jari |
* @param c Negative group 1 |
2 |
26 Feb 07 |
jari |
* @param d Negative group 2 |
2 |
26 Feb 07 |
jari |
* @return |
2 |
26 Feb 07 |
jari |
1474 |
*/ |
2 |
26 Feb 07 |
jari |
1475 |
private int [][] getOrderedClassIndices(int [] a, int [] b, int [] c, int [] d){ |
2 |
26 Feb 07 |
jari |
// int [][] result = new int[a.length + b.length][c.length + d.length]; |
2 |
26 Feb 07 |
jari |
1477 |
|
2 |
26 Feb 07 |
jari |
1478 |
int [][] result = new int[2][]; |
2 |
26 Feb 07 |
jari |
1479 |
result[0] = new int[a.length + b.length]; |
2 |
26 Feb 07 |
jari |
1480 |
result[1] = new int[c.length + d.length]; |
2 |
26 Feb 07 |
jari |
1481 |
|
2 |
26 Feb 07 |
jari |
1482 |
|
2 |
26 Feb 07 |
jari |
1483 |
System.arraycopy(a, 0, result[0], 0, a.length); |
2 |
26 Feb 07 |
jari |
1484 |
System.arraycopy(b, 0, result[0], a.length, b.length); |
2 |
26 Feb 07 |
jari |
1485 |
System.arraycopy(c, 0, result[1], 0, c.length); |
2 |
26 Feb 07 |
jari |
1486 |
System.arraycopy(d, 0, result[1], c.length, d.length); |
2 |
26 Feb 07 |
jari |
1487 |
return result; |
2 |
26 Feb 07 |
jari |
1488 |
} |
2 |
26 Feb 07 |
jari |
1489 |
|
2 |
26 Feb 07 |
jari |
1490 |
|
2 |
26 Feb 07 |
jari |
1491 |
/** |
2 |
26 Feb 07 |
jari |
* Extracts the indices of retained and recruited positives |
2 |
26 Feb 07 |
jari |
* @param pos Positive element indices |
2 |
26 Feb 07 |
jari |
* @param classification classification indices |
2 |
26 Feb 07 |
jari |
* @return |
2 |
26 Feb 07 |
jari |
1496 |
*/ |
2 |
26 Feb 07 |
jari |
1497 |
private int [][] getTruePositives(int [] pos, int [] classification){ |
2 |
26 Feb 07 |
jari |
1498 |
int [][] result = null; |
2 |
26 Feb 07 |
jari |
1499 |
Vector pVector = new Vector(); |
2 |
26 Feb 07 |
jari |
1500 |
for(int i= 0; i < pos.length; i++){ |
2 |
26 Feb 07 |
jari |
1501 |
if(classification[pos[i]] == 1){ |
2 |
26 Feb 07 |
jari |
1502 |
pVector.add(new Integer(pos[i])); |
2 |
26 Feb 07 |
jari |
1503 |
} |
2 |
26 Feb 07 |
jari |
1504 |
} |
2 |
26 Feb 07 |
jari |
1505 |
result = new int[1][pVector.size()]; |
2 |
26 Feb 07 |
jari |
1506 |
for(int i = 0; i < result[0].length ; i++){ |
2 |
26 Feb 07 |
jari |
1507 |
result[0][i] = ((Integer)pVector.elementAt(i)).intValue(); |
2 |
26 Feb 07 |
jari |
1508 |
} |
2 |
26 Feb 07 |
jari |
1509 |
return result; |
2 |
26 Feb 07 |
jari |
1510 |
} |
2 |
26 Feb 07 |
jari |
1511 |
|
2 |
26 Feb 07 |
jari |
1512 |
/** |
2 |
26 Feb 07 |
jari |
* Extracts the indices of recuited positives |
2 |
26 Feb 07 |
jari |
1514 |
*/ |
2 |
26 Feb 07 |
jari |
1515 |
private int [][] getFalseNegatives(int [] pos, int [] classification){ |
2 |
26 Feb 07 |
jari |
1516 |
int [][] result = null; |
2 |
26 Feb 07 |
jari |
1517 |
Vector pVector = new Vector(); |
2 |
26 Feb 07 |
jari |
1518 |
for(int i= 0; i < pos.length; i++){ |
2 |
26 Feb 07 |
jari |
1519 |
if(classification[pos[i]] <= 0){ |
2 |
26 Feb 07 |
jari |
1520 |
pVector.add(new Integer(pos[i])); |
2 |
26 Feb 07 |
jari |
1521 |
} |
2 |
26 Feb 07 |
jari |
1522 |
} |
2 |
26 Feb 07 |
jari |
1523 |
result = new int[1][pVector.size()]; |
2 |
26 Feb 07 |
jari |
1524 |
for(int i = 0; i < result[0].length ; i++){ |
2 |
26 Feb 07 |
jari |
1525 |
result[0][i] = ((Integer)pVector.elementAt(i)).intValue(); |
2 |
26 Feb 07 |
jari |
1526 |
} |
2 |
26 Feb 07 |
jari |
1527 |
return result; |
2 |
26 Feb 07 |
jari |
1528 |
} |
2 |
26 Feb 07 |
jari |
1529 |
|
2 |
26 Feb 07 |
jari |
1530 |
/** |
2 |
26 Feb 07 |
jari |
* Extracts recruitred negatives |
2 |
26 Feb 07 |
jari |
1532 |
*/ |
2 |
26 Feb 07 |
jari |
1533 |
private int [][] getFalsePositives(int [] neg, int [] classification){ |
2 |
26 Feb 07 |
jari |
1534 |
int [][] result = null; |
2 |
26 Feb 07 |
jari |
1535 |
Vector pVector = new Vector(); |
2 |
26 Feb 07 |
jari |
1536 |
for(int i= 0; i < neg.length; i++){ |
2 |
26 Feb 07 |
jari |
1537 |
|
2 |
26 Feb 07 |
jari |
1538 |
if(classification[neg[i]] == 1){ |
2 |
26 Feb 07 |
jari |
1539 |
pVector.add(new Integer(neg[i])); |
2 |
26 Feb 07 |
jari |
1540 |
} |
2 |
26 Feb 07 |
jari |
1541 |
} |
2 |
26 Feb 07 |
jari |
1542 |
result = new int[1][pVector.size()]; |
2 |
26 Feb 07 |
jari |
1543 |
for(int i = 0; i < result[0].length ; i++){ |
2 |
26 Feb 07 |
jari |
1544 |
result[0][i] = ((Integer)pVector.elementAt(i)).intValue(); |
2 |
26 Feb 07 |
jari |
1545 |
} |
2 |
26 Feb 07 |
jari |
1546 |
return result; |
2 |
26 Feb 07 |
jari |
1547 |
} |
2 |
26 Feb 07 |
jari |
1548 |
|
2 |
26 Feb 07 |
jari |
1549 |
/** |
2 |
26 Feb 07 |
jari |
* Extracts retained negatives |
2 |
26 Feb 07 |
jari |
1551 |
*/ |
2 |
26 Feb 07 |
jari |
1552 |
private int [][] getTrueNegatives(int [] neg, int [] classification){ |
2 |
26 Feb 07 |
jari |
1553 |
int [][] result = null; |
2 |
26 Feb 07 |
jari |
1554 |
Vector pVector = new Vector(); |
2 |
26 Feb 07 |
jari |
1555 |
for(int i= 0; i < neg.length; i++){ |
2 |
26 Feb 07 |
jari |
1556 |
if(classification[neg[i]] <= 0){ |
2 |
26 Feb 07 |
jari |
1557 |
pVector.add(new Integer(neg[i])); |
2 |
26 Feb 07 |
jari |
1558 |
} |
2 |
26 Feb 07 |
jari |
1559 |
} |
2 |
26 Feb 07 |
jari |
1560 |
result = new int[1][pVector.size()]; |
2 |
26 Feb 07 |
jari |
1561 |
for(int i = 0; i < result[0].length ; i++){ |
2 |
26 Feb 07 |
jari |
1562 |
result[0][i] = ((Integer)pVector.elementAt(i)).intValue(); |
2 |
26 Feb 07 |
jari |
1563 |
} |
2 |
26 Feb 07 |
jari |
1564 |
return result; |
2 |
26 Feb 07 |
jari |
1565 |
} |
2 |
26 Feb 07 |
jari |
1566 |
|
2 |
26 Feb 07 |
jari |
1567 |
|
2 |
26 Feb 07 |
jari |
1568 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the number of positive training examples |
2 |
26 Feb 07 |
jari |
1570 |
*/ |
2 |
26 Feb 07 |
jari |
1571 |
private int getNumberOfPositiveExamples(int [] c){ |
2 |
26 Feb 07 |
jari |
1572 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
1573 |
for(int i = 0; i < c.length; i++){ |
2 |
26 Feb 07 |
jari |
1574 |
if(c[i] == 1) |
2 |
26 Feb 07 |
jari |
1575 |
cnt++; |
2 |
26 Feb 07 |
jari |
1576 |
} |
2 |
26 Feb 07 |
jari |
1577 |
return cnt; |
2 |
26 Feb 07 |
jari |
1578 |
} |
2 |
26 Feb 07 |
jari |
1579 |
|
2 |
26 Feb 07 |
jari |
1580 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the number of negative training examples |
2 |
26 Feb 07 |
jari |
1582 |
*/ |
2 |
26 Feb 07 |
jari |
1583 |
private int getNumberOfNegativeExamples(int [] c){ |
2 |
26 Feb 07 |
jari |
1584 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
1585 |
for(int i = 0; i < c.length; i++){ |
2 |
26 Feb 07 |
jari |
1586 |
if(c[i] == -1) |
2 |
26 Feb 07 |
jari |
1587 |
cnt++; |
2 |
26 Feb 07 |
jari |
1588 |
} |
2 |
26 Feb 07 |
jari |
1589 |
return cnt; |
2 |
26 Feb 07 |
jari |
1590 |
} |
2 |
26 Feb 07 |
jari |
1591 |
|
2 |
26 Feb 07 |
jari |
1592 |
private int getNumPosRecFromNeutrals(int [] pos, int [] c){ |
2 |
26 Feb 07 |
jari |
1593 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
1594 |
for(int i = 0; i < pos.length; i++){ |
2 |
26 Feb 07 |
jari |
1595 |
if(c[pos[i]] == 0 ) //initially neutral gene |
2 |
26 Feb 07 |
jari |
1596 |
cnt++; |
2 |
26 Feb 07 |
jari |
1597 |
} |
2 |
26 Feb 07 |
jari |
1598 |
return cnt; |
2 |
26 Feb 07 |
jari |
1599 |
} |
2 |
26 Feb 07 |
jari |
1600 |
|
2 |
26 Feb 07 |
jari |
1601 |
private int getNumNegRecFromNeutrals(int [] neg, int [] c){ |
2 |
26 Feb 07 |
jari |
1602 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
1603 |
for(int i = 0; i < neg.length; i++){ |
2 |
26 Feb 07 |
jari |
1604 |
if(c[neg[i]] == 0 ) //initially neutral gene |
2 |
26 Feb 07 |
jari |
1605 |
cnt++; |
2 |
26 Feb 07 |
jari |
1606 |
} |
2 |
26 Feb 07 |
jari |
1607 |
return cnt; |
2 |
26 Feb 07 |
jari |
1608 |
} |
2 |
26 Feb 07 |
jari |
1609 |
|
2 |
26 Feb 07 |
jari |
1610 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the result classification as element indices |
2 |
26 Feb 07 |
jari |
* (merges pos and neg into 2D array) |
2 |
26 Feb 07 |
jari |
* @param pos |
2 |
26 Feb 07 |
jari |
* @param neg |
2 |
26 Feb 07 |
jari |
* @return holds merged positive and negative |
2 |
26 Feb 07 |
jari |
1616 |
*/ |
2 |
26 Feb 07 |
jari |
1617 |
private int [][] getClassificationIndices(int [] pos, int [] neg){ |
2 |
26 Feb 07 |
jari |
1618 |
int [][] results = new int[2][]; |
2 |
26 Feb 07 |
jari |
1619 |
results[0] = pos; |
2 |
26 Feb 07 |
jari |
1620 |
results[1] = neg; |
2 |
26 Feb 07 |
jari |
1621 |
return results; |
2 |
26 Feb 07 |
jari |
1622 |
} |
2 |
26 Feb 07 |
jari |
1623 |
|
2 |
26 Feb 07 |
jari |
1624 |
private void accumulateResult(FloatMatrix cumMatrix, int index){ |
2 |
26 Feb 07 |
jari |
1625 |
for(int i = 0; i < this.discriminantMatrix.getColumnDimension(); i++){ |
2 |
26 Feb 07 |
jari |
1626 |
cumMatrix.set(index, i, discriminantMatrix.get(index, i)); |
2 |
26 Feb 07 |
jari |
1627 |
} |
2 |
26 Feb 07 |
jari |
1628 |
} |
2 |
26 Feb 07 |
jari |
1629 |
|
2 |
26 Feb 07 |
jari |
1630 |
private float [] toFloatArray(int [] array){ |
2 |
26 Feb 07 |
jari |
1631 |
float [] result = new float[array.length]; |
2 |
26 Feb 07 |
jari |
1632 |
for(int i = 0; i < result.length; i++){ |
2 |
26 Feb 07 |
jari |
1633 |
result[i] = (float)array[i]; |
2 |
26 Feb 07 |
jari |
1634 |
} |
2 |
26 Feb 07 |
jari |
1635 |
return result; |
2 |
26 Feb 07 |
jari |
1636 |
} |
2 |
26 Feb 07 |
jari |
1637 |
|
2 |
26 Feb 07 |
jari |
1638 |
private int getNumberOfNonNeutrals(){ |
2 |
26 Feb 07 |
jari |
1639 |
int n = 0; |
2 |
26 Feb 07 |
jari |
1640 |
for(int i = 0; i < classes.length; i++){ |
2 |
26 Feb 07 |
jari |
1641 |
if(classes[i] != 0) |
2 |
26 Feb 07 |
jari |
1642 |
n++; |
2 |
26 Feb 07 |
jari |
1643 |
} |
2 |
26 Feb 07 |
jari |
1644 |
return n; |
2 |
26 Feb 07 |
jari |
1645 |
} |
2 |
26 Feb 07 |
jari |
1646 |
|
2 |
26 Feb 07 |
jari |
// GUI helpers |
2 |
26 Feb 07 |
jari |
1648 |
protected void showLogger( String caption ) { |
2 |
26 Feb 07 |
jari |
1649 |
if (logger == null) { |
2 |
26 Feb 07 |
jari |
1650 |
logger = new Logger(parentFrame, caption , listener ); |
2 |
26 Feb 07 |
jari |
1651 |
logger.show(); |
2 |
26 Feb 07 |
jari |
1652 |
} |
2 |
26 Feb 07 |
jari |
1653 |
} |
2 |
26 Feb 07 |
jari |
1654 |
|
2 |
26 Feb 07 |
jari |
1655 |
|
2 |
26 Feb 07 |
jari |
1656 |
|
2 |
26 Feb 07 |
jari |
1657 |
|
2 |
26 Feb 07 |
jari |
1658 |
|
2 |
26 Feb 07 |
jari |
1659 |
|
2 |
26 Feb 07 |
jari |
1660 |
|
2 |
26 Feb 07 |
jari |
1661 |
/*************************************************************************************** |
2 |
26 Feb 07 |
jari |
* Code to order sample clustering results based on HCL runs. sampleClusters contain an array |
2 |
26 Feb 07 |
jari |
* of sample indices for each experiment cluster. Note that these indicies are ordered in |
2 |
26 Feb 07 |
jari |
* an order which matches HCL input matrix sample order so that HCL results (node-order) can |
2 |
26 Feb 07 |
jari |
* be used to order leaf indices to match HCL samples results |
2 |
26 Feb 07 |
jari |
1666 |
*/ |
2 |
26 Feb 07 |
jari |
1667 |
private int [][] getOrderedIndices(NodeList nodeList, int [][] sampleClusters, boolean calcGeneHCL){ |
2 |
26 Feb 07 |
jari |
1668 |
HCLTreeData result; |
2 |
26 Feb 07 |
jari |
1669 |
for(int i = 0; i < sampleClusters.length ; i++){ |
2 |
26 Feb 07 |
jari |
1670 |
if(sampleClusters[i].length > 0){ |
2 |
26 Feb 07 |
jari |
1671 |
result = getResult(nodeList.getNode(i), calcGeneHCL ? 4 : 0); //get sample Result |
2 |
26 Feb 07 |
jari |
1672 |
sampleClusters[i] = getSampleOrder(result, sampleClusters[i]); |
2 |
26 Feb 07 |
jari |
1673 |
} |
2 |
26 Feb 07 |
jari |
1674 |
} |
2 |
26 Feb 07 |
jari |
1675 |
return sampleClusters; |
2 |
26 Feb 07 |
jari |
1676 |
} |
2 |
26 Feb 07 |
jari |
1677 |
|
2 |
26 Feb 07 |
jari |
1678 |
private int[] getSampleOrder(HCLTreeData result, int[] indices) { |
2 |
26 Feb 07 |
jari |
1679 |
return getLeafOrder(result.node_order, result.child_1_array, result.child_2_array, indices); |
2 |
26 Feb 07 |
jari |
1680 |
} |
2 |
26 Feb 07 |
jari |
1681 |
|
2 |
26 Feb 07 |
jari |
1682 |
private int[] getLeafOrder(int[] nodeOrder, int[] child1, int[] child2, int[] indices) { |
2 |
26 Feb 07 |
jari |
1683 |
int[] leafOrder = new int[nodeOrder.length]; |
2 |
26 Feb 07 |
jari |
1684 |
Arrays.fill(leafOrder, -1); |
2 |
26 Feb 07 |
jari |
1685 |
fillLeafOrder(leafOrder, child1, child2, 0, child1.length-2, indices); |
2 |
26 Feb 07 |
jari |
1686 |
return leafOrder; |
2 |
26 Feb 07 |
jari |
1687 |
} |
2 |
26 Feb 07 |
jari |
1688 |
|
2 |
26 Feb 07 |
jari |
1689 |
private int fillLeafOrder(int[] leafOrder, int[] child1, int[] child2, int pos, int index, int[] indices) { |
2 |
26 Feb 07 |
jari |
1690 |
if (child1[index] != -1) { |
2 |
26 Feb 07 |
jari |
1691 |
pos = fillLeafOrder(leafOrder, child1, child2, pos, child1[index], indices); |
2 |
26 Feb 07 |
jari |
1692 |
} |
2 |
26 Feb 07 |
jari |
1693 |
if (child2[index] != -1) { |
2 |
26 Feb 07 |
jari |
1694 |
pos = fillLeafOrder(leafOrder, child1, child2, pos, child2[index], indices); |
2 |
26 Feb 07 |
jari |
1695 |
} else { |
2 |
26 Feb 07 |
jari |
1696 |
leafOrder[pos] = indices == null ? index : indices[index]; |
2 |
26 Feb 07 |
jari |
1697 |
pos++; |
2 |
26 Feb 07 |
jari |
1698 |
} |
2 |
26 Feb 07 |
jari |
1699 |
return pos; |
2 |
26 Feb 07 |
jari |
1700 |
} |
2 |
26 Feb 07 |
jari |
1701 |
|
2 |
26 Feb 07 |
jari |
1702 |
/**************************************************************************************** |
2 |
26 Feb 07 |
jari |
* End of Sample Cluster index ordering code |
2 |
26 Feb 07 |
jari |
1704 |
*/ |
2 |
26 Feb 07 |
jari |
1705 |
|
2 |
26 Feb 07 |
jari |
1706 |
|
2 |
26 Feb 07 |
jari |
1707 |
|
2 |
26 Feb 07 |
jari |
1708 |
/** |
2 |
26 Feb 07 |
jari |
* Returns positive element index list |
2 |
26 Feb 07 |
jari |
1710 |
*/ |
2 |
26 Feb 07 |
jari |
1711 |
private int [] getPositives(FloatMatrix matrix){ |
2 |
26 Feb 07 |
jari |
1712 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
1713 |
|
2 |
26 Feb 07 |
jari |
1714 |
for(int i = 0; i < matrix.getRowDimension(); i++){ |
2 |
26 Feb 07 |
jari |
1715 |
if( matrix.get( i, 0 ) == 1.0 ) |
2 |
26 Feb 07 |
jari |
1716 |
cnt++; |
2 |
26 Feb 07 |
jari |
1717 |
} |
2 |
26 Feb 07 |
jari |
1718 |
|
2 |
26 Feb 07 |
jari |
1719 |
int [] pos = new int[cnt]; |
2 |
26 Feb 07 |
jari |
1720 |
cnt = 0; |
2 |
26 Feb 07 |
jari |
1721 |
|
2 |
26 Feb 07 |
jari |
1722 |
for(int i = 0; i < matrix.getRowDimension(); i++){ |
2 |
26 Feb 07 |
jari |
1723 |
if( matrix.get( i, 0 ) == 1.0 ){ |
2 |
26 Feb 07 |
jari |
1724 |
pos[cnt] = i; |
2 |
26 Feb 07 |
jari |
1725 |
cnt++; |
2 |
26 Feb 07 |
jari |
1726 |
} |
2 |
26 Feb 07 |
jari |
1727 |
} |
2 |
26 Feb 07 |
jari |
1728 |
return pos; |
2 |
26 Feb 07 |
jari |
1729 |
} |
2 |
26 Feb 07 |
jari |
1730 |
|
2 |
26 Feb 07 |
jari |
1731 |
|
2 |
26 Feb 07 |
jari |
1732 |
|
2 |
26 Feb 07 |
jari |
1733 |
/** |
2 |
26 Feb 07 |
jari |
* Returns negative element index list |
2 |
26 Feb 07 |
jari |
1735 |
*/ |
2 |
26 Feb 07 |
jari |
1736 |
private int [] getNegatives(FloatMatrix matrix){ |
2 |
26 Feb 07 |
jari |
1737 |
int cnt = 0; |
2 |
26 Feb 07 |
jari |
1738 |
for(int i = 0; i < matrix.getRowDimension(); i++){ |
2 |
26 Feb 07 |
jari |
1739 |
if( matrix.get( i, 0 ) <= 0 ) |
2 |
26 Feb 07 |
jari |
1740 |
cnt++; |
2 |
26 Feb 07 |
jari |
1741 |
} |
2 |
26 Feb 07 |
jari |
1742 |
|
2 |
26 Feb 07 |
jari |
1743 |
int [] neg = new int[cnt]; |
2 |
26 Feb 07 |
jari |
1744 |
cnt = 0; |
2 |
26 Feb 07 |
jari |
1745 |
|
2 |
26 Feb 07 |
jari |
1746 |
for(int i = 0; i < matrix.getRowDimension(); i++){ |
2 |
26 Feb 07 |
jari |
1747 |
if( matrix.get( i, 0 ) <= 0 ){ |
2 |
26 Feb 07 |
jari |
1748 |
neg[cnt] = i; |
2 |
26 Feb 07 |
jari |
1749 |
cnt++; |
2 |
26 Feb 07 |
jari |
1750 |
} |
2 |
26 Feb 07 |
jari |
1751 |
} |
2 |
26 Feb 07 |
jari |
1752 |
return neg; |
2 |
26 Feb 07 |
jari |
1753 |
} |
2 |
26 Feb 07 |
jari |
1754 |
|
2 |
26 Feb 07 |
jari |
1755 |
|
2 |
26 Feb 07 |
jari |
1756 |
|
2 |
26 Feb 07 |
jari |
1757 |
/** |
2 |
26 Feb 07 |
jari |
* Retuns means values for each column within positives and negatives |
2 |
26 Feb 07 |
jari |
1759 |
*/ |
2 |
26 Feb 07 |
jari |
1760 |
private FloatMatrix getMeans(FloatMatrix discMatrix){ |
2 |
26 Feb 07 |
jari |
1761 |
FloatMatrix expMatrix = experiment.getExperiment().getMatrix(); |
2 |
26 Feb 07 |
jari |
1762 |
|
2 |
26 Feb 07 |
jari |
1763 |
if(!this.classifyGenes) |
2 |
26 Feb 07 |
jari |
1764 |
expMatrix = expMatrix.transpose(); |
2 |
26 Feb 07 |
jari |
1765 |
|
2 |
26 Feb 07 |
jari |
1766 |
int numSamples = expMatrix.getColumnDimension(); |
2 |
26 Feb 07 |
jari |
1767 |
int numGenes = expMatrix.getRowDimension(); |
2 |
26 Feb 07 |
jari |
1768 |
|
2 |
26 Feb 07 |
jari |
1769 |
FloatMatrix means = new FloatMatrix(2, numSamples); |
2 |
26 Feb 07 |
jari |
1770 |
float posMean = 0; |
2 |
26 Feb 07 |
jari |
1771 |
float negMean = 0; |
2 |
26 Feb 07 |
jari |
1772 |
float value; |
2 |
26 Feb 07 |
jari |
1773 |
int posCnt = 0; |
2 |
26 Feb 07 |
jari |
1774 |
int negCnt = 0; |
2 |
26 Feb 07 |
jari |
1775 |
float c; |
2 |
26 Feb 07 |
jari |
1776 |
|
2 |
26 Feb 07 |
jari |
1777 |
for(int j = 0; j < numSamples; j++){ |
2 |
26 Feb 07 |
jari |
1778 |
for(int i = 0; i < numGenes; i++){ |
2 |
26 Feb 07 |
jari |
1779 |
|
2 |
26 Feb 07 |
jari |
1780 |
c = discMatrix.get(i,0); |
2 |
26 Feb 07 |
jari |
1781 |
if(c == 1){ |
2 |
26 Feb 07 |
jari |
1782 |
value = expMatrix.get(i,j); |
2 |
26 Feb 07 |
jari |
1783 |
if(!Float.isNaN(value)){ |
2 |
26 Feb 07 |
jari |
1784 |
posCnt++; |
2 |
26 Feb 07 |
jari |
1785 |
posMean += value; |
2 |
26 Feb 07 |
jari |
1786 |
} |
2 |
26 Feb 07 |
jari |
1787 |
} |
2 |
26 Feb 07 |
jari |
1788 |
else{ |
2 |
26 Feb 07 |
jari |
1789 |
|
2 |
26 Feb 07 |
jari |
1790 |
value = expMatrix.get(i,j); |
2 |
26 Feb 07 |
jari |
1791 |
if(!Float.isNaN(value)){ |
2 |
26 Feb 07 |
jari |
1792 |
negCnt++; |
2 |
26 Feb 07 |
jari |
1793 |
negMean += value; |
2 |
26 Feb 07 |
jari |
1794 |
} |
2 |
26 Feb 07 |
jari |
1795 |
} |
2 |
26 Feb 07 |
jari |
1796 |
} |
2 |
26 Feb 07 |
jari |
1797 |
means.set( 0, j, (float)(posCnt != 0 ? posMean/posCnt : 0.0f)); |
2 |
26 Feb 07 |
jari |
1798 |
means.set( 1, j, (float)(negCnt != 0 ? negMean/negCnt : 0.0f)); |
2 |
26 Feb 07 |
jari |
1799 |
posCnt = 0; |
2 |
26 Feb 07 |
jari |
1800 |
negCnt = 0; |
2 |
26 Feb 07 |
jari |
1801 |
posMean = 0; |
2 |
26 Feb 07 |
jari |
1802 |
negMean = 0; |
2 |
26 Feb 07 |
jari |
1803 |
} |
2 |
26 Feb 07 |
jari |
1804 |
return means; |
2 |
26 Feb 07 |
jari |
1805 |
} |
2 |
26 Feb 07 |
jari |
1806 |
|
2 |
26 Feb 07 |
jari |
1807 |
|
2 |
26 Feb 07 |
jari |
1808 |
/** |
2 |
26 Feb 07 |
jari |
* Retuns variance values for each column within positives and negatives |
2 |
26 Feb 07 |
jari |
1810 |
*/ |
2 |
26 Feb 07 |
jari |
1811 |
private FloatMatrix getVariance(FloatMatrix discMatrix, FloatMatrix means){ |
2 |
26 Feb 07 |
jari |
1812 |
FloatMatrix expMatrix = experiment.getExperiment().getMatrix(); |
2 |
26 Feb 07 |
jari |
1813 |
|
2 |
26 Feb 07 |
jari |
1814 |
if(!this.classifyGenes) |
2 |
26 Feb 07 |
jari |
1815 |
expMatrix = expMatrix.transpose(); |
2 |
26 Feb 07 |
jari |
1816 |
|
2 |
26 Feb 07 |
jari |
1817 |
int numSamples = expMatrix.getColumnDimension(); |
2 |
26 Feb 07 |
jari |
1818 |
int numGenes = expMatrix.getRowDimension(); |
2 |
26 Feb 07 |
jari |
1819 |
FloatMatrix vars = new FloatMatrix(2, numSamples); |
2 |
26 Feb 07 |
jari |
1820 |
float value; |
2 |
26 Feb 07 |
jari |
1821 |
float c; |
2 |
26 Feb 07 |
jari |
1822 |
float mean; |
2 |
26 Feb 07 |
jari |
1823 |
float ssePos = 0; |
2 |
26 Feb 07 |
jari |
1824 |
int posCnt = 0; |
2 |
26 Feb 07 |
jari |
1825 |
float sseNeg = 0; |
2 |
26 Feb 07 |
jari |
1826 |
int negCnt = 0; |
2 |
26 Feb 07 |
jari |
1827 |
for(int i = 0; i < numSamples; i++){ |
2 |
26 Feb 07 |
jari |
1828 |
|
2 |
26 Feb 07 |
jari |
1829 |
for(int j = 0; j < numGenes; j++){ |
2 |
26 Feb 07 |
jari |
1830 |
c = discMatrix.get(j, 0); |
2 |
26 Feb 07 |
jari |
1831 |
|
2 |
26 Feb 07 |
jari |
1832 |
if(c == 1){ |
2 |
26 Feb 07 |
jari |
1833 |
value = expMatrix.get(j,i); |
2 |
26 Feb 07 |
jari |
1834 |
if(!Float.isNaN(value)){ |
2 |
26 Feb 07 |
jari |
1835 |
ssePos += Math.pow(value - means.get(0, i), 2); |
2 |
26 Feb 07 |
jari |
1836 |
posCnt++; |
2 |
26 Feb 07 |
jari |
1837 |
} |
2 |
26 Feb 07 |
jari |
1838 |
} |
2 |
26 Feb 07 |
jari |
1839 |
else{ |
2 |
26 Feb 07 |
jari |
1840 |
value = expMatrix.get(j,i); |
2 |
26 Feb 07 |
jari |
1841 |
if(!Float.isNaN(value)){ |
2 |
26 Feb 07 |
jari |
1842 |
sseNeg += Math.pow(value - means.get(1, i), 2); |
2 |
26 Feb 07 |
jari |
1843 |
negCnt++; |
2 |
26 Feb 07 |
jari |
1844 |
} |
2 |
26 Feb 07 |
jari |
1845 |
} |
2 |
26 Feb 07 |
jari |
1846 |
} |
2 |
26 Feb 07 |
jari |
1847 |
vars.set( 0, i, (float)(posCnt > 1 ? Math.sqrt(ssePos/(posCnt - 1)) : 0.0f)); |
2 |
26 Feb 07 |
jari |
1848 |
vars.set( 1, i, (float)(negCnt > 1 ? Math.sqrt(sseNeg/(negCnt - 1)) : 0.0f)); |
2 |
26 Feb 07 |
jari |
1849 |
posCnt = 0; |
2 |
26 Feb 07 |
jari |
1850 |
negCnt = 0; |
2 |
26 Feb 07 |
jari |
1851 |
ssePos = 0; |
2 |
26 Feb 07 |
jari |
1852 |
sseNeg = 0; |
2 |
26 Feb 07 |
jari |
1853 |
} |
2 |
26 Feb 07 |
jari |
1854 |
return vars; |
2 |
26 Feb 07 |
jari |
1855 |
} |
2 |
26 Feb 07 |
jari |
1856 |
|
2 |
26 Feb 07 |
jari |
1857 |
|
2 |
26 Feb 07 |
jari |
1858 |
private void calculateHCL(AlgorithmData data, FloatMatrix discriminantMatrix) throws AbortException, AlgorithmException{ |
2 |
26 Feb 07 |
jari |
1859 |
|
2 |
26 Feb 07 |
jari |
1860 |
int [][] clusters = new int[2][]; |
2 |
26 Feb 07 |
jari |
1861 |
clusters[0] = getPositives(discriminantMatrix); |
2 |
26 Feb 07 |
jari |
1862 |
clusters[1] = getNegatives(discriminantMatrix); |
2 |
26 Feb 07 |
jari |
1863 |
|
2 |
26 Feb 07 |
jari |
1864 |
if(this.data.calculateHCL){ |
2 |
26 Feb 07 |
jari |
//preparation for HCL |
2 |
26 Feb 07 |
jari |
1866 |
Cluster result_cluster = new Cluster(); |
2 |
26 Feb 07 |
jari |
1867 |
NodeList nodeList = result_cluster.getNodeList(); |
2 |
26 Feb 07 |
jari |
1868 |
int[] features; |
2 |
26 Feb 07 |
jari |
1869 |
for (int i=0; i<clusters.length; i++) { |
2 |
26 Feb 07 |
jari |
1870 |
if (stop) { |
2 |
26 Feb 07 |
jari |
1871 |
throw new AbortException(); |
2 |
26 Feb 07 |
jari |
1872 |
} |
2 |
26 Feb 07 |
jari |
1873 |
features = clusters[i]; |
2 |
26 Feb 07 |
jari |
1874 |
Node node = new Node(features); |
2 |
26 Feb 07 |
jari |
1875 |
nodeList.addNode(node); |
2 |
26 Feb 07 |
jari |
1876 |
node.setValues(calculateHierarchicalTree(features, this.data.hclMethod, this.data.calcGeneHCL, this.data.calcSampleHCL)); |
2 |
26 Feb 07 |
jari |
1877 |
} |
2 |
26 Feb 07 |
jari |
1878 |
data.addCluster("cluster", result_cluster); |
2 |
26 Feb 07 |
jari |
1879 |
} |
2 |
26 Feb 07 |
jari |
1880 |
} |
2 |
26 Feb 07 |
jari |
1881 |
|
2 |
26 Feb 07 |
jari |
1882 |
|
2 |
26 Feb 07 |
jari |
1883 |
/** |
2 |
26 Feb 07 |
jari |
* Creates HCL results |
2 |
26 Feb 07 |
jari |
1885 |
*/ |
2 |
26 Feb 07 |
jari |
1886 |
private NodeValueList calculateHierarchicalTree(int[] features, int method, boolean genes, boolean experiments) throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
1887 |
NodeValueList nodeList = new NodeValueList(); |
2 |
26 Feb 07 |
jari |
1888 |
AlgorithmData data = new AlgorithmData(); |
2 |
26 Feb 07 |
jari |
1889 |
FloatMatrix experiment; |
2 |
26 Feb 07 |
jari |
1890 |
FloatMatrix expMatrix = this.experimentMap.getMatrix(); |
2 |
26 Feb 07 |
jari |
1891 |
if(this.data.classifyGenes){ |
2 |
26 Feb 07 |
jari |
1892 |
experiment = getSubExperiment(expMatrix, features); |
2 |
26 Feb 07 |
jari |
1893 |
} |
2 |
26 Feb 07 |
jari |
1894 |
else{ |
2 |
26 Feb 07 |
jari |
1895 |
expMatrix = expMatrix.transpose(); |
2 |
26 Feb 07 |
jari |
1896 |
experiment = getSubExperimentReducedCols(expMatrix, features); |
2 |
26 Feb 07 |
jari |
1897 |
} |
2 |
26 Feb 07 |
jari |
1898 |
data.addMatrix("experiment", experiment); |
2 |
26 Feb 07 |
jari |
1899 |
data.addParam("hcl-distance-function", String.valueOf(this.data.distanceFunction)); |
2 |
26 Feb 07 |
jari |
1900 |
data.addParam("hcl-distance-absolute", String.valueOf(this.data.absoluteDistance)); |
2 |
26 Feb 07 |
jari |
1901 |
data.addParam("method-linkage", String.valueOf(this.data.hclMethod)); |
2 |
26 Feb 07 |
jari |
1902 |
|
2 |
26 Feb 07 |
jari |
1903 |
Algorithm hcl = framework.getAlgorithmFactory().getAlgorithm("HCL"); |
2 |
26 Feb 07 |
jari |
1904 |
|
2 |
26 Feb 07 |
jari |
// HCL hcl = new HCL(); |
2 |
26 Feb 07 |
jari |
1906 |
|
2 |
26 Feb 07 |
jari |
1907 |
AlgorithmData result; |
2 |
26 Feb 07 |
jari |
1908 |
|
2 |
26 Feb 07 |
jari |
1909 |
if (genes) { |
2 |
26 Feb 07 |
jari |
1910 |
data.addParam("calculate-genes", String.valueOf(true)); |
2 |
26 Feb 07 |
jari |
1911 |
result = hcl.execute(data); |
2 |
26 Feb 07 |
jari |
1912 |
validate(result); |
2 |
26 Feb 07 |
jari |
1913 |
addNodeValues(nodeList, result); |
2 |
26 Feb 07 |
jari |
1914 |
} |
2 |
26 Feb 07 |
jari |
1915 |
if (experiments) { |
2 |
26 Feb 07 |
jari |
1916 |
data.addParam("calculate-genes", String.valueOf(false)); |
2 |
26 Feb 07 |
jari |
1917 |
result = hcl.execute(data); |
2 |
26 Feb 07 |
jari |
1918 |
int [] nodes = result.getIntArray("node-order"); |
2 |
26 Feb 07 |
jari |
1919 |
validate(result); |
2 |
26 Feb 07 |
jari |
1920 |
addNodeValues(nodeList, result); |
2 |
26 Feb 07 |
jari |
1921 |
} |
2 |
26 Feb 07 |
jari |
1922 |
return nodeList; |
2 |
26 Feb 07 |
jari |
1923 |
} |
2 |
26 Feb 07 |
jari |
1924 |
|
2 |
26 Feb 07 |
jari |
1925 |
|
2 |
26 Feb 07 |
jari |
1926 |
/** |
2 |
26 Feb 07 |
jari |
* Accumulates hcl results |
2 |
26 Feb 07 |
jari |
1928 |
*/ |
2 |
26 Feb 07 |
jari |
1929 |
private void addNodeValues(NodeValueList target_list, AlgorithmData source_result) { |
2 |
26 Feb 07 |
jari |
1930 |
target_list.addNodeValue(new NodeValue("child-1-array", source_result.getIntArray("child-1-array"))); |
2 |
26 Feb 07 |
jari |
1931 |
target_list.addNodeValue(new NodeValue("child-2-array", source_result.getIntArray("child-2-array"))); |
2 |
26 Feb 07 |
jari |
1932 |
target_list.addNodeValue(new NodeValue("node-order", source_result.getIntArray("node-order"))); |
2 |
26 Feb 07 |
jari |
1933 |
target_list.addNodeValue(new NodeValue("height", source_result.getMatrix("height").getRowPackedCopy())); |
2 |
26 Feb 07 |
jari |
1934 |
} |
2 |
26 Feb 07 |
jari |
1935 |
|
2 |
26 Feb 07 |
jari |
1936 |
|
2 |
26 Feb 07 |
jari |
1937 |
/** |
2 |
26 Feb 07 |
jari |
* Gets sub experiment (cluster membership only, dictated by features) |
2 |
26 Feb 07 |
jari |
1939 |
*/ |
2 |
26 Feb 07 |
jari |
1940 |
private FloatMatrix getSubExperiment(FloatMatrix experiment, int[] features) { |
2 |
26 Feb 07 |
jari |
1941 |
FloatMatrix subExperiment = new FloatMatrix(features.length, experiment.getColumnDimension()); |
2 |
26 Feb 07 |
jari |
1942 |
for (int i=0; i<features.length; i++) { |
2 |
26 Feb 07 |
jari |
1943 |
subExperiment.A[i] = experiment.A[features[i]]; |
2 |
26 Feb 07 |
jari |
1944 |
} |
2 |
26 Feb 07 |
jari |
1945 |
return subExperiment; |
2 |
26 Feb 07 |
jari |
1946 |
} |
2 |
26 Feb 07 |
jari |
1947 |
|
2 |
26 Feb 07 |
jari |
1948 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a matrix with reduced columns (samples) as during experiment classification |
2 |
26 Feb 07 |
jari |
1950 |
*/ |
2 |
26 Feb 07 |
jari |
1951 |
private FloatMatrix getSubExperimentReducedCols(FloatMatrix experiment, int[] features) { |
2 |
26 Feb 07 |
jari |
1952 |
FloatMatrix copyMatrix = experiment.copy(); |
2 |
26 Feb 07 |
jari |
1953 |
FloatMatrix subExperiment = new FloatMatrix(features.length, copyMatrix.getColumnDimension()); |
2 |
26 Feb 07 |
jari |
1954 |
for (int i=0; i<features.length; i++) { |
2 |
26 Feb 07 |
jari |
1955 |
subExperiment.A[i] = copyMatrix.A[features[i]]; |
2 |
26 Feb 07 |
jari |
1956 |
} |
2 |
26 Feb 07 |
jari |
1957 |
subExperiment = subExperiment.transpose(); |
2 |
26 Feb 07 |
jari |
1958 |
return subExperiment; |
2 |
26 Feb 07 |
jari |
1959 |
} |
2 |
26 Feb 07 |
jari |
1960 |
|
2 |
26 Feb 07 |
jari |
1961 |
/** |
2 |
26 Feb 07 |
jari |
* Checks the result of hcl algorithm calculation. |
2 |
26 Feb 07 |
jari |
* @throws AlgorithmException, if the result is incorrect. |
2 |
26 Feb 07 |
jari |
1964 |
*/ |
2 |
26 Feb 07 |
jari |
1965 |
private void validate(AlgorithmData result) throws AlgorithmException { |
2 |
26 Feb 07 |
jari |
1966 |
if (result.getIntArray("child-1-array") == null) { |
2 |
26 Feb 07 |
jari |
1967 |
throw new AlgorithmException("parameter 'child-1-array' is null"); |
2 |
26 Feb 07 |
jari |
1968 |
} |
2 |
26 Feb 07 |
jari |
1969 |
if (result.getIntArray("child-2-array") == null) { |
2 |
26 Feb 07 |
jari |
1970 |
throw new AlgorithmException("parameter 'child-2-array' is null"); |
2 |
26 Feb 07 |
jari |
1971 |
} |
2 |
26 Feb 07 |
jari |
1972 |
if (result.getIntArray("node-order") == null) { |
2 |
26 Feb 07 |
jari |
1973 |
throw new AlgorithmException("parameter 'node-order' is null"); |
2 |
26 Feb 07 |
jari |
1974 |
} |
2 |
26 Feb 07 |
jari |
1975 |
if (result.getMatrix("height") == null) { |
2 |
26 Feb 07 |
jari |
1976 |
throw new AlgorithmException("parameter 'height' is null"); |
2 |
26 Feb 07 |
jari |
1977 |
} |
2 |
26 Feb 07 |
jari |
1978 |
} |
2 |
26 Feb 07 |
jari |
1979 |
|
2 |
26 Feb 07 |
jari |
1980 |
private int[] convert2int(ArrayList source) { |
2 |
26 Feb 07 |
jari |
1981 |
int[] int_matrix = new int[source.size()]; |
2 |
26 Feb 07 |
jari |
1982 |
for (int i=0; i<int_matrix.length; i++) { |
2 |
26 Feb 07 |
jari |
1983 |
int_matrix[i] = (int)((Float)source.get(i)).floatValue(); |
2 |
26 Feb 07 |
jari |
1984 |
} |
2 |
26 Feb 07 |
jari |
1985 |
return int_matrix; |
2 |
26 Feb 07 |
jari |
1986 |
} |
2 |
26 Feb 07 |
jari |
1987 |
|
2 |
26 Feb 07 |
jari |
1988 |
|
2 |
26 Feb 07 |
jari |
1989 |
private class Listener extends DialogListener implements AlgorithmListener { |
2 |
26 Feb 07 |
jari |
1990 |
|
2 |
26 Feb 07 |
jari |
1991 |
/** |
2 |
26 Feb 07 |
jari |
* Handles algorithm events |
2 |
26 Feb 07 |
jari |
* @param event |
2 |
26 Feb 07 |
jari |
1994 |
*/ |
2 |
26 Feb 07 |
jari |
1995 |
public void valueChanged(AlgorithmEvent event) { |
2 |
26 Feb 07 |
jari |
1996 |
int id = event.getId(); |
2 |
26 Feb 07 |
jari |
1997 |
switch (id) { |
2 |
26 Feb 07 |
jari |
1998 |
case AlgorithmEvent.SET_UNITS: |
2 |
26 Feb 07 |
jari |
1999 |
break; |
2 |
26 Feb 07 |
jari |
2000 |
case AlgorithmEvent.PROGRESS_VALUE: |
2 |
26 Feb 07 |
jari |
2001 |
logger.append( event.getDescription() ); |
2 |
26 Feb 07 |
jari |
2002 |
break; |
2 |
26 Feb 07 |
jari |
2003 |
case AlgorithmEvent.MONITOR_VALUE: |
2 |
26 Feb 07 |
jari |
2004 |
if (monitor == null) { |
2 |
26 Feb 07 |
jari |
2005 |
monitor = new Monitor(parentFrame, "Convergence",75,125,(0.1/data.convergenceThreshold)); |
2 |
26 Feb 07 |
jari |
2006 |
monitor.show(); |
2 |
26 Feb 07 |
jari |
2007 |
} else { |
2 |
26 Feb 07 |
jari |
2008 |
monitor.update( event.getFloatValue() ); |
2 |
26 Feb 07 |
jari |
2009 |
} |
2 |
26 Feb 07 |
jari |
2010 |
break; |
2 |
26 Feb 07 |
jari |
2011 |
} |
2 |
26 Feb 07 |
jari |
2012 |
} |
2 |
26 Feb 07 |
jari |
2013 |
|
2 |
26 Feb 07 |
jari |
2014 |
/** |
2 |
26 Feb 07 |
jari |
* Handles action events during run |
2 |
26 Feb 07 |
jari |
* @param e |
2 |
26 Feb 07 |
jari |
2017 |
*/ |
2 |
26 Feb 07 |
jari |
2018 |
public void actionPerformed(ActionEvent e) { |
2 |
26 Feb 07 |
jari |
2019 |
String command = e.getActionCommand(); |
2 |
26 Feb 07 |
jari |
2020 |
if (command.equals("cancel-command")) { |
2 |
26 Feb 07 |
jari |
2021 |
algorithm.abort(); |
2 |
26 Feb 07 |
jari |
2022 |
if (monitor != null) |
2 |
26 Feb 07 |
jari |
2023 |
monitor.dispose(); |
2 |
26 Feb 07 |
jari |
2024 |
if (logger != null) |
2 |
26 Feb 07 |
jari |
2025 |
logger.dispose(); |
2 |
26 Feb 07 |
jari |
2026 |
} |
2 |
26 Feb 07 |
jari |
2027 |
} |
2 |
26 Feb 07 |
jari |
2028 |
|
2 |
26 Feb 07 |
jari |
2029 |
/** |
2 |
26 Feb 07 |
jari |
* Handles dialog closing events |
2 |
26 Feb 07 |
jari |
* @param e |
2 |
26 Feb 07 |
jari |
2032 |
*/ |
2 |
26 Feb 07 |
jari |
2033 |
public void windowClosing(WindowEvent e) { |
2 |
26 Feb 07 |
jari |
2034 |
algorithm.abort(); |
2 |
26 Feb 07 |
jari |
2035 |
if (monitor != null) |
2 |
26 Feb 07 |
jari |
2036 |
monitor.dispose(); |
2 |
26 Feb 07 |
jari |
2037 |
if (logger != null) |
2 |
26 Feb 07 |
jari |
2038 |
logger.dispose(); |
2 |
26 Feb 07 |
jari |
2039 |
|
2 |
26 Feb 07 |
jari |
2040 |
} |
2 |
26 Feb 07 |
jari |
2041 |
} |
2 |
26 Feb 07 |
jari |
2042 |
} |