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.jbpcafill; 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.enums.FileFormat; 033 import org.ujmp.core.exceptions.MatrixException; 034 035 public class ImputeBPCA extends AbstractDoubleCalculation { 036 private static final long serialVersionUID = -8635313044017639669L; 037 038 private Matrix xImputed = null; 039 040 public ImputeBPCA(Matrix matrix) { 041 super(matrix); 042 } 043 044 045 public double getDouble(long... coordinates) throws MatrixException { 046 if (xImputed == null) { 047 createMatrix(); 048 } 049 return xImputed.getAsDouble(coordinates); 050 } 051 052 private void createMatrix() { 053 try { 054 Matrix m = getSource(); 055 m = m.replaceRegex(Ret.NEW, "NaN", "999"); 056 File file1 = File.createTempFile("matrix", ".csv"); 057 File file2 = File.createTempFile("matrix", ".csv"); 058 m.exportToFile(FileFormat.CSV, file1); 059 Class<?> c = Class.forName("JBPCAfill"); 060 Method me = c.getMethod("main", String[].class); 061 me.invoke(null, new Object[] { new String[] { file1.toString(), file2.toString() } }); 062 m = MatrixFactory.importFromFile(FileFormat.CSV, file2, "\\s"); 063 m = m.replaceRegex(Ret.NEW, ",", ""); 064 file1.delete(); 065 file2.delete(); 066 xImputed = m; 067 } catch (Exception e) { 068 e.printStackTrace(); 069 } 070 } 071 }