001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.lsimpute;
025    
026    import java.io.File;
027    import java.lang.reflect.Method;
028    
029    import org.ujmp.core.Matrix;
030    import org.ujmp.core.MatrixFactory;
031    import org.ujmp.core.doublematrix.calculation.AbstractDoubleCalculation;
032    import org.ujmp.core.doublematrix.calculation.general.missingvalues.Impute.ImputationMethod;
033    import org.ujmp.core.enums.FileFormat;
034    import org.ujmp.core.exceptions.MatrixException;
035    import org.ujmp.core.util.io.IntelligentFileWriter;
036    
037    public class LSImpute extends AbstractDoubleCalculation {
038            private static final long serialVersionUID = -5527190803466818167L;
039    
040            private int method = 0;
041    
042            private Matrix xImputed = null;
043    
044            public LSImpute(Matrix matrix, ImputationMethod impMethod) {
045                    super(matrix);
046                    switch (impMethod) {
047                    case RowMean:
048                            method = 0;
049                            break;
050                    case EMimputeGene:
051                            method = 1;
052                            break;
053                    case EMimputeArray:
054                            method = 2;
055                            break;
056                    case LSimputeGene:
057                            method = 3;
058                            break;
059                    case LSimputeArray:
060                            method = 4;
061                            break;
062                    case LSimputeCombined:
063                            method = 5;
064                            break;
065                    case LSimputeAdaptive:
066                            method = 6;
067                            break;
068                    default:
069                            throw new MatrixException("Imputation method is not supported: "
070                                            + impMethod);
071                    }
072    
073            }
074    
075            
076            public double getDouble(long... coordinates) throws MatrixException {
077                    if (xImputed == null) {
078                            createMatrix();
079                    }
080                    return xImputed.getAsDouble(coordinates);
081            }
082    
083            private void createMatrix() {
084                    try {
085                            Matrix m = getSource();
086                            m = m.replaceRegex(Ret.NEW, "NaN", "NULL");
087                            File file1 = File.createTempFile("matrix", ".csv");
088                            File file2 = File.createTempFile("matrix", ".csv");
089                            IntelligentFileWriter fw = new IntelligentFileWriter(file1);
090                            fw.write("---\t");
091                            for (int c = 0; c < m.getColumnCount(); c++) {
092                                    fw.write("Col" + c + "\t");
093                            }
094                            fw.write("\n");
095                            for (int r = 0; r < m.getRowCount(); r++) {
096                                    fw.write("Row" + r + "\t");
097                                    for (int c = 0; c < m.getColumnCount(); c++) {
098                                            fw.write("" + m.getAsObject(r, c));
099                                            fw.write("\t");
100                                    }
101                                    fw.write("\n");
102                            }
103                            fw.close();
104                            Class<?> c = Class.forName("Impute");
105                            Method me = c.getMethod("main", String[].class);
106                            me.invoke(null, new Object[] { new String[] { file1.toString(),
107                                            file2.toString(), "" + method } });
108                            m = MatrixFactory.importFromFile(FileFormat.CSV, file2, "\t");
109                            m = m.deleteRows(Ret.NEW, 0);
110                            m = m.deleteColumns(Ret.NEW, 0);
111                            m = m.replaceRegex(Ret.NEW, ",", "");
112                            file1.delete();
113                            file2.delete();
114                            xImputed = m;
115                    } catch (Exception e) {
116                            e.printStackTrace();
117                    }
118            }
119    }