001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP 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     * JDMP 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 JDMP; 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.jdmp.core.dataset.wrappers;
025    
026    import org.jdmp.core.algorithm.regression.Regressor;
027    import org.jdmp.core.dataset.DefaultDataSet;
028    import org.jdmp.core.sample.Sample;
029    import org.ujmp.core.Matrix;
030    import org.ujmp.core.coordinates.Coordinates;
031    import org.ujmp.core.doublematrix.stub.AbstractDenseDoubleMatrix2D;
032    import org.ujmp.core.exceptions.MatrixException;
033    import org.ujmp.core.interfaces.Wrapper;
034    
035    public class DataSetInputMatrixWrapper extends AbstractDenseDoubleMatrix2D implements
036                    Wrapper<DefaultDataSet> {
037            private static final long serialVersionUID = -817570097594349208L;
038    
039            public static final Object INPUT = Regressor.INPUT;
040    
041            private DefaultDataSet dataSet = null;
042    
043            public DataSetInputMatrixWrapper(DefaultDataSet ds) {
044                    this.dataSet = ds;
045            }
046    
047            public long[] getSize() {
048                    if (dataSet.getSamples().isEmpty()) {
049                            return Coordinates.ZERO2D;
050                    }
051                    Sample p = dataSet.getSamples().getElementAt(0);
052                    if (p == null) {
053                            return Coordinates.ZERO2D;
054                    }
055                    Matrix input = p.getMatrix(INPUT);
056                    if (input != null) {
057                            return new long[] { dataSet.getSamples().getSize(), input.getValueCount() };
058                    } else {
059                            return Coordinates.ZERO2D;
060                    }
061    
062            }
063    
064            public double getDouble(long row, long column) throws MatrixException {
065                    return getDouble((int) row, (int) column);
066            }
067    
068            public void setDouble(double value, long row, long column) throws MatrixException {
069                    setDouble(value, (int) row, (int) column);
070            }
071    
072            public double getDouble(int row, int column) throws MatrixException {
073                    try {
074                            Sample p = dataSet.getSamples().getElementAt(row);
075                            if (p != null) {
076                                    if (p.getMatrix(INPUT) != null) {
077                                            long r = column / p.getMatrix(INPUT).getColumnCount();
078                                            long c = column % p.getMatrix(INPUT).getColumnCount();
079                                            return p.getMatrix(INPUT).getAsDouble(r, c);
080                                    }
081                            }
082                            return 0.0;
083                    } catch (Exception e) {
084                            e.printStackTrace();
085                            return 0.0;
086                    }
087            }
088    
089            public void setDouble(double value, int row, int column) throws MatrixException {
090                    Sample p = dataSet.getSamples().getElementAt(row);
091                    if (p != null) {
092                            if (p.getMatrix(INPUT) != null) {
093                                    long r = column / p.getMatrix(INPUT).getColumnCount();
094                                    long c = column % p.getMatrix(INPUT).getColumnCount();
095                                    p.getMatrix(INPUT).setAsDouble(value, r, c);
096                            }
097                    }
098            }
099    
100            public DefaultDataSet getWrappedObject() {
101                    return dataSet;
102            }
103    
104            public void setWrappedObject(DefaultDataSet object) {
105                    this.dataSet = object;
106            }
107    
108    }