ref #7549 Add/use apache common math for frequency distribution
authorPatrick Plitzner <p.plitzner@bgbm.org>
Fri, 13 Jul 2018 06:32:01 +0000 (08:32 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Fri, 13 Jul 2018 06:32:01 +0000 (08:32 +0200)
eu.etaxonomy.taxeditor.cdmlib/.classpath
eu.etaxonomy.taxeditor.cdmlib/META-INF/MANIFEST.MF
eu.etaxonomy.taxeditor.cdmlib/build.properties
eu.etaxonomy.taxeditor.cdmlib/lib/commons-math3-3.6.1.jar [new file with mode: 0644]
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/CategoricalDataHistogram.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/descriptiveDataSet/matrix/ChartTooltip.java

index c03a7c6..2b1ea6a 100644 (file)
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
+       <classpathentry exported="true" kind="lib" path="lib/commons-math3-3.6.1.jar"/>
        <classpathentry exported="true" kind="lib" path="lib/org.swtchart_0.10.0.v20160212.jar"/>
        <classpathentry exported="true" kind="lib" path="lib/redmine-java-api-3.1.0.jar"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
index 50d2891..ba8b680 100644 (file)
@@ -231,6 +231,7 @@ Export-Package: com.google.api,
  org.apache.commons.lang.exception,
  org.apache.commons.lang.text,
  org.apache.commons.logging,
+ org.apache.commons.math3.stat,
  org.apache.fop.apps,
  org.apache.http,
  org.apache.http.client,
@@ -869,5 +870,6 @@ Bundle-ClassPath: .,
  lib/lucene-sandbox-5.4.1.jar,
  lib/lucene-suggest-5.4.1.jar,
  lib/redmine-java-api-3.1.0.jar,
- lib/org.swtchart_0.10.0.v20160212.jar
+ lib/org.swtchart_0.10.0.v20160212.jar,
+ lib/commons-math3-3.6.1.jar
 Import-Package: eu.etaxonomy.cdm.api.application
index 0ece366..2c48480 100644 (file)
@@ -223,7 +223,8 @@ bin.includes = META-INF/,\
                lib/lucene-suggest-5.4.1.jar,\
                lib/cdmlib-cache-5.2.0-SNAPSHOT.jar,\
                lib/redmine-java-api-3.1.0.jar,\
-               lib/org.swtchart_0.10.0.v20160212.jar
+               lib/org.swtchart_0.10.0.v20160212.jar,\
+               lib/commons-math3-3.6.1.jar
 
 jars.compile.order = .
 output.. = bin/
diff --git a/eu.etaxonomy.taxeditor.cdmlib/lib/commons-math3-3.6.1.jar b/eu.etaxonomy.taxeditor.cdmlib/lib/commons-math3-3.6.1.jar
new file mode 100644 (file)
index 0000000..0ff582c
Binary files /dev/null and b/eu.etaxonomy.taxeditor.cdmlib/lib/commons-math3-3.6.1.jar differ
index 5a25d8c..d84cc8a 100644 (file)
@@ -8,8 +8,10 @@
 */
 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.math3.stat.Frequency;
 
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.State;
@@ -22,23 +24,28 @@ import eu.etaxonomy.cdm.model.description.State;
 public class CategoricalDataHistogram {
 
     private Feature feature;
-    private Map<State, Integer> stateCountMap;
+    private Frequency frequency;
+    private List<State> states;
 
     public CategoricalDataHistogram(Feature feature) {
         this.feature = feature;
-        this.stateCountMap = new HashMap<>();
+        this.states = new ArrayList<>();
+        this.frequency = new Frequency();
         feature.getSupportedCategoricalEnumerations()
         .forEach(voc->voc.getTerms()
-                .forEach(state->stateCountMap.put(state, 0)));
+                .forEach(state->states.add(state)));
     }
 
     public void addState(State state){
-        Integer integer = stateCountMap.get(state);
-        stateCountMap.put(state, integer+1);
+        frequency.addValue(state.getLabel());
+    }
+
+    public long getCount(State state){
+        return frequency.getCount(state.getLabel());
     }
 
-    public Map<State, Integer> getStateCountMap() {
-        return stateCountMap;
+    public List<State> getStates() {
+        return states;
     }
 
     public Feature getFeature() {
index 6070248..f586ffc 100644 (file)
@@ -8,9 +8,7 @@
 */
 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
 
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Map.Entry;
+import java.util.List;
 
 import org.eclipse.jface.window.ToolTip;
 import org.eclipse.swt.SWT;
@@ -57,22 +55,25 @@ public class ChartTooltip extends ToolTip {
         Chart chart = new Chart(parent, SWT.NONE);
         chart.setLayoutData(new GridData(600, 300));
 
+        //get histogram for column
         int colPos = matrix.getNatTable().getColumnPositionByX(event.x);
         int colIndex = matrix.getNatTable().getColumnIndexByPosition(colPos);
         Feature feature = matrix.getIndexToFeatureMap().get(colIndex);
         CategoricalDataHistogram histogram = matrix.getFeatureToHistogramMap().get(feature);
 
-        // set titles
+        // set chart title
         chart.getTitle().setText(feature.getLabel());
         IAxis xAxis = chart.getAxisSet().getXAxis(0);
-        Map<State, Integer> stateCountMap = histogram.getStateCountMap();
-        String[] states = new String[stateCountMap.size()];
-        double[] counts = new double[stateCountMap.size()];
+
+        //initalize x and y axis values
+        List<State> statesList = histogram.getStates();
+        String[] states = new String[statesList.size()];
+        double[] counts = new double[statesList.size()];
+
         int index = 0;
-        for(Iterator<Entry<State, Integer>> iterator = histogram.getStateCountMap().entrySet().iterator();iterator.hasNext();){
-            Entry<State, Integer> entry = iterator.next();
-            states[index] = entry.getKey().getLabel();
-            counts[index] = entry.getValue();
+        for (State state : statesList) {
+            states[index] = state.getLabel();
+            counts[index] = histogram.getCount(state);
             index++;
         }
         xAxis.setCategorySeries(states);
@@ -82,12 +83,9 @@ public class ChartTooltip extends ToolTip {
 
         IAxis yAxis = chart.getAxisSet().getYAxis(0);
         yAxis.getTitle().setVisible(false);
-//        yAxis.getTick().setVisible(false);
 
-//        chart.setOrientation(SWT.VERTICAL);
         chart.getLegend().setVisible(false);
 
-        // create bar series
         IBarSeries barSeries = (IBarSeries) chart.getSeriesSet()
             .createSeries(SeriesType.BAR, "state counts");
         ISeriesLabel seriesLabel = barSeries.getLabel();
@@ -95,7 +93,6 @@ public class ChartTooltip extends ToolTip {
         seriesLabel.setVisible(true);
         barSeries.setYSeries(counts);
 
-        // adjust the axis range
         chart.getAxisSet().adjustRange();
 
         return chart;
@@ -116,7 +113,7 @@ public class ChartTooltip extends ToolTip {
                 && rowPos==1
                 && colIndex>=CharacterMatrix.LEADING_COLUMN_COUNT
                 && histogram!=null
-                && !histogram.getStateCountMap().isEmpty()) {
+                && !histogram.getStates().isEmpty()) {
             return true;
         }
         return false;