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.weka.clusterer;
025    
026    import java.lang.reflect.Constructor;
027    
028    import org.jdmp.core.algorithm.clustering.AbstractClusterer;
029    import org.jdmp.core.dataset.RegressionDataSet;
030    import org.jdmp.weka.wrappers.DataSetToInstancesWrapper;
031    import org.jdmp.weka.wrappers.SampleToInstanceWrapper;
032    import org.ujmp.core.Matrix;
033    import org.ujmp.core.MatrixFactory;
034    
035    import weka.clusterers.Clusterer;
036    import weka.clusterers.NumberOfClustersRequestable;
037    import weka.core.Instance;
038    import weka.core.Instances;
039    
040    public class WekaClusterer extends AbstractClusterer {
041            private static final long serialVersionUID = 1308337964347655655L;
042    
043            public enum WekaClustererType {
044                    Cobweb, DBScan, EM, FarthestFirst, FilteredClusterer, MakeDensityBasedClusterer, SimpleKMeans, XMeans
045            };
046    
047            private Clusterer wekaClusterer = null;
048    
049            private Instances instances = null;
050    
051            private WekaClustererType clustererName = null;
052    
053            private String[] options = null;
054    
055            private boolean discrete = false;
056    
057            public WekaClusterer(WekaClustererType classifierName, boolean discrete, String... options)
058                            throws Exception {
059                    setLabel("Weka-" + classifierName.name());
060                    this.clustererName = classifierName;
061                    this.options = options;
062                    this.discrete = discrete;
063                    createAlgorithm();
064            }
065    
066            public void createAlgorithm() throws Exception {
067                    Class<?> c = null;
068                    if (c == null) {
069                            try {
070                                    c = Class.forName(clustererName.name());
071                            } catch (ClassNotFoundException e) {
072                            }
073                    }
074                    if (c == null) {
075                            try {
076                                    c = Class.forName("weka.clusterers." + clustererName.name());
077                            } catch (ClassNotFoundException e) {
078                            }
079                    }
080    
081                    if (c == null) {
082                            throw new ClassNotFoundException("class not found: " + clustererName);
083                    } else {
084                            Constructor<?> constr = c.getConstructor(new Class[] {});
085                            wekaClusterer = (Clusterer) constr.newInstance(new Object[] {});
086                            if (options != null || options.length != 0) {
087                                    // wekaClusterer.setOptions(options);
088                            }
089                    }
090            }
091    
092            public void reset() throws Exception {
093                    createAlgorithm();
094            }
095    
096            public void train(RegressionDataSet dataSet) throws Exception {
097                    instances = new DataSetToInstancesWrapper(dataSet, discrete, false);
098                    wekaClusterer.buildClusterer(instances);
099            }
100    
101            public Matrix predict(Matrix input, Matrix weight) throws Exception {
102                    double[] probabilities = null;
103                    Instance instance = new SampleToInstanceWrapper(input, weight, null, discrete, false);
104                    instance.setDataset(instances);
105                    probabilities = wekaClusterer.distributionForInstance(instance);
106                    double[][] v = new double[1][];
107                    v[0] = probabilities;
108                    Matrix output = MatrixFactory.linkToArray(v);
109                    return output;
110            }
111    
112            public void setNumberOfClusters(int numberOfClusters) throws Exception {
113                    if (wekaClusterer instanceof NumberOfClustersRequestable) {
114                            ((NumberOfClustersRequestable) wekaClusterer).setNumClusters(numberOfClusters);
115                    } else {
116                            throw new RuntimeException("Cannot set number of Clusters for this Clusterer");
117                    }
118            }
119    
120    }