📄 matlabica.java
字号:
*/ private void dumpInstances(String tempFile) { try { PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(tempFile))); for (int k = 0; k < m_numInstances; k++) { Instance instance = m_trainInstances.instance(k); for (int j = 0; j < m_numAttribs; j++) { writer.print(instance.value(j) + " "); } writer.println(); } writer.close(); } catch (Exception e) { System.err.println("Could not create a temporary file for dumping the data matrix: " + e); } } /** Create matlab m-file for ICA * @param filename file where matlab script is created */ public void prepareMatlab(String filename) { try{ PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(filename))); writer.println("addpath /var/local;"); writer.println("DATA = load('/var/local/ICAdataMatrix.txt');"); if (m_loadEigenValuesFromFile == true) { writer.println("E = load('/var/local/PCAeigenValues.txt');"); } if (m_loadEigenVectorsFromFile == true) { writer.println("V = load('/var/local/PCAeigenVectors.txt');"); } writer.print("[IC, A, invA] = fastica(DATA'"); // take transpose of data matrix, to make each instance a column if (m_loadEigenValuesFromFile == true) { writer.print(",'pcaD',E"); } if (m_loadEigenVectorsFromFile == true) { writer.print(",'pcaE',V"); } writer.print(",'approach','" + TAGS_APPROACH[m_ICAapproach].getReadable() + "'"); writer.print(",'g','" + TAGS_FUNCTION[m_ICAfunction].getReadable() + "'"); writer.print(",'numOfIC'," + m_NumIndependentComponents); writer.println(",'displayMode','off','stabilization','on');"); writer.println("[ICnumRows, ICnumCols] = size(IC);"); writer.println("[AnumRows, AnumCols] = size(A);"); writer.println("[invAnumRows, invAnumCols] = size(invA);\n"); writer.println("save " + m_mixingMatrixFilename + " AnumRows AnumCols A -ASCII -DOUBLE"); writer.println("save " + m_inverseMixingMatrixFilename + " invAnumRows invAnumCols invA -ASCII -DOUBLE"); writer.println("save " + m_independentComponentsFilename + " ICnumRows ICnumCols IC -ASCII -DOUBLE"); writer.close(); } catch (Exception e) { System.err.println("Could not create matlab file: " + e); } } /** Run matlab in command line with a given argument * @param inFile file to be input to Matlab * @param outFile file where results are stored */ public static void runMatlab(String inFile, String outFile) { // call matlab to do the dirty work try { int exitValue; do { System.out.println("Starting to run matlab"); Process proc = Runtime.getRuntime().exec("matlab -tty < " + inFile + " > " + outFile); exitValue = proc.waitFor(); if (exitValue != 0) { System.err.println("WARNING!!!!! Matlab returned exit value 1, trying again in 5 mins!"); Thread.sleep(300000); } } while (exitValue != 0); System.out.println("End of running matlab, exitValue = " + exitValue); } catch (Exception e) { System.err.println("Problems running matlab: " + e); } } /** * Return a summary of the analysis * @return a summary of the analysis. */ private String independentComponentsSummary() { StringBuffer result = new StringBuffer(); double cumulative = 0.0; Instances output = null; int numVectors=0; try { output = setOutputFormat(); numVectors = (output.classIndex() < 0) ? output.numAttributes() : output.numAttributes()-1; } catch (Exception ex) { } // Todo: Add IC summary to result string result.append("\nAttribute ranking filter:\n"); result.append(m_eval.toString()); return result.toString(); } /** * Returns a description of this attribute transformer * @return a String describing this attribute transformer */ public String toString() { if (m_independentComponents == null) { return "Independent components hasn't been built yet!"; } else { return "\tIndependent Components Attribute Transformer\n\n" +independentComponentsSummary(); } } /** * Return a matrix as a String * @param matrix that is decribed as a string * @return a String describing a matrix */ private String matrixToString(double [][] matrix) { StringBuffer result = new StringBuffer(); int last = matrix.length - 1; for (int i = 0; i <= last; i++) { for (int j = 0; j <= last; j++) { result.append(Utils.doubleToString(matrix[i][j],6,2)+" "); if (j == last) { result.append('\n'); } } } return result.toString(); } /** * Convert a ic transformed instance back to the original space */ private Instance convertInstanceToOriginal(Instance inst) throws Exception { double[] newVals = null; if (m_hasClass) { newVals = new double[m_numAttribs+1]; } else { newVals = new double[m_numAttribs]; } if (m_hasClass) { // class is always appended as the last attribute newVals[m_numAttribs] = inst.value(inst.numAttributes() - 1); } for (int i = 0; i < m_numAttribs; i++) { for (int j = 0; j < m_outputNumAtts - 1; j++) { newVals[i] += m_mixingMatrix[i][j] * inst.value(j); } } if (inst instanceof SparseInstance) { return new SparseInstance(inst.weight(), newVals); } else { return new Instance(inst.weight(), newVals); } } /** * Transform an instance in original (unormalized) format. Convert back * to the original space if requested. * @param instance an instance in the original (unormalized) format * @return a transformed instance * @exception Exception if instance cant be transformed */ public Instance convertInstance(Instance instance) throws Exception { if (m_independentComponents == null) { // throw new Exception("convertInstance: Independent components not " +"built yet"); System.out.println("WARNING!! Independent components could not be built, returning original data"); } double[] newVals = new double[m_outputNumAtts]; Instance tempInst = (Instance)instance.copy(); if (!instance.equalHeaders(m_trainCopy.instance(0))) { throw new Exception("Can't convert instance: header's don't match: MatlabICA"); } m_replaceMissingFilter.input(tempInst); m_replaceMissingFilter.batchFinished(); tempInst = m_replaceMissingFilter.output(); if (m_normalize) { m_normalizeFilter.input(tempInst); m_normalizeFilter.batchFinished(); tempInst = m_normalizeFilter.output(); } if (m_attributeFilter != null) { m_attributeFilter.input(tempInst); m_attributeFilter.batchFinished(); tempInst = m_attributeFilter.output(); } if (m_hasClass) { newVals[m_outputNumAtts - 1] = instance.value(instance.classIndex()); } for (int i=0; i<m_outputNumAtts-1; i++) { for (int j = 0; j < m_numAttribs; j++) { newVals[i] += (m_inverseMixingMatrix[i][j] * tempInst.value(j)); } } if (!m_transBackToOriginal) { if (instance instanceof SparseInstance) { return new SparseInstance(instance.weight(), newVals); } else { return new Instance(instance.weight(), newVals); } } else { if (instance instanceof SparseInstance) { return convertInstanceToOriginal(new SparseInstance(instance.weight(), newVals)); } else { return convertInstanceToOriginal(new Instance(instance.weight(), newVals)); } } } /** * Set up the header for the IC->original space dataset */ private Instances setOutputFormatOriginal() throws Exception { FastVector attributes = new FastVector(); for (int i = 0; i < m_numAttribs; i++) { String att = m_trainInstances.attribute(i).name(); attributes.addElement(new Attribute(att)); } if (m_hasClass) { attributes.addElement(m_trainCopy.classAttribute().copy()); } Instances outputFormat = new Instances(m_trainCopy.relationName()+"->IC->original space", attributes, 0); // set the class to be the last attribute if necessary if (m_hasClass) { outputFormat.setClassIndex(outputFormat.numAttributes()-1); } return outputFormat; } /** * Set the format for the transformed data * @return a set of empty Instances (header only) in the new format * @exception Exception if the output format can't be set */ private Instances setOutputFormat() throws Exception { if (m_independentComponents == null) { return null; } double cumulative = 0.0; FastVector attributes = new FastVector(); for (int i=0; i<m_inverseMixingMatrix.length; i++) { StringBuffer attName = new StringBuffer("ICAattribute" + i); attributes.addElement(new Attribute(attName.toString())); } if (m_hasClass) { attributes.addElement(m_trainCopy.classAttribute().copy()); } Instances outputFormat = new Instances(m_trainInstances.relationName()+"_independent components", attributes, 0); // set the class to be the last attribute if necessary if (m_hasClass) { outputFormat.setClassIndex(outputFormat.numAttributes()-1); } m_outputNumAtts = outputFormat.numAttributes(); System.out.println("m_outputNumAtts: " + m_outputNumAtts); return outputFormat; } /** Get a timestamp string as a weak uniqueid * @returns a timestamp string in the form "mmddhhmmssS" */ public static String getLogTimestamp() { Calendar cal = Calendar.getInstance(TimeZone.getDefault()); String DATE_FORMAT = "MMddHHmmssS"; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); return (sdf.format(cal.getTime())); } /** * Main method for testing this class * @param argv should contain the command line arguments to the * evaluator/transformer (see AttributeSelection) */ public static void main(String [] argv) { try { // String name = "../../data/20newsgroups/different-100_fromCCS.arff"; String name = "/u/ml/software/weka-latest/data/iris.arff"; if (argv.length == 1) { name = argv[0]; } else { System.err.println("No data filename given as argument, running on default file " + name); } Reader r = new BufferedReader(new FileReader(name)); Instances data = new Instances(r); data.setClassIndex(data.numAttributes() - 1); MatlabICA mica = new MatlabICA(); mica.setNumIndependentComponents(2); mica.buildEvaluator(data); mica.transformedData(); } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -