Example usage for org.apache.commons.math3.ml.clustering DBSCANClusterer cluster

List of usage examples for org.apache.commons.math3.ml.clustering DBSCANClusterer cluster

Introduction

In this page you can find the example usage for org.apache.commons.math3.ml.clustering DBSCANClusterer cluster.

Prototype

@Override
public List<Cluster<T>> cluster(final Collection<T> points) throws NullArgumentException 

Source Link

Document

Performs DBSCAN cluster analysis.

Usage

From source file:DBClust.java

public static void Ident(double[] eps, int[] minPts, int[] pixelSize, boolean[] doCluster) {
    ArrayList<Particle> InpParticle = TableIO.Load(); // Get current table data.

    ij.measure.ResultsTable tab = Analyzer.getResultsTable();
    double width = tab.getValue("width", 0);
    double height = tab.getValue("height", 0);
    tab.reset();/*from www  .j a v  a2 s  .com*/
    //      tab.incrementCounter();

    for (int Ch = 1; Ch <= InpParticle.get(InpParticle.size() - 1).channel; Ch++) {
        if (doCluster[Ch - 1]) {
            List<DoublePoint> points = new ArrayList<DoublePoint>();

            for (int i = 0; i < InpParticle.size(); i++) {
                double[] p = new double[2];
                if (InpParticle.get(i).include == 1 && InpParticle.get(i).channel == Ch) {
                    p[0] = InpParticle.get(i).x;
                    p[1] = InpParticle.get(i).y;
                    points.add(new DoublePoint(p));
                }
            }
            DBSCANClusterer<DoublePoint> DB = new DBSCANClusterer<DoublePoint>(eps[Ch - 1], minPts[Ch - 1]);
            List<Cluster<DoublePoint>> cluster = DB.cluster(points);
            int ClustIdx = 1;
            int[] IndexList = new int[InpParticle.size()];
            for (Cluster<DoublePoint> c : cluster) {
                for (int j = 0; j < c.getPoints().size(); j++) {
                    DoublePoint p = c.getPoints().get(j);
                    double[] Coord = p.getPoint();
                    for (int i = 0; i < InpParticle.size(); i++) {
                        Particle tempParticle = InpParticle.get(i);
                        if (tempParticle.x == Coord[0] && tempParticle.y == Coord[1]) {
                            IndexList[i] = ClustIdx;
                        }
                    }
                }
                ClustIdx++;
            }
            boolean first = true;
            for (int i = 0; i < InpParticle.size(); i++) {

                if (InpParticle.get(i).channel == Ch) {
                    tab.incrementCounter();
                    tab.addValue("Cluster", IndexList[i]);
                    tab.addValue("x0", InpParticle.get(i).x);
                    tab.addValue("y0", InpParticle.get(i).y);
                    tab.addValue("z0", InpParticle.get(i).z);
                    tab.addValue("frame", InpParticle.get(i).frame);
                    tab.addValue("channel", InpParticle.get(i).channel);
                    tab.addValue("sigma_x", InpParticle.get(i).sigma_x);
                    tab.addValue("sigma_y", InpParticle.get(i).sigma_y);
                    tab.addValue("precision_x", InpParticle.get(i).precision_x);
                    tab.addValue("precision_y", InpParticle.get(i).precision_y);
                    tab.addValue("precision_z", InpParticle.get(i).precision_z);
                    tab.addValue("r_square", InpParticle.get(i).r_square);
                    tab.addValue("photons", InpParticle.get(i).photons);
                    if (IndexList[i] > 0)
                        tab.addValue("include", 1);
                    else
                        tab.addValue("include", 0);
                    if (first) {
                        first = false;
                        tab.addValue("width", width);
                        tab.addValue("height", height);
                    }
                }
            }
        }
    } // Channel loop.
    tab.show("Results");

    RenderIm.run(doCluster, pixelSize, false);

}

From source file:Data.Utilities.java

public static List<Cluster<DoublePoint>> DBSCANClusterer(ArrayList<StayPoint> stayPoints, Double eParam,
        Integer minClust) {//from  w ww.  jav a2  s.  c  o m

    DBSCANClusterer dbscan = new DBSCANClusterer(eParam, minClust);

    ArrayList<DoublePoint> input = new ArrayList<DoublePoint>();

    for (StayPoint st : stayPoints) {
        double coord[] = new double[2];
        coord[0] = Double.parseDouble(st.getLatitude());
        coord[1] = Double.parseDouble(st.getLongitude());
        input.add(new DoublePoint(coord));
    }

    List<Cluster<DoublePoint>> cluster = dbscan.cluster(input);

    return cluster;
}

From source file:bigdataproject.MainJFrame.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    this.jLabel8.setText("");
    ReadDataSet read = new ReadDataSet();
    read.readFromFile();/*from   w w w .j  a va  2s  . c  o  m*/
    read.filter();
    matrix = read.getMatrix();
    PCA pca = new PCA(matrix);
    double[][] matrix2DPCA = pca.reduceDimensions();
    BlockRealMatrix pcaMatrix = new BlockRealMatrix(matrix2DPCA);
    BlockRealMatrix pcaMatrixTranspose = pcaMatrix.transpose();
    List<DoublePoint> list = read.getCollection(read.getHashMap(pcaMatrixTranspose.getData()));
    List<Cluster<DoublePoint>> clusterList;
    if (kMeans) {
        int k;
        if (this.jCheckBox1.isSelected()) {
            KMeansKFinder kFinder = new KMeansKFinder(list);
            k = kFinder.find(0.15);
        } else
            k = (int) this.jSpinner1.getValue();
        KMeansPlusPlusClusterer kmeans = new KMeansPlusPlusClusterer(k, 1000, new EuclideanDistance());
        clusterList = kmeans.cluster(list);
    } else {
        int minPts;
        double eps;
        if (this.jCheckBox2.isSelected()) {
            minPts = 6;
            //KDistances dist = new KDistances(pcaMatrixTranspose.getData());
            //dist.calculateDistances();
            //dist.getKSortedNearestNeighbors(minPts);
            //dist.printKdistances();
            eps = 1.0;
        } else {
            minPts = (int) this.jSpinner2.getValue();
            try {
                eps = Double.parseDouble(this.jTextField1.getText());
            } catch (NumberFormatException e) {
                this.jLabel8.setText("Wrong eps Value");
                return;
            }
        }
        DBSCANClusterer dbscan = new DBSCANClusterer(eps, minPts);
        clusterList = dbscan.cluster(list);
    }
    final ScatterPlot demo = new ScatterPlot("Big Data Clustering Project", matrix2DPCA, clusterList);
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
}

From source file:msi.gaml.operators.Stats.java

@operator(value = "dbscan", can_be_const = false, type = IType.LIST, category = {
        IOperatorCategory.STATISTICAL }, concept = { IConcept.STATISTIC, IConcept.CLUSTERING })
@doc(value = "returns the list of clusters (list of instance indices) computed with the dbscan (density-based spatial clustering of applications with noise) algorithm from the first operand data according to the maximum radius of the neighborhood to be considered (eps) and the minimum number of points needed for a cluster (minPts). Usage: dbscan(data,eps,minPoints)", special_cases = "if the lengths of two vectors in the right-hand aren't equal, returns 0", examples = {
        @example(value = "dbscan ([[2,4,5], [3,8,2], [1,1,3], [4,3,4]],10,2)", equals = "[]") })
public static IList<GamaList> DBscanApache(final IScope scope, final GamaList data, final Double eps,
        final Integer minPts) throws GamaRuntimeException {

    final DBSCANClusterer<DoublePoint> dbscan = new DBSCANClusterer(eps, minPts);
    final List<DoublePoint> instances = new ArrayList<>();
    for (int i = 0; i < data.size(); i++) {
        final GamaList d = (GamaList) data.get(i);
        final double point[] = new double[d.size()];
        for (int j = 0; j < d.size(); j++) {
            point[j] = Cast.asFloat(scope, d.get(j));
        }/*from   w w w  . j  a  va2s . c om*/
        instances.add(new Instance(i, point));
    }
    final List<Cluster<DoublePoint>> clusters = dbscan.cluster(instances);
    final GamaList results = (GamaList) GamaListFactory.create();
    for (final Cluster<DoublePoint> cl : clusters) {
        final GamaList clG = (GamaList) GamaListFactory.create();
        for (final DoublePoint pt : cl.getPoints()) {
            clG.addValue(scope, ((Instance) pt).getId());
        }
        results.addValue(scope, clG);
    }
    return results;
}

From source file:org.lpe.common.util.LpeNumericUtils.java

public static <T extends Number, S extends Number> List<NumericPairList<T, S>> dbscanNormalized(
        NumericPairList<T, S> points, double epsilon, int minNumPoints, double keyRange, double valueRange) {
    NormalizedDistanceMeasure distanceMeasure = new NormalizedDistanceMeasure(1.0 / keyRange, 1.0 / valueRange);
    System.out.println("########## STARTED CLUSTERING ###################");
    DBSCANClusterer<NumericPair<T, S>> clusterer = new DBSCANClusterer<NumericPair<T, S>>(epsilon, minNumPoints,
            distanceMeasure);/*from  w w w  .  j  a v a2s.c  o m*/

    List<Cluster<NumericPair<T, S>>> clusters = clusterer.cluster(points.getPairs());
    System.out.println("########## FINISHED CLUSTERING ###################");
    List<NumericPairList<T, S>> result = new ArrayList<>();
    for (Cluster<NumericPair<T, S>> c : clusters) {
        NumericPairList<T, S> pairList = new NumericPairList<>();
        for (NumericPair<T, S> pair : c.getPoints()) {
            pairList.add(pair);
        }
        result.add(pairList);

    }
    System.out.println("########## RETURNED CLUSTERS ###################");
    return result;
}

From source file:org.lpe.common.util.LpeNumericUtils.java

public static <T extends Number, S extends Number> List<NumericPairList<T, S>> dbscan(
        NumericPairList<T, S> points, double epsilon, int minNumPoints) {
    DBSCANClusterer<NumericPair<T, S>> clusterer = new DBSCANClusterer<NumericPair<T, S>>(epsilon,
            minNumPoints);//  w ww .j a  va  2s .c  o  m
    List<Cluster<NumericPair<T, S>>> clusters = clusterer.cluster(points.getPairs());
    List<NumericPairList<T, S>> result = new ArrayList<>();
    for (Cluster<NumericPair<T, S>> c : clusters) {
        NumericPairList<T, S> pairList = new NumericPairList<>();
        for (NumericPair<T, S> pair : c.getPoints()) {
            pairList.add(pair);
        }
        result.add(pairList);

    }
    return result;
}

From source file:org.meresco.lucene.search.MerescoClusterer.java

public void finish() {
    this.clusters = new ArrayList<Cluster<MerescoVector>>();
    for (StrategyClusterer strategyClusterer : this.strategyClusterers) {
        DBSCANClusterer<MerescoVector> clusterer = new DBSCANClusterer<MerescoVector>(strategyClusterer.eps,
                strategyClusterer.minPoints, new GeneralizedJaccardDistance());
        this.clusters.addAll(clusterer.cluster(strategyClusterer.docvectors));
    }//from   w  w  w  .  j  a v  a  2  s.c o  m
}

From source file:uk.ac.diamond.scisoft.ncd.core.data.stats.ClusterOutlierRemoval.java

@Override
public Dataset getStatsData() {
    if (referenceData != null) {
        int[] shape = referenceData.getShape();
        IndexIterator itr = referenceData.getIterator(true);
        Map<DoublePoint, Integer> clusterInputMap = new HashMap<DoublePoint, Integer>();
        while (itr.hasNext()) {
            int[] pos = itr.getPos();
            int idx = AbstractDataset.getFlat1DIndex(shape, pos);
            double val = referenceData.getDouble(pos);
            clusterInputMap.put(new DoublePoint(new double[] { val }), idx);
        }//w  w  w .  j  a  v a  2  s . c om

        DBSCANClusterer<DoublePoint> clusterer = new DBSCANClusterer<DoublePoint>(dbSCANClustererEpsilon,
                dbSCANClustererMinPoints);
        List<Cluster<DoublePoint>> clusterResults = clusterer.cluster(clusterInputMap.keySet());
        // output the clusters
        List<Double> dev = new ArrayList<Double>(clusterResults.size());
        for (int i = 0; i < clusterResults.size(); i++) {
            System.out.println("Cluster " + i);
            List<DoublePoint> points = clusterResults.get(i).getPoints();
            int size = points.size();
            if (size == 1) {
                dev.add(Double.MAX_VALUE);
                continue;
            }
            double[] values = new double[size];
            for (int j = 0; j < values.length; j++) {
                DoublePoint point = points.get(j);
                values[j] = point.getPoint()[0];
            }
            dev.add(StatUtils.variance(values));
            System.out.println("Number of points : " + Integer.toString(size));
            System.out.println("Variance : " + Double.toString(dev.get(i)));
            System.out.println();
        }

        Dataset result = DatasetFactory.zeros(shape, Dataset.INT);
        //int selectIndex = dev.indexOf(Collections.min(dev));
        for (int selectIndex = 0; selectIndex < clusterResults.size(); selectIndex++) {
            Cluster<DoublePoint> selectCluster = clusterResults.get(selectIndex);
            for (DoublePoint point : selectCluster.getPoints()) {
                Integer idx = clusterInputMap.get(point);
                result.set(selectIndex, AbstractDataset.getNDPositionFromShape(idx, shape));
            }
        }
        return result;
    }
    return null;
}