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;
025
026 import java.lang.reflect.Constructor;
027 import java.util.ArrayList;
028 import java.util.Collections;
029 import java.util.List;
030 import java.util.Random;
031 import java.util.logging.Level;
032
033 import org.jdmp.core.AbstractCoreObject;
034 import org.jdmp.core.dataset.wrappers.SampleListToMatrixWrapper;
035 import org.jdmp.core.sample.Sample;
036 import org.jdmp.core.util.DefaultObservableList;
037 import org.jdmp.core.util.DefaultObservableMap;
038 import org.jdmp.core.util.ObservableList;
039 import org.jdmp.core.util.ObservableMap;
040 import org.jdmp.core.variable.Variable;
041 import org.jdmp.core.variable.VariableFactory;
042 import org.ujmp.core.Matrix;
043 import org.ujmp.core.MatrixFactory;
044 import org.ujmp.core.interfaces.GUIObject;
045 import org.ujmp.core.util.StringUtil;
046
047 public abstract class AbstractDataSet extends AbstractCoreObject implements DataSet {
048 private static final long serialVersionUID = -4168834188998259018L;
049
050 private transient GUIObject guiObject = null;
051
052 private ObservableList<Sample> sampleList = new DefaultObservableList<Sample>();
053
054 private ObservableMap<Variable> variableList = new DefaultObservableMap<Variable>();
055
056 private final ObservableList<DataSet> dataSetList = new DefaultObservableList<DataSet>();
057
058 public final String getDescription() {
059 return getString(DataSet.DESCRIPTION);
060 }
061
062 public final void setDescription(String description) {
063 setObject(DataSet.DESCRIPTION, description);
064 }
065
066 public final String getLabel() {
067 return getString(DataSet.LABEL);
068 }
069
070 public final void setLabel(String label) {
071 setObject(DataSet.LABEL, label);
072 }
073
074 public AbstractDataSet() {
075 super();
076 Matrix m = new SampleListToMatrixWrapper(getSamples());
077 Variable v = VariableFactory.labeledVariable("AllSamples");
078 v.addMatrix(m);
079 getVariables().put("AllSamples", v);
080 }
081
082 public AbstractDataSet(String label) {
083 this();
084 setLabel(label);
085 }
086
087 public final String getString(Object variableKey) {
088 return StringUtil.convert(getMatrix(variableKey));
089 }
090
091 public Matrix getMatrix(Object variableKey) {
092 Variable v = getVariables().get(variableKey);
093 if (v != null) {
094 return v.getMatrix();
095 } else {
096 return null;
097 }
098 }
099
100 public final String getAsString(Object variableKey) {
101 return StringUtil.convert(getMatrix(variableKey));
102 }
103
104 public final void setObject(Object variableKey, Object value) {
105 if (value == null) {
106 setMatrix(variableKey, MatrixFactory.emptyMatrix());
107 } else if (value instanceof Matrix) {
108 setMatrix(variableKey, (Matrix) value);
109 } else {
110 setMatrix(variableKey, MatrixFactory.linkToValue(value));
111 }
112 }
113
114 public void setMatrix(Object variableKey, Matrix matrix) {
115 Variable v = getVariables().get(variableKey);
116 if (v == null) {
117 v = VariableFactory.labeledVariable(variableKey.toString());
118 getVariables().put(variableKey, v);
119 }
120 v.addMatrix(matrix);
121 }
122
123 public ObservableList<Sample> getSamples() {
124 return sampleList;
125 }
126
127 public final ObservableMap<Variable> getVariables() {
128 return variableList;
129 }
130
131 public final ObservableList<DataSet> getDataSets() {
132 return dataSetList;
133 }
134
135 public void clear() {
136 sampleList.clear();
137 }
138
139 public List<DataSet> splitByCount(boolean shuffle, int... count) {
140 List<DataSet> dataSets = new ArrayList<DataSet>();
141
142 List<Integer> ids = new ArrayList<Integer>(getSamples().getSize());
143 for (int i = getSamples().getSize() - 1; i != -1; i--) {
144 ids.add(i);
145 }
146
147 if (shuffle) {
148 Collections.shuffle(ids);
149 }
150
151 for (int i = 0; i < count.length; i++) {
152 DataSet ds = DataSetFactory.classificationDataSet("DataSet" + i);
153 for (int c = 0; c < count[i]; c++) {
154 ds.getSamples().add(getSamples().getElementAt(ids.remove(0)).clone());
155 }
156 dataSets.add(ds);
157 }
158 DataSet ds = DataSetFactory.classificationDataSet("DataSet" + count.length);
159 while (ids.size() != 0) {
160 ds.getSamples().add(getSamples().getElementAt(ids.remove(0)).clone());
161 }
162 dataSets.add(ds);
163
164 return dataSets;
165 }
166
167 public List<DataSet> splitForCV(int numberOfCVSets, int idOfCVSet, long randomSeed) {
168 List<DataSet> returnDataSets = new ArrayList<DataSet>();
169 List<List<Sample>> tempSampleLists = new ArrayList<List<Sample>>();
170 List<Sample> allSamples = new ArrayList<Sample>(getSamples().toCollection());
171 Collections.shuffle(allSamples, new Random(randomSeed));
172
173 for (int set = 0; set < numberOfCVSets; set++) {
174 List<Sample> partSamples = new ArrayList<Sample>();
175 tempSampleLists.add(partSamples);
176 }
177
178 while (!allSamples.isEmpty()) {
179 for (int set = 0; set < numberOfCVSets; set++) {
180 if (!allSamples.isEmpty()) {
181 tempSampleLists.get(set).add(allSamples.remove(0));
182 }
183 }
184 }
185
186 DataSet testSet = DataSetFactory.classificationDataSet("TestSet" + randomSeed + "-"
187 + idOfCVSet);
188 testSet.getSamples().addAll(tempSampleLists.get(idOfCVSet));
189 DataSet trainingSet = DataSetFactory.classificationDataSet("TrainingSet" + randomSeed + "-"
190 + idOfCVSet);
191 for (int i = 0; i < numberOfCVSets; i++) {
192 if (i != idOfCVSet) {
193 trainingSet.getSamples().addAll(tempSampleLists.get(i));
194 }
195 }
196
197 returnDataSets.add(trainingSet);
198 returnDataSets.add(testSet);
199
200 return returnDataSets;
201 }
202
203 public List<DataSet> splitByPercent(boolean shuffle, double... percent) {
204 int[] counts = new int[percent.length];
205 int sampleCount = getSamples().getSize();
206 for (int i = 0; i < percent.length; i++) {
207 counts[i] = (int) Math.round(percent[i] * sampleCount);
208 }
209 return splitByCount(shuffle, counts);
210 }
211
212 public final GUIObject getGUIObject() {
213 if (guiObject == null) {
214 try {
215 Class<?> c = Class.forName("org.jdmp.gui.dataset.DataSetGUIObject");
216 Constructor<?> con = c.getConstructor(new Class<?>[] { DataSet.class });
217 guiObject = (GUIObject) con.newInstance(new Object[] { this });
218 } catch (Exception e) {
219 logger.log(Level.WARNING, "cannot create dataset gui object", e);
220 }
221 }
222 return guiObject;
223 }
224
225 @Override
226 public final String toString() {
227 if (getLabel() == null) {
228 return getClass().getSimpleName();
229 } else {
230 return getClass().getSimpleName() + " [" + getLabel() + "]";
231 }
232 }
233
234 @Override
235 public void setSamples(ObservableList<Sample> samples) {
236 this.sampleList = samples;
237 }
238
239 public void setVariables(ObservableMap<Variable> variables) {
240 this.variableList = variables;
241 }
242
243 public void notifyGUIObject() {
244 if (guiObject != null) {
245 guiObject.fireValueChanged();
246 }
247 }
248
249 @Override
250 public abstract AbstractDataSet clone();
251 }