MtasDataItemFull.java

  1. package mtas.codec.util.collector;

  2. import java.io.IOException;
  3. import java.io.Serializable;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
  11. import mtas.codec.util.CodecUtil;

  12. /**
  13.  * The Class MtasDataItemFull.
  14.  *
  15.  * @param <T1> the generic type
  16.  * @param <T2> the generic type
  17.  */
  18. abstract class MtasDataItemFull<T1 extends Number & Comparable<T1>, T2 extends Number & Comparable<T2>>
  19.     extends MtasDataItem<T1, T2> implements Serializable {

  20.   /** The Constant serialVersionUID. */
  21.   private static final long serialVersionUID = 1L;

  22.   /** The full values. */
  23.   public T1[] fullValues;

  24.   /** The operations. */
  25.   protected MtasDataOperations<T1, T2> operations;

  26.   /** The stats. */
  27.   protected DescriptiveStatistics stats = null;

  28.   /** The fp stats function items. */
  29.   private Pattern fpStatsFunctionItems = Pattern
  30.       .compile("(([^\\(,]+)(\\(([^\\)]*)\\))?)");

  31.   /**
  32.    * Instantiates a new mtas data item full.
  33.    *
  34.    * @param value the value
  35.    * @param sub the sub
  36.    * @param statsItems the stats items
  37.    * @param sortType the sort type
  38.    * @param sortDirection the sort direction
  39.    * @param errorNumber the error number
  40.    * @param errorList the error list
  41.    * @param operations the operations
  42.    * @param sourceNumber the source number
  43.    */
  44.   public MtasDataItemFull(T1[] value, MtasDataCollector<?, ?> sub,
  45.       Set<String> statsItems, String sortType, String sortDirection,
  46.       int errorNumber, Map<String, Integer> errorList,
  47.       MtasDataOperations<T1, T2> operations, int sourceNumber) {
  48.     super(sub, statsItems, sortType, sortDirection, errorNumber, errorList,
  49.         sourceNumber);
  50.     this.fullValues = value;
  51.     this.operations = operations;
  52.   }

  53.   /*
  54.    * (non-Javadoc)
  55.    *
  56.    * @see mtas.codec.util.DataCollector.MtasDataItem#add(mtas.codec.util.
  57.    * DataCollector.MtasDataItem)
  58.    */
  59.   @Override
  60.   public void add(MtasDataItem<T1, T2> newItem) throws IOException {
  61.     if (newItem instanceof MtasDataItemFull) {
  62.       MtasDataItemFull<T1, T2> newTypedItem = (MtasDataItemFull<T1, T2>) newItem;
  63.       T1[] tmpValue = operations
  64.           .createVector1(fullValues.length + newTypedItem.fullValues.length);
  65.       System.arraycopy(fullValues, 0, tmpValue, 0, fullValues.length);
  66.       System.arraycopy(newTypedItem.fullValues, 0, tmpValue, fullValues.length,
  67.           newTypedItem.fullValues.length);
  68.       fullValues = tmpValue;
  69.       recomputeComparableSortValue = true;
  70.     } else {
  71.       throw new IOException("can only add MtasDataItemFull");
  72.     }
  73.   }

  74.   /**
  75.    * Creates the stats.
  76.    */
  77.   protected void createStats() {
  78.     if (stats == null) {
  79.       stats = new DescriptiveStatistics();
  80.       for (T1 value : fullValues) {
  81.         stats.addValue(value.doubleValue());
  82.       }
  83.     }
  84.   }

  85.   /**
  86.    * Gets the distribution.
  87.    *
  88.    * @param arguments the arguments
  89.    * @return the distribution
  90.    */
  91.   abstract protected HashMap<String, Object> getDistribution(String arguments);

  92.   /*
  93.    * (non-Javadoc)
  94.    *
  95.    * @see mtas.codec.util.DataCollector.MtasDataItem#rewrite()
  96.    */
  97.   @Override
  98.   public Map<String, Object> rewrite(boolean showDebugInfo) throws IOException {
  99.     createStats();
  100.     Map<String, Object> response = new HashMap<>();
  101.     for (String statsItem : getStatsItems()) {
  102.       if (statsItem.equals(CodecUtil.STATS_TYPE_SUM)) {
  103.         response.put(statsItem, stats.getSum());
  104.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_N)) {
  105.         response.put(statsItem, stats.getN());
  106.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_MAX)) {
  107.         response.put(statsItem, stats.getMax());
  108.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_MIN)) {
  109.         response.put(statsItem, stats.getMin());
  110.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_SUMSQ)) {
  111.         response.put(statsItem, stats.getSumsq());
  112.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_SUMOFLOGS)) {
  113.         response.put(statsItem,
  114.             stats.getN() * Math.log(stats.getGeometricMean()));
  115.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_MEAN)) {
  116.         response.put(statsItem, stats.getMean());
  117.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_GEOMETRICMEAN)) {
  118.         response.put(statsItem, stats.getGeometricMean());
  119.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_STANDARDDEVIATION)) {
  120.         response.put(statsItem, stats.getStandardDeviation());
  121.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_VARIANCE)) {
  122.         response.put(statsItem, stats.getVariance());
  123.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_POPULATIONVARIANCE)) {
  124.         response.put(statsItem, stats.getPopulationVariance());
  125.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_QUADRATICMEAN)) {
  126.         response.put(statsItem, Math.sqrt(stats.getSumsq() / stats.getN()));
  127.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_KURTOSIS)) {
  128.         response.put(statsItem, stats.getKurtosis());
  129.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_MEDIAN)) {
  130.         response.put(statsItem, stats.getPercentile(50));
  131.       } else if (statsItem.equals(CodecUtil.STATS_TYPE_SKEWNESS)) {
  132.         response.put(statsItem, stats.getSkewness());
  133.       } else {
  134.         Matcher m = fpStatsFunctionItems.matcher(statsItem);
  135.         if (m.find()) {
  136.           String function = m.group(2).trim();
  137.           if (function.equals(CodecUtil.STATS_FUNCTION_DISTRIBUTION)) {
  138.             response.put(statsItem, getDistribution(m.group(4)));
  139.           }
  140.         }
  141.       }
  142.     }
  143.     if (errorNumber > 0) {
  144.       Map<String, Object> errorResponse = new HashMap<>();
  145.       for (Entry<String, Integer> entry : getErrorList().entrySet()) {
  146.         errorResponse.put(entry.getKey(), entry.getValue());
  147.       }
  148.       response.put("errorNumber", errorNumber);
  149.       response.put("errorList", errorResponse);
  150.     }
  151.     if (showDebugInfo) {
  152.       response.put("sourceNumber", sourceNumber);
  153.       response.put("stats", "full");
  154.     }
  155.     return response;
  156.   }

  157.   /*
  158.    * (non-Javadoc)
  159.    *
  160.    * @see mtas.codec.util.collector.MtasDataItem#getCompareValueType()
  161.    */
  162.   @Override
  163.   public final int getCompareValueType() throws IOException {
  164.     switch (sortType) {
  165.     case CodecUtil.STATS_TYPE_N:
  166.       return 0;
  167.     case CodecUtil.STATS_TYPE_SUM:
  168.     case CodecUtil.STATS_TYPE_MAX:
  169.     case CodecUtil.STATS_TYPE_MIN:
  170.     case CodecUtil.STATS_TYPE_SUMSQ:
  171.       return 1;
  172.     case CodecUtil.STATS_TYPE_SUMOFLOGS:
  173.     case CodecUtil.STATS_TYPE_MEAN:
  174.     case CodecUtil.STATS_TYPE_GEOMETRICMEAN:
  175.     case CodecUtil.STATS_TYPE_STANDARDDEVIATION:
  176.     case CodecUtil.STATS_TYPE_VARIANCE:
  177.     case CodecUtil.STATS_TYPE_POPULATIONVARIANCE:
  178.     case CodecUtil.STATS_TYPE_QUADRATICMEAN:
  179.     case CodecUtil.STATS_TYPE_KURTOSIS:
  180.     case CodecUtil.STATS_TYPE_MEDIAN:
  181.     case CodecUtil.STATS_TYPE_SKEWNESS:
  182.       return 2;
  183.     default:
  184.       throw new IOException("sortType " + sortType + " not supported");
  185.     }
  186.   }

  187.   /*
  188.    * (non-Javadoc)
  189.    *
  190.    * @see mtas.codec.util.collector.MtasDataItem#getCompareValue0()
  191.    */
  192.   public final MtasDataItemNumberComparator<Long> getCompareValue0() {
  193.     createStats();
  194.     switch (sortType) {
  195.     case CodecUtil.STATS_TYPE_N:
  196.       return new MtasDataItemNumberComparator<>(stats.getN(), sortDirection);
  197.     default:
  198.       return null;
  199.     }
  200.   }

  201. }