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.core.doublematrix.calculation.general.missingvalues; 025 026 import org.ujmp.core.Matrix; 027 import org.ujmp.core.doublematrix.calculation.AbstractDoubleCalculation; 028 import org.ujmp.core.doublematrix.calculation.DoubleCalculation; 029 import org.ujmp.core.exceptions.MatrixException; 030 031 public class Impute extends AbstractDoubleCalculation { 032 private static final long serialVersionUID = -8899889992449926887L; 033 034 public enum ImputationMethod { 035 Zero, RowMean, ColumnMean, Regression, KNN, EM, BPCA, EMimputeGene, EMimputeArray, LSimputeGene, LSimputeArray, LSimputeCombined, LSimputeAdaptive 036 }; 037 038 private Matrix imp = null; 039 040 private ImputationMethod method = null; 041 042 private Object[] parameters = null; 043 044 public Impute(Matrix matrix, ImputationMethod method, Object... parameters) { 045 super(matrix); 046 this.method = method; 047 this.parameters = parameters; 048 } 049 050 051 public double getDouble(long... coordinates) throws MatrixException { 052 if (imp == null) { 053 try { 054 055 DoubleCalculation calc = null; 056 057 switch (method) { 058 case Zero: 059 calc = new ImputeZero(getSource()); 060 break; 061 case RowMean: 062 calc = new ImputeMean(Matrix.ROW, getSource()); 063 break; 064 case ColumnMean: 065 calc = new ImputeMean(Matrix.COLUMN, getSource()); 066 break; 067 case BPCA: 068 calc = new ImputeBPCA(getSource()); 069 break; 070 case KNN: 071 calc = new ImputeKNN(getSource(), parameters); 072 break; 073 case EM: 074 calc = new ImputeEM(getSource()); 075 break; 076 case Regression: 077 calc = new ImputeRegression(getSource()); 078 break; 079 case EMimputeGene: 080 calc = new ImputeLS(getSource(), method); 081 break; 082 case EMimputeArray: 083 calc = new ImputeLS(getSource(), method); 084 break; 085 case LSimputeGene: 086 calc = new ImputeLS(getSource(), method); 087 break; 088 case LSimputeArray: 089 calc = new ImputeLS(getSource(), method); 090 break; 091 case LSimputeCombined: 092 calc = new ImputeLS(getSource(), method); 093 break; 094 case LSimputeAdaptive: 095 calc = new ImputeLS(getSource(), method); 096 break; 097 } 098 099 imp = calc.calcNew(); 100 101 } catch (Exception e) { 102 throw new MatrixException(e); 103 } 104 } 105 return imp.getAsDouble(coordinates); 106 } 107 108 }