Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1.     /**
  2.      * Gets the participation ratio of the given items against the global counts.
  3.      *
  4.      * Each List is a set of points, which is assumed to be the end point of the algorithm (despite whether the algorithm
  5.      * has run to completion). These points are then checked for types against the global count of types. If a given item contains
  6.      * all points of a given type, then its Correlation reduces to Correlation.MAX_VALUE, or 1/1.  Otherwise,
  7.      * it is the ratio to points of a given type in the item, over the global counts. If a given List<Type> is
  8.      * duplicated in the input values, the output values will have one entry with the values summed.
  9.      *
  10.      * @param items The items, as a result of the closure
  11.      * @param globalCounts The global counts - all types, associated to the number of points in each
  12.      * @return The list of types in the input, associated with the correlation of included versus results
  13.      */
  14.     Map<List<Type>, Correlation> getParticipationRatio(final Iterable<? extends List<Point>> items, final Map<Point.Type, Integer> globalCounts) {
  15.         /*
  16.             1) Make sure all local counts only have unique points - that's what we got coming in, that's what we shoudl
  17.             compare against going out.
  18.             3) Check local counts against global counts
  19.         */
  20.  
  21.         //Step 1 - Make sure all points are unique
  22.         final Set<Point> uniquePoints = Sets.newHashSet();
  23.         List<Set<Point>> uniqueItems = Lists.newArrayList();
  24.         for(List<Point> ps : items) {
  25.             Set<Point> resultSet = Sets.filter(Sets.newHashSet(ps), new Predicate<Point>() { //we filter out the repeat items while maintaining structure
  26.                 @Override
  27.                 public boolean apply(final Point input) {
  28.                     return uniquePoints.add(input);
  29.                 }
  30.             });
  31.             uniqueItems.add(resultSet);
  32.         }
  33.  
  34.         //Step 2 - perform our result-finding
  35.         Map<List<Type>, Correlation> result = Maps.newHashMap();
  36.         for(Collection<Point> ps : uniqueItems) {
  37.             List<Point> pli = Lists.newArrayList(ps);
  38.             List<Type> types = getTypes(pli);
  39.  
  40.             int global = 0; //determine the global counts
  41.             for(Type t : types) {
  42.                 Integer i = globalCounts.get(t);
  43.                 global += (i == null) ? 0 : i;
  44.             }
  45.  
  46.             final Correlation correlation = new Correlation(ps.size(), global); //current counts over total counts
  47.  
  48.             Correlation oldCorrelation = result.get(types); //if we've already done this, we need to add the fractions
  49.             if(oldCorrelation == null) {
  50.                 oldCorrelation = Correlation.MIN_VALUE;
  51.             }
  52.             result.put(types, oldCorrelation.add(correlation)); //we add the fractions here
  53.         }
  54.  
  55.         return result;
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement